|
| 1 | +import React, { useEffect, useRef, useState } from "react" |
| 2 | +import { Modal, SafeAreaView, Text, View } from "react-native" |
| 3 | +import { FlattenView, type FlattenHandle } from "../capture/flatten" |
| 4 | +import { StepForm } from "./step-form" |
| 5 | +import { StepAnnotate } from "./step-annotate" |
| 6 | +import { StepSubmit } from "./step-submit" |
| 7 | +import { createAnnotationStore } from "../annotation/store" |
| 8 | + |
| 9 | +export interface WizardArgs { |
| 10 | + initialTitle?: string |
| 11 | + initialDescription?: string |
| 12 | + screenshot: { uri: string; width: number; height: number } | null |
| 13 | + onSubmit: (result: { |
| 14 | + title: string |
| 15 | + description: string |
| 16 | + annotatedUri: string | null |
| 17 | + rawUri: string | null |
| 18 | + }) => Promise<void> |
| 19 | + onClose: () => void |
| 20 | +} |
| 21 | + |
| 22 | +export function WizardSheet({ |
| 23 | + initialTitle, |
| 24 | + initialDescription, |
| 25 | + screenshot, |
| 26 | + onSubmit, |
| 27 | + onClose, |
| 28 | +}: WizardArgs) { |
| 29 | + const [step, setStep] = useState<"form" | "annotate" | "submit">("form") |
| 30 | + const [title, setTitle] = useState(initialTitle ?? "") |
| 31 | + const [description, setDescription] = useState(initialDescription ?? "") |
| 32 | + const [submitting, setSubmitting] = useState(false) |
| 33 | + const [error, setError] = useState<string | null>(null) |
| 34 | + const store = useRef(createAnnotationStore()).current |
| 35 | + const flattenRef = useRef<FlattenHandle | null>(null) |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + setStep("form") |
| 39 | + }, [screenshot]) |
| 40 | + |
| 41 | + async function handleSubmit() { |
| 42 | + setSubmitting(true) |
| 43 | + setError(null) |
| 44 | + try { |
| 45 | + let annotated: string | null = null |
| 46 | + if (screenshot && store.snapshot().length > 0 && flattenRef.current) { |
| 47 | + const flat = await flattenRef.current.flatten() |
| 48 | + annotated = flat.uri |
| 49 | + } |
| 50 | + await onSubmit({ |
| 51 | + title, |
| 52 | + description, |
| 53 | + annotatedUri: annotated, |
| 54 | + rawUri: screenshot?.uri ?? null, |
| 55 | + }) |
| 56 | + } catch (e) { |
| 57 | + setError((e as Error).message) |
| 58 | + } finally { |
| 59 | + setSubmitting(false) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <Modal visible animationType="slide" onRequestClose={onClose}> |
| 65 | + <SafeAreaView style={{ flex: 1 }}> |
| 66 | + <View style={{ padding: 12, borderBottomWidth: 1, borderBottomColor: "#eee" }}> |
| 67 | + <Text style={{ fontSize: 18, fontWeight: "600" }}>Report a bug</Text> |
| 68 | + </View> |
| 69 | + {step === "form" && ( |
| 70 | + <StepForm |
| 71 | + title={title} |
| 72 | + description={description} |
| 73 | + onTitleChange={setTitle} |
| 74 | + onDescriptionChange={setDescription} |
| 75 | + /> |
| 76 | + )} |
| 77 | + {step === "annotate" && screenshot && ( |
| 78 | + <StepAnnotate |
| 79 | + imageUri={screenshot.uri} |
| 80 | + width={screenshot.width} |
| 81 | + height={screenshot.height} |
| 82 | + store={store} |
| 83 | + /> |
| 84 | + )} |
| 85 | + {step === "submit" && ( |
| 86 | + <StepSubmit |
| 87 | + submitting={submitting} |
| 88 | + error={error} |
| 89 | + onSubmit={handleSubmit} |
| 90 | + onCancel={onClose} |
| 91 | + /> |
| 92 | + )} |
| 93 | + {screenshot && ( |
| 94 | + <FlattenView |
| 95 | + ref={flattenRef} |
| 96 | + uri={screenshot.uri} |
| 97 | + width={screenshot.width} |
| 98 | + height={screenshot.height} |
| 99 | + shapes={store.snapshot()} |
| 100 | + /> |
| 101 | + )} |
| 102 | + <View style={{ flexDirection: "row", padding: 12, gap: 8 }}> |
| 103 | + {step !== "form" && ( |
| 104 | + <Text onPress={() => setStep(step === "submit" ? "annotate" : "form")}>Back</Text> |
| 105 | + )} |
| 106 | + {step !== "submit" && ( |
| 107 | + <Text onPress={() => setStep(step === "form" ? "annotate" : "submit")}>Next</Text> |
| 108 | + )} |
| 109 | + </View> |
| 110 | + </SafeAreaView> |
| 111 | + </Modal> |
| 112 | + ) |
| 113 | +} |
0 commit comments