import { useMemo } from 'react'; import { analyze, type Analysis } from '../analysis/analyzer'; import type { SeoEditorEnv } from '../env'; import { useAnalysisInput } from './use-analysis-input'; import { useEditedPost, usePostMeta } from './use-seo-meta'; /** * The analysis of the post as it is edited, against its first keyword, * outside the panel: for the toolbar score and the publish check. * * @param env - The editor's environment. * @return The analysis, null where the object keeps no score, and the keyword. */ export const useLiveAnalysis = ( env: SeoEditorEnv ): { analysis: Analysis | null; keyword: string } => { const { meta } = usePostMeta(); const post = useEditedPost(); const { input, live } = useAnalysisInput(env, meta, post); const scored = env.fields.includes(env.keys.score); const analysis = useMemo( () => scored ? analyze({ ...input, keyword: live.keyword, keywordUsedElsewhere: null, }) : null, [scored, input, live.keyword] ); return { analysis, keyword: live.keyword }; };