diff --git a/frontend/src/components/Demo.tsx b/frontend/src/components/Demo.tsx index 2334ca4..88e5bc8 100644 --- a/frontend/src/components/Demo.tsx +++ b/frontend/src/components/Demo.tsx @@ -1,7 +1,7 @@ import Badge from "@/components/Badge"; import Emboldened from "@/components/Emboldened"; import useSocket from "@/components/useSocket"; -import { cn, plural, type ClassValue } from "@/util"; +import { cn, plural, toHex, type ClassValue } from "@/util"; import { useRef, useState } from "preact/hooks"; type DemoProps = { @@ -42,7 +42,7 @@ const Demo = ({ class: className }: DemoProps) => {
Your session is{" "} - {id != null ? "0x" + id?.toString(16).toUpperCase() : null} + {id != null ? toHex(id) : null} . You have{" "} @@ -66,7 +66,7 @@ const Demo = ({ class: className }: DemoProps) => { audio.play(); }} > - {download} + {toHex(download.token)} ))} diff --git a/frontend/src/components/useSocket.ts b/frontend/src/components/useSocket.ts index 1f4f54c..537cd81 100644 --- a/frontend/src/components/useSocket.ts +++ b/frontend/src/components/useSocket.ts @@ -1,4 +1,4 @@ -import { useEffect, useState } from "preact/hooks"; +import { useEffect, useRef, useState } from "preact/hooks"; interface Download { token: number; @@ -24,8 +24,23 @@ function useSocket(): UseSocketResult { const [id, setId] = useState(null); const [downloads, setDownloads] = useState(null); const [executables, setExecutables] = useState(null); + const socketRef = useRef(null); - function deleteDownload() {} + function deleteDownload(download_token: number) { + if (socketRef.current == null) { + console.error("Socket is null"); + return; + } else if (socketRef.current.readyState !== WebSocket.OPEN) { + console.error("Socket is not open", socketRef.current.readyState); + return; + } + socketRef.current.send( + JSON.stringify({ + type: "delete", + token: download_token, + }) + ); + } useEffect(() => { const socket = new WebSocket( @@ -33,6 +48,7 @@ function useSocket(): UseSocketResult { (import.meta.env.DEV ? "localhost:5800" : window.location.host) + "/ws" ); + socketRef.current = socket; socket.onmessage = (event) => { const data = JSON.parse(event.data); @@ -42,7 +58,7 @@ function useSocket(): UseSocketResult { switch (data.type) { case "state": - setId(data.id as number); + setId(data.session.id as number); setDownloads(data.session.downloads as Download[]); break; case "executables": diff --git a/frontend/src/util.ts b/frontend/src/util.ts index 1c047d2..e2a4c02 100644 --- a/frontend/src/util.ts +++ b/frontend/src/util.ts @@ -18,3 +18,7 @@ export function os(): Platform | "other" { else if (navigator.userAgent.includes("linux")) return "linux"; return "other"; } + +export function toHex(value: number): string { + return "0x" + value.toString(16).toUpperCase(); +}