mirror of
https://github.com/Steffo99/todocolors.git
synced 2024-11-25 17:54:18 +00:00
Create initial board viewer page
This commit is contained in:
parent
c832166e2a
commit
1536b495c7
3 changed files with 61 additions and 0 deletions
15
todoblue/src/app/board/[board]/page.tsx
Normal file
15
todoblue/src/app/board/[board]/page.tsx
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
'use client';
|
||||||
|
import {default as React} from "react";
|
||||||
|
import {useBoard} from "@/app/board/[board]/useBoard"
|
||||||
|
|
||||||
|
|
||||||
|
export default function Page({params: {board}}: {params: {board: string}}) {
|
||||||
|
const wsUrl = React.useMemo(() => `ws://127.0.0.1:8080/board/${board}/ws`, [board])
|
||||||
|
useBoard(wsUrl);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<p>
|
||||||
|
TODO
|
||||||
|
</p>
|
||||||
|
)
|
||||||
|
}
|
16
todoblue/src/app/board/[board]/useBoard.tsx
Normal file
16
todoblue/src/app/board/[board]/useBoard.tsx
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
'use client';
|
||||||
|
import {default as React} from "react";
|
||||||
|
import {useWs} from "@/app/board/[board]/useWs"
|
||||||
|
|
||||||
|
|
||||||
|
export function useBoard(url: string) {
|
||||||
|
const socket = useWs(url, {
|
||||||
|
onopen: React.useCallback(() => {
|
||||||
|
console.debug("[useBoard] Connected to the server!");
|
||||||
|
}, []),
|
||||||
|
onmessage: React.useCallback((event: MessageEvent) => {
|
||||||
|
const data = JSON.parse(event.data);
|
||||||
|
console.debug("[useBoard] Received ServerOperation: ", data);
|
||||||
|
}, [])
|
||||||
|
});
|
||||||
|
}
|
30
todoblue/src/app/board/[board]/useWs.tsx
Normal file
30
todoblue/src/app/board/[board]/useWs.tsx
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
'use client';
|
||||||
|
import {default as React} from "react";
|
||||||
|
|
||||||
|
|
||||||
|
export interface UseWsHandlers {
|
||||||
|
onclose?: (event: CloseEvent) => void,
|
||||||
|
onerror?: (event: Event) => void,
|
||||||
|
onmessage?: (event: MessageEvent) => void,
|
||||||
|
onopen?: (event: Event) => void,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function useWs(url: string, {onclose, onerror, onmessage, onopen}: UseWsHandlers) {
|
||||||
|
const [websocket, setWebsocket] = React.useState<WebSocket | null>(null)
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const sock = new WebSocket(url);
|
||||||
|
sock.onclose = onclose ?? null;
|
||||||
|
sock.onerror = onerror ?? null;
|
||||||
|
sock.onmessage = onmessage ?? null;
|
||||||
|
sock.onopen = onopen ?? null;
|
||||||
|
setWebsocket(sock);
|
||||||
|
return () => {
|
||||||
|
sock.close();
|
||||||
|
setWebsocket(null);
|
||||||
|
}
|
||||||
|
}, [url, onclose, onerror, onmessage, onopen])
|
||||||
|
|
||||||
|
return websocket
|
||||||
|
}
|
Loading…
Reference in a new issue