Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
1.3 kB · 39 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940// Hook that creates a LivestreamStore for a given user and manages// the WebSocket connection. The store is recreated when `user` changes,// preventing state from leaking between users on client-side navigation.import { getStreamplaceUrl } from "@/lib/streamplace-url";import { connectLivestreamWebsocket, makeLivestreamStore, type LivestreamStore,} from "@streamplace/core";import { useEffect, useMemo, useState } from "react";
export function useLivestreamStore( user: string, enabled = true,): { store: LivestreamStore | null; ready: boolean;} { // Recreate the store when user changes so stale state from the // previous user (chat, profile, viewers, segment, pinned message, // livestream record) is cleared immediately. const store = useMemo(() => makeLivestreamStore(), [user]); const [ready, setReady] = useState(false);
useEffect(() => { setReady(enabled); }, [store, enabled]);
useEffect(() => { if (!enabled) return; const wsUrl = `${getStreamplaceUrl()}/api/websocket/${user}`; const { disconnect } = connectLivestreamWebsocket(store, wsUrl, { reconnectDelayMs: 3000, }); return disconnect; }, [enabled, store, user]);
return { store: enabled && ready ? store : null, ready: enabled && ready };}