2022-07-22 17:09:28 -04:00
|
|
|
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
2022-07-25 21:23:40 -04:00
|
|
|
import { ChannelContext, ClientIdContext, HomeServerContext } from './App';
|
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-23 17:37:24 -04:00
|
|
|
import NameTextbox from './NameTextbox';
|
2022-07-25 21:23:40 -04:00
|
|
|
import LoginQR from './LoginQR';
|
2022-07-26 21:05:26 -04:00
|
|
|
import Totp from '../components/Totp';
|
2022-07-21 23:45:52 -04:00
|
|
|
|
|
|
|
|
interface IChannel {
|
|
|
|
|
uid: string;
|
|
|
|
|
name: string;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 01:45:11 -04:00
|
|
|
function Hashmark() {
|
|
|
|
|
return <span style={{
|
|
|
|
|
fontWeight: 'bold',
|
|
|
|
|
marginRight: '8px',
|
|
|
|
|
marginLeft: '8px',
|
|
|
|
|
}}>#</span>;
|
|
|
|
|
}
|
|
|
|
|
|
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-25 21:23:40 -04:00
|
|
|
const { channel, setChannel } = useContext(ChannelContext);
|
|
|
|
|
const { clientId } = useContext(ClientIdContext);
|
|
|
|
|
|
|
|
|
|
const { setHomeServer } = useContext(HomeServerContext);
|
2022-07-22 20:29:24 -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;
|
|
|
|
|
send('client:get', clientId);
|
|
|
|
|
}, [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-07-22 01:45:11 -04:00
|
|
|
<br></br>
|
2022-07-22 17:09:28 -04:00
|
|
|
{channels.map(c => (
|
|
|
|
|
<div key={c.uid} style={{
|
2022-07-22 01:45:11 -04:00
|
|
|
margin: '8px 0px',
|
2022-07-22 17:09:28 -04:00
|
|
|
color: channel === c.uid ? 'cyan' : 'inherit',
|
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
}} onClick={() => {
|
|
|
|
|
setChannel(c.uid);
|
2022-07-22 01:45:11 -04:00
|
|
|
}}>
|
2022-07-22 20:29:24 -04:00
|
|
|
<Hashmark></Hashmark>
|
|
|
|
|
{(c.uid in unreads) && (unreads[c.uid] > 0) && (
|
|
|
|
|
<span style={{ paddingRight: '8px' }}>({unreads[c.uid]})</span>
|
|
|
|
|
)}
|
|
|
|
|
<span style={{
|
|
|
|
|
fontWeight: (unreads[c.uid] ?? 0) > 0 ? 'bold' : '300',
|
|
|
|
|
}}>
|
|
|
|
|
{c.name}
|
|
|
|
|
</span>
|
|
|
|
|
<a style={{
|
|
|
|
|
color: 'rgba(0, 100, 200, 1)',
|
|
|
|
|
marginLeft: '8px',
|
|
|
|
|
fontSize: '10px',
|
|
|
|
|
}} href="#" onClick={() => {}}>Delete</a>
|
2022-07-21 23:45:52 -04:00
|
|
|
</div>
|
|
|
|
|
))}
|
2022-07-22 01:45:11 -04:00
|
|
|
<Hashmark></Hashmark><input
|
|
|
|
|
ref={textbox}
|
|
|
|
|
style={{
|
|
|
|
|
background: '#343746',
|
|
|
|
|
border: 'none',
|
|
|
|
|
padding: '8px',
|
|
|
|
|
borderRadius: '8px',
|
|
|
|
|
outline: 'none',
|
|
|
|
|
color: 'white',
|
|
|
|
|
fontSize: '16px',
|
|
|
|
|
width: '90px',
|
|
|
|
|
}}
|
|
|
|
|
/><button onClick={add} style={{
|
|
|
|
|
marginLeft: '8px',
|
|
|
|
|
background: '#bd93f9',
|
|
|
|
|
border: 'none',
|
|
|
|
|
color: 'white',
|
|
|
|
|
padding: '8px',
|
|
|
|
|
fontSize: '16px',
|
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
borderRadius: '8px',
|
|
|
|
|
// lineHeight: '20px'
|
|
|
|
|
}}>ADD</button>
|
2022-07-25 21:23:40 -04:00
|
|
|
<NameTextbox></NameTextbox><br></br>
|
|
|
|
|
<button onClick={() => setHomeServer(null)}>leave</button><br></br>
|
2022-07-29 00:01:01 -04:00
|
|
|
{/* <LoginQR></LoginQR> */}
|
|
|
|
|
{/* <Totp></Totp> */}
|
2022-07-25 03:33:49 -04:00
|
|
|
</div>
|
2022-07-21 23:45:52 -04:00
|
|
|
);
|
|
|
|
|
}
|