Files
bridget/assets/ts/imageState.tsx
Sped0n f25b71a858 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>
2026-03-22 19:45:05 +08:00

42 lines
891 B
TypeScript

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
}