mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-04-11 17:47:37 +03:00
remove duplicated component
This commit is contained in:
parent
c52a5b5a3a
commit
3052d1968f
@ -10,23 +10,26 @@ import {
|
|||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useFormContext } from "react-hook-form";
|
import { useFormContext } from "react-hook-form";
|
||||||
|
import { generateFixedHash, isValidId } from "@/utils/stringUtil";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
type NameAndIdFieldsProps = {
|
type NameAndIdFieldsProps = {
|
||||||
control: Control;
|
control: Control<any>;
|
||||||
|
type?: string;
|
||||||
nameField: string;
|
nameField: string;
|
||||||
idField: string;
|
idField: string;
|
||||||
nameLabel: string;
|
nameLabel: string;
|
||||||
nameDescription?: string;
|
nameDescription?: string;
|
||||||
idLabel?: string;
|
idLabel?: string;
|
||||||
idDescription?: string;
|
idDescription?: string;
|
||||||
processId: (name: string) => string;
|
processId?: (name: string) => string;
|
||||||
placeholderName?: string;
|
placeholderName?: string;
|
||||||
placeholderId?: string;
|
placeholderId?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function NameAndIdFields({
|
export default function NameAndIdFields({
|
||||||
control,
|
control,
|
||||||
|
type,
|
||||||
nameField,
|
nameField,
|
||||||
idField,
|
idField,
|
||||||
nameLabel,
|
nameLabel,
|
||||||
@ -41,16 +44,27 @@ export default function NameAndIdFields({
|
|||||||
const { watch, setValue, trigger } = useFormContext();
|
const { watch, setValue, trigger } = useFormContext();
|
||||||
const [isIdVisible, setIsIdVisible] = useState(false);
|
const [isIdVisible, setIsIdVisible] = useState(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(() => {
|
useEffect(() => {
|
||||||
const subscription = watch((value, { name }) => {
|
const subscription = watch((value, { name }) => {
|
||||||
if (name === nameField) {
|
if (name === nameField) {
|
||||||
const processedId = processId(value[nameField] || "");
|
const processedId = effectiveProcessId(value[nameField] || "");
|
||||||
setValue(idField, processedId);
|
setValue(idField, processedId);
|
||||||
trigger(idField);
|
trigger(idField);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return () => subscription.unsubscribe();
|
return () => subscription.unsubscribe();
|
||||||
}, [watch, setValue, trigger, nameField, idField, processId]);
|
}, [watch, setValue, trigger, nameField, idField, effectiveProcessId]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@ -62,18 +76,22 @@ export default function NameAndIdFields({
|
|||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
<FormLabel>{nameLabel}</FormLabel>
|
<FormLabel>{nameLabel}</FormLabel>
|
||||||
<span
|
<span
|
||||||
className="cursor-pointer text-right text-xs text-muted-foreground hover:underline"
|
className="cursor-pointer text-right text-xs text-muted-foreground"
|
||||||
onClick={() => setIsIdVisible(!isIdVisible)}
|
onClick={() => setIsIdVisible(!isIdVisible)}
|
||||||
>
|
>
|
||||||
(
|
|
||||||
{isIdVisible
|
{isIdVisible
|
||||||
? `${t("label.hide")} ${idLabel ?? t("label.ID")}`
|
? t("label.hide", { item: idLabel ?? t("label.ID") })
|
||||||
: `${t("label.show")} ${idLabel ?? t("label.ID")}`}
|
: t("label.show", {
|
||||||
)
|
item: idLabel ?? t("label.ID"),
|
||||||
|
})}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input placeholder={placeholderName} {...field} />
|
<Input
|
||||||
|
className="text-md"
|
||||||
|
placeholder={placeholderName}
|
||||||
|
{...field}
|
||||||
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
{nameDescription && (
|
{nameDescription && (
|
||||||
<FormDescription>{nameDescription}</FormDescription>
|
<FormDescription>{nameDescription}</FormDescription>
|
||||||
@ -88,12 +106,16 @@ export default function NameAndIdFields({
|
|||||||
name={idField}
|
name={idField}
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>{idLabel}</FormLabel>
|
<FormLabel>{idLabel ?? t("label.ID")}</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input placeholder={placeholderId} {...field} />
|
<Input
|
||||||
|
className="text-md"
|
||||||
|
placeholder={placeholderId}
|
||||||
|
{...field}
|
||||||
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormDescription>
|
<FormDescription>
|
||||||
{idDescription ?? t("field.description")}
|
{idDescription ?? t("field.internalID")}
|
||||||
</FormDescription>
|
</FormDescription>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|||||||
@ -47,7 +47,7 @@ import {
|
|||||||
MobilePageHeader,
|
MobilePageHeader,
|
||||||
MobilePageTitle,
|
MobilePageTitle,
|
||||||
} from "../mobile/MobilePage";
|
} from "../mobile/MobilePage";
|
||||||
import NameAndIdFields from "@/components/ui/NameAndIdFields";
|
import NameAndIdFields from "@/components/input/NameAndIdFields";
|
||||||
|
|
||||||
type CreateTriggerDialogProps = {
|
type CreateTriggerDialogProps = {
|
||||||
show: boolean;
|
show: boolean;
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import { useForm } from "react-hook-form";
|
|||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
import NameAndIdFields from "@/components/ui/NameAndIdFields";
|
import NameAndIdFields from "@/components/input/NameAndIdFields";
|
||||||
import { Form, FormDescription } from "@/components/ui/form";
|
import { Form, FormDescription } from "@/components/ui/form";
|
||||||
import {
|
import {
|
||||||
FormControl,
|
FormControl,
|
||||||
|
|||||||
@ -1,119 +0,0 @@
|
|||||||
import { Control } from "react-hook-form";
|
|
||||||
import {
|
|
||||||
FormField,
|
|
||||||
FormItem,
|
|
||||||
FormLabel,
|
|
||||||
FormControl,
|
|
||||||
FormDescription,
|
|
||||||
FormMessage,
|
|
||||||
} from "@/components/ui/form";
|
|
||||||
import { Input } from "@/components/ui/input";
|
|
||||||
import { useState, useEffect } from "react";
|
|
||||||
import { useFormContext } from "react-hook-form";
|
|
||||||
import { generateFixedHash, isValidId } from "@/utils/stringUtil";
|
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
|
|
||||||
type NameAndIdFieldsProps = {
|
|
||||||
control: Control<any>;
|
|
||||||
type?: string;
|
|
||||||
nameField: string;
|
|
||||||
idField: string;
|
|
||||||
nameLabel: string;
|
|
||||||
nameDescription?: string;
|
|
||||||
idLabel?: string;
|
|
||||||
idDescription?: string;
|
|
||||||
processId?: (name: string) => string;
|
|
||||||
placeholderName?: string;
|
|
||||||
placeholderId?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function NameAndIdFields({
|
|
||||||
control,
|
|
||||||
type,
|
|
||||||
nameField,
|
|
||||||
idField,
|
|
||||||
nameLabel,
|
|
||||||
nameDescription,
|
|
||||||
idLabel,
|
|
||||||
idDescription,
|
|
||||||
processId,
|
|
||||||
placeholderName,
|
|
||||||
placeholderId,
|
|
||||||
}: NameAndIdFieldsProps) {
|
|
||||||
const { t } = useTranslation(["common"]);
|
|
||||||
const { watch, setValue, trigger } = useFormContext();
|
|
||||||
const [isIdVisible, setIsIdVisible] = useState(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(() => {
|
|
||||||
const subscription = watch((value, { name }) => {
|
|
||||||
if (name === nameField) {
|
|
||||||
const processedId = effectiveProcessId(value[nameField] || "");
|
|
||||||
setValue(idField, processedId);
|
|
||||||
trigger(idField);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return () => subscription.unsubscribe();
|
|
||||||
}, [watch, setValue, trigger, nameField, idField, effectiveProcessId]);
|
|
||||||
|
|
||||||
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} {...field} />
|
|
||||||
</FormControl>
|
|
||||||
<FormDescription>
|
|
||||||
{idDescription ?? t("field.internalID")}
|
|
||||||
</FormDescription>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user