rework state
This commit is contained in:
@@ -88,14 +88,14 @@ export const schema = z.object({
|
||||
disableHaptics: z.boolean().optional(),
|
||||
disableAutoplay: z.boolean().optional(),
|
||||
kawaii: z.boolean().optional(),
|
||||
usedStarterPack: z
|
||||
currentStarterPack: z
|
||||
.object({
|
||||
uri: z.string(),
|
||||
initialFeed: z.string().optional(),
|
||||
isClip: z.boolean().optional(),
|
||||
lastUsedUri: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
usedStarterPacks: z.array(z.string()).optional(),
|
||||
})
|
||||
export type Schema = z.infer<typeof schema>
|
||||
|
||||
@@ -134,5 +134,6 @@ export const defaults: Schema = {
|
||||
disableHaptics: false,
|
||||
disableAutoplay: prefersReducedMotion,
|
||||
kawaii: false,
|
||||
usedStarterPack: undefined,
|
||||
currentStarterPack: undefined,
|
||||
usedStarterPacks: [],
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@ import * as persisted from '#/state/persisted'
|
||||
type StateContext =
|
||||
| {
|
||||
uri: string
|
||||
cid?: string
|
||||
initialFeed?: string
|
||||
isClip?: boolean
|
||||
lastUsedUri?: string
|
||||
}
|
||||
| undefined
|
||||
type SetContext = (v: StateContext) => void
|
||||
@@ -20,12 +20,12 @@ export function Provider({children}: {children: React.ReactNode}) {
|
||||
|
||||
const setStateWrapped = (v: StateContext) => {
|
||||
setState(v)
|
||||
persisted.write('usedStarterPack', v)
|
||||
persisted.write('currentStarterPack', v)
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
return persisted.onUpdate(() => {
|
||||
setState(persisted.get('usedStarterPack'))
|
||||
setState(persisted.get('currentStarterPack'))
|
||||
})
|
||||
}, [])
|
||||
|
||||
@@ -38,5 +38,5 @@ export function Provider({children}: {children: React.ReactNode}) {
|
||||
)
|
||||
}
|
||||
|
||||
export const useUsedStarterPack = () => React.useContext(stateContext)
|
||||
export const useSetUsedStarterPack = () => React.useContext(setContext)
|
||||
export const useCurrentStarterPack = () => React.useContext(stateContext)
|
||||
export const useSetCurrentStarterPack = () => React.useContext(setContext)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import React from 'react'
|
||||
|
||||
import * as persisted from '#/state/persisted'
|
||||
|
||||
type StateContext = string[] | undefined
|
||||
type SetContext = (v: string) => void
|
||||
|
||||
const stateContext = React.createContext<StateContext>([])
|
||||
const setContext = React.createContext<SetContext>((_: string) => {})
|
||||
|
||||
export function Provider({children}: {children: React.ReactNode}) {
|
||||
const [state, setState] = React.useState<StateContext>()
|
||||
|
||||
const setStateWrapped = (v: StateContext) => {
|
||||
persisted.write('usedStarterPacks', [...state, v])
|
||||
setState(prev => [...prev, v])
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
return persisted.onUpdate(() => {
|
||||
setState(persisted.get('usedStarterPack'))
|
||||
})
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<stateContext.Provider value={state}>
|
||||
<setContext.Provider value={setStateWrapped}>
|
||||
{children}
|
||||
</setContext.Provider>
|
||||
</stateContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export const useUsedStarterPacks = () => React.useContext(stateContext)
|
||||
export const useAddUsedStarterPack = () => React.useContext(setContext)
|
||||
Reference in New Issue
Block a user