question.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import { createStore } from "solid-js/store"
  2. import { createMemo, For, Show } from "solid-js"
  3. import { useKeyboard } from "@opentui/solid"
  4. import type { TextareaRenderable } from "@opentui/core"
  5. import { useKeybind } from "../../context/keybind"
  6. import { useTheme } from "../../context/theme"
  7. import type { QuestionAnswer, QuestionRequest } from "@opencode-ai/sdk/v2"
  8. import { useSDK } from "../../context/sdk"
  9. import { SplitBorder } from "../../component/border"
  10. import { useTextareaKeybindings } from "../../component/textarea-keybindings"
  11. import { useDialog } from "../../ui/dialog"
  12. export function QuestionPrompt(props: { request: QuestionRequest }) {
  13. const sdk = useSDK()
  14. const { theme } = useTheme()
  15. const keybind = useKeybind()
  16. const bindings = useTextareaKeybindings()
  17. const questions = createMemo(() => props.request.questions)
  18. const single = createMemo(() => questions().length === 1 && questions()[0]?.multiple !== true)
  19. const tabs = createMemo(() => (single() ? 1 : questions().length + 1)) // questions + confirm tab (no confirm for single select)
  20. const [store, setStore] = createStore({
  21. tab: 0,
  22. answers: [] as QuestionAnswer[],
  23. custom: [] as string[],
  24. selected: 0,
  25. editing: false,
  26. })
  27. let textarea: TextareaRenderable | undefined
  28. const question = createMemo(() => questions()[store.tab])
  29. const confirm = createMemo(() => !single() && store.tab === questions().length)
  30. const options = createMemo(() => question()?.options ?? [])
  31. const custom = createMemo(() => question()?.custom !== false)
  32. const other = createMemo(() => custom() && store.selected === options().length)
  33. const input = createMemo(() => store.custom[store.tab] ?? "")
  34. const multi = createMemo(() => question()?.multiple === true)
  35. const customPicked = createMemo(() => {
  36. const value = input()
  37. if (!value) return false
  38. return store.answers[store.tab]?.includes(value) ?? false
  39. })
  40. function submit() {
  41. const answers = questions().map((_, i) => store.answers[i] ?? [])
  42. sdk.client.question.reply({
  43. requestID: props.request.id,
  44. answers,
  45. })
  46. }
  47. function reject() {
  48. sdk.client.question.reject({
  49. requestID: props.request.id,
  50. })
  51. }
  52. function pick(answer: string, custom: boolean = false) {
  53. const answers = [...store.answers]
  54. answers[store.tab] = [answer]
  55. setStore("answers", answers)
  56. if (custom) {
  57. const inputs = [...store.custom]
  58. inputs[store.tab] = answer
  59. setStore("custom", inputs)
  60. }
  61. if (single()) {
  62. sdk.client.question.reply({
  63. requestID: props.request.id,
  64. answers: [[answer]],
  65. })
  66. return
  67. }
  68. setStore("tab", store.tab + 1)
  69. setStore("selected", 0)
  70. }
  71. function toggle(answer: string) {
  72. const existing = store.answers[store.tab] ?? []
  73. const next = [...existing]
  74. const index = next.indexOf(answer)
  75. if (index === -1) next.push(answer)
  76. if (index !== -1) next.splice(index, 1)
  77. const answers = [...store.answers]
  78. answers[store.tab] = next
  79. setStore("answers", answers)
  80. }
  81. function moveTo(index: number) {
  82. setStore("selected", index)
  83. }
  84. function selectTab(index: number) {
  85. setStore("tab", index)
  86. setStore("selected", 0)
  87. }
  88. function selectOption() {
  89. if (other()) {
  90. if (!multi()) {
  91. setStore("editing", true)
  92. return
  93. }
  94. const value = input()
  95. if (value && customPicked()) {
  96. toggle(value)
  97. return
  98. }
  99. setStore("editing", true)
  100. return
  101. }
  102. const opt = options()[store.selected]
  103. if (!opt) return
  104. if (multi()) {
  105. toggle(opt.label)
  106. return
  107. }
  108. pick(opt.label)
  109. }
  110. const dialog = useDialog()
  111. useKeyboard((evt) => {
  112. // Skip processing if a dialog (e.g., command palette) is open
  113. if (dialog.stack.length > 0) return
  114. // When editing "Other" textarea
  115. if (store.editing && !confirm()) {
  116. if (evt.name === "escape") {
  117. evt.preventDefault()
  118. setStore("editing", false)
  119. return
  120. }
  121. if (keybind.match("input_clear", evt)) {
  122. evt.preventDefault()
  123. const text = textarea?.plainText ?? ""
  124. if (!text) {
  125. setStore("editing", false)
  126. return
  127. }
  128. textarea?.setText("")
  129. return
  130. }
  131. if (evt.name === "return") {
  132. evt.preventDefault()
  133. const text = textarea?.plainText?.trim() ?? ""
  134. const prev = store.custom[store.tab]
  135. if (!text) {
  136. if (prev) {
  137. const inputs = [...store.custom]
  138. inputs[store.tab] = ""
  139. setStore("custom", inputs)
  140. const answers = [...store.answers]
  141. answers[store.tab] = (answers[store.tab] ?? []).filter((x) => x !== prev)
  142. setStore("answers", answers)
  143. }
  144. setStore("editing", false)
  145. return
  146. }
  147. if (multi()) {
  148. const inputs = [...store.custom]
  149. inputs[store.tab] = text
  150. setStore("custom", inputs)
  151. const existing = store.answers[store.tab] ?? []
  152. const next = [...existing]
  153. if (prev) {
  154. const index = next.indexOf(prev)
  155. if (index !== -1) next.splice(index, 1)
  156. }
  157. if (!next.includes(text)) next.push(text)
  158. const answers = [...store.answers]
  159. answers[store.tab] = next
  160. setStore("answers", answers)
  161. setStore("editing", false)
  162. return
  163. }
  164. pick(text, true)
  165. setStore("editing", false)
  166. return
  167. }
  168. // Let textarea handle all other keys
  169. return
  170. }
  171. if (evt.name === "left" || evt.name === "h") {
  172. evt.preventDefault()
  173. selectTab((store.tab - 1 + tabs()) % tabs())
  174. }
  175. if (evt.name === "right" || evt.name === "l") {
  176. evt.preventDefault()
  177. selectTab((store.tab + 1) % tabs())
  178. }
  179. if (evt.name === "tab") {
  180. evt.preventDefault()
  181. const direction = evt.shift ? -1 : 1
  182. selectTab((store.tab + direction + tabs()) % tabs())
  183. }
  184. if (confirm()) {
  185. if (evt.name === "return") {
  186. evt.preventDefault()
  187. submit()
  188. }
  189. if (evt.name === "escape" || keybind.match("app_exit", evt)) {
  190. evt.preventDefault()
  191. reject()
  192. }
  193. } else {
  194. const opts = options()
  195. const total = opts.length + (custom() ? 1 : 0)
  196. const max = Math.min(total, 9)
  197. const digit = Number(evt.name)
  198. if (!Number.isNaN(digit) && digit >= 1 && digit <= max) {
  199. evt.preventDefault()
  200. const index = digit - 1
  201. moveTo(index)
  202. selectOption()
  203. return
  204. }
  205. if (evt.name === "up" || evt.name === "k") {
  206. evt.preventDefault()
  207. moveTo((store.selected - 1 + total) % total)
  208. }
  209. if (evt.name === "down" || evt.name === "j") {
  210. evt.preventDefault()
  211. moveTo((store.selected + 1) % total)
  212. }
  213. if (evt.name === "return") {
  214. evt.preventDefault()
  215. selectOption()
  216. }
  217. if (evt.name === "escape" || keybind.match("app_exit", evt)) {
  218. evt.preventDefault()
  219. reject()
  220. }
  221. }
  222. })
  223. return (
  224. <box
  225. backgroundColor={theme.backgroundPanel}
  226. border={["left"]}
  227. borderColor={theme.accent}
  228. customBorderChars={SplitBorder.customBorderChars}
  229. >
  230. <box gap={1} paddingLeft={1} paddingRight={3} paddingTop={1} paddingBottom={1}>
  231. <Show when={!single()}>
  232. <box flexDirection="row" gap={1} paddingLeft={1}>
  233. <For each={questions()}>
  234. {(q, index) => {
  235. const isActive = () => index() === store.tab
  236. const isAnswered = () => {
  237. return (store.answers[index()]?.length ?? 0) > 0
  238. }
  239. return (
  240. <box
  241. paddingLeft={1}
  242. paddingRight={1}
  243. backgroundColor={isActive() ? theme.accent : theme.backgroundElement}
  244. onMouseUp={() => selectTab(index())}
  245. >
  246. <text fg={isActive() ? theme.selectedListItemText : isAnswered() ? theme.text : theme.textMuted}>
  247. {q.header}
  248. </text>
  249. </box>
  250. )
  251. }}
  252. </For>
  253. <box
  254. paddingLeft={1}
  255. paddingRight={1}
  256. backgroundColor={confirm() ? theme.accent : theme.backgroundElement}
  257. onMouseUp={() => selectTab(questions().length)}
  258. >
  259. <text fg={confirm() ? theme.selectedListItemText : theme.textMuted}>Confirm</text>
  260. </box>
  261. </box>
  262. </Show>
  263. <Show when={!confirm()}>
  264. <box paddingLeft={1} gap={1}>
  265. <box>
  266. <text fg={theme.text}>
  267. {question()?.question}
  268. {multi() ? " (select all that apply)" : ""}
  269. </text>
  270. </box>
  271. <box>
  272. <For each={options()}>
  273. {(opt, i) => {
  274. const active = () => i() === store.selected
  275. const picked = () => store.answers[store.tab]?.includes(opt.label) ?? false
  276. return (
  277. <box onMouseOver={() => moveTo(i())} onMouseUp={() => selectOption()}>
  278. <box flexDirection="row" gap={1}>
  279. <box backgroundColor={active() ? theme.backgroundElement : undefined}>
  280. <text fg={active() ? theme.secondary : picked() ? theme.success : theme.text}>
  281. {multi()
  282. ? `${i() + 1}. [${picked() ? "✓" : " "}] ${opt.label}`
  283. : `${i() + 1}. ${opt.label}`}
  284. </text>
  285. </box>
  286. <Show when={!multi()}>
  287. <text fg={theme.success}>{picked() ? "✓" : ""}</text>
  288. </Show>
  289. </box>
  290. <box paddingLeft={3}>
  291. <text fg={theme.textMuted}>{opt.description}</text>
  292. </box>
  293. </box>
  294. )
  295. }}
  296. </For>
  297. <Show when={custom()}>
  298. <box onMouseOver={() => moveTo(options().length)} onMouseUp={() => selectOption()}>
  299. <box flexDirection="row" gap={1}>
  300. <box backgroundColor={other() ? theme.backgroundElement : undefined}>
  301. <text fg={other() ? theme.secondary : customPicked() ? theme.success : theme.text}>
  302. {multi()
  303. ? `${options().length + 1}. [${customPicked() ? "✓" : " "}] Type your own answer`
  304. : `${options().length + 1}. Type your own answer`}
  305. </text>
  306. </box>
  307. <Show when={!multi()}>
  308. <text fg={theme.success}>{customPicked() ? "✓" : ""}</text>
  309. </Show>
  310. </box>
  311. <Show when={store.editing}>
  312. <box paddingLeft={3}>
  313. <textarea
  314. ref={(val: TextareaRenderable) => {
  315. textarea = val
  316. queueMicrotask(() => {
  317. val.focus()
  318. val.gotoLineEnd()
  319. })
  320. }}
  321. initialValue={input()}
  322. placeholder="Type your own answer"
  323. textColor={theme.text}
  324. focusedTextColor={theme.text}
  325. cursorColor={theme.primary}
  326. keyBindings={bindings()}
  327. />
  328. </box>
  329. </Show>
  330. <Show when={!store.editing && input()}>
  331. <box paddingLeft={3}>
  332. <text fg={theme.textMuted}>{input()}</text>
  333. </box>
  334. </Show>
  335. </box>
  336. </Show>
  337. </box>
  338. </box>
  339. </Show>
  340. <Show when={confirm() && !single()}>
  341. <box paddingLeft={1}>
  342. <text fg={theme.text}>Review</text>
  343. </box>
  344. <For each={questions()}>
  345. {(q, index) => {
  346. const value = () => store.answers[index()]?.join(", ") ?? ""
  347. const answered = () => Boolean(value())
  348. return (
  349. <box paddingLeft={1}>
  350. <text>
  351. <span style={{ fg: theme.textMuted }}>{q.header}:</span>{" "}
  352. <span style={{ fg: answered() ? theme.text : theme.error }}>
  353. {answered() ? value() : "(not answered)"}
  354. </span>
  355. </text>
  356. </box>
  357. )
  358. }}
  359. </For>
  360. </Show>
  361. </box>
  362. <box
  363. flexDirection="row"
  364. flexShrink={0}
  365. gap={1}
  366. paddingLeft={2}
  367. paddingRight={3}
  368. paddingBottom={1}
  369. justifyContent="space-between"
  370. >
  371. <box flexDirection="row" gap={2}>
  372. <Show when={!single()}>
  373. <text fg={theme.text}>
  374. {"⇆"} <span style={{ fg: theme.textMuted }}>tab</span>
  375. </text>
  376. </Show>
  377. <Show when={!confirm()}>
  378. <text fg={theme.text}>
  379. {"↑↓"} <span style={{ fg: theme.textMuted }}>select</span>
  380. </text>
  381. </Show>
  382. <text fg={theme.text}>
  383. enter{" "}
  384. <span style={{ fg: theme.textMuted }}>
  385. {confirm() ? "submit" : multi() ? "toggle" : single() ? "submit" : "confirm"}
  386. </span>
  387. </text>
  388. <text fg={theme.text}>
  389. esc <span style={{ fg: theme.textMuted }}>dismiss</span>
  390. </text>
  391. </box>
  392. </box>
  393. </box>
  394. )
  395. }