2023-12-13 00:21:42 +01:00
|
|
|
import { baseUrl } from "@/api/baseUrl";
|
|
|
|
import ExportCard from "@/components/card/ExportCard";
|
|
|
|
import {
|
|
|
|
AlertDialog,
|
|
|
|
AlertDialogCancel,
|
|
|
|
AlertDialogContent,
|
|
|
|
AlertDialogDescription,
|
|
|
|
AlertDialogFooter,
|
|
|
|
AlertDialogHeader,
|
|
|
|
AlertDialogTitle,
|
|
|
|
} from "@/components/ui/alert-dialog";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import { Calendar } from "@/components/ui/calendar";
|
2024-03-10 14:25:16 +01:00
|
|
|
import { Dialog, DialogContent, DialogTrigger } from "@/components/ui/dialog";
|
|
|
|
import { Drawer, DrawerContent, DrawerTrigger } from "@/components/ui/drawer";
|
2023-12-13 00:21:42 +01:00
|
|
|
import {
|
|
|
|
DropdownMenuRadioGroup,
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
DropdownMenu,
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuLabel,
|
|
|
|
DropdownMenuSeparator,
|
|
|
|
DropdownMenuRadioItem,
|
|
|
|
} from "@/components/ui/dropdown-menu";
|
2024-02-06 00:54:08 +01:00
|
|
|
import { Toaster } from "@/components/ui/sonner";
|
2023-12-13 00:21:42 +01:00
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
|
|
|
import axios from "axios";
|
|
|
|
import { format } from "date-fns";
|
|
|
|
import { useCallback, useState } from "react";
|
|
|
|
import { DateRange } from "react-day-picker";
|
2024-03-10 14:25:16 +01:00
|
|
|
import { isDesktop } from "react-device-detect";
|
2024-02-06 00:54:08 +01:00
|
|
|
import { toast } from "sonner";
|
2023-12-13 00:21:42 +01:00
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
type ExportItem = {
|
|
|
|
name: string;
|
|
|
|
};
|
2023-12-08 14:33:22 +01:00
|
|
|
|
|
|
|
function Export() {
|
2023-12-13 00:21:42 +01:00
|
|
|
const { data: config } = useSWR<FrigateConfig>("config");
|
|
|
|
const { data: exports, mutate } = useSWR<ExportItem[]>(
|
|
|
|
"exports/",
|
2024-02-28 23:23:56 +01:00
|
|
|
(url: string) => axios({ baseURL: baseUrl, url }).then((res) => res.data),
|
2023-12-13 00:21:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Export States
|
|
|
|
const [camera, setCamera] = useState<string | undefined>();
|
|
|
|
const [playback, setPlayback] = useState<string | undefined>();
|
|
|
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
currentDate.setHours(0, 0, 0, 0);
|
|
|
|
|
|
|
|
const [date, setDate] = useState<DateRange | undefined>({
|
|
|
|
from: currentDate,
|
|
|
|
});
|
|
|
|
const [startTime, setStartTime] = useState("00:00:00");
|
|
|
|
const [endTime, setEndTime] = useState("23:59:59");
|
|
|
|
|
|
|
|
const [deleteClip, setDeleteClip] = useState<string | undefined>();
|
|
|
|
|
|
|
|
const onHandleExport = () => {
|
2024-02-06 00:54:08 +01:00
|
|
|
if (!camera) {
|
|
|
|
toast.error("A camera needs to be selected.", { position: "top-center" });
|
2023-12-13 00:21:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-06 00:54:08 +01:00
|
|
|
if (!playback) {
|
|
|
|
toast.error("A playback factor needs to be selected.", {
|
|
|
|
position: "top-center",
|
2023-12-13 00:21:42 +01:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!date?.from || !startTime || !endTime) {
|
2024-02-06 00:54:08 +01:00
|
|
|
toast.error("A start and end time needs to be selected", {
|
|
|
|
position: "top-center",
|
2023-12-13 00:21:42 +01:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const startDate = new Date(date.from.getTime());
|
|
|
|
const [startHour, startMin, startSec] = startTime.split(":");
|
|
|
|
startDate.setHours(
|
|
|
|
parseInt(startHour),
|
|
|
|
parseInt(startMin),
|
|
|
|
parseInt(startSec),
|
2024-02-28 23:23:56 +01:00
|
|
|
0,
|
2023-12-13 00:21:42 +01:00
|
|
|
);
|
|
|
|
const start = startDate.getTime() / 1000;
|
|
|
|
const endDate = new Date((date.to || date.from).getTime());
|
|
|
|
const [endHour, endMin, endSec] = endTime.split(":");
|
|
|
|
endDate.setHours(parseInt(endHour), parseInt(endMin), parseInt(endSec), 0);
|
|
|
|
const end = endDate.getTime() / 1000;
|
|
|
|
|
|
|
|
if (end <= start) {
|
2024-02-06 00:54:08 +01:00
|
|
|
toast.error("The end time must be after the start time.", {
|
|
|
|
position: "top-center",
|
2023-12-13 00:21:42 +01:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
axios
|
|
|
|
.post(`export/${camera}/start/${start}/end/${end}`, { playback })
|
|
|
|
.then((response) => {
|
|
|
|
if (response.status == 200) {
|
2024-02-06 00:54:08 +01:00
|
|
|
toast.success(
|
|
|
|
"Successfully started export. View the file in the /exports folder.",
|
2024-02-28 23:23:56 +01:00
|
|
|
{ position: "top-center" },
|
2024-02-06 00:54:08 +01:00
|
|
|
);
|
2023-12-13 00:21:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mutate();
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
if (error.response?.data?.message) {
|
2024-02-06 00:54:08 +01:00
|
|
|
toast.error(
|
|
|
|
`Failed to start export: ${error.response.data.message}`,
|
2024-02-28 23:23:56 +01:00
|
|
|
{ position: "top-center" },
|
2024-02-06 00:54:08 +01:00
|
|
|
);
|
2023-12-13 00:21:42 +01:00
|
|
|
} else {
|
2024-02-06 00:54:08 +01:00
|
|
|
toast.error(`Failed to start export: ${error.message}`, {
|
|
|
|
position: "top-center",
|
2023-12-13 00:21:42 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onHandleDelete = useCallback(() => {
|
|
|
|
if (!deleteClip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
axios.delete(`export/${deleteClip}`).then((response) => {
|
|
|
|
if (response.status == 200) {
|
|
|
|
setDeleteClip(undefined);
|
|
|
|
mutate();
|
|
|
|
}
|
|
|
|
});
|
2024-02-28 23:23:56 +01:00
|
|
|
}, [deleteClip, mutate]);
|
2023-12-13 00:21:42 +01:00
|
|
|
|
2024-03-10 14:25:16 +01:00
|
|
|
const Create = isDesktop ? Dialog : Drawer;
|
|
|
|
const Trigger = isDesktop ? DialogTrigger : DrawerTrigger;
|
|
|
|
const Content = isDesktop ? DialogContent : DrawerContent;
|
|
|
|
|
2023-12-08 14:33:22 +01:00
|
|
|
return (
|
2024-03-10 14:25:16 +01:00
|
|
|
<div className="size-full p-2 overflow-hidden flex flex-col">
|
2024-02-06 00:54:08 +01:00
|
|
|
<Toaster />
|
2023-12-13 00:21:42 +01:00
|
|
|
|
|
|
|
<AlertDialog
|
|
|
|
open={deleteClip != undefined}
|
2024-02-28 23:23:56 +01:00
|
|
|
onOpenChange={() => setDeleteClip(undefined)}
|
2023-12-13 00:21:42 +01:00
|
|
|
>
|
|
|
|
<AlertDialogContent>
|
|
|
|
<AlertDialogHeader>
|
|
|
|
<AlertDialogTitle>Delete Export</AlertDialogTitle>
|
|
|
|
<AlertDialogDescription>
|
|
|
|
Confirm deletion of {deleteClip}.
|
|
|
|
</AlertDialogDescription>
|
|
|
|
</AlertDialogHeader>
|
|
|
|
<AlertDialogFooter>
|
|
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
|
|
<Button variant="destructive" onClick={() => onHandleDelete()}>
|
|
|
|
Delete
|
|
|
|
</Button>
|
|
|
|
</AlertDialogFooter>
|
|
|
|
</AlertDialogContent>
|
|
|
|
</AlertDialog>
|
|
|
|
|
2024-03-10 14:25:16 +01:00
|
|
|
<div className="w-full h-14">
|
|
|
|
<Create>
|
|
|
|
<Trigger>
|
|
|
|
<Button variant="select">New Export</Button>
|
|
|
|
</Trigger>
|
|
|
|
<Content className="flex flex-col justify-center items-center">
|
2024-03-15 12:59:03 +01:00
|
|
|
<div className="w-full flex justify-evenly items-center mt-4 md:mt-0">
|
2023-12-13 00:21:42 +01:00
|
|
|
<DropdownMenu>
|
|
|
|
<DropdownMenuTrigger asChild>
|
2024-03-10 14:25:16 +01:00
|
|
|
<Button className="capitalize" variant="secondary">
|
2024-03-15 12:59:03 +01:00
|
|
|
{camera?.replaceAll("_", " ") || "Select Camera"}
|
2024-03-10 14:25:16 +01:00
|
|
|
</Button>
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent>
|
|
|
|
<DropdownMenuLabel className="flex justify-center items-center">
|
2024-03-15 12:59:03 +01:00
|
|
|
Select Camera
|
2024-03-10 14:25:16 +01:00
|
|
|
</DropdownMenuLabel>
|
|
|
|
<DropdownMenuSeparator />
|
|
|
|
<DropdownMenuRadioGroup
|
|
|
|
value={camera}
|
|
|
|
onValueChange={setCamera}
|
|
|
|
>
|
|
|
|
{Object.keys(config?.cameras || {}).map((item) => (
|
|
|
|
<DropdownMenuRadioItem
|
|
|
|
className="capitalize"
|
|
|
|
key={item}
|
|
|
|
value={item}
|
|
|
|
>
|
|
|
|
{item.replaceAll("_", " ")}
|
|
|
|
</DropdownMenuRadioItem>
|
|
|
|
))}
|
|
|
|
</DropdownMenuRadioGroup>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</DropdownMenu>
|
|
|
|
<DropdownMenu>
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
<Button className="capitalize" variant="secondary">
|
2024-03-15 12:59:03 +01:00
|
|
|
{playback?.split("_")[0] || "Select Playback"}
|
2023-12-13 00:21:42 +01:00
|
|
|
</Button>
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent>
|
2024-03-10 14:25:16 +01:00
|
|
|
<DropdownMenuLabel className="flex justify-center items-center">
|
2024-03-15 12:59:03 +01:00
|
|
|
Select Playback
|
2023-12-13 00:21:42 +01:00
|
|
|
</DropdownMenuLabel>
|
|
|
|
<DropdownMenuSeparator />
|
|
|
|
<DropdownMenuRadioGroup
|
|
|
|
value={playback}
|
|
|
|
onValueChange={setPlayback}
|
|
|
|
>
|
|
|
|
<DropdownMenuRadioItem value="realtime">
|
|
|
|
Realtime
|
|
|
|
</DropdownMenuRadioItem>
|
|
|
|
<DropdownMenuRadioItem value="timelapse_25x">
|
|
|
|
Timelapse
|
|
|
|
</DropdownMenuRadioItem>
|
|
|
|
</DropdownMenuRadioGroup>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</DropdownMenu>
|
|
|
|
</div>
|
2024-03-10 14:25:16 +01:00
|
|
|
<Calendar mode="range" selected={date} onSelect={setDate} />
|
|
|
|
<div className="w-full flex justify-evenly">
|
|
|
|
<input
|
|
|
|
className="w-36 p-1 border border-input bg-background text-secondary-foreground hover:bg-accent hover:text-accent-foreground dark:[color-scheme:dark]"
|
|
|
|
id="startTime"
|
|
|
|
type="time"
|
|
|
|
value={startTime}
|
|
|
|
step="1"
|
|
|
|
onChange={(e) => setStartTime(e.target.value)}
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
className="w-36 p-1 mx-2 border border-input bg-background text-secondary-foreground hover:bg-accent hover:text-accent-foreground dark:[color-scheme:dark]"
|
|
|
|
id="endTime"
|
|
|
|
type="time"
|
|
|
|
value={endTime}
|
|
|
|
step="1"
|
|
|
|
onChange={(e) => setEndTime(e.target.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="w-full flex items-center justify-between px-4">
|
|
|
|
{`${
|
2023-12-13 00:21:42 +01:00
|
|
|
date?.from ? format(date?.from, "LLL dd, y") : ""
|
|
|
|
} ${startTime} -> ${
|
|
|
|
date?.to ? format(date?.to, "LLL dd, y") : ""
|
2024-03-10 14:25:16 +01:00
|
|
|
} ${endTime}`}
|
|
|
|
<Button
|
|
|
|
className="my-4"
|
|
|
|
variant="select"
|
|
|
|
onClick={() => onHandleExport()}
|
|
|
|
>
|
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Content>
|
|
|
|
</Create>
|
|
|
|
</div>
|
2023-12-13 00:21:42 +01:00
|
|
|
|
2024-03-10 14:25:16 +01:00
|
|
|
<div className="size-full overflow-hidden">
|
2023-12-13 00:21:42 +01:00
|
|
|
{exports && (
|
2024-03-10 14:25:16 +01:00
|
|
|
<div className="size-full grid gap-2 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 overflow-y-auto">
|
2023-12-13 00:21:42 +01:00
|
|
|
{Object.values(exports).map((item) => (
|
|
|
|
<ExportCard
|
|
|
|
key={item.name}
|
|
|
|
file={item}
|
|
|
|
onDelete={(file) => setDeleteClip(file)}
|
|
|
|
/>
|
|
|
|
))}
|
2024-03-10 14:25:16 +01:00
|
|
|
</div>
|
2023-12-13 00:21:42 +01:00
|
|
|
)}
|
|
|
|
</div>
|
2024-02-24 01:26:26 +01:00
|
|
|
</div>
|
2023-12-08 14:33:22 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Export;
|