predikt/oracle/web/hooks/use-graph-user.ts
caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

54 lines
1.5 KiB
TypeScript

import { DisplayUser } from 'common/api/user-types'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { api } from 'web/lib/api/api'
const GRAPH_USER_KEY = 'graphUser'
export const useGraphUserFromUrl = () => {
const router = useRouter()
const [graphUser, setGraphUserState] = useState<DisplayUser | undefined>()
const [ready, setReady] = useState(false)
// On page load, read graphUser from URL and fetch user data
useEffect(() => {
if (!router.isReady) return
const graphUserId = router.query[GRAPH_USER_KEY]
if (typeof graphUserId === 'string' && graphUserId) {
// Fetch user by ID
api('user/by-id/:id/lite', { id: graphUserId })
.then((user) => {
if (user) {
setGraphUserState(user)
}
})
.catch(() => {
// User not found, clear from URL
updateUrl(undefined)
})
.finally(() => setReady(true))
} else {
setReady(true)
}
}, [router.isReady])
const updateUrl = (userId: string | undefined) => {
const { pathname, query } = router
const newQuery = { ...query }
if (userId) {
newQuery[GRAPH_USER_KEY] = userId
} else {
delete newQuery[GRAPH_USER_KEY]
}
router.replace({ pathname, query: newQuery }, undefined, { shallow: true })
}
const setGraphUser = (user: DisplayUser | undefined) => {
setGraphUserState(user)
updateUrl(user?.id)
}
return { graphUser, setGraphUser, ready }
}