Spaces:
Running
Running
File size: 9,427 Bytes
53c9876 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | import { useState, useEffect } from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { Plus } from "lucide-react";
import { useCreateSensor } from "@/hooks/use-sensors";
import { useToast } from "@/hooks/use-toast";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
const formSchema = z.object({
sensorId: z.string().min(1, "Sensor ID is required"),
latitude: z.coerce.number(),
longitude: z.coerce.number(),
locationName: z.string().optional(),
});
type FormData = z.infer<typeof formSchema>;
interface CreateSensorDialogProps {
/** If true, dialog open state is controlled externally */
controlled?: boolean;
open?: boolean;
onOpenChange?: (open: boolean) => void;
defaultLatitude?: number;
defaultLongitude?: number;
}
export function CreateSensorDialog({
controlled = false,
open: controlledOpen,
onOpenChange: controlledOnOpenChange,
defaultLatitude,
defaultLongitude,
}: CreateSensorDialogProps) {
const [internalOpen, setInternalOpen] = useState(false);
const create = useCreateSensor();
const { toast } = useToast();
const isOpen = controlled ? (controlledOpen ?? false) : internalOpen;
const setIsOpen = controlled ? (controlledOnOpenChange ?? (() => {})) : setInternalOpen;
const form = useForm<FormData>({
resolver: zodResolver(formSchema) as any,
defaultValues: { sensorId: "", latitude: 0, longitude: 0, locationName: "" },
});
// Pre-fill lat/lng when defaults change (from map click)
useEffect(() => {
if (defaultLatitude !== undefined && defaultLongitude !== undefined) {
form.setValue("latitude", parseFloat(defaultLatitude.toFixed(6)));
form.setValue("longitude", parseFloat(defaultLongitude.toFixed(6)));
}
}, [defaultLatitude, defaultLongitude, form]);
const onSubmit = (data: FormData) => {
create.mutate(data, {
onSuccess: () => {
toast({
title: "Node Deployed",
description: `Sensor ${data.sensorId} integrated into global grid.`,
});
setIsOpen(false);
form.reset();
},
onError: (error) => {
toast({
variant: "destructive",
title: "Deployment Failed",
description: error.message,
});
}
});
};
// If controlled mode, don't render trigger button
if (controlled) {
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogContent className="border-cyan-500/50 bg-[#0A0A0F]/95 backdrop-blur-2xl text-slate-200 shadow-[0_0_50px_rgba(0,243,255,0.15)] sm:max-w-[425px]">
<DialogHeader>
<DialogTitle className="neon-text font-display text-2xl tracking-[0.2em] uppercase">
Deploy Sensor Node
</DialogTitle>
</DialogHeader>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-5 mt-4">
<div className="space-y-2">
<Label className="text-cyan-500/80 font-mono text-[10px] tracking-widest">SENSOR ID</Label>
<Input
{...form.register("sensorId")}
className="bg-[#05050A] border-slate-800 focus-visible:border-cyan-500 font-mono text-cyan-50"
placeholder="AQUA-NODE-001"
/>
{form.formState.errors.sensorId && <p className="text-red-400 text-xs mt-1">{form.formState.errors.sensorId.message}</p>}
</div>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label className="text-cyan-500/80 font-mono text-[10px] tracking-widest">LATITUDE</Label>
<Input
{...form.register("latitude")}
className="bg-[#05050A] border-slate-800 focus-visible:border-cyan-500 font-mono text-cyan-50"
placeholder="40.7128"
/>
{form.formState.errors.latitude && <p className="text-red-400 text-xs mt-1">{form.formState.errors.latitude.message}</p>}
</div>
<div className="space-y-2">
<Label className="text-cyan-500/80 font-mono text-[10px] tracking-widest">LONGITUDE</Label>
<Input
{...form.register("longitude")}
className="bg-[#05050A] border-slate-800 focus-visible:border-cyan-500 font-mono text-cyan-50"
placeholder="-74.0060"
/>
{form.formState.errors.longitude && <p className="text-red-400 text-xs mt-1">{form.formState.errors.longitude.message}</p>}
</div>
</div>
<div className="space-y-2">
<Label className="text-cyan-500/80 font-mono text-[10px] tracking-widest">LOCATION DESIGNATION</Label>
<Input
{...form.register("locationName")}
className="bg-[#05050A] border-slate-800 focus-visible:border-cyan-500 font-mono text-cyan-50"
placeholder="Hudson River Sector Alpha"
/>
</div>
<div className="pt-6">
<Button
type="submit"
disabled={create.isPending}
className="w-full bg-cyan-500 text-[#05050A] hover:bg-cyan-400 font-display tracking-[0.3em] text-lg py-6 transition-all hover:shadow-[0_0_20px_rgba(0,243,255,0.4)]"
>
{create.isPending ? "INITIALIZING..." : "EXECUTE DEPLOYMENT"}
</Button>
</div>
</form>
</DialogContent>
</Dialog>
);
}
// Default: uncontrolled mode with trigger button
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
<Button
size="sm"
className="neon-border bg-cyan-950/40 hover:bg-cyan-900/60 text-cyan-400 font-display tracking-[0.2em] uppercase transition-all shadow-[0_0_15px_rgba(0,243,255,0.1)] hover:shadow-[0_0_25px_rgba(0,243,255,0.3)]"
>
<Plus className="h-4 w-4 mr-2" /> Deploy Node
</Button>
</DialogTrigger>
<DialogContent className="border-cyan-500/50 bg-[#0A0A0F]/95 backdrop-blur-2xl text-slate-200 shadow-[0_0_50px_rgba(0,243,255,0.15)] sm:max-w-[425px]">
<DialogHeader>
<DialogTitle className="neon-text font-display text-2xl tracking-[0.2em] uppercase">
Deploy Sensor Node
</DialogTitle>
</DialogHeader>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-5 mt-4">
<div className="space-y-2">
<Label className="text-cyan-500/80 font-mono text-[10px] tracking-widest">SENSOR ID</Label>
<Input
{...form.register("sensorId")}
className="bg-[#05050A] border-slate-800 focus-visible:border-cyan-500 font-mono text-cyan-50"
placeholder="AQUA-NODE-001"
/>
{form.formState.errors.sensorId && <p className="text-red-400 text-xs mt-1">{form.formState.errors.sensorId.message}</p>}
</div>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label className="text-cyan-500/80 font-mono text-[10px] tracking-widest">LATITUDE</Label>
<Input
{...form.register("latitude")}
className="bg-[#05050A] border-slate-800 focus-visible:border-cyan-500 font-mono text-cyan-50"
placeholder="40.7128"
/>
{form.formState.errors.latitude && <p className="text-red-400 text-xs mt-1">{form.formState.errors.latitude.message}</p>}
</div>
<div className="space-y-2">
<Label className="text-cyan-500/80 font-mono text-[10px] tracking-widest">LONGITUDE</Label>
<Input
{...form.register("longitude")}
className="bg-[#05050A] border-slate-800 focus-visible:border-cyan-500 font-mono text-cyan-50"
placeholder="-74.0060"
/>
{form.formState.errors.longitude && <p className="text-red-400 text-xs mt-1">{form.formState.errors.longitude.message}</p>}
</div>
</div>
<div className="space-y-2">
<Label className="text-cyan-500/80 font-mono text-[10px] tracking-widest">LOCATION DESIGNATION</Label>
<Input
{...form.register("locationName")}
className="bg-[#05050A] border-slate-800 focus-visible:border-cyan-500 font-mono text-cyan-50"
placeholder="Hudson River Sector Alpha"
/>
</div>
<div className="pt-6">
<Button
type="submit"
disabled={create.isPending}
className="w-full bg-cyan-500 text-[#05050A] hover:bg-cyan-400 font-display tracking-[0.3em] text-lg py-6 transition-all hover:shadow-[0_0_20px_rgba(0,243,255,0.4)]"
>
{create.isPending ? "INITIALIZING..." : "EXECUTE DEPLOYMENT"}
</Button>
</div>
</form>
</DialogContent>
</Dialog>
);
}
|