"use client"; import { SettingsFormCell, SettingsFormGrid, SettingsSubsectionDescription, SettingsSubsectionHeader, SettingsSubsectionTitle } from "@app/components/Settings"; import { SwitchInput } from "@app/components/SwitchInput"; import { FormControl, FormField, FormItem, FormLabel, FormMessage } from "@app/components/ui/form"; import { Input } from "@app/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@app/components/ui/select"; import { getPortModeFromString, getPortStringFromMode, type PortMode } from "@app/lib/privateResourceForm"; import { useTranslations } from "next-intl"; import { useEffect, useState, type ReactNode } from "react"; import type { Control, UseFormSetValue } from "react-hook-form"; type PrivateResourceNetworkAccessFieldsProps = { control: Control; setValue: UseFormSetValue; showPortRanges?: boolean; initialTcp?: string | null; initialUdp?: string | null; disabled?: boolean; icmpId?: string; embedInParentGrid?: boolean; }; export function PrivateResourceAllowIcmpField({ control, id = "private-resource-allow-icmp", disabled = false }: { control: Control; id?: string; disabled?: boolean; }) { const t = useTranslations(); return ( ( field.onChange(!checked) } disabled={disabled} /> )} /> ); } function PrivateResourceNetworkAccessHeader() { const t = useTranslations(); return ( {t("privateResourceNetworkAccess")} {t("privateResourceNetworkAccessDescription")} ); } export function PrivateResourceNetworkAccessFields({ control, setValue, showPortRanges = true, initialTcp, initialUdp, disabled = false, icmpId = "private-resource-allow-icmp", embedInParentGrid = false }: PrivateResourceNetworkAccessFieldsProps) { const t = useTranslations(); const resolvedInitialTcp = initialTcp !== undefined ? initialTcp : "*"; const resolvedInitialUdp = initialUdp !== undefined ? initialUdp : "*"; const [tcpPortMode, setTcpPortMode] = useState(() => getPortModeFromString(resolvedInitialTcp) ); const [udpPortMode, setUdpPortMode] = useState(() => getPortModeFromString(resolvedInitialUdp) ); const [tcpCustomPorts, setTcpCustomPorts] = useState(() => resolvedInitialTcp && resolvedInitialTcp !== "*" ? resolvedInitialTcp : "" ); const [udpCustomPorts, setUdpCustomPorts] = useState(() => resolvedInitialUdp && resolvedInitialUdp !== "*" ? resolvedInitialUdp : "" ); useEffect(() => { if (!showPortRanges) return; setValue( "tcpPortRangeString", getPortStringFromMode(tcpPortMode, tcpCustomPorts) ); }, [showPortRanges, tcpPortMode, tcpCustomPorts, setValue]); useEffect(() => { if (!showPortRanges) return; setValue( "udpPortRangeString", getPortStringFromMode(udpPortMode, udpCustomPorts) ); }, [showPortRanges, udpPortMode, udpCustomPorts, setValue]); const content: ReactNode = ( <> {showPortRanges ? ( <> ( {t("editInternalResourceDialogTcp")}
{tcpPortMode === "custom" ? ( setTcpCustomPorts( e.target.value ) } /> ) : ( )}
)} />
( {t("editInternalResourceDialogUdp")}
{udpPortMode === "custom" ? ( setUdpCustomPorts( e.target.value ) } /> ) : ( )}
)} />
) : null} ); if (embedInParentGrid) { return content; } return {content}; } export function PrivateResourcePortRanges( props: Omit< PrivateResourceNetworkAccessFieldsProps, "showPortRanges" | "embedInParentGrid" > ) { return ; }