|
| 1 | +import { SelectInput } from "@components/Form/FormSelect.tsx"; |
| 2 | +import { FieldWrapper } from "@components/Form/FormWrapper.tsx"; |
| 3 | +import { Protobuf } from "@meshtastic/sdk"; |
| 4 | +import type { Control } from "react-hook-form"; |
| 5 | +import { useWatch } from "react-hook-form"; |
| 6 | +import { useTranslation } from "react-i18next"; |
| 7 | +import type { RawSecurity } from "@app/validation/config/security.ts"; |
| 8 | +import { PACKET_SIGNATURE_POLICY_OPTIONS } from "./packetAuthenticityPolicy.ts"; |
| 9 | + |
| 10 | +const Policy = Protobuf.Config.Config_SecurityConfig_PacketSignaturePolicy; |
| 11 | + |
| 12 | +export { PACKET_SIGNATURE_POLICY_OPTIONS } from "./packetAuthenticityPolicy.ts"; |
| 13 | + |
| 14 | +const POLICY_DESCRIPTION_KEYS: Record< |
| 15 | + Protobuf.Config.Config_SecurityConfig_PacketSignaturePolicy, |
| 16 | + string |
| 17 | +> = { |
| 18 | + [Policy.COMPATIBLE]: |
| 19 | + "security.packetAuthenticity.options.compatible.description", |
| 20 | + [Policy.BALANCED]: "security.packetAuthenticity.options.balanced.description", |
| 21 | + [Policy.STRICT]: "security.packetAuthenticity.options.strict.description", |
| 22 | +}; |
| 23 | + |
| 24 | +interface PacketAuthenticityPolicyFieldProps { |
| 25 | + control: Control<RawSecurity>; |
| 26 | + supported: boolean; |
| 27 | + validateSelection: (policyKey: string) => Promise<boolean>; |
| 28 | +} |
| 29 | + |
| 30 | +export const PacketAuthenticityPolicyField = ({ |
| 31 | + control, |
| 32 | + supported, |
| 33 | + validateSelection, |
| 34 | +}: PacketAuthenticityPolicyFieldProps) => { |
| 35 | + const { t } = useTranslation("config"); |
| 36 | + const selectedPolicy = useWatch({ |
| 37 | + control, |
| 38 | + name: "packetSignaturePolicy", |
| 39 | + }); |
| 40 | + const description = supported |
| 41 | + ? t(POLICY_DESCRIPTION_KEYS[selectedPolicy] ?? POLICY_DESCRIPTION_KEYS[0]) |
| 42 | + : t("security.packetAuthenticity.unavailable"); |
| 43 | + |
| 44 | + return ( |
| 45 | + <FieldWrapper |
| 46 | + label={t("security.packetAuthenticity.protectionLevel")} |
| 47 | + fieldName="packetSignaturePolicy" |
| 48 | + description={description} |
| 49 | + > |
| 50 | + <SelectInput<RawSecurity> |
| 51 | + control={control} |
| 52 | + disabled={!supported} |
| 53 | + field={{ |
| 54 | + type: "select", |
| 55 | + name: "packetSignaturePolicy", |
| 56 | + label: t("security.packetAuthenticity.protectionLevel"), |
| 57 | + validate: validateSelection, |
| 58 | + properties: { |
| 59 | + enumValue: PACKET_SIGNATURE_POLICY_OPTIONS, |
| 60 | + optionLabels: { |
| 61 | + COMPATIBLE: t( |
| 62 | + "security.packetAuthenticity.options.compatible.label", |
| 63 | + ), |
| 64 | + BALANCED: t("security.packetAuthenticity.options.balanced.label"), |
| 65 | + STRICT: t("security.packetAuthenticity.options.strict.label"), |
| 66 | + }, |
| 67 | + "aria-label": t("security.packetAuthenticity.protectionLevel"), |
| 68 | + }, |
| 69 | + }} |
| 70 | + /> |
| 71 | + </FieldWrapper> |
| 72 | + ); |
| 73 | +}; |
0 commit comments