mirror of
https://github.com/Sped0n/bridget.git
synced 2026-04-22 14:09:30 -07:00
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:
41
assets/ts/imageState.tsx
Normal file
41
assets/ts/imageState.tsx
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user