This repository has been archived on 2023-11-14. You can view files and clone it, but cannot push or open issues/pull-requests.
viscord/packages/renderer/src/pages/Channels.tsx

103 lines
2.3 KiB
TypeScript
Raw Normal View History

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-07-23 17:37:24 -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]);
useEffect(() => {
2022-07-29 00:01:01 -04:00
// console.log('unreads', unreads);
2022-07-22 20:29:24 -04:00
}, [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(() => {
2022-07-29 00:01:01 -04:00
// console.log(channel, channels);
2022-07-22 17:09:28 -04:00
if(channels.length === 0) return;
if(channel !== null) return;
2022-07-29 00:01:01 -04:00
// console.log('this is what setChannel is', setChannel);
2022-07-22 17:09:28 -04:00
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%',
background: '#21222c',
2022-08-01 05:53:14 -04:00
padding: '0px 8px'
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
);
}