import React, { createContext, useContext, useMemo } from 'react'; import type { Shade } from '@shade/sdk'; import type { FilesNamespace } from '../integration/files-namespace.js'; export interface ShadeFilesContextValue { shade: Shade; files: FilesNamespace; } const ShadeFilesContext = createContext(null); export interface ShadeFilesProviderProps { /** Initialized `Shade` instance. `files` namespace is read off it lazily. */ shade: Shade; children: React.ReactNode; } /** * Provider for `@shade/files` React hooks. Distinct from * `` in `@shade/widgets` so file-RPC consumers * don't pull in the widget tree. */ export function ShadeFilesProvider({ shade, children }: ShadeFilesProviderProps): React.ReactElement { const value = useMemo(() => ({ shade, files: shade.files }), [shade]); return React.createElement(ShadeFilesContext.Provider, { value }, children); } /** Throws if no `` is mounted above. */ export function useShadeFilesContext(): ShadeFilesContextValue { const ctx = useContext(ShadeFilesContext); if (ctx === null) { throw new Error('useShadeFilesContext: missing '); } return ctx; }