predikt/oracle/web/hooks/use-chart-annotations.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

61 lines
1.8 KiB
TypeScript

import { useState, useEffect } from 'react'
import { PointerMode } from 'web/components/charts/helpers'
import { useUser } from 'web/hooks/use-user'
import { Contract } from 'common/contract'
import { isAdminId, isModId } from 'common/envs/constants'
import { ChartAnnotation } from 'common/supabase/chart-annotations'
import { useApiSubscription } from 'client-common/hooks/use-api-subscription'
import { sortBy } from 'lodash'
export const useAnnotateChartTools = (
contract: Contract,
initialChartAnnotations: ChartAnnotation[]
) => {
const [pointerMode, setPointerMode] = useState<PointerMode>('zoom')
const [hoveredAnnotation, setHoveredAnnotation] = useState<number | null>(
null
)
const user = useUser()
const [chartAnnotations, setChartAnnotations] = useState<ChartAnnotation[]>(
initialChartAnnotations
)
const updateHoveredAnnotation = async (hoveredAnnotation: number | null) => {
if (pointerMode === 'annotate') return
if (hoveredAnnotation !== null) {
setPointerMode('examine')
} else {
setPointerMode('zoom')
}
setHoveredAnnotation(hoveredAnnotation)
}
useEffect(() => {
if (pointerMode === 'annotate') setPointerMode('zoom')
}, [chartAnnotations.length])
useApiSubscription({
topics: [`contract/${contract.id}/chart-annotation`],
onBroadcast: (msg) => {
const newAnnotation = msg.data.annotation as ChartAnnotation
setChartAnnotations((prevAnnotations) =>
sortBy([...prevAnnotations, newAnnotation], (c) => c.event_time)
)
},
})
const enableAdd =
user &&
(user.id === contract.creatorId || isModId(user.id) || isAdminId(user.id))
return {
pointerMode,
setPointerMode,
hoveredAnnotation,
setHoveredAnnotation: updateHoveredAnnotation,
chartAnnotations,
enableAdd,
}
}