A tab body keys memos and effects off `scope` (the diff pane resolves its
media/html routes from it, the editor restores its preview scroll, the side
chat rebuilds its prose-rewrite cache), but the tab cell built
`{ sessionId, cwd }` fresh on every render. Every such dependency therefore
looked changed whenever the cell re-rendered — which the memo comparator
allows exactly when a COMPARED field moves: `expanded` on a directory toggle,
plus `revealed`, `localeRevision`, `tabsVersion`. The diff preview then
re-ran its whole-document rewrite and its content tab re-issued a whole-file
`fs.read`, flashing its loading row.
`useSessionScope` is now the one place a scope for a component tree is built;
the tab cell, the diff tab, the native tab adapter, and the shell's badge /
rightActions / `+` menu / lifecycle-callback seams all use it. Those last
ones are documented external plug-in surfaces, so a plug-in memoizing on
`scope` stops churning on every unrelated re-render too.
Stabilizing the object removes the only path the diff pane's `content` tab
had to re-read after the file changed on disk, so the header's refresh button
now drives it explicitly (the pane owns the gesture; the loader no longer
leaks its internal counter).
Seven comments asserted that the scope object's identity churns on every
render; that premise is gone, so the ones that still apply say what they
actually guard.
35 lines
1.6 KiB
TypeScript
35 lines
1.6 KiB
TypeScript
/**
|
|
* The one place a {@link SessionScope} object is built for a component tree.
|
|
*
|
|
* A scope is handed to tab bodies, badges, right-actions and the + menu, and
|
|
* those key memos and effects off it. Building `{ sessionId, cwd }` inline on
|
|
* every render made every such dependency look changed whenever the sidebar
|
|
* shell re-rendered, so the same bug kept reappearing one call site at a time
|
|
* (the tab cell's diff preview re-ran its whole-document rewrite and its
|
|
* content tab re-issued a whole-file `fs.read`; a badge or an external
|
|
* plugin's memo would churn the same way).
|
|
*
|
|
* Consumers that hand a scope to a tab body must use this; consumers that only
|
|
* build one to pass INTO an api call (`api.fsRead({ sessionId, cwd }, …)`) do
|
|
* not — the object never outlives the call there.
|
|
*/
|
|
import { useMemo } from 'react'
|
|
import type { SessionScope } from './api.ts'
|
|
|
|
/**
|
|
* A scope object keyed by CONTENT: the same object is returned across renders
|
|
* until the session or the cwd really changes.
|
|
*
|
|
* @param sessionId - the conversation id, or `undefined` for a shell mounted
|
|
* with no conversation selected (the scope is then absent).
|
|
* @param cwd - the session's working directory, when known.
|
|
*/
|
|
export function useSessionScope(sessionId: string, cwd: string | undefined): SessionScope
|
|
export function useSessionScope(sessionId: string | undefined, cwd: string | undefined): SessionScope | undefined
|
|
export function useSessionScope(sessionId: string | undefined, cwd: string | undefined): SessionScope | undefined {
|
|
return useMemo<SessionScope | undefined>(
|
|
() => (sessionId === undefined ? undefined : { sessionId, cwd }),
|
|
[sessionId, cwd],
|
|
)
|
|
}
|