import { useCallback, useContext, useEffect, useRef, useState } from 'react'; import { ChannelContext, ClientIdContext, HomeServerContext } from './App'; import { useApi } from '../lib/useApi'; import type { IMessage } from './Message'; import NameTextbox from './NameTextbox'; import LoginQR from './LoginQR'; import Totp from '../components/Totp'; interface IChannel { uid: string; name: string; } function Hashmark() { return #; } interface IUnreads { [uid: string]: number } export default function Channels() { const [channels, setChannels] = useState([]); const [unreads, setUnreads] = useState({}); const { channel, setChannel } = useContext(ChannelContext); const { clientId } = useContext(ClientIdContext); const { setHomeServer } = useContext(HomeServerContext); const { send } = useApi({ 'channels:list'(data: IChannel[]) { setChannels(data); }, 'channel:add'(channel: IChannel) { setChannels([...channels, channel]); }, 'message:message'(message: IMessage) { if(channel === message.channel) return; setUnreads({ ...unreads, [message.channel]: (unreads[message.channel] ?? 0) + 1, }); }, }, [channels, unreads]); useEffect(() => { console.log('unreads', unreads); }, [unreads]); useEffect(() => { if(channels.length === 0) { send('channels:list'); } }, [channels]); useEffect(() => { console.log(channel, channels); if(channels.length === 0) return; if(channel !== null) return; console.log('this is what setChannel is', setChannel); setChannel(channels[0].uid); }, [channel, channels]); useEffect(() => { if(!channel) return; setUnreads({ ...unreads, [channel]: 0, }); }, [channel]); useEffect(() => { if(clientId === null) return; send('client:get', clientId); }, [clientId]); const textbox = useRef(null); const add = useCallback(() => { if(textbox.current === null) return; const name = textbox.current.value; textbox.current.value = ''; send('channel:add', { name }); }, []); return (


{channels.map(c => (
{ setChannel(c.uid); }}> {(c.uid in unreads) && (unreads[c.uid] > 0) && ( ({unreads[c.uid]}) )} 0 ? 'bold' : '300', }}> {c.name} {}}>Delete
))}



); }