channel ui
parent
bf6d521963
commit
fee353383a
|
|
@ -5,14 +5,22 @@ import Chat from './pages/Chat';
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
(
|
(
|
||||||
<>
|
<div style={{
|
||||||
{/* <Chat
|
display: 'grid',
|
||||||
|
gridTemplateColumns: '200px 1fr',
|
||||||
></Chat> */}
|
gridTemplateRows: '1fr',
|
||||||
<Channels
|
height: '100%',
|
||||||
|
}}>
|
||||||
></Channels>
|
<div style={{
|
||||||
</>
|
background: 'rgba(25, 26, 33)',
|
||||||
|
borderRight: '1px solid #bd93f9',
|
||||||
|
}}>
|
||||||
|
<Channels></Channels>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<Chat></Chat>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
),
|
),
|
||||||
document.getElementById('app'),
|
document.getElementById('app'),
|
||||||
);
|
);
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
let socket: WebSocket | null = null;
|
let socket: WebSocket | null = null;
|
||||||
let connectionAttempts = 0;
|
let connectionAttempts = 0;
|
||||||
const url = 'wss://dev.valnet.xyz';
|
const url = 'wss://macos.valnet.xyz';
|
||||||
|
|
||||||
let routers: any[] = [];
|
let routers: any[] = [];
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { useEffect, useState } from 'react';
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
import { registerRouter, router, send, unregisterRouter } from '../lib/api';
|
import { registerRouter, router, send, unregisterRouter } from '../lib/api';
|
||||||
|
|
||||||
function useRouter(actions: Function | object, deps: any[]) {
|
function useRouter(actions: Function | object, deps: any[]) {
|
||||||
|
|
@ -20,15 +20,26 @@ interface IChannel {
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Hashmark() {
|
||||||
|
return <span style={{
|
||||||
|
fontWeight: 'bold',
|
||||||
|
marginRight: '8px',
|
||||||
|
marginLeft: '8px',
|
||||||
|
}}>#</span>;
|
||||||
|
}
|
||||||
|
|
||||||
export default function Channels() {
|
export default function Channels() {
|
||||||
|
|
||||||
const [channels, setChannels] = useState<IChannel[]>([]);
|
const [channels, setChannels] = useState<IChannel[]>([]);
|
||||||
|
|
||||||
const { send } = useRouter({
|
const { send } = useRouter({
|
||||||
'channels:list'(data: any) {
|
'channels:list'(data: IChannel[]) {
|
||||||
// console.log(data)
|
// console.log(data)
|
||||||
setChannels(data);
|
setChannels(data);
|
||||||
},
|
},
|
||||||
|
'channel:add'(channel: IChannel) {
|
||||||
|
setChannels([...channels, channel]);
|
||||||
|
},
|
||||||
}, [channels]);
|
}, [channels]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -37,15 +48,48 @@ export default function Channels() {
|
||||||
}
|
}
|
||||||
}, [channels]);
|
}, [channels]);
|
||||||
|
|
||||||
|
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 });
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
<br></br>
|
||||||
{channels.map(channel => (
|
{channels.map(channel => (
|
||||||
<div key={channel.uid}>
|
<div key={channel.uid} style={{
|
||||||
<span style={{
|
margin: '8px 0px',
|
||||||
fontWeight: 'bold',
|
}}>
|
||||||
}}>#</span>{channel.name}
|
<Hashmark></Hashmark>{channel.name}
|
||||||
|
<a style={{ color: 'rgba(0, 100, 200, 1)', marginLeft: '8px', fontSize: '10px' }} href="#" onClick={() => {}}>Delete</a>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
<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>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -11,6 +11,7 @@ export default async function(a: any, ...opts: any[]): Promise<any[] | null> {
|
||||||
connection.query(text, [...opts], (err, results) => {
|
connection.query(text, [...opts], (err, results) => {
|
||||||
if(!err) return resolve(results);
|
if(!err) return resolve(results);
|
||||||
console.error(err.errno, err.sqlMessage);
|
console.error(err.errno, err.sqlMessage);
|
||||||
|
console.error('--- Query ---');
|
||||||
console.error(err.sql);
|
console.error(err.sql);
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
INSERT INTO channels (name, uid)
|
||||||
|
VALUES (?, ?);
|
||||||
|
|
@ -6,7 +6,11 @@ export default function router(routes: any) {
|
||||||
if('routes' in route) {
|
if('routes' in route) {
|
||||||
for(const suffix of route.routes) {
|
for(const suffix of route.routes) {
|
||||||
const combinedRouteName = routeName + ':' + suffix;
|
const combinedRouteName = routeName + ':' + suffix;
|
||||||
routes[combinedRouteName] = (_: never, ...args: any[]) => route(suffix, args);
|
routes[combinedRouteName] = function(data: any) {
|
||||||
|
// console.log(suffix, route, data)
|
||||||
|
return route(suffix, data);
|
||||||
|
// console.log('INCOMMING', args)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
delete routes[routeName];
|
delete routes[routeName];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,25 @@
|
||||||
import query from '../db/query';
|
import query from '../db/query';
|
||||||
import router from '../lib/router';
|
import router from '../lib/router';
|
||||||
import list from '../db/snippets/channel/list.sql';
|
import list from '../db/snippets/channel/list.sql';
|
||||||
import { reply } from '../lib/WebSocketServer';
|
import add from '../db/snippets/channel/new.sql';
|
||||||
|
import { broadcast, reply } from '../lib/WebSocketServer';
|
||||||
|
import { v4 } from 'uuid';
|
||||||
|
|
||||||
export default router({
|
export default router({
|
||||||
async list() {
|
async list() {
|
||||||
const res = await query(list);
|
const res = await query(list);
|
||||||
return reply(res ?? undefined);
|
return reply(res ?? undefined);
|
||||||
},
|
},
|
||||||
|
async add(channel: any) {
|
||||||
|
const name = channel.name;
|
||||||
|
const uid = v4();
|
||||||
|
console.log(channel);
|
||||||
|
const res = await query(add, name, uid);
|
||||||
|
if(res === null) return;
|
||||||
|
console.log(res);
|
||||||
|
return broadcast({
|
||||||
|
uid,
|
||||||
|
name,
|
||||||
|
});
|
||||||
|
},
|
||||||
});
|
});
|
||||||
Reference in New Issue