predikt/oracle/web/hooks/use-portfolio-history.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

51 lines
1.6 KiB
TypeScript

import { Period, getCutoff } from 'common/period'
import { PortfolioSnapshot } from 'web/lib/supabase/portfolio-history'
import { useEffect } from 'react'
import { getCurrentPortfolio } from 'common/supabase/portfolio-metrics'
import { db } from 'web/lib/supabase/db'
import { usePersistentInMemoryState } from 'client-common/hooks/use-persistent-in-memory-state'
import { api } from 'web/lib/api/api'
export type PeriodToSnapshots = {
[time: number]: PortfolioSnapshot[]
}
export const usePortfolioHistory = (userId: string, period: Period) => {
const cutoff = getCutoff(period)
const [portfolioHistories, setPortfolioHistories] =
usePersistentInMemoryState<PeriodToSnapshots>(
{},
`user-portfolio-history-${userId}`
)
useEffect(() => {
if (portfolioHistories[cutoff]) return
api('get-user-portfolio-history', { userId, period })
.then((portfolioHistory) => {
setPortfolioHistories((prev) => ({
...prev,
[cutoff]: portfolioHistory,
}))
})
.catch((e) => {
console.error('Failed to get portfolio history', e)
})
}, [userId, setPortfolioHistories, cutoff])
return portfolioHistories[cutoff] as PortfolioSnapshot[] | undefined
}
export const useCurrentPortfolio = (userId: string | null | undefined) => {
const [portfolio, setPortfolio] = usePersistentInMemoryState<
PortfolioSnapshot | null | undefined
>(undefined, `current-portfolio-${userId}`)
useEffect(() => {
if (userId) {
getCurrentPortfolio(userId, db).then(setPortfolio)
}
}, [userId])
return portfolio
}