Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
parkpow
GitHub Repository: parkpow/deep-license-plate-recognition
Path: blob/master/webhooks/webhook_preview/app/page.tsx
1091 views
1
"use client";
2
3
import { useState, useEffect } from "react";
4
import { useRouter } from "next/navigation";
5
import { Button } from "@/components/ui/button";
6
import {
7
Card,
8
CardContent,
9
CardDescription,
10
CardFooter,
11
CardHeader,
12
CardTitle,
13
} from "@/components/ui/card";
14
import { Input } from "@/components/ui/input";
15
import { generateUUID } from "@/lib/utils";
16
import { useToast } from "@/hooks/use-toast";
17
18
export default function HomePage() {
19
const [uuid, setUuid] = useState<string>("");
20
const router = useRouter();
21
const { toast } = useToast();
22
23
useEffect(() => {
24
// Generate a UUID if none exists
25
setUuid(generateUUID());
26
}, []);
27
28
const handleCreateWebhook = async () => {
29
const uuidRegex =
30
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
31
32
if (!uuidRegex.test(uuid)) {
33
toast({
34
title: "Invalid UUID",
35
description: "Please use the generated UUID or generate a new one",
36
variant: "destructive",
37
});
38
return;
39
}
40
41
try {
42
const response = await fetch("/api/new-webhook/", {
43
method: "POST",
44
headers: {
45
"Content-Type": "application/json",
46
},
47
body: JSON.stringify({ uuid }),
48
});
49
50
if (!response.ok) {
51
throw new Error("Failed to create webhook");
52
}
53
54
const data = await response.json();
55
56
if (data?.success) {
57
router.push(`/${uuid}`);
58
} else {
59
toast({
60
title: "Error",
61
description: "Webhook could not be created",
62
variant: "destructive",
63
});
64
}
65
} catch (error: unknown) {
66
if (error instanceof Error) {
67
toast({
68
title: "Error",
69
description: error.message,
70
variant: "destructive",
71
});
72
} else {
73
toast({
74
title: "Error",
75
description: "Something went wrong",
76
variant: "destructive",
77
});
78
}
79
}
80
};
81
82
return (
83
<div className="min-h-screen flex items-center justify-center bg-gray-50 p-4">
84
<Card className="w-full max-w-md">
85
<CardHeader>
86
<CardTitle>License Plate Recognition Dashboard</CardTitle>
87
<CardDescription>
88
Create a new webhook URL to receive license plate recognition data
89
</CardDescription>
90
</CardHeader>
91
<CardContent>
92
<div className="space-y-4">
93
<div className="space-y-2">
94
<label htmlFor="uuid" className="text-sm font-medium">
95
Webhook UUID
96
</label>
97
<Input
98
id="uuid"
99
value={uuid}
100
readOnly
101
placeholder="Generated UUID"
102
className="font-mono"
103
/>
104
<p className="text-xs text-muted-foreground">
105
This UUID will be used to access your webhook data
106
</p>
107
</div>
108
</div>
109
</CardContent>
110
<CardFooter className="flex justify-between">
111
<Button variant="outline" onClick={() => setUuid(generateUUID())}>
112
Generate New UUID
113
</Button>
114
<Button onClick={handleCreateWebhook}>Create Webhook</Button>
115
</CardFooter>
116
</Card>
117
</div>
118
);
119
}
120
121