Files
frigate/web/src/components/overlay/FaceSelectionDialog.tsx
T

154 lines
4.7 KiB
TypeScript
Raw Normal View History

2025-04-10 15:33:51 -06:00
import {
Drawer,
DrawerClose,
DrawerContent,
DrawerDescription,
DrawerHeader,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
2025-11-17 08:12:05 -06:00
DropdownMenuSeparator,
2025-04-10 15:33:51 -06:00
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { isDesktop, isMobile } from "react-device-detect";
import { useTranslation } from "react-i18next";
import { cn } from "@/lib/utils";
2026-05-13 11:04:11 -05:00
import React, { ReactNode, useCallback, useMemo, useState } from "react";
2025-04-10 15:33:51 -06:00
import TextEntryDialog from "./dialog/TextEntryDialog";
import { Button } from "../ui/button";
type FaceSelectionDialogProps = {
className?: string;
faceNames: string[];
2026-03-24 08:18:06 -05:00
excludeName?: string;
dialogLabel?: string;
tooltipLabel?: string;
2025-04-10 15:33:51 -06:00
onTrainAttempt: (name: string) => void;
children: ReactNode;
};
export default function FaceSelectionDialog({
className,
faceNames,
2026-03-24 08:18:06 -05:00
excludeName,
dialogLabel,
tooltipLabel,
2025-04-10 15:33:51 -06:00
onTrainAttempt,
children,
}: FaceSelectionDialogProps) {
const { t } = useTranslation(["views/faceLibrary"]);
2026-03-24 08:18:06 -05:00
const filteredNames = useMemo(
() =>
excludeName ? faceNames.filter((n) => n !== excludeName) : faceNames,
[faceNames, excludeName],
);
2025-04-10 15:33:51 -06:00
const isChildButton = useMemo(
() => React.isValidElement(children) && children.type === Button,
[children],
);
// control
const [newFace, setNewFace] = useState(false);
2026-05-13 11:04:11 -05:00
// Non-modal Radix DropdownMenu doesn't propagate wheel events to nested
// scroll containers, so attach a non-passive listener that scrolls manually.
const scrollContainerRef = useCallback((el: HTMLDivElement | null) => {
if (!el || !isDesktop) return;
const handleWheel = (e: WheelEvent) => {
if (el.scrollHeight <= el.clientHeight) return;
e.preventDefault();
el.scrollTop += e.deltaY;
};
el.addEventListener("wheel", handleWheel, { passive: false });
return () => el.removeEventListener("wheel", handleWheel);
}, []);
2025-04-10 15:33:51 -06:00
// components
const Selector = isDesktop ? DropdownMenu : Drawer;
const SelectorTrigger = isDesktop ? DropdownMenuTrigger : DrawerTrigger;
const SelectorContent = isDesktop ? DropdownMenuContent : DrawerContent;
2025-05-22 10:38:14 -05:00
const SelectorItem = isDesktop
? DropdownMenuItem
: (props: React.HTMLAttributes<HTMLDivElement>) => (
<DrawerClose asChild>
<div {...props} className={cn(props.className, "my-2")} />
</DrawerClose>
);
2025-04-10 15:33:51 -06:00
2026-05-13 11:04:11 -05:00
// keep modal false on desktop to prevent dismissable layer pointer events
// issue with dialog auto-close
2025-04-10 15:33:51 -06:00
return (
2025-10-22 07:36:09 -06:00
<div className={className ?? "flex"}>
2025-04-10 15:33:51 -06:00
{newFace && (
<TextEntryDialog
open={true}
setOpen={setNewFace}
title={t("createFaceLibrary.new")}
onSave={(newName) => onTrainAttempt(newName)}
/>
)}
2026-05-13 11:04:11 -05:00
<Selector {...(isDesktop ? { modal: false } : {})}>
<Tooltip>
<TooltipTrigger asChild={isChildButton}>
<SelectorTrigger asChild>{children}</SelectorTrigger>
</TooltipTrigger>
<TooltipContent>{tooltipLabel ?? t("trainFace")}</TooltipContent>
</Tooltip>
<SelectorContent
ref={scrollContainerRef}
className={cn(
isDesktop && "scrollbar-container max-h-[40dvh] overflow-y-auto",
isMobile && "mx-1 gap-2 rounded-t-2xl px-4",
)}
onCloseAutoFocus={(e) => e.preventDefault()}
>
{isMobile && (
<DrawerHeader className="sr-only">
<DrawerTitle>Details</DrawerTitle>
<DrawerDescription>Details</DrawerDescription>
</DrawerHeader>
)}
<DropdownMenuLabel>
{dialogLabel ?? t("trainFaceAs")}
</DropdownMenuLabel>
<div
className={cn(
"flex flex-col",
isMobile &&
"max-h-[40dvh] gap-2 overflow-y-auto overflow-x-hidden pb-4",
2025-04-10 15:33:51 -06:00
)}
2026-05-13 11:04:11 -05:00
>
{filteredNames.sort().map((faceName) => (
2025-11-17 08:12:05 -06:00
<SelectorItem
2026-05-13 11:04:11 -05:00
key={faceName}
2025-11-17 08:12:05 -06:00
className="flex cursor-pointer gap-2 smart-capitalize"
2026-05-13 11:04:11 -05:00
onClick={() => onTrainAttempt(faceName)}
2025-11-17 08:12:05 -06:00
>
2026-05-13 11:04:11 -05:00
{faceName}
2025-11-17 08:12:05 -06:00
</SelectorItem>
2026-05-13 11:04:11 -05:00
))}
<DropdownMenuSeparator />
<SelectorItem
className="flex cursor-pointer gap-2 smart-capitalize"
onClick={() => setNewFace(true)}
>
{t("createFaceLibrary.new")}
</SelectorItem>
</div>
</SelectorContent>
</Selector>
2025-04-10 15:33:51 -06:00
</div>
);
}