2024-10-22 17:01:01 +03:00
|
|
|
import { useState, ReactNode } from "react";
|
|
|
|
|
import { SearchResult } from "@/types/search";
|
|
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
|
|
|
|
import { baseUrl } from "@/api/baseUrl";
|
|
|
|
|
import { toast } from "sonner";
|
|
|
|
|
import axios from "axios";
|
2025-03-06 19:50:37 +03:00
|
|
|
import { FiMoreVertical } from "react-icons/fi";
|
2024-10-22 17:01:01 +03:00
|
|
|
import { buttonVariants } from "@/components/ui/button";
|
|
|
|
|
import {
|
|
|
|
|
ContextMenu,
|
|
|
|
|
ContextMenuContent,
|
|
|
|
|
ContextMenuItem,
|
|
|
|
|
ContextMenuTrigger,
|
|
|
|
|
} from "@/components/ui/context-menu";
|
|
|
|
|
import {
|
|
|
|
|
DropdownMenu,
|
|
|
|
|
DropdownMenuContent,
|
|
|
|
|
DropdownMenuItem,
|
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
|
} from "@/components/ui/dropdown-menu";
|
|
|
|
|
import {
|
|
|
|
|
AlertDialog,
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
AlertDialogTitle,
|
|
|
|
|
} from "@/components/ui/alert-dialog";
|
|
|
|
|
import useSWR from "swr";
|
2025-03-16 18:36:20 +03:00
|
|
|
import { Trans, useTranslation } from "react-i18next";
|
2025-10-27 15:44:34 +03:00
|
|
|
import BlurredIconButton from "../button/BlurredIconButton";
|
2025-03-16 18:36:20 +03:00
|
|
|
|
2024-10-22 17:01:01 +03:00
|
|
|
type SearchResultActionsProps = {
|
|
|
|
|
searchResult: SearchResult;
|
|
|
|
|
findSimilar: () => void;
|
|
|
|
|
refreshResults: () => void;
|
2025-10-26 21:12:20 +03:00
|
|
|
showTrackingDetails: () => void;
|
2025-07-07 17:03:57 +03:00
|
|
|
addTrigger: () => void;
|
2024-10-22 17:01:01 +03:00
|
|
|
isContextMenu?: boolean;
|
|
|
|
|
children?: ReactNode;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function SearchResultActions({
|
|
|
|
|
searchResult,
|
|
|
|
|
findSimilar,
|
|
|
|
|
refreshResults,
|
2025-10-26 21:12:20 +03:00
|
|
|
showTrackingDetails,
|
2025-07-07 17:03:57 +03:00
|
|
|
addTrigger,
|
2024-10-22 17:01:01 +03:00
|
|
|
isContextMenu = false,
|
|
|
|
|
children,
|
|
|
|
|
}: SearchResultActionsProps) {
|
2025-03-16 18:36:20 +03:00
|
|
|
const { t } = useTranslation(["views/explore"]);
|
|
|
|
|
|
2024-10-22 17:01:01 +03:00
|
|
|
const { data: config } = useSWR<FrigateConfig>("config");
|
|
|
|
|
|
|
|
|
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
|
|
|
|
|
|
|
|
|
const handleDelete = () => {
|
|
|
|
|
axios
|
|
|
|
|
.delete(`events/${searchResult.id}`)
|
|
|
|
|
.then((resp) => {
|
|
|
|
|
if (resp.status == 200) {
|
2025-03-16 18:36:20 +03:00
|
|
|
toast.success(t("searchResult.deleteTrackedObject.toast.success"), {
|
2024-10-22 17:01:01 +03:00
|
|
|
position: "top-center",
|
|
|
|
|
});
|
|
|
|
|
refreshResults();
|
|
|
|
|
}
|
|
|
|
|
})
|
2025-03-08 19:01:08 +03:00
|
|
|
.catch((error) => {
|
|
|
|
|
const errorMessage =
|
|
|
|
|
error.response?.data?.message ||
|
|
|
|
|
error.response?.data?.detail ||
|
|
|
|
|
"Unknown error";
|
2025-03-16 18:36:20 +03:00
|
|
|
toast.error(
|
|
|
|
|
t("searchResult.deleteTrackedObject.toast.error", { errorMessage }),
|
|
|
|
|
{
|
|
|
|
|
position: "top-center",
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-10-22 17:01:01 +03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const MenuItem = isContextMenu ? ContextMenuItem : DropdownMenuItem;
|
|
|
|
|
|
|
|
|
|
const menuItems = (
|
|
|
|
|
<>
|
|
|
|
|
{searchResult.has_clip && (
|
2025-03-16 18:36:20 +03:00
|
|
|
<MenuItem aria-label={t("itemMenu.downloadVideo.aria")}>
|
2024-10-22 17:01:01 +03:00
|
|
|
<a
|
|
|
|
|
className="flex items-center"
|
|
|
|
|
href={`${baseUrl}api/events/${searchResult.id}/clip.mp4`}
|
|
|
|
|
download={`${searchResult.camera}_${searchResult.label}.mp4`}
|
|
|
|
|
>
|
2025-03-16 20:13:34 +03:00
|
|
|
<span>{t("itemMenu.downloadVideo.label")}</span>
|
2024-10-22 17:01:01 +03:00
|
|
|
</a>
|
|
|
|
|
</MenuItem>
|
|
|
|
|
)}
|
|
|
|
|
{searchResult.has_snapshot && (
|
2025-03-16 18:36:20 +03:00
|
|
|
<MenuItem aria-label={t("itemMenu.downloadSnapshot.aria")}>
|
2024-10-22 17:01:01 +03:00
|
|
|
<a
|
|
|
|
|
className="flex items-center"
|
|
|
|
|
href={`${baseUrl}api/events/${searchResult.id}/snapshot.jpg`}
|
|
|
|
|
download={`${searchResult.camera}_${searchResult.label}.jpg`}
|
|
|
|
|
>
|
2025-03-16 18:36:20 +03:00
|
|
|
<span>{t("itemMenu.downloadSnapshot.label")}</span>
|
2024-10-22 17:01:01 +03:00
|
|
|
</a>
|
|
|
|
|
</MenuItem>
|
|
|
|
|
)}
|
2024-12-01 21:08:03 +03:00
|
|
|
{searchResult.data.type == "object" && (
|
|
|
|
|
<MenuItem
|
2025-10-26 21:12:20 +03:00
|
|
|
aria-label={t("itemMenu.viewTrackingDetails.aria")}
|
|
|
|
|
onClick={showTrackingDetails}
|
2024-12-01 21:08:03 +03:00
|
|
|
>
|
2025-10-26 21:12:20 +03:00
|
|
|
<span>{t("itemMenu.viewTrackingDetails.label")}</span>
|
2024-12-01 21:08:03 +03:00
|
|
|
</MenuItem>
|
|
|
|
|
)}
|
2025-07-07 17:03:57 +03:00
|
|
|
{config?.semantic_search?.enabled &&
|
|
|
|
|
searchResult.data.type == "object" && (
|
|
|
|
|
<MenuItem
|
2025-11-17 17:12:05 +03:00
|
|
|
aria-label={t("itemMenu.findSimilar.aria")}
|
|
|
|
|
onClick={findSimilar}
|
2025-07-07 17:03:57 +03:00
|
|
|
>
|
2025-11-17 17:12:05 +03:00
|
|
|
<span>{t("itemMenu.findSimilar.label")}</span>
|
2025-07-07 17:03:57 +03:00
|
|
|
</MenuItem>
|
|
|
|
|
)}
|
2025-11-07 16:53:27 +03:00
|
|
|
{config?.semantic_search?.enabled &&
|
|
|
|
|
searchResult.data.type == "object" && (
|
2025-03-16 18:36:20 +03:00
|
|
|
<MenuItem
|
2025-11-17 17:12:05 +03:00
|
|
|
aria-label={t("itemMenu.addTrigger.aria")}
|
|
|
|
|
onClick={addTrigger}
|
2025-03-16 18:36:20 +03:00
|
|
|
>
|
2025-11-17 17:12:05 +03:00
|
|
|
<span>{t("itemMenu.addTrigger.label")}</span>
|
2024-10-22 17:01:01 +03:00
|
|
|
</MenuItem>
|
|
|
|
|
)}
|
2024-10-23 01:07:42 +03:00
|
|
|
<MenuItem
|
2025-03-16 18:36:20 +03:00
|
|
|
aria-label={t("itemMenu.deleteTrackedObject.label")}
|
2024-10-23 01:07:42 +03:00
|
|
|
onClick={() => setDeleteDialogOpen(true)}
|
|
|
|
|
>
|
2025-03-16 18:36:20 +03:00
|
|
|
<span>{t("button.delete", { ns: "common" })}</span>
|
2024-10-22 17:01:01 +03:00
|
|
|
</MenuItem>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<AlertDialog
|
|
|
|
|
open={deleteDialogOpen}
|
|
|
|
|
onOpenChange={() => setDeleteDialogOpen(!deleteDialogOpen)}
|
|
|
|
|
>
|
|
|
|
|
<AlertDialogContent>
|
|
|
|
|
<AlertDialogHeader>
|
2025-03-17 15:26:01 +03:00
|
|
|
<AlertDialogTitle>
|
|
|
|
|
{t("dialog.confirmDelete.title")}
|
|
|
|
|
</AlertDialogTitle>
|
2024-10-22 17:01:01 +03:00
|
|
|
</AlertDialogHeader>
|
|
|
|
|
<AlertDialogDescription>
|
2025-03-16 18:36:20 +03:00
|
|
|
<Trans ns="views/explore">dialog.confirmDelete.desc</Trans>
|
2024-10-22 17:01:01 +03:00
|
|
|
</AlertDialogDescription>
|
|
|
|
|
<AlertDialogFooter>
|
2025-03-16 18:36:20 +03:00
|
|
|
<AlertDialogCancel>
|
|
|
|
|
{t("button.cancel", { ns: "common" })}
|
|
|
|
|
</AlertDialogCancel>
|
2024-10-22 17:01:01 +03:00
|
|
|
<AlertDialogAction
|
|
|
|
|
className={buttonVariants({ variant: "destructive" })}
|
|
|
|
|
onClick={handleDelete}
|
|
|
|
|
>
|
2025-03-16 18:36:20 +03:00
|
|
|
{t("button.delete", { ns: "common" })}
|
2024-10-22 17:01:01 +03:00
|
|
|
</AlertDialogAction>
|
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
</AlertDialog>
|
|
|
|
|
{isContextMenu ? (
|
|
|
|
|
<ContextMenu>
|
|
|
|
|
<ContextMenuTrigger>{children}</ContextMenuTrigger>
|
|
|
|
|
<ContextMenuContent>{menuItems}</ContextMenuContent>
|
|
|
|
|
</ContextMenu>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<DropdownMenu>
|
2025-10-27 15:44:34 +03:00
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
|
<BlurredIconButton aria-label={t("itemMenu.more.aria")}>
|
|
|
|
|
<FiMoreVertical className="size-5" />
|
|
|
|
|
</BlurredIconButton>
|
2024-10-22 17:01:01 +03:00
|
|
|
</DropdownMenuTrigger>
|
|
|
|
|
<DropdownMenuContent align="end">{menuItems}</DropdownMenuContent>
|
|
|
|
|
</DropdownMenu>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|