Skip to content

Commit

Permalink
useUser: also refetch on page becoming visible
Browse files Browse the repository at this point in the history
  • Loading branch information
jahooma committed Jun 20, 2024
1 parent 477e5d8 commit 73b8376
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion web/hooks/use-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -27,6 +28,8 @@ export const useIsAuthorized = () => {
export const useWebsocketUser = (userId: string | undefined) => {
const [user, setUser] = useState<User | null | undefined>()

const isPageVisible = useIsPageVisible()

useApiSubscription({
topics: [`user/${userId ?? '_'}`],
onBroadcast: ({ data }) => {
Expand All @@ -45,14 +48,16 @@ export const useWebsocketUser = (userId: string | undefined) => {
})

useEffect(() => {
if (!isPageVisible) return

if (userId) {
getFullUserById(userId).then((result) => {
setUser(result)
})
} else {
setUser(null)
}
}, [userId])
}, [userId, isPageVisible])

return user
}
Expand Down

0 comments on commit 73b8376

Please sign in to comment.