2022-07-22 01:45:11 -04:00
|
|
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
2022-07-21 23:45:52 -04:00
|
|
|
import { registerRouter, router, send, unregisterRouter } from '../lib/api';
|
|
|
|
|
|
|
|
|
|
function useRouter(actions: Function | object, deps: any[]) {
|
|
|
|
|
const _router = typeof actions === 'object' ? router(actions) : actions;
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
registerRouter(_router);
|
|
|
|
|
return () => {
|
|
|
|
|
unregisterRouter(_router);
|
|
|
|
|
};
|
|
|
|
|
}, deps);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
send: send,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-21 23:45:52 -04:00
|
|
|
export default function Channels() {
|
|
|
|
|
|
|
|
|
|
const [channels, setChannels] = useState<IChannel[]>([]);
|
|
|
|
|
|
|
|
|
|
const { send } = useRouter({
|
2022-07-22 01:45:11 -04:00
|
|
|
'channels:list'(data: IChannel[]) {
|
2022-07-21 23:45:52 -04:00
|
|
|
// console.log(data)
|
|
|
|
|
setChannels(data);
|
|
|
|
|
},
|
2022-07-22 01:45:11 -04:00
|
|
|
'channel:add'(channel: IChannel) {
|
|
|
|
|
setChannels([...channels, channel]);
|
|
|
|
|
},
|
2022-07-21 23:45:52 -04:00
|
|
|
}, [channels]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if(channels.length === 0) {
|
|
|
|
|
send('channels:list');
|
|
|
|
|
}
|
|
|
|
|
}, [channels]);
|
|
|
|
|
|
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-22 01:45:11 -04:00
|
|
|
<br></br>
|
2022-07-21 23:45:52 -04:00
|
|
|
{channels.map(channel => (
|
2022-07-22 01:45:11 -04:00
|
|
|
<div key={channel.uid} style={{
|
|
|
|
|
margin: '8px 0px',
|
|
|
|
|
}}>
|
|
|
|
|
<Hashmark></Hashmark>{channel.name}
|
|
|
|
|
<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-21 23:45:52 -04:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|