frigate/web/src/components/input/NameAndIdFields.tsx
Josh Hawkins 6fdd65ddb5
Some checks failed
CI / AMD64 Build (push) Has been cancelled
CI / ARM Build (push) Has been cancelled
CI / Jetson Jetpack 6 (push) Has been cancelled
CI / AMD64 Extra Build (push) Has been cancelled
CI / ARM Extra Build (push) Has been cancelled
CI / Synaptics Build (push) Has been cancelled
CI / Assemble and push default build (push) Has been cancelled
UI tweaks (#23346)
* remove redundant per-view toasters in settings

* add variants to standardize dialog footer button layouts

* remove text-md

this class name compiles to nothing in tailwind. we used to add it to prevent iOS from zooming when focusing on an input, but that is now solved via the viewport meta in index.html

* make wizard footers consistent with dialog footers

* consistent destructive button style

remove text-white from individual buttons and add it to the variant
2026-05-29 16:00:30 -06:00

149 lines
4.0 KiB
TypeScript

import { Control, FieldValues, Path, PathValue } from "react-hook-form";
import {
FormField,
FormItem,
FormLabel,
FormControl,
FormDescription,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { useState, useEffect, useRef } from "react";
import { useFormContext } from "react-hook-form";
import { generateFixedHash, isValidId } from "@/utils/stringUtil";
import { useTranslation } from "react-i18next";
type NameAndIdFieldsProps<T extends FieldValues = FieldValues> = {
control: Control<T>;
type?: string;
nameField: Path<T>;
idField: Path<T>;
nameLabel: string;
nameDescription?: string;
idLabel?: string;
idDescription?: string;
processId?: (name: string) => string;
placeholderName?: string;
placeholderId?: string;
idVisible?: boolean;
idDisabled?: boolean;
};
export default function NameAndIdFields<T extends FieldValues = FieldValues>({
control,
type,
nameField,
idField,
nameLabel,
nameDescription,
idLabel,
idDescription,
processId,
placeholderName,
placeholderId,
idVisible,
idDisabled,
}: NameAndIdFieldsProps<T>) {
const { t } = useTranslation(["common"]);
const { watch, setValue, trigger, formState } = useFormContext<T>();
const [isIdVisible, setIsIdVisible] = useState(idVisible ?? false);
const hasUserTypedRef = useRef(false);
const defaultProcessId = (name: string) => {
const normalized = name.replace(/\s+/g, "_").toLowerCase();
if (isValidId(normalized)) {
return normalized;
} else {
return generateFixedHash(name, type);
}
};
const effectiveProcessId = processId || defaultProcessId;
useEffect(() => {
if (idDisabled) {
return;
}
const subscription = watch((value, { name }) => {
if (name === nameField) {
hasUserTypedRef.current = true;
const processedId = effectiveProcessId(value[nameField] || "");
setValue(idField, processedId as PathValue<T, Path<T>>);
trigger(idField);
}
});
return () => subscription.unsubscribe();
}, [
watch,
setValue,
trigger,
nameField,
idField,
effectiveProcessId,
idDisabled,
]);
// Auto-expand if there's an error on the ID field after user has typed
useEffect(() => {
const idError = formState.errors[idField];
if (idError && hasUserTypedRef.current && !isIdVisible) {
setIsIdVisible(true);
}
}, [formState.errors, idField, isIdVisible]);
return (
<>
<FormField
control={control}
name={nameField}
render={({ field }) => (
<FormItem>
<div className="flex items-center justify-between">
<FormLabel>{nameLabel}</FormLabel>
<span
className="cursor-pointer text-right text-xs text-muted-foreground"
onClick={() => setIsIdVisible(!isIdVisible)}
>
{isIdVisible
? t("label.hide", { item: idLabel ?? t("label.ID") })
: t("label.show", {
item: idLabel ?? t("label.ID"),
})}
</span>
</div>
<FormControl>
<Input placeholder={placeholderName} {...field} />
</FormControl>
{nameDescription && (
<FormDescription>{nameDescription}</FormDescription>
)}
<FormMessage />
</FormItem>
)}
/>
{isIdVisible && (
<FormField
control={control}
name={idField}
render={({ field }) => (
<FormItem>
<FormLabel>{idLabel ?? t("label.ID")}</FormLabel>
<FormControl>
<Input
placeholder={placeholderId}
disabled={idDisabled}
{...field}
/>
</FormControl>
<FormDescription>
{idDescription ?? t("field.internalID")}
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
)}
</>
);
}