From 7a40a8b43f60bd5ddba3e52869b33b4263fca748 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 28 Jun 2026 16:36:53 +0000 Subject: [PATCH] Avoid repeated composer skill metadata initialization Co-authored-by: Julius Marminge --- .../src/components/ComposerPromptEditor.tsx | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/apps/web/src/components/ComposerPromptEditor.tsx b/apps/web/src/components/ComposerPromptEditor.tsx index 18579cda6d..2f75bb54af 100644 --- a/apps/web/src/components/ComposerPromptEditor.tsx +++ b/apps/web/src/components/ComposerPromptEditor.tsx @@ -237,6 +237,34 @@ function skillMetadataByName( ); } +function useLazyRef(factory: () => T): { current: T } { + const ref = useRef(null); + if (ref.current === null) { + ref.current = factory(); + } + return ref as { current: T }; +} + +function useSkillMetadataRef( + skills: ReadonlyArray, +): { current: ReadonlyMap } { + const ref = useLazyRef(() => skillMetadataByName(skills)); + useEffect(() => { + ref.current = skillMetadataByName(skills); + }, [ref, skills]); + return ref; +} + +function useLayoutSkillMetadataRef( + skills: ReadonlyArray, +): { current: ReadonlyMap } { + const ref = useLazyRef(() => skillMetadataByName(skills)); + useLayoutEffect(() => { + ref.current = skillMetadataByName(skills); + }, [ref, skills]); + return ref; +} + function ComposerSkillDecorator(props: { skillLabel: string; skillDescription: string | null }) { const chip = ( { - skillMetadataRef.current = skillMetadataByName(props.skills); - }, [props.skills]); - const applySurroundInsertion = useEffectEvent((inputData: string): boolean => { const surroundCloseSymbol = SURROUND_SYMBOLS_MAP.get(inputData); const pendingSurroundSelection = pendingSurroundSelectionRef.current; @@ -1404,7 +1428,7 @@ function ComposerPromptEditorInner({ const terminalContextsSignatureRef = useRef(terminalContextsSignature); const skillsSignature = skillSignature(skills); const skillsSignatureRef = useRef(skillsSignature); - const skillMetadataRef = useRef(skillMetadataByName(skills)); + const skillMetadataRef = useLayoutSkillMetadataRef(skills); const snapshotRef = useRef({ value, cursor: initialCursor, @@ -1421,10 +1445,6 @@ function ComposerPromptEditorInner({ onChangeRef.current = onChange; }, [onChange]); - useLayoutEffect(() => { - skillMetadataRef.current = skillMetadataByName(skills); - }, [skills]); - useEffect(() => { editor.setEditable(!disabled); }, [disabled, editor]); @@ -1656,7 +1676,7 @@ export function ComposerPromptEditor({ }: ComposerPromptEditorProps) { const initialValueRef = useRef(value); const initialTerminalContextsRef = useRef(terminalContexts); - const initialSkillMetadataRef = useRef(skillMetadataByName(skills)); + const initialSkillMetadataRef = useLazyRef(() => skillMetadataByName(skills)); const initialConfig = useMemo( () => ({ namespace: "t3tools-composer-editor",