From 73b837639dfae6d130e57d3bdd667ca576d30b76 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Wed, 19 Jun 2024 19:03:20 -0700 Subject: [PATCH] useUser: also refetch on page becoming visible --- web/hooks/use-user.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/web/hooks/use-user.ts b/web/hooks/use-user.ts index aee47439a7..37a1c143b2 100644 --- a/web/hooks/use-user.ts +++ b/web/hooks/use-user.ts @@ -8,6 +8,7 @@ import { useLiveUpdates } from './use-persistent-supabase-polling' import { run } from 'common/supabase/utils' import { useApiSubscription } from './use-api-subscription' import { getFullUserById, getPrivateUserSafe } from 'web/lib/supabase/users' +import { useIsPageVisible } from './use-page-visible' export const useUser = () => { const authUser = useContext(AuthContext) @@ -27,6 +28,8 @@ export const useIsAuthorized = () => { export const useWebsocketUser = (userId: string | undefined) => { const [user, setUser] = useState() + const isPageVisible = useIsPageVisible() + useApiSubscription({ topics: [`user/${userId ?? '_'}`], onBroadcast: ({ data }) => { @@ -45,6 +48,8 @@ export const useWebsocketUser = (userId: string | undefined) => { }) useEffect(() => { + if (!isPageVisible) return + if (userId) { getFullUserById(userId).then((result) => { setUser(result) @@ -52,7 +57,7 @@ export const useWebsocketUser = (userId: string | undefined) => { } else { setUser(null) } - }, [userId]) + }, [userId, isPageVisible]) return user }