|
| 1 | +<script lang="ts"> |
| 2 | + import { formatAskUserQuestionAnswer } from "./ask-user-question"; |
| 3 | + import type { AskUserQuestionInput } from "./types"; |
| 4 | +
|
| 5 | + interface Props { |
| 6 | + input: AskUserQuestionInput; |
| 7 | + disabled: boolean; |
| 8 | + onSubmit: (text: string) => void; |
| 9 | + } |
| 10 | +
|
| 11 | + const { input, disabled, onSubmit }: Props = $props(); |
| 12 | +
|
| 13 | + // One single-select question can answer on a single tap; anything else |
| 14 | + // (multiple questions, or a multi-select) needs an explicit Submit. |
| 15 | + const autoSend = $derived(input.questions.length === 1 && input.questions[0]?.multiSelect !== true); |
| 16 | +
|
| 17 | + let selections = $state<Record<number, string[]>>({}); |
| 18 | + let customText = $state<Record<number, string>>({}); |
| 19 | +
|
| 20 | + function setSelection(qIndex: number, next: string[]): void { |
| 21 | + selections = { ...selections, [qIndex]: next }; |
| 22 | + } |
| 23 | +
|
| 24 | + function setCustom(qIndex: number, value: string): void { |
| 25 | + customText = { ...customText, [qIndex]: value }; |
| 26 | + } |
| 27 | +
|
| 28 | + function isSelected(qIndex: number, label: string): boolean { |
| 29 | + return (selections[qIndex] ?? []).includes(label); |
| 30 | + } |
| 31 | +
|
| 32 | + function buildAnswers(): Array<{ header: string; values: string[] }> { |
| 33 | + return input.questions.map((question, index) => { |
| 34 | + const custom = customText[index]?.trim() ?? ""; |
| 35 | + const values = [...(selections[index] ?? [])]; |
| 36 | + if (custom.length > 0) values.push(custom); |
| 37 | + return { header: question.header, values }; |
| 38 | + }); |
| 39 | + } |
| 40 | +
|
| 41 | + const canSubmit = $derived(!disabled && buildAnswers().some((answer) => answer.values.length > 0)); |
| 42 | +
|
| 43 | + function submitSingle(header: string, value: string): void { |
| 44 | + onSubmit(formatAskUserQuestionAnswer([{ header, values: [value] }])); |
| 45 | + } |
| 46 | +
|
| 47 | + function submitAll(): void { |
| 48 | + if (disabled) return; |
| 49 | + const text = formatAskUserQuestionAnswer(buildAnswers()); |
| 50 | + if (text.length === 0) return; |
| 51 | + onSubmit(text); |
| 52 | + } |
| 53 | +
|
| 54 | + function toggleOption(qIndex: number, label: string): void { |
| 55 | + if (disabled) return; |
| 56 | + const question = input.questions[qIndex]; |
| 57 | + if (!question) return; |
| 58 | + if (autoSend) { |
| 59 | + submitSingle(question.header, label); |
| 60 | + return; |
| 61 | + } |
| 62 | + const current = selections[qIndex] ?? []; |
| 63 | + if (question.multiSelect) { |
| 64 | + setSelection(qIndex, current.includes(label) ? current.filter((value) => value !== label) : [...current, label]); |
| 65 | + } else { |
| 66 | + setSelection(qIndex, current.includes(label) ? [] : [label]); |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | + function handleCustomKeydown(event: KeyboardEvent, qIndex: number): void { |
| 71 | + if (event.key !== "Enter" || event.shiftKey) return; |
| 72 | + event.preventDefault(); |
| 73 | + if (disabled) return; |
| 74 | + const custom = customText[qIndex]?.trim() ?? ""; |
| 75 | + if (autoSend) { |
| 76 | + const question = input.questions[qIndex]; |
| 77 | + if (!question || custom.length === 0) return; |
| 78 | + submitSingle(question.header, custom); |
| 79 | + return; |
| 80 | + } |
| 81 | + if (canSubmit) submitAll(); |
| 82 | + } |
| 83 | +</script> |
| 84 | + |
| 85 | +<div class="self-start w-full max-w-[94%] min-w-0 rounded-md border border-accent/40 bg-topbar/40 text-xs text-primary"> |
| 86 | + <div class="border-b border-edge/60 px-3 py-2 text-[10px] uppercase tracking-[0.12em] text-muted"> |
| 87 | + Question |
| 88 | + </div> |
| 89 | + |
| 90 | + <div class="flex flex-col gap-4 px-3 py-3"> |
| 91 | + {#each input.questions as question, qIndex (`${question.header}:${qIndex}`)} |
| 92 | + <div class="flex min-w-0 flex-col gap-2"> |
| 93 | + <div class="text-[10px] uppercase tracking-[0.12em] text-muted">{question.header}</div> |
| 94 | + <div class="text-sm text-primary">{question.question}</div> |
| 95 | + <div class="flex flex-wrap gap-2"> |
| 96 | + {#each question.options as option (option.label)} |
| 97 | + <button |
| 98 | + type="button" |
| 99 | + class={`min-w-0 max-w-full rounded-md border px-3 py-1.5 text-left transition disabled:cursor-not-allowed disabled:opacity-60 ${ |
| 100 | + isSelected(qIndex, option.label) |
| 101 | + ? "border-accent bg-accent text-white" |
| 102 | + : "border-edge bg-surface text-primary enabled:hover:bg-hover" |
| 103 | + }`} |
| 104 | + {disabled} |
| 105 | + onclick={() => toggleOption(qIndex, option.label)} |
| 106 | + > |
| 107 | + <span class="block break-words font-medium">{option.label}</span> |
| 108 | + {#if option.description} |
| 109 | + <span class={`mt-0.5 block break-words text-[10px] ${isSelected(qIndex, option.label) ? "text-white/80" : "text-muted"}`}> |
| 110 | + {option.description} |
| 111 | + </span> |
| 112 | + {/if} |
| 113 | + </button> |
| 114 | + {/each} |
| 115 | + </div> |
| 116 | + <input |
| 117 | + type="text" |
| 118 | + class="w-full rounded-md border border-edge bg-surface px-3 py-1.5 text-xs text-primary outline-none transition placeholder:text-muted/70 focus:border-accent disabled:cursor-not-allowed disabled:opacity-60" |
| 119 | + placeholder="Custom answer…" |
| 120 | + value={customText[qIndex] ?? ""} |
| 121 | + oninput={(event) => setCustom(qIndex, event.currentTarget.value)} |
| 122 | + onkeydown={(event) => handleCustomKeydown(event, qIndex)} |
| 123 | + {disabled} |
| 124 | + /> |
| 125 | + </div> |
| 126 | + {/each} |
| 127 | + </div> |
| 128 | + |
| 129 | + {#if !autoSend} |
| 130 | + <div class="flex justify-end border-t border-edge/60 px-3 py-2"> |
| 131 | + <button |
| 132 | + type="button" |
| 133 | + class="rounded-md border border-accent bg-accent px-3 py-1.5 text-xs font-medium text-white transition enabled:hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-45" |
| 134 | + onclick={submitAll} |
| 135 | + disabled={!canSubmit} |
| 136 | + > |
| 137 | + Submit answer |
| 138 | + </button> |
| 139 | + </div> |
| 140 | + {/if} |
| 141 | +</div> |
0 commit comments