mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-03-07 02:18:07 +01:00
Face Library UI tweaks (#17525)
* install react-dropzone * use react-dropzone with preview when uploading new face * spacing consistency * text tweaks
This commit is contained in:
@@ -1,38 +1,82 @@
|
||||
import { Form, FormControl, FormField, FormItem } from "@/components/ui/form";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import React, { useCallback } from "react";
|
||||
import { useCallback, useState } from "react";
|
||||
import { useDropzone } from "react-dropzone";
|
||||
import { useForm } from "react-hook-form";
|
||||
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { LuUpload, LuX } from "react-icons/lu";
|
||||
import { z } from "zod";
|
||||
|
||||
type ImageEntryProps = {
|
||||
onSave: (file: File) => void;
|
||||
children?: React.ReactNode;
|
||||
maxSize?: number;
|
||||
accept?: Record<string, string[]>;
|
||||
};
|
||||
export default function ImageEntry({ onSave, children }: ImageEntryProps) {
|
||||
|
||||
export default function ImageEntry({
|
||||
onSave,
|
||||
children,
|
||||
maxSize = 10 * 1024 * 1024, // 10MB default
|
||||
accept = { "image/*": [".jpeg", ".jpg", ".png", ".gif", ".webp"] },
|
||||
}: ImageEntryProps) {
|
||||
const { t } = useTranslation(["views/faceLibrary"]);
|
||||
const [preview, setPreview] = useState<string | null>(null);
|
||||
|
||||
const formSchema = z.object({
|
||||
file: z.instanceof(FileList, { message: "Please select an image file." }),
|
||||
file: z.instanceof(File, { message: "Please select an image file." }),
|
||||
});
|
||||
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
});
|
||||
const fileRef = form.register("file");
|
||||
|
||||
// upload handler
|
||||
const onDrop = useCallback(
|
||||
(acceptedFiles: File[]) => {
|
||||
if (acceptedFiles.length > 0) {
|
||||
const file = acceptedFiles[0];
|
||||
form.setValue("file", file, { shouldValidate: true });
|
||||
|
||||
// Create preview
|
||||
const objectUrl = URL.createObjectURL(file);
|
||||
setPreview(objectUrl);
|
||||
|
||||
// Clean up preview URL when component unmounts
|
||||
return () => URL.revokeObjectURL(objectUrl);
|
||||
}
|
||||
},
|
||||
[form],
|
||||
);
|
||||
|
||||
const { getRootProps, getInputProps, isDragActive, isDragReject } =
|
||||
useDropzone({
|
||||
onDrop,
|
||||
maxSize,
|
||||
accept,
|
||||
multiple: false,
|
||||
});
|
||||
|
||||
const onSubmit = useCallback(
|
||||
(data: z.infer<typeof formSchema>) => {
|
||||
if (!data["file"] || Object.keys(data.file).length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
onSave(data["file"]["0"]);
|
||||
if (!data.file) return;
|
||||
onSave(data.file);
|
||||
},
|
||||
[onSave],
|
||||
);
|
||||
|
||||
const clearSelection = () => {
|
||||
form.reset();
|
||||
setPreview(null);
|
||||
};
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)}>
|
||||
@@ -42,16 +86,55 @@ export default function ImageEntry({ onSave, children }: ImageEntryProps) {
|
||||
render={() => (
|
||||
<FormItem>
|
||||
<FormControl>
|
||||
<Input
|
||||
className="aspect-video h-40 w-full"
|
||||
type="file"
|
||||
{...fileRef}
|
||||
/>
|
||||
<div className="w-full">
|
||||
{!preview ? (
|
||||
<div
|
||||
{...getRootProps()}
|
||||
className={cn(
|
||||
"flex h-40 flex-col items-center justify-center rounded-lg border-2 border-dashed p-6 transition-colors",
|
||||
isDragActive && "border-primary bg-primary/5",
|
||||
isDragReject && "border-destructive bg-destructive/5",
|
||||
"cursor-pointer hover:border-primary hover:bg-primary/5",
|
||||
)}
|
||||
>
|
||||
<input {...getInputProps()} />
|
||||
<LuUpload className="mb-2 h-10 w-10 text-muted-foreground" />
|
||||
<p className="text-center text-sm text-muted-foreground">
|
||||
{isDragActive
|
||||
? t("imageEntry.dropActive")
|
||||
: t("imageEntry.dropInstructions")}
|
||||
</p>
|
||||
<p className="mt-1 text-xs text-muted-foreground">
|
||||
{t("imageEntry.maxSize", {
|
||||
size: Math.round(maxSize / (1024 * 1024)),
|
||||
})}
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="relative h-40 w-full">
|
||||
<img
|
||||
src={preview}
|
||||
alt="Preview"
|
||||
className="h-full w-full rounded-lg border object-contain"
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
variant="destructive"
|
||||
size="icon"
|
||||
className="absolute right-2 top-2 size-5 rounded-full"
|
||||
onClick={clearSelection}
|
||||
>
|
||||
<LuX className="size-3" />
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
{children}
|
||||
<div className="mt-4 flex justify-end">{children}</div>
|
||||
</form>
|
||||
</Form>
|
||||
);
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { Form, FormControl, FormField, FormItem } from "@/components/ui/form";
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import React, { useCallback } from "react";
|
||||
@@ -14,50 +20,47 @@ type TextEntryProps = {
|
||||
children?: React.ReactNode;
|
||||
};
|
||||
export default function TextEntry({
|
||||
defaultValue,
|
||||
defaultValue = "",
|
||||
placeholder,
|
||||
allowEmpty,
|
||||
allowEmpty = false,
|
||||
onSave,
|
||||
children,
|
||||
}: TextEntryProps) {
|
||||
const formSchema = z.object({
|
||||
text: z.string(),
|
||||
text: allowEmpty
|
||||
? z.string().optional()
|
||||
: z.string().min(1, "Field is required"),
|
||||
});
|
||||
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: { text: defaultValue },
|
||||
});
|
||||
const fileRef = form.register("text");
|
||||
|
||||
// upload handler
|
||||
|
||||
const onSubmit = useCallback(
|
||||
(data: z.infer<typeof formSchema>) => {
|
||||
if (!allowEmpty && !data["text"]) {
|
||||
return;
|
||||
}
|
||||
onSave(data["text"]);
|
||||
onSave(data.text || "");
|
||||
},
|
||||
[onSave, allowEmpty],
|
||||
[onSave],
|
||||
);
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="text"
|
||||
render={() => (
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormControl>
|
||||
<Input
|
||||
className="aspect-video h-8 w-full"
|
||||
{...field}
|
||||
className="w-full"
|
||||
placeholder={placeholder}
|
||||
type="text"
|
||||
{...fileRef}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage className="text-xs text-destructive" />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
@@ -20,7 +20,7 @@ import { cn } from "@/lib/utils";
|
||||
import axios from "axios";
|
||||
import { useCallback, useState } from "react";
|
||||
import { isDesktop } from "react-device-detect";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Trans, useTranslation } from "react-i18next";
|
||||
import { LuExternalLink } from "react-icons/lu";
|
||||
import { Link } from "react-router-dom";
|
||||
import { toast } from "sonner";
|
||||
@@ -101,7 +101,7 @@ export default function CreateFaceWizardDialog({
|
||||
}}
|
||||
>
|
||||
<Content
|
||||
className={cn("flex flex-col gap-4", isDesktop ? "max-w-[50%]" : "p-4")}
|
||||
className={cn("flex flex-col gap-4", isDesktop ? "max-w-3xl" : "p-4")}
|
||||
>
|
||||
<Header>
|
||||
<Title>{t("button.addFace")}</Title>
|
||||
@@ -110,7 +110,7 @@ export default function CreateFaceWizardDialog({
|
||||
<StepIndicator steps={STEPS} currentStep={step} />
|
||||
{step == 0 && (
|
||||
<TextEntry
|
||||
placeholder="Enter Face Name"
|
||||
placeholder={t("description.placeholder")}
|
||||
onSave={(name) => {
|
||||
setName(name);
|
||||
setStep(1);
|
||||
@@ -133,12 +133,16 @@ export default function CreateFaceWizardDialog({
|
||||
</ImageEntry>
|
||||
)}
|
||||
{step == 2 && (
|
||||
<div>
|
||||
<div className="mt-2">
|
||||
{t("toast.success.addFaceLibrary", { name })}
|
||||
<p className="py-4 text-sm text-secondary-foreground">
|
||||
{t("createFaceLibrary.nextSteps")}
|
||||
<p className="py-4 text-sm text-primary-variant">
|
||||
<ul className="list-inside list-disc">
|
||||
<Trans ns="views/faceLibrary">
|
||||
createFaceLibrary.nextSteps
|
||||
</Trans>
|
||||
</ul>
|
||||
</p>
|
||||
<div className="text-s my-4 flex items-center text-primary">
|
||||
<div className="my-2 flex items-center text-sm text-primary">
|
||||
<Link
|
||||
to="https://docs.frigate.video/configuration/face_recognition"
|
||||
target="_blank"
|
||||
|
||||
Reference in New Issue
Block a user