|
| 1 | +import { useEffect, useRef } from "react"; |
| 2 | + |
| 3 | +import { DASHBOARD_DEVELOPMENT_WS_PORT } from "@repo/common/constants"; |
| 4 | +import { WsData, WsDataSchema } from "@repo/common/types-schemas"; |
| 5 | + |
| 6 | +export const initializeWebSocket = () => { |
| 7 | + const isDev = import.meta.env.DEV; |
| 8 | + let wsUrl: string; |
| 9 | + |
| 10 | + if (isDev) { |
| 11 | + wsUrl = `ws://localhost:${DASHBOARD_DEVELOPMENT_WS_PORT}`; |
| 12 | + } else { |
| 13 | + const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; |
| 14 | + wsUrl = `${protocol}//${window.location.host}`; |
| 15 | + } |
| 16 | + |
| 17 | + const ws = new WebSocket(wsUrl); |
| 18 | + |
| 19 | + ws.onopen = () => { |
| 20 | + ws.send(JSON.stringify({ type: "ready" } satisfies WsData)); |
| 21 | + }; |
| 22 | + |
| 23 | + ws.onmessage = async (event) => { |
| 24 | + try { |
| 25 | + const data = JSON.parse(event.data); |
| 26 | + const validated = WsDataSchema.safeParse(data); |
| 27 | + if (!validated.success) { |
| 28 | + console.error("Invalid message shape"); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const { type } = validated.data; |
| 33 | + |
| 34 | + if (type === "navigate") { |
| 35 | + const { path } = validated.data; |
| 36 | + |
| 37 | + window.history.pushState({}, "", path); |
| 38 | + window.dispatchEvent(new PopStateEvent("popstate")); |
| 39 | + |
| 40 | + if (window.Notification && Notification.permission === "granted") { |
| 41 | + const notification = new Notification("Dashboard", { |
| 42 | + body: "Click to go to the dashboard", |
| 43 | + icon: "/moon.svg", |
| 44 | + requireInteraction: false, |
| 45 | + }); |
| 46 | + |
| 47 | + notification.onclick = () => { |
| 48 | + window.focus(); |
| 49 | + notification.close(); |
| 50 | + }; |
| 51 | + |
| 52 | + setTimeout(() => notification.close(), 3000); |
| 53 | + } else if ( |
| 54 | + window.Notification && |
| 55 | + Notification.permission === "default" |
| 56 | + ) { |
| 57 | + Notification.requestPermission(); |
| 58 | + } else { |
| 59 | + window.focus(); |
| 60 | + } |
| 61 | + |
| 62 | + ws.send(JSON.stringify({ type: "navigated", path } satisfies WsData)); |
| 63 | + } |
| 64 | + } catch (error) { |
| 65 | + console.error("Failed to handle WebSocket message:", error); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + ws.onerror = (error) => { |
| 70 | + console.error("WebSocket error:", error); |
| 71 | + }; |
| 72 | + |
| 73 | + return ws; |
| 74 | +}; |
| 75 | + |
| 76 | +const useExtensionWebsocket = () => { |
| 77 | + const wsRef = useRef<WebSocket>(null); |
| 78 | + |
| 79 | + useEffect(() => { |
| 80 | + wsRef.current = initializeWebSocket(); |
| 81 | + |
| 82 | + return () => { |
| 83 | + if (wsRef.current) { |
| 84 | + if (wsRef.current.readyState === WebSocket.OPEN) { |
| 85 | + wsRef.current.send( |
| 86 | + JSON.stringify({ type: "closed" } satisfies WsData), |
| 87 | + ); |
| 88 | + } |
| 89 | + wsRef.current.close(); |
| 90 | + } |
| 91 | + }; |
| 92 | + }, []); |
| 93 | +}; |
| 94 | + |
| 95 | +export default useExtensionWebsocket; |
0 commit comments