fix: sidebar crash #310 — referenceInChat hook sat behind the no-session early return

The new useCallback was declared after the early return for the no-session
state, so the first render (no session) ran fewer hooks than the follow-up
render once a session arrived — React error #310, boundary strip on load.
Moved the hook above the early return (with a sessionId guard) and verified
headlessly: the error no longer appears and the panel renders fully.
This commit is contained in:
Menghuan1918
2026-08-08 23:51:42 +08:00
parent 6e36a99c88
commit f091ac322b
+22 -13
View File
@@ -185,6 +185,28 @@ export function Sidebar(props: { ctx: Context }) {
},
}), [sessionId, cwd])
/**
* The explorer's @-reference button: append `@<relative path>` to the
* session's composer draft (space-separated). Resolves the session-scope
* ctx and the conversation input service at click time; a missing service
* or scope degrades to a logged no-op, never a crash. Defined above the
* no-session early return — a hook must never sit behind a conditional
* return (React counts hooks per render).
*/
const referenceInChat = useCallback((path: string): void => {
if (sessionId === undefined) return
try {
const actx = ctx.sessions.scope(sessionId)
if (actx === undefined) return
const input = ctx.conversation.input.for(actx)
const mention = `@${relativeTo(cwd ?? '', path)}`
const draft = input.state.getSnapshot().draft
input.setDraft(draft.trim() === '' ? mention : `${draft} ${mention}`)
} catch (error) {
console.warn('[dsh-better-sidebar] reference insert failed:', error)
}
}, [ctx, sessionId, cwd])
if (state === undefined || sessionId === undefined) {
return (
<div className={css.toggleRail}>
@@ -225,19 +247,6 @@ export function Sidebar(props: { ctx: Context }) {
* ctx and the conversation input service at click time; a missing service
* or scope degrades to a logged no-op, never a crash.
*/
const referenceInChat = useCallback((path: string): void => {
try {
const actx = ctx.sessions.scope(sessionId)
if (actx === undefined) return
const input = ctx.conversation.input.for(actx)
const mention = `@${relativeTo(cwd ?? '', path)}`
const draft = input.state.getSnapshot().draft
input.setDraft(draft.trim() === '' ? mention : `${draft} ${mention}`)
} catch (error) {
console.warn('[dsh-better-sidebar] reference insert failed:', error)
}
}, [ctx, sessionId, cwd])
const renderTab = (tab: SidebarTab) => (
<TabContent
tab={tab}