Skip to content

Commit 07348d1

Browse files
authored
fix(app): preserve question dock state across session switches (#16173)
1 parent 5f40bd4 commit 07348d1

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

packages/app/src/pages/session/composer/session-question-dock.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,27 @@ import type { QuestionAnswer, QuestionRequest } from "@opencode-ai/sdk/v2"
88
import { useLanguage } from "@/context/language"
99
import { useSDK } from "@/context/sdk"
1010

11+
const cache = new Map<string, { tab: number; answers: QuestionAnswer[]; custom: string[]; customOn: boolean[] }>()
12+
1113
export const SessionQuestionDock: Component<{ request: QuestionRequest; onSubmit: () => void }> = (props) => {
1214
const sdk = useSDK()
1315
const language = useLanguage()
1416

1517
const questions = createMemo(() => props.request.questions)
1618
const total = createMemo(() => questions().length)
1719

20+
const cached = cache.get(props.request.id)
1821
const [store, setStore] = createStore({
19-
tab: 0,
20-
answers: [] as QuestionAnswer[],
21-
custom: [] as string[],
22-
customOn: [] as boolean[],
22+
tab: cached?.tab ?? 0,
23+
answers: cached?.answers ?? ([] as QuestionAnswer[]),
24+
custom: cached?.custom ?? ([] as string[]),
25+
customOn: cached?.customOn ?? ([] as boolean[]),
2326
editing: false,
2427
sending: false,
2528
})
2629

2730
let root: HTMLDivElement | undefined
31+
let replied = false
2832

2933
const question = createMemo(() => questions()[store.tab])
3034
const options = createMemo(() => question()?.options ?? [])
@@ -107,6 +111,16 @@ export const SessionQuestionDock: Component<{ request: QuestionRequest; onSubmit
107111
})
108112
})
109113

114+
onCleanup(() => {
115+
if (replied) return
116+
cache.set(props.request.id, {
117+
tab: store.tab,
118+
answers: store.answers.map((a) => (a ? [...a] : [])),
119+
custom: store.custom.map((s) => s ?? ""),
120+
customOn: store.customOn.map((b) => b ?? false),
121+
})
122+
})
123+
110124
const fail = (err: unknown) => {
111125
const message = err instanceof Error ? err.message : String(err)
112126
showToast({ title: language.t("common.requestFailed"), description: message })
@@ -119,6 +133,8 @@ export const SessionQuestionDock: Component<{ request: QuestionRequest; onSubmit
119133
setStore("sending", true)
120134
try {
121135
await sdk.client.question.reply({ requestID: props.request.id, answers })
136+
replied = true
137+
cache.delete(props.request.id)
122138
} catch (err) {
123139
fail(err)
124140
} finally {
@@ -133,6 +149,8 @@ export const SessionQuestionDock: Component<{ request: QuestionRequest; onSubmit
133149
setStore("sending", true)
134150
try {
135151
await sdk.client.question.reject({ requestID: props.request.id })
152+
replied = true
153+
cache.delete(props.request.id)
136154
} catch (err) {
137155
fail(err)
138156
} finally {

0 commit comments

Comments
 (0)