Automerge backed Markdown editor for the Atmosphere
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334// src/plugin/hooks/useReamsActiveFile.tsimport { 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<TFile | null>( () => 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}