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
+44
View File
@@ -0,0 +1,44 @@
import { useCallback, useTransition } from 'react'
import { atom, useAtom } from 'jotai'
import { useConfigInject, useProfile, useProfileInject } from '@remake/hooks'
import { get, set } from '@/storage'
import { config } from '@/config'
const initedAtom = atom(false)
export const useInit = () => {
const configInject = useConfigInject()
const profileInject = useProfileInject()
const [inited, setInited] = useAtom(initedAtom)
const [_, startTransition] = useTransition()
const loader = useCallback(() => {
configInject(config)
startTransition(async () => {
const { profile } = await get(['profile'])
const parsed = profile ? JSON.parse(profile) || {} : {}
profileInject({
...parsed,
times: parsed.times || 0,
achievements: new Set(parsed.achievements || []),
events: new Set(parsed.events || []),
talents: new Set(parsed.talents || []),
})
setInited(true)
})
}, [])
return [inited, loader] as const
}
export const useSaver = () => {
const [profile] = useProfile()
const saver = useCallback(() => {
const str = JSON.stringify({
times: profile.times,
achievements: Array.from(profile.achievements),
events: Array.from(profile.events),
talents: Array.from(profile.talents),
})
set({ profile: str })
}, [profile])
return saver
}