TypeScript
refactor(web): find reveal refs stay cached without "use memo" (#20818)
Julius Marminge8eddd16067aa
diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsxindex d1b810ed56f..68ef3e7c138 100644--- a/apps/web/src/components/ChatMarkdown.tsx+++ b/apps/web/src/components/ChatMarkdown.tsx@@ -1,4 +1,4 @@-import { MarkdownFindContext, useFindRevealRef } from "./chat/markdownFindContext";+import { MarkdownFindContext, findRevealRef } from "./chat/markdownFindContext";import {buildFileLinkParentSuffixByPath,fileLinkLabel,@@ -668,7 +668,7 @@ function MarkdownDetails({const revealForFind = () => setIsOpen(true);// Base UI only listens for `beforematch` on a panel mounted at its first// render; a closed panel mounts later, when find starts, so listen here.- const findRevealRef = useFindRevealRef(revealForFind);+ const revealRef = findRevealRef(revealForFind);const childNodes = Children.toArray(children);const summaryIndex = childNodes.findIndex((child) => isValidElement(child) && child.type === "summary",@@ -698,7 +698,7 @@ function MarkdownDetails({/><span>{summary}</span></CollapsibleTrigger>- <CollapsiblePanel ref={findRevealRef} hiddenUntilFound={searching}>+ <CollapsiblePanel ref={revealRef} hiddenUntilFound={searching}><divclassName="pb-3 ps-6 text-foreground/[calc(80%+var(--appearance-contrast-boost)/5)]"data-markdown-details-content=""@@ -984,7 +984,7 @@ function MarkdownMermaidCodeBlock({const [showCode, setShowCode] = useState(false);const searching = use(MarkdownFindContext);const revealSource = () => setShowCode(true);- const sourceRevealRef = useFindRevealRef(revealSource);+ const sourceRevealRef = findRevealRef(revealSource);const showDiagram = !showCode && !isStreaming && code.trim().length > 0;const toggleLabel = showCode ? "Show diagram" : "Show code";return (diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex 8f16ffd2f6b..7e3e8ece56b 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -1,7 +1,7 @@import { ThreadFindTimelineContext } from "./ThreadFindProvider";import { shallow } from "zustand/vanilla/shallow";import { shouldPreserveAssistantLineBreaks } from "@t3tools/shared/markdownPipeline";-import { MarkdownFindContext, useFindRevealRef } from "./markdownFindContext";+import { MarkdownFindContext, findRevealRef } from "./markdownFindContext";import { useChatCanvas } from "./ChatCanvasContext";import { WorkLogBlock, WorkLogButton, WorkLogDetails, WorkLogList, WorkLogRow } from "./WorkLog";import { PullRequestGlyph } from "../pullRequest/pullRequestIcons";@@ -4509,7 +4509,7 @@ const CollapsibleUserMessageBody = memo(function CollapsibleUserMessageBody(prop}) {const [expanded, setExpanded] = useState(false);const revealForFind = () => setExpanded(true);- const findRevealRef = useFindRevealRef(revealForFind);+ const revealRef = findRevealRef(revealForFind);const hasVisibleBody = props.text.trim().length > 0;const canCollapse = hasVisibleBody && shouldCollapseUserMessage(props.text);const isCollapsed = canCollapse && !expanded;@@ -4519,7 +4519,7 @@ const CollapsibleUserMessageBody = memo(function CollapsibleUserMessageBody(prop<div>{hasVisibleBody ? (<div- ref={findRevealRef}+ ref={revealRef}className={cn("relative", isCollapsed && "max-h-44 overflow-hidden")}data-user-message-body="true"data-thread-find-text="true"diff --git a/apps/web/src/components/chat/ProposedPlanCard.tsx b/apps/web/src/components/chat/ProposedPlanCard.tsxindex 3f72baad200..534c3ba93f8 100644--- a/apps/web/src/components/chat/ProposedPlanCard.tsx+++ b/apps/web/src/components/chat/ProposedPlanCard.tsx@@ -1,6 +1,6 @@import { proposedPlanTitle, stripDisplayedPlanMarkdown } from "@t3tools/shared/proposedPlanText";import { memo, useState, useId } from "react";-import { useFindRevealRef } from "./markdownFindContext";+import { findRevealRef } from "./markdownFindContext";import {isAtomCommandInterrupted,squashAtomCommandFailure,@@ -85,7 +85,7 @@ export const ProposedPlanCard = memo(function ProposedPlanCard({// While finding, the full plan stays mounted but clipped, so a match past the// preview can be counted and then opened only once it is selected.const showPreview = isCollapsed && !findActive;- const findRevealRef = useFindRevealRef(() => setExpanded(true));+ const revealRef = findRevealRef(() => setExpanded(true));const downloadFilename = buildProposedPlanMarkdownFilename(planMarkdown);const saveContents = normalizePlanMarkdownForExport(planMarkdown);@@ -191,7 +191,7 @@ export const ProposedPlanCard = memo(function ProposedPlanCard({</div><div className="mt-4"><div- ref={findRevealRef}+ ref={revealRef}className={cn("relative", isCollapsed && "max-h-104 overflow-hidden")}data-thread-find-text="true"data-thread-find-fold={isCollapsed ? "" : undefined}diff --git a/apps/web/src/components/chat/markdownFindContext.ts b/apps/web/src/components/chat/markdownFindContext.tsindex 0f9ad661a91..23a8e372009 100644--- a/apps/web/src/components/chat/markdownFindContext.ts+++ b/apps/web/src/components/chat/markdownFindContext.ts@@ -6,11 +6,10 @@ export const MarkdownFindContext = createContext(false);/*** Find dispatches `beforematch` on hidden or clipped ancestors of the selected- * match, like the browser's own find-in-page. `onReveal` must be stable.+ * match, like the browser's own find-in-page. Callers create the ref during render+ * from a stable `onReveal`, so the compiler caches it and React attaches it once.*/-export function useFindRevealRef(onReveal: () => void) {- // The compiler skips hookless functions; a new ref callback each render would re-add the listener.- "use memo";+export function findRevealRef(onReveal: () => void) {return (element: HTMLElement | null) => {if (!element) return;element.addEventListener("beforematch", onReveal);