onboarding completed

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-04-16 11:41:15 +02:00
committed by Carl-Gerhard Lindesvärd
parent 97627583ec
commit 7d22d2ddad
79 changed files with 2542 additions and 805 deletions

View 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);
}
},
});
}