traintrape-moi/client/components/GameProvider.tsx

26 lines
780 B
TypeScript

import { useAuth } from '@/hooks/useAuth'
import { useUpdateGameState } from '@/hooks/useGame'
import { useQuery } from '@tanstack/react-query'
import { ReactNode, useEffect } from 'react'
export default function GameProvider({ children }: { children: ReactNode }) {
const auth = useAuth()
const updateGameState = useUpdateGameState()
const gameQuery = useQuery({
queryKey: ['update-game'],
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/`, {
headers: { "Authorization": `Bearer ${auth.token}` }}
).then(resp => resp.json()),
enabled: auth.loggedIn,
refetchInterval: 5000,
})
const game = gameQuery.data
useEffect(() => {
if (game)
updateGameState(game)
}, [game])
return <>
{children}
</>
}