(props: { request: QuestionRequest; directory?: string })
| 12 | const QUESTION_MODE = "question" |
| 13 | |
| 14 | export function QuestionPrompt(props: { request: QuestionRequest; directory?: string }) { |
| 15 | const sdk = useSDK() |
| 16 | const { theme } = useTheme() |
| 17 | const renderer = useRenderer() |
| 18 | const tuiConfig = useTuiConfig() |
| 19 | const modeStack = useOpencodeModeStack() |
| 20 | |
| 21 | const questions = createMemo(() => props.request.questions) |
| 22 | const single = createMemo(() => questions().length === 1 && questions()[0]?.multiple !== true) |
| 23 | const tabs = createMemo(() => (single() ? 1 : questions().length + 1)) // questions + confirm tab (no confirm for single select) |
| 24 | const [tabHover, setTabHover] = createSignal<number | "confirm" | null>(null) |
| 25 | const [store, setStore] = createStore({ |
| 26 | tab: 0, |
| 27 | answers: [] as QuestionAnswer[], |
| 28 | custom: [] as string[], |
| 29 | selected: 0, |
| 30 | editing: false, |
| 31 | }) |
| 32 | |
| 33 | let textarea: TextareaRenderable | undefined |
| 34 | |
| 35 | const question = createMemo(() => questions()[store.tab]) |
| 36 | const confirm = createMemo(() => !single() && store.tab === questions().length) |
| 37 | const options = createMemo(() => question()?.options ?? []) |
| 38 | const custom = createMemo(() => question()?.custom !== false) |
| 39 | const other = createMemo(() => custom() && store.selected === options().length) |
| 40 | const input = createMemo(() => store.custom[store.tab] ?? "") |
| 41 | const multi = createMemo(() => question()?.multiple === true) |
| 42 | const customPicked = createMemo(() => { |
| 43 | const value = input() |
| 44 | if (!value) return false |
| 45 | return store.answers[store.tab]?.includes(value) ?? false |
| 46 | }) |
| 47 | |
| 48 | function submit() { |
| 49 | const answers = questions().map((_, i) => store.answers[i] ?? []) |
| 50 | void sdk.client.question.reply({ |
| 51 | requestID: props.request.id, |
| 52 | directory: props.directory, |
| 53 | answers, |
| 54 | }) |
| 55 | } |
| 56 | |
| 57 | function reject() { |
| 58 | void sdk.client.question.reject({ |
| 59 | requestID: props.request.id, |
| 60 | directory: props.directory, |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | function pick(answer: string, custom: boolean = false) { |
| 65 | const answers = [...store.answers] |
| 66 | answers[store.tab] = [answer] |
| 67 | setStore("answers", answers) |
| 68 | if (custom) { |
| 69 | const inputs = [...store.custom] |
| 70 | inputs[store.tab] = answer |
| 71 | setStore("custom", inputs) |
nothing calls this directly
no test coverage detected