Spaces:
Sleeping
Sleeping
Naufal Arhab
chore: full codebase audit fixes, L2/whale/calibration upgrades, and rebuild frontend dist
48f60d7 Download frontend/src/hooks/useSignals.ts from agus1111/Perps-Terminal: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/spaces/agus1111/Perps-Terminal/resolve/main/frontend/src/hooks/useSignals.ts
- Command line
-
hf download hf://spaces/agus1111/Perps-Terminal/frontend/src/hooks/useSignals.ts
-
curl -L -o useSignals.ts https://huggingface.co/spaces/agus1111/Perps-Terminal/resolve/main/frontend/src/hooks/useSignals.ts
2.19 kB
| import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; | |
| import { signalsApi } from '../api/signals'; | |
| import { resolveWsUrl } from '../api/client'; | |
| import type { Signal } from '../types'; | |
| export const SIGNALS_QUERY_KEY = ['signals'] as const; | |
| export function useSignals() { | |
| const hasWs = Boolean(resolveWsUrl()); | |
| return useQuery<Signal[], Error>({ | |
| queryKey: SIGNALS_QUERY_KEY, | |
| queryFn: signalsApi.getAll, | |
| placeholderData: (previousData) => previousData, | |
| // When WebSocket is active, poll passively every 30s. If serverless without WS, poll every 6s for live updates. | |
| refetchInterval: () => (resolveWsUrl() ? 30000 : 6000), | |
| refetchOnWindowFocus: false, // Prevent abrupt full re-renders on tab switch | |
| retry: 2, | |
| staleTime: hasWs ? 15000 : 4000, | |
| }); | |
| } | |
| export function useSignal(symbol: string) { | |
| return useQuery<Signal, Error>({ | |
| queryKey: [...SIGNALS_QUERY_KEY, symbol], | |
| queryFn: () => signalsApi.getBySymbol(symbol), | |
| placeholderData: (previousData) => previousData, | |
| refetchInterval: () => (resolveWsUrl() ? 30000 : 6000), | |
| refetchOnWindowFocus: false, | |
| enabled: !!symbol, | |
| }); | |
| } | |
| export function useRefreshSignal() { | |
| const queryClient = useQueryClient(); | |
| return useMutation({ | |
| mutationFn: (symbol: string) => signalsApi.refresh(symbol), | |
| onSuccess: (data) => { | |
| queryClient.setQueryData([...SIGNALS_QUERY_KEY, data.symbol], data); | |
| queryClient.setQueryData<Signal[]>(SIGNALS_QUERY_KEY, (old) => { | |
| if (!old) return [data]; | |
| const idx = old.findIndex((s) => s.symbol === data.symbol); | |
| if (idx === -1) return [...old, data]; | |
| const next = [...old]; | |
| next[idx] = data; | |
| return next; | |
| }); | |
| queryClient.invalidateQueries({ queryKey: SIGNALS_QUERY_KEY }); | |
| }, | |
| }); | |
| } | |
| export function useRefreshAllSignals() { | |
| const queryClient = useQueryClient(); | |
| return useMutation({ | |
| mutationFn: () => signalsApi.refreshAll(), | |
| onSuccess: (data) => { | |
| if (data?.signals) { | |
| queryClient.setQueryData(SIGNALS_QUERY_KEY, data.signals); | |
| } | |
| queryClient.invalidateQueries({ queryKey: SIGNALS_QUERY_KEY }); | |
| }, | |
| }); | |
| } | |