|
| 1 | +import { useMutation } from "@tanstack/react-query"; |
| 2 | +import { Link } from "@tanstack/react-router"; |
| 3 | +import { useId, useRef, useState } from "react"; |
| 4 | +import { |
| 5 | + IconChevronRight, |
| 6 | + IconCircleCheck, |
| 7 | + IconFileText, |
| 8 | + IconPhoto, |
| 9 | + IconX, |
| 10 | +} from "@/components/icons"; |
| 11 | +import { |
| 12 | + PageCard, |
| 13 | + PageRows, |
| 14 | + PageSection, |
| 15 | + PageShell, |
| 16 | +} from "@/components/layout/page-shell"; |
| 17 | +import { Button } from "@/components/ui/button"; |
| 18 | +import { |
| 19 | + Field, |
| 20 | + FieldDescription, |
| 21 | + FieldError, |
| 22 | + FieldGroup, |
| 23 | + FieldLabel, |
| 24 | +} from "@/components/ui/field"; |
| 25 | +import { |
| 26 | + Item, |
| 27 | + ItemActions, |
| 28 | + ItemContent, |
| 29 | + ItemDescription, |
| 30 | + ItemMedia, |
| 31 | + ItemTitle, |
| 32 | +} from "@/components/ui/item"; |
| 33 | +import { Separator } from "@/components/ui/separator"; |
| 34 | +import { Textarea } from "@/components/ui/textarea"; |
| 35 | +import { |
| 36 | + ATTACHMENT_ACCEPT, |
| 37 | + ATTACHMENT_HINT, |
| 38 | + addAttachments, |
| 39 | + fileSize, |
| 40 | + MAX_ATTACHMENTS, |
| 41 | +} from "@/lib/bug-reports/attachments"; |
| 42 | +import { idempotencyKey } from "@/lib/api/request"; |
| 43 | +import { |
| 44 | + type BugReportInput, |
| 45 | + createBugReportMutationOptions, |
| 46 | +} from "@/lib/bug-reports/mutations"; |
| 47 | +import { reportName } from "@/lib/bug-reports/words"; |
| 48 | +import { failureSaid } from "@/lib/load-failure"; |
| 49 | +import { queryClient } from "@/query-client"; |
| 50 | + |
| 51 | +/** The most either text field takes, as the server does. */ |
| 52 | +export const MAX_TEXT = 4000; |
| 53 | + |
| 54 | +/** |
| 55 | + * Report a bug: what went wrong, what the person expected, and a screenshot or a file if they have |
| 56 | + * one. Nothing else to fill in. OpenBot adds its version and the app's recent failed requests itself |
| 57 | + * (lib/bug-reports/mutations.ts), and the server adds the rest; the person never sees a log. |
| 58 | + * |
| 59 | + * Its own screen, reached from Help in the account menu with the page the person was on (`from`), so |
| 60 | + * the report can say where it happened. |
| 61 | + */ |
| 62 | +export function ReportBugPage({ from }: { from?: string }) { |
| 63 | + const create = useMutation(createBugReportMutationOptions(queryClient)); |
| 64 | + const sent = create.data; |
| 65 | + return ( |
| 66 | + <PageShell |
| 67 | + description="Tell the OpenBot team what went wrong, and they'll look into it." |
| 68 | + title="Report a bug" |
| 69 | + > |
| 70 | + {sent ? ( |
| 71 | + /* In the card the form was in, so the answer reads as the form's own. */ |
| 72 | + <PageCard |
| 73 | + footer={ |
| 74 | + <> |
| 75 | + <Button onClick={() => create.reset()} variant="outline"> |
| 76 | + Report another bug |
| 77 | + </Button> |
| 78 | + <Button |
| 79 | + render={(props) => ( |
| 80 | + <Link |
| 81 | + {...props} |
| 82 | + params={{ reportId: sent.id }} |
| 83 | + to="/bug-reports/$reportId" |
| 84 | + /> |
| 85 | + )} |
| 86 | + > |
| 87 | + See the report |
| 88 | + </Button> |
| 89 | + </> |
| 90 | + } |
| 91 | + > |
| 92 | + <div className="flex items-start gap-3" role="status"> |
| 93 | + <IconCircleCheck className="mt-0.5 size-4 shrink-0 text-success" /> |
| 94 | + <div className="flex flex-col gap-1"> |
| 95 | + <h2 className="font-medium text-sm"> |
| 96 | + {`${reportName(sent.number)} is saved`} |
| 97 | + </h2> |
| 98 | + <p className="text-muted-foreground text-sm"> |
| 99 | + The OpenBot team will look at it. You can see where it stands in |
| 100 | + Your bug reports. |
| 101 | + </p> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + </PageCard> |
| 105 | + ) : ( |
| 106 | + <ReportForm |
| 107 | + from={from} |
| 108 | + onSend={(input) => create.mutate(input)} |
| 109 | + error={create.error} |
| 110 | + sending={create.isPending} |
| 111 | + /> |
| 112 | + )} |
| 113 | + <PageSection> |
| 114 | + <PageRows> |
| 115 | + <Item render={<Link to="/bug-reports" />} size="sm"> |
| 116 | + <ItemContent> |
| 117 | + <ItemTitle>Your bug reports</ItemTitle> |
| 118 | + <ItemDescription> |
| 119 | + See the bugs you've reported and where each one stands. |
| 120 | + </ItemDescription> |
| 121 | + </ItemContent> |
| 122 | + <ItemActions> |
| 123 | + <IconChevronRight className="size-4 text-muted-foreground" /> |
| 124 | + </ItemActions> |
| 125 | + </Item> |
| 126 | + </PageRows> |
| 127 | + </PageSection> |
| 128 | + </PageShell> |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +function ReportForm({ |
| 133 | + error, |
| 134 | + from, |
| 135 | + onSend, |
| 136 | + sending, |
| 137 | +}: { |
| 138 | + error: Error | null; |
| 139 | + from: string | undefined; |
| 140 | + onSend: (input: BugReportInput) => void; |
| 141 | + sending: boolean; |
| 142 | +}) { |
| 143 | + const ids = useId(); |
| 144 | + const [whatHappened, setWhatHappened] = useState(""); |
| 145 | + const [expected, setExpected] = useState(""); |
| 146 | + const [files, setFiles] = useState<File[]>([]); |
| 147 | + const [refused, setRefused] = useState<string | null>(null); |
| 148 | + const [missing, setMissing] = useState(false); |
| 149 | + /* |
| 150 | + * One key for the report as it stands: pressing Send again after a send that got no answer sends |
| 151 | + * the same key, so the server makes the report once. Any change makes it another report. |
| 152 | + */ |
| 153 | + const [key, setKey] = useState(idempotencyKey); |
| 154 | + const changed = () => setKey(idempotencyKey()); |
| 155 | + const picker = useRef<HTMLInputElement>(null); |
| 156 | + |
| 157 | + return ( |
| 158 | + <form |
| 159 | + noValidate |
| 160 | + onSubmit={(event) => { |
| 161 | + event.preventDefault(); |
| 162 | + if (!whatHappened.trim()) { |
| 163 | + setMissing(true); |
| 164 | + return; |
| 165 | + } |
| 166 | + onSend({ |
| 167 | + whatHappened, |
| 168 | + expected, |
| 169 | + pageUrl: from, |
| 170 | + attachments: files, |
| 171 | + idempotencyKey: key, |
| 172 | + }); |
| 173 | + }} |
| 174 | + > |
| 175 | + <PageCard |
| 176 | + footer={ |
| 177 | + <Button disabled={sending} type="submit"> |
| 178 | + {sending ? "Sending…" : "Send report"} |
| 179 | + </Button> |
| 180 | + } |
| 181 | + > |
| 182 | + <FieldGroup> |
| 183 | + <Field data-invalid={missing || undefined}> |
| 184 | + <FieldLabel htmlFor={`${ids}-what`}>What went wrong</FieldLabel> |
| 185 | + <Textarea |
| 186 | + aria-invalid={missing || undefined} |
| 187 | + id={`${ids}-what`} |
| 188 | + maxLength={MAX_TEXT} |
| 189 | + name="whatHappened" |
| 190 | + onChange={(event) => { |
| 191 | + setWhatHappened(event.target.value); |
| 192 | + changed(); |
| 193 | + if (event.target.value.trim()) setMissing(false); |
| 194 | + }} |
| 195 | + placeholder="I asked my Bot to draft an email and it stopped halfway." |
| 196 | + required |
| 197 | + rows={4} |
| 198 | + value={whatHappened} |
| 199 | + /> |
| 200 | + {missing ? <FieldError>Say what went wrong.</FieldError> : null} |
| 201 | + </Field> |
| 202 | + <Field> |
| 203 | + <FieldLabel htmlFor={`${ids}-expected`}> |
| 204 | + What did you expect |
| 205 | + </FieldLabel> |
| 206 | + <Textarea |
| 207 | + id={`${ids}-expected`} |
| 208 | + maxLength={MAX_TEXT} |
| 209 | + name="expected" |
| 210 | + onChange={(event) => { |
| 211 | + setExpected(event.target.value); |
| 212 | + changed(); |
| 213 | + }} |
| 214 | + placeholder="Optional" |
| 215 | + rows={3} |
| 216 | + value={expected} |
| 217 | + /> |
| 218 | + </Field> |
| 219 | + {/* |
| 220 | + * Not marked invalid when a file is refused: that turns the whole field red, the files |
| 221 | + * already added with it. The refusal says itself, under the button. |
| 222 | + */} |
| 223 | + <Field> |
| 224 | + <FieldLabel htmlFor={`${ids}-files`}> |
| 225 | + Add a screenshot or file |
| 226 | + </FieldLabel> |
| 227 | + <FieldDescription>{ATTACHMENT_HINT}</FieldDescription> |
| 228 | + <input |
| 229 | + accept={ATTACHMENT_ACCEPT} |
| 230 | + className="sr-only" |
| 231 | + id={`${ids}-files`} |
| 232 | + multiple |
| 233 | + onChange={(event) => { |
| 234 | + const picked = [...(event.target.files ?? [])]; |
| 235 | + // The same file can be picked again after it was removed. |
| 236 | + event.target.value = ""; |
| 237 | + const next = addAttachments(files, picked); |
| 238 | + setFiles(next.files); |
| 239 | + setRefused(next.refused); |
| 240 | + changed(); |
| 241 | + }} |
| 242 | + ref={picker} |
| 243 | + tabIndex={-1} |
| 244 | + type="file" |
| 245 | + /> |
| 246 | + {files.length > 0 ? ( |
| 247 | + <PageRows> |
| 248 | + {files.map((file, index) => ( |
| 249 | + <div key={`${file.name}-${file.size}-${file.lastModified}`}> |
| 250 | + {index > 0 ? <Separator /> : null} |
| 251 | + <Item size="sm"> |
| 252 | + <ItemMedia variant="icon"> |
| 253 | + {file.type.startsWith("image/") ? ( |
| 254 | + <IconPhoto /> |
| 255 | + ) : ( |
| 256 | + <IconFileText /> |
| 257 | + )} |
| 258 | + </ItemMedia> |
| 259 | + <ItemContent> |
| 260 | + <ItemTitle className="break-all">{file.name}</ItemTitle> |
| 261 | + <ItemDescription>{fileSize(file.size)}</ItemDescription> |
| 262 | + </ItemContent> |
| 263 | + <ItemActions> |
| 264 | + <Button |
| 265 | + aria-label={`Remove ${file.name}`} |
| 266 | + onClick={() => { |
| 267 | + setFiles(files.filter((one) => one !== file)); |
| 268 | + setRefused(null); |
| 269 | + changed(); |
| 270 | + }} |
| 271 | + size="icon-sm" |
| 272 | + type="button" |
| 273 | + variant="ghost" |
| 274 | + > |
| 275 | + <IconX /> |
| 276 | + </Button> |
| 277 | + </ItemActions> |
| 278 | + </Item> |
| 279 | + </div> |
| 280 | + ))} |
| 281 | + </PageRows> |
| 282 | + ) : null} |
| 283 | + <div> |
| 284 | + <Button |
| 285 | + disabled={files.length >= MAX_ATTACHMENTS} |
| 286 | + onClick={() => picker.current?.click()} |
| 287 | + size="sm" |
| 288 | + type="button" |
| 289 | + variant="outline" |
| 290 | + > |
| 291 | + Choose a file |
| 292 | + </Button> |
| 293 | + </div> |
| 294 | + {refused ? <FieldError>{refused}</FieldError> : null} |
| 295 | + </Field> |
| 296 | + </FieldGroup> |
| 297 | + <p className="mt-6 text-muted-foreground text-sm"> |
| 298 | + OpenBot adds the technical details itself, such as its version and |
| 299 | + recent errors. None of your messages are sent. |
| 300 | + </p> |
| 301 | + {error ? ( |
| 302 | + <p className="mt-4 text-destructive text-sm" role="alert"> |
| 303 | + {failureSaid(error)} |
| 304 | + </p> |
| 305 | + ) : null} |
| 306 | + </PageCard> |
| 307 | + </form> |
| 308 | + ); |
| 309 | +} |
0 commit comments