// src/plugin/hooks/useReamsActiveFile.ts import { useState, useEffect } from 'react' import type { App } from '../app' import type { TFile } from '../files' /** * React hook that returns the currently active file via workspace 'file-open' event. * Used by panel components to track which document is active without coupling to * LayoutContext. * * The workspace is the single source of truth for active file — this hook only reads. * * @internal Built-in UI uses this for React integration. External plugins should * use `app.workspace.getActiveFile()` and `app.workspace.on('file-open', ...)` directly. */ export function useReamsActiveFile(app: App): TFile | null { const [activeFile, setActiveFile] = useState( () => app.workspace.getActiveFile() ) useEffect(() => { // Sync on mount in case state changed between initial render and effect setActiveFile(app.workspace.getActiveFile()) const ref = app.workspace.on('file-open', (file) => { setActiveFile(file) }) return () => { app.workspace.offref(ref) } }, [app]) return activeFile }