onboarding completed
This commit is contained in:
committed by
Carl-Gerhard Lindesvärd
parent
97627583ec
commit
7d22d2ddad
32
apps/dashboard/src/hooks/useWS.ts
Normal file
32
apps/dashboard/src/hooks/useWS.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import useWebSocket from 'react-use-websocket';
|
||||
|
||||
import { getSuperJson } from '@openpanel/common';
|
||||
|
||||
export default function useWS<T>(path: string, onMessage: (event: T) => void) {
|
||||
const ws = String(process.env.NEXT_PUBLIC_API_URL)
|
||||
.replace(/^https/, 'wss')
|
||||
.replace(/^http/, 'ws');
|
||||
const [socketUrl, setSocketUrl] = useState(`${ws}${path}`);
|
||||
|
||||
useEffect(() => {
|
||||
if (socketUrl === `${ws}${path}`) return;
|
||||
setSocketUrl(`${ws}${path}`);
|
||||
}, [path, socketUrl, ws]);
|
||||
|
||||
useWebSocket(socketUrl, {
|
||||
shouldReconnect: () => true,
|
||||
onMessage(event) {
|
||||
try {
|
||||
const data = getSuperJson<T>(event.data);
|
||||
if (data) {
|
||||
onMessage(data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error parsing message', error);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user