import { useDevice } from "@core/stores/deviceStore.ts"; import { create } from "@bufbuild/protobuf"; import { Button } from "@components/UI/Button.tsx"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@components/UI/Dialog.tsx"; import { Protobuf } from "@meshtastic/core"; import { useForm } from "react-hook-form"; import { GenericInput } from "@components/Form/FormInput.tsx"; import { useTranslation } from "react-i18next"; import { validateMaxByteLength } from "@core/utils/string.ts"; import { Label } from "../UI/Label.tsx"; export interface User { longName: string; shortName: string; } export interface DeviceNameDialogProps { open: boolean; onOpenChange: (open: boolean) => void; } const MAX_LONG_NAME_BYTE_LENGTH = 40; const MAX_SHORT_NAME_BYTE_LENGTH = 4; export const DeviceNameDialog = ({ open, onOpenChange, }: DeviceNameDialogProps) => { const { t } = useTranslation("dialog"); const { hardware, getNode, connection } = useDevice(); const myNode = getNode(hardware.myNodeNum); const defaultValues = { longName: myNode?.user?.longName ?? t("unknown.longName"), shortName: myNode?.user?.shortName ?? t("unknown.shortName"), }; const { getValues, setValue, reset, control, handleSubmit } = useForm({ values: defaultValues, }); const { currentLength: currentLongNameLength } = validateMaxByteLength( getValues("longName"), MAX_LONG_NAME_BYTE_LENGTH, ); const { currentLength: currentShortNameLength } = validateMaxByteLength( getValues("shortName"), MAX_SHORT_NAME_BYTE_LENGTH, ); const onSubmit = handleSubmit((data) => { connection?.setOwner( create(Protobuf.Mesh.UserSchema, { ...(myNode?.user ?? {}), ...data, }), ); onOpenChange(false); }); const handleReset = () => { reset({ longName: "", shortName: "" }); setValue("longName", ""); setValue("shortName", ""); }; return ( {t("deviceName.title")} {t("deviceName.description")}
); };