2022-08-01 05:53:14 -04:00
|
|
|
import {
|
|
|
|
|
useCallback,
|
|
|
|
|
useEffect,
|
|
|
|
|
useRef,
|
|
|
|
|
useState
|
|
|
|
|
} from 'react';
|
2022-07-23 17:37:24 -04:00
|
|
|
import { useApi } from '../lib/useApi';
|
2022-07-22 20:29:24 -04:00
|
|
|
import type { IMessage } from './Message';
|
2022-07-29 13:52:59 -04:00
|
|
|
import useChannel from '../hooks/useChannel';
|
|
|
|
|
import useClientId from '../hooks/useClientId';
|
|
|
|
|
import useHomeServer from '../contexts/PersistentState/useHomeServerNative';
|
2022-07-30 21:27:56 -04:00
|
|
|
import Channel from './Channel';
|
2022-07-21 23:45:52 -04:00
|
|
|
|
|
|
|
|
interface IChannel {
|
|
|
|
|
uid: string;
|
|
|
|
|
name: string;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 20:29:24 -04:00
|
|
|
interface IUnreads {
|
|
|
|
|
[uid: string]: number
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-21 23:45:52 -04:00
|
|
|
export default function Channels() {
|
|
|
|
|
|
|
|
|
|
const [channels, setChannels] = useState<IChannel[]>([]);
|
2022-07-22 20:29:24 -04:00
|
|
|
const [unreads, setUnreads] = useState<IUnreads>({});
|
2022-08-03 01:05:22 -04:00
|
|
|
|
2022-07-29 13:52:59 -04:00
|
|
|
const { channel, setChannel } = useChannel()
|
|
|
|
|
const { clientId } = useClientId()
|
2022-07-25 21:23:40 -04:00
|
|
|
|
2022-07-23 17:37:24 -04:00
|
|
|
const { send } = useApi({
|
2022-07-22 01:45:11 -04:00
|
|
|
'channels:list'(data: IChannel[]) {
|
2022-07-21 23:45:52 -04:00
|
|
|
setChannels(data);
|
|
|
|
|
},
|
2022-07-22 01:45:11 -04:00
|
|
|
'channel:add'(channel: IChannel) {
|
|
|
|
|
setChannels([...channels, channel]);
|
|
|
|
|
},
|
2022-07-22 20:29:24 -04:00
|
|
|
'message:message'(message: IMessage) {
|
|
|
|
|
if(channel === message.channel) return;
|
|
|
|
|
setUnreads({
|
|
|
|
|
...unreads,
|
|
|
|
|
[message.channel]: (unreads[message.channel] ?? 0) + 1,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
}, [channels, unreads]);
|
|
|
|
|
|
2022-07-21 23:45:52 -04:00
|
|
|
useEffect(() => {
|
|
|
|
|
if(channels.length === 0) {
|
|
|
|
|
send('channels:list');
|
|
|
|
|
}
|
|
|
|
|
}, [channels]);
|
|
|
|
|
|
2022-07-22 17:09:28 -04:00
|
|
|
useEffect(() => {
|
|
|
|
|
if(channels.length === 0) return;
|
|
|
|
|
if(channel !== null) return;
|
|
|
|
|
setChannel(channels[0].uid);
|
|
|
|
|
}, [channel, channels]);
|
|
|
|
|
|
2022-07-22 20:29:24 -04:00
|
|
|
useEffect(() => {
|
|
|
|
|
if(!channel) return;
|
|
|
|
|
setUnreads({
|
|
|
|
|
...unreads,
|
|
|
|
|
[channel]: 0,
|
|
|
|
|
});
|
|
|
|
|
}, [channel]);
|
|
|
|
|
|
2022-07-23 17:37:24 -04:00
|
|
|
useEffect(() => {
|
|
|
|
|
if(clientId === null) return;
|
2022-07-30 21:27:56 -04:00
|
|
|
send('client:get', { clientId });
|
2022-07-23 17:37:24 -04:00
|
|
|
}, [clientId]);
|
|
|
|
|
|
2022-07-22 01:45:11 -04:00
|
|
|
const textbox = useRef<HTMLInputElement>(null);
|
|
|
|
|
const add = useCallback(() => {
|
|
|
|
|
if(textbox.current === null) return;
|
|
|
|
|
const name = textbox.current.value;
|
|
|
|
|
textbox.current.value = '';
|
|
|
|
|
send('channel:add', { name });
|
|
|
|
|
}, []);
|
|
|
|
|
|
2022-07-21 23:45:52 -04:00
|
|
|
return (
|
2022-07-25 03:33:49 -04:00
|
|
|
<div style={{
|
|
|
|
|
height: '100%',
|
2022-08-03 01:05:22 -04:00
|
|
|
background: 'var(--neutral-3)',
|
|
|
|
|
padding: '0px 8px',
|
|
|
|
|
overflowY: 'auto',
|
|
|
|
|
overflowX: 'hidden',
|
2022-07-25 03:33:49 -04:00
|
|
|
}}>
|
2022-07-22 01:45:11 -04:00
|
|
|
<br></br>
|
2022-07-22 17:09:28 -04:00
|
|
|
{channels.map(c => (
|
2022-07-30 21:27:56 -04:00
|
|
|
<Channel
|
|
|
|
|
key={c.uid}
|
|
|
|
|
uid={c.uid}
|
|
|
|
|
unread={unreads[c.uid] ?? 0}
|
|
|
|
|
name={c.name}
|
|
|
|
|
></Channel>
|
2022-07-21 23:45:52 -04:00
|
|
|
))}
|
2022-07-25 03:33:49 -04:00
|
|
|
</div>
|
2022-07-21 23:45:52 -04:00
|
|
|
);
|
|
|
|
|
}
|