Files
ottyl/webapp/src/lib/docsNav.tsx
T
Hugh KaznowskiandClaude Sonnet 5 00c21c8032 Trim comments across Rust and webapp source to direct one-liners (ticket 030)
Pass over src/*.rs, webapp/src/**, and the in-app "?" docs prose:
condensed multi-paragraph doc comments to single lines that state the
non-obvious why, deleted comments that only restated adjacent code, and
tightened the Docs.tsx help-panel wording. No logic, structure, or code
formatting changed — verified via cargo test (143 passed) and
tsc -b && vite build.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XFUNZwCvz3kcod3352ZR2o
2026-09-05 17:02:33 +01:00

36 lines
1.2 KiB
TypeScript

import { createContext, useCallback, useContext, useMemo, useRef, useState, type ReactNode } from 'react'
// One request to open the Docs panel scrolled to a specific section.
export interface DocsTarget {
sectionId: string
/** Bumped on every request, even to the same section, so a repeat click re-triggers the flash. */
nonce: number
}
interface DocsNavValue {
target: DocsTarget | null
openSection: (sectionId: string) => void
}
const DocsNavContext = createContext<DocsNavValue | null>(null)
/** Wraps the app so any component can open Docs to a specific section via `useDocsNav`. */
export function DocsNavProvider({ children }: { children: ReactNode }) {
const [target, setTarget] = useState<DocsTarget | null>(null)
const nonceRef = useRef(0)
const openSection = useCallback((sectionId: string) => {
nonceRef.current += 1
setTarget({ sectionId, nonce: nonceRef.current })
}, [])
const value = useMemo(() => ({ target, openSection }), [target, openSection])
return <DocsNavContext.Provider value={value}>{children}</DocsNavContext.Provider>
}
export function useDocsNav(): DocsNavValue {
const ctx = useContext(DocsNavContext)
if (!ctx) throw new Error('useDocsNav must be used within a DocsNavProvider')
return ctx
}