update: hooks, web

This commit is contained in:
Vick Scarlet
2026-08-16 20:06:16 +08:00
committed by 神戸小鳥
parent 2f91709d1e
commit 322f775796
67 changed files with 2339 additions and 2243 deletions
+100
View File
@@ -0,0 +1,100 @@
import { useState, useCallback } from 'react'
import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai'
import { useConfig } from './config'
import { useProfile } from './profile'
import { useSetStep, Step } from './play'
import { pull, exclude, talentsPicked } from '@remake/core'
import type { TalentsPickedResult, RNG } from '@remake/core'
import type { Talent } from '@remake/data/talent'
const pickedAtom = atom(new Set<Talent['id']>())
const replacedAtom = atom<TalentsPickedResult | null>(null)
export const usePicked = () => {
const picked = useAtomValue(pickedAtom)
if (!picked) throw new Error('No talents picked')
return picked
}
export const useReplaced = () => {
const replaced = useAtomValue(replacedAtom)
if (!replaced) throw new Error('No talents replaced')
return replaced
}
export const useTalentPuller = () => {
const { pull: p } = useConfig()
const [profile] = useProfile()
const [pulled, setPulled] = useState<Talent['id'][] | null>(null)
const puller = useCallback(
(rng?: RNG) => setPulled(pull(p, profile, rng)),
// (rng?: RNG) =>
// setPulled([
// 1142, 1143, 1144, 1145, 1146, 1086, 1122, 1111, 1130, 1048,
// ]),
[p, profile, setPulled],
)
return [pulled, puller] as const
}
export type TalentPickerResult =
| {
type: 'ok'
talent?: never
}
| {
type: 'not-enough'
talent?: never
}
| {
type: 'exclude'
talent: Talent['id']
}
export const useTalentPicker = () => {
const { pick } = useConfig()
const [picked, setPicked] = useAtom(pickedAtom)
const picker = useCallback(
(talent: Talent['id']): TalentPickerResult => {
if (!picked) {
setPicked(new Set([talent]))
return { type: 'ok' }
}
if (picked.has(talent)) {
const next = new Set(picked)
next.delete(talent)
setPicked(next)
return { type: 'ok' }
}
if (picked.size >= pick) return { type: 'not-enough' }
const e = exclude(talent, picked)
if (e) return { type: 'exclude', talent: e }
const next = new Set([...picked, talent])
setPicked(next)
return { type: 'ok' }
},
[pick, picked, setPicked],
)
return [picked, picker] as const
}
export const useSubmitIsEnable = () => {
const { pick } = useConfig()
const picked = useAtomValue(pickedAtom)
const enabled = picked.size >= pick
return [enabled, pick] as const
}
export const useTalentSubmit = () => {
const picked = useAtomValue(pickedAtom)
const setReplaced = useSetAtom(replacedAtom)
const [enabled] = useSubmitIsEnable()
const next = useSetStep()
return useCallback(
(rng?: RNG) => {
if (!enabled) throw new Error('Not enough talents picked')
setReplaced(talentsPicked(picked, rng))
next(Step.Alloc)
},
[enabled, picked, setReplaced, next],
)
}