refactor: split monolithic state into context-based modules

Extract image, desktop, mobile, and config state into separate context
providers to improve modularity and reduce unnecessary re-renders.

Signed-off-by: Sped0n <hi@sped0n.com>
This commit is contained in:
Sped0n
2026-03-22 19:45:05 +08:00
parent 1c386386f3
commit f25b71a858
20 changed files with 1165 additions and 894 deletions

41
assets/ts/imageState.tsx Normal file
View File

@@ -0,0 +1,41 @@
import {
createContext,
createMemo,
useContext,
type Accessor,
type JSX
} from 'solid-js'
import invariant from 'tiny-invariant'
import type { ImageJSON } from './resources'
export interface ImageState {
images: ImageJSON[]
length: number
}
type ImageStateContextType = Accessor<ImageState>
const ImageStateContext = createContext<ImageStateContextType>()
export function ImageStateProvider(props: {
children?: JSX.Element
images: ImageJSON[]
}): JSX.Element {
const state = createMemo<ImageState>(() => ({
images: props.images,
length: props.images.length
}))
return (
<ImageStateContext.Provider value={state}>
{props.children}
</ImageStateContext.Provider>
)
}
export function useImageState(): ImageStateContextType {
const context = useContext(ImageStateContext)
invariant(context, 'undefined image context')
return context
}