Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download web_app/src/components/stance/ActiveScreen.tsx from CommunityOne/open-navigator: direct link, hf CLI and curl.
- Browser
- Download file 2.11 kB
-
https://huggingface.co/spaces/CommunityOne/open-navigator/resolve/main/web_app/src/components/stance/ActiveScreen.tsx
- Command line
-
hf download hf://spaces/CommunityOne/open-navigator/web_app/src/components/stance/ActiveScreen.tsx
-
curl -L -o ActiveScreen.tsx https://huggingface.co/spaces/CommunityOne/open-navigator/resolve/main/web_app/src/components/stance/ActiveScreen.tsx
2.11 kB
| import { createContext, memo, useContext, useSyncExternalStore, type ReactNode } from 'react' | |
| /** | |
| * The screen the reader is on (#1411). Moving to the next screen used to re-render every panel of the story (all of them are | |
| * mounted at once), because `active` was a prop of each. The index now lives in a small store: a panel subscribes to the one | |
| * question it asks ("am I the active screen?"), so a Next re-renders the two frames whose answer changed, and the rest of each | |
| * panel, held by `MemoPanel`, is left alone. | |
| */ | |
| export interface ActiveScreenStore { | |
| get: () => number | null | |
| set: (i: number) => void | |
| subscribe: (fn: () => void) => () => void | |
| } | |
| export function createActiveScreenStore(): ActiveScreenStore { | |
| let at: number | null = null | |
| const subs = new Set<() => void>() | |
| return { | |
| get: () => at, | |
| set: (i) => { | |
| if (i === at) return | |
| at = i | |
| subs.forEach((fn) => fn()) | |
| }, | |
| subscribe: (fn) => { | |
| subs.add(fn) | |
| return () => subs.delete(fn) | |
| }, | |
| } | |
| } | |
| export const ActiveScreenContext = createContext<ActiveScreenStore | null>(null) | |
| /** Whether screen `index` is the one in view. Without a provider (a story that still passes `active`), `fallback` decides. */ | |
| export function useScreenActive(index: number, fallback = false): boolean { | |
| const store = useContext(ActiveScreenContext) | |
| return useSyncExternalStore( | |
| store ? store.subscribe : noSubscribe, | |
| () => (store ? store.get() === index : fallback), | |
| () => (store ? store.get() === index : fallback), | |
| ) | |
| } | |
| const noSubscribe = () => () => {} | |
| interface MemoPanelProps { | |
| /** Everything `render` reads other than the active screen; the panel is drawn again only when one of them changes. */ | |
| deps: readonly unknown[] | |
| render: () => ReactNode | |
| } | |
| /** One screen's content, drawn again only when its `deps` change (never because the reader moved to another screen). */ | |
| export const MemoPanel = memo( | |
| function MemoPanel({ render }: MemoPanelProps) { | |
| return <>{render()}</> | |
| }, | |
| (a, b) => a.deps.length === b.deps.length && a.deps.every((d, i) => Object.is(d, b.deps[i])), | |
| ) | |