gpt-engineer-app[bot] commited on
Commit
cf8c244
·
1 Parent(s): c54233a
Files changed (1) hide show
  1. src/components/upload-dialog.tsx +141 -0
src/components/upload-dialog.tsx ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useState } from "react";
2
+ import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, DialogDescription } from "@/components/ui/dialog";
3
+ import { Button } from "@/components/ui/button";
4
+ import { Input } from "@/components/ui/input";
5
+ import { Label } from "@/components/ui/label";
6
+ import { Textarea } from "@/components/ui/textarea";
7
+ import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs";
8
+ import { Upload, FileText, FileType, Video, Loader2 } from "lucide-react";
9
+ import { supabase } from "@/integrations/supabase/client";
10
+ import { toast } from "sonner";
11
+ import { useServerFn } from "@tanstack/react-start";
12
+ import { analyzeIncident } from "@/server/incidents.functions";
13
+
14
+ export function UploadDialog({ onCreated }: { onCreated?: (id: string) => void }) {
15
+ const [open, setOpen] = useState(false);
16
+ const [busy, setBusy] = useState(false);
17
+ const [title, setTitle] = useState("");
18
+ const [text, setText] = useState("");
19
+ const [file, setFile] = useState<File | null>(null);
20
+ const [tab, setTab] = useState<"text" | "file">("text");
21
+ const analyze = useServerFn(analyzeIncident);
22
+
23
+ const reset = () => { setTitle(""); setText(""); setFile(null); setTab("text"); };
24
+
25
+ const submit = async () => {
26
+ if (!title.trim()) return toast.error("Title is required");
27
+ setBusy(true);
28
+ try {
29
+ const { data: u } = await supabase.auth.getUser();
30
+ if (!u.user) throw new Error("Not authenticated");
31
+
32
+ let raw_text: string | null = null;
33
+ let file_url: string | null = null;
34
+ let file_name: string | null = null;
35
+ let source_type: "text" | "file" | "pdf" | "video" = "text";
36
+
37
+ if (tab === "text") {
38
+ if (!text.trim()) throw new Error("Paste an incident report or transcript");
39
+ raw_text = text;
40
+ } else {
41
+ if (!file) throw new Error("Select a file");
42
+ file_name = file.name;
43
+ const ext = file.name.split(".").pop()?.toLowerCase() ?? "";
44
+ source_type = ext === "pdf" ? "pdf" : ["mp4","mov","webm","avi","mkv"].includes(ext) ? "video" : "file";
45
+
46
+ // Upload to storage
47
+ const path = `${u.user.id}/${Date.now()}-${file.name}`;
48
+ const { error: upErr } = await supabase.storage.from("incident-files").upload(path, file);
49
+ if (upErr) throw upErr;
50
+ file_url = path;
51
+
52
+ // Extract text for plain text-ish files
53
+ if (["txt","csv","json","log","md","xml","yaml","yml"].includes(ext)) {
54
+ raw_text = await file.text();
55
+ source_type = "file";
56
+ } else if (ext === "pdf") {
57
+ // Best-effort: client cannot parse PDFs without lib; we send a note
58
+ raw_text = `[PDF file: ${file.name}, ${(file.size/1024).toFixed(1)} KB. Extract incident details from the document referenced by file_name.]`;
59
+ } else if (source_type === "video") {
60
+ raw_text = `[Video file: ${file.name}, ${(file.size/1024/1024).toFixed(2)} MB. Treat as recorded incident footage; analyze based on title + any supplied notes. Recommend transcription if not yet performed.]`;
61
+ }
62
+ }
63
+
64
+ const { data: inc, error: insErr } = await supabase
65
+ .from("incidents")
66
+ .insert({ user_id: u.user.id, title, raw_text, file_url, file_name, source_type, status: "pending" })
67
+ .select()
68
+ .single();
69
+ if (insErr) throw insErr;
70
+
71
+ toast.success("Incident logged. Running multi-agent analysis…");
72
+ setOpen(false); reset(); onCreated?.(inc.id);
73
+
74
+ try {
75
+ await analyze({ data: { incidentId: inc.id } });
76
+ toast.success("Analysis complete");
77
+ } catch (e: any) {
78
+ toast.error(`Analysis failed: ${e.message}`);
79
+ }
80
+ } catch (e: any) {
81
+ toast.error(e.message);
82
+ } finally { setBusy(false); }
83
+ };
84
+
85
+ return (
86
+ <Dialog open={open} onOpenChange={setOpen}>
87
+ <DialogTrigger asChild>
88
+ <Button className="bg-primary text-primary-foreground hover:bg-primary/90 font-semibold gap-2">
89
+ <Upload className="h-4 w-4" /> New Incident
90
+ </Button>
91
+ </DialogTrigger>
92
+ <DialogContent className="max-w-2xl bg-card border-border">
93
+ <DialogHeader>
94
+ <DialogTitle>Submit incident for analysis</DialogTitle>
95
+ <DialogDescription>
96
+ Paste a report/transcript or upload sensor logs, PDF reports, or video evidence. AVISYS will route it through Event, Safety, Risk, and Documentation agents.
97
+ </DialogDescription>
98
+ </DialogHeader>
99
+
100
+ <div className="space-y-4">
101
+ <div className="space-y-2">
102
+ <Label>Incident title</Label>
103
+ <Input value={title} onChange={(e) => setTitle(e.target.value)} placeholder="e.g. Sudden braking event — I-280 NB" />
104
+ </div>
105
+
106
+ <Tabs value={tab} onValueChange={(v) => setTab(v as any)}>
107
+ <TabsList className="grid grid-cols-2 w-full bg-surface">
108
+ <TabsTrigger value="text" className="gap-2"><FileText className="h-3.5 w-3.5"/>Paste text</TabsTrigger>
109
+ <TabsTrigger value="file" className="gap-2"><FileType className="h-3.5 w-3.5"/>Upload file</TabsTrigger>
110
+ </TabsList>
111
+ <TabsContent value="text" className="mt-3">
112
+ <Textarea
113
+ value={text}
114
+ onChange={(e) => setText(e.target.value)}
115
+ placeholder="Paste incident report, video transcript, or sensor log summary…"
116
+ className="min-h-[200px] font-mono text-xs"
117
+ />
118
+ </TabsContent>
119
+ <TabsContent value="file" className="mt-3 space-y-3">
120
+ <Input type="file" accept=".txt,.csv,.json,.log,.md,.pdf,.mp4,.mov,.webm,.xml,.yaml,.yml" onChange={(e) => setFile(e.target.files?.[0] ?? null)} />
121
+ {file && (
122
+ <div className="text-xs text-muted-foreground flex items-center gap-2 font-mono">
123
+ {file.name.endsWith(".pdf") ? <FileType className="h-3.5 w-3.5"/> : file.name.match(/\.(mp4|mov|webm)$/i) ? <Video className="h-3.5 w-3.5"/> : <FileText className="h-3.5 w-3.5"/>}
124
+ {file.name} · {(file.size/1024).toFixed(1)} KB
125
+ </div>
126
+ )}
127
+ <p className="text-[11px] text-muted-foreground">Supported: text logs (.txt/.csv/.json/.log), PDF reports, video files. Files are stored privately to your account.</p>
128
+ </TabsContent>
129
+ </Tabs>
130
+ </div>
131
+
132
+ <div className="flex justify-end gap-2 pt-2">
133
+ <Button variant="ghost" onClick={() => setOpen(false)} disabled={busy}>Cancel</Button>
134
+ <Button onClick={submit} disabled={busy} className="bg-primary text-primary-foreground hover:bg-primary/90">
135
+ {busy ? <><Loader2 className="h-4 w-4 animate-spin mr-2"/>Analyzing…</> : "Submit & analyze"}
136
+ </Button>
137
+ </div>
138
+ </DialogContent>
139
+ </Dialog>
140
+ );
141
+ }