mirror of
https://github.com/VickScarlet/lifeRestart.git
synced 2026-08-29 01:36:47 +08:00
update: character
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { useCallback } from 'react'
|
||||
import { useEffect, useCallback, useTransition } from 'react'
|
||||
import { atom, useAtom } from 'jotai'
|
||||
import { useConfigInject, useRawProfile, useProfileInject } from '@remake/hooks'
|
||||
import { useConfigInject, useProfileInject, useRawProfile } from '@remake/hooks'
|
||||
import { useUniqueInject, useUnique } from '@remake/hooks'
|
||||
import { get, set } from '@/storage'
|
||||
import { config } from '@/config'
|
||||
|
||||
@@ -9,11 +10,12 @@ const initedAtom = atom(false)
|
||||
export const useInit = () => {
|
||||
const configInject = useConfigInject()
|
||||
const profileInject = useProfileInject()
|
||||
const uniqueInject = useUniqueInject()
|
||||
const [inited, setInited] = useAtom(initedAtom)
|
||||
const loader = useCallback(async () => {
|
||||
if (inited) return
|
||||
configInject(config)
|
||||
const { profile } = await get(['profile'])
|
||||
const { profile, unique } = await get(['profile', 'unique'])
|
||||
const parsed = profile ? JSON.parse(profile) || {} : {}
|
||||
profileInject({
|
||||
...parsed,
|
||||
@@ -22,15 +24,19 @@ export const useInit = () => {
|
||||
events: new Set(parsed.events || []),
|
||||
talents: new Set(parsed.talents || []),
|
||||
})
|
||||
if (unique) uniqueInject(JSON.parse(unique))
|
||||
setInited(true)
|
||||
}, [inited, configInject, profileInject, setInited])
|
||||
}, [inited, configInject, profileInject, uniqueInject, setInited])
|
||||
return [inited, loader] as const
|
||||
}
|
||||
|
||||
export const useSaver = () => {
|
||||
export const useWatcher = () => {
|
||||
const [profile] = useRawProfile()
|
||||
const unique = useUnique()
|
||||
const [inited] = useAtom(initedAtom)
|
||||
return useCallback(async () => {
|
||||
const [p, saveProfile] = useTransition()
|
||||
const [u, saveUnique] = useTransition()
|
||||
useEffect(() => {
|
||||
if (!inited || !profile) return
|
||||
const str = JSON.stringify({
|
||||
...profile,
|
||||
@@ -39,6 +45,17 @@ export const useSaver = () => {
|
||||
events: Array.from(profile.events),
|
||||
talents: Array.from(profile.talents),
|
||||
})
|
||||
return await set({ profile: str })
|
||||
}, [inited, profile])
|
||||
saveProfile(async () => {
|
||||
await set({ profile: str })
|
||||
})
|
||||
}, [inited, profile, saveProfile])
|
||||
useEffect(() => {
|
||||
if (!inited || !unique) return
|
||||
const str = JSON.stringify(unique)
|
||||
saveUnique(async () => {
|
||||
await set({ unique: str })
|
||||
})
|
||||
}, [inited, unique, saveUnique])
|
||||
|
||||
return p || u
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user