"use client"; import { SettingsFormCell, SettingsFormGrid, SettingsSubsectionDescription, SettingsSubsectionHeader, SettingsSubsectionTitle } from "@app/components/Settings"; import { PaidFeaturesAlert } from "@app/components/PaidFeaturesAlert"; import { SshServerSettingsFields } from "@app/components/SshServerSettingsFields"; import { PrivateResourceAliasField } from "@app/components/PrivateResourceDestinationFields"; import { PrivateResourceSitesField } from "@app/components/PrivateResourceSitesField"; import { inferSshPamMode } from "@app/lib/privateResourceForm"; import { getSshUseMultiSiteTargetForm } from "@app/lib/privateResourceUtils"; import { FormControl, FormField, FormItem, FormLabel, FormMessage } from "@app/components/ui/form"; import { Input } from "@app/components/ui/input"; import { tierMatrix } from "@server/lib/billing/tierMatrix"; import { useTranslations } from "next-intl"; import { useState, type ReactNode } from "react"; import type { Control, UseFormSetValue, UseFormWatch } from "react-hook-form"; import type { Selectedsite } from "@app/components/site-selector"; type PrivateResourceSshFieldsProps = { control: Control; setValue: UseFormSetValue; watch: UseFormWatch; orgId?: string; disabled?: boolean; selectedSites: Selectedsite[]; onSelectedSitesChange: (sites: Selectedsite[]) => void; labelPrefix?: "create" | "edit"; showSshSettings?: boolean; layout?: "default" | "wizard"; showPaidFeaturesAlert?: boolean; hideAlias?: boolean; embedInParentGrid?: boolean; isNativeSsh?: boolean; }; export function PrivateResourceSshFields({ control, setValue, watch, orgId, disabled = false, selectedSites, onSelectedSitesChange, labelPrefix = "edit", showSshSettings = true, layout = "default", showPaidFeaturesAlert = true, hideAlias = false, embedInParentGrid = false, isNativeSsh: isNativeSshProp }: PrivateResourceSshFieldsProps) { const t = useTranslations(); const destinationLabelKey = labelPrefix === "create" ? "createInternalResourceDialogDestination" : "editInternalResourceDialogDestination"; const destinationPortLabelKey = labelPrefix === "create" ? "createInternalResourceDialogModePort" : "editInternalResourceDialogModePort"; const authDaemonMode = watch("authDaemonMode") ?? "site"; const pamMode = inferSshPamMode(authDaemonMode, watch("pamMode")); const standardDaemonLocation = watch("standardDaemonLocation") ?? (authDaemonMode === "remote" ? "remote" : "site"); const formAuthDaemonPort = watch("authDaemonPort"); const [authDaemonPortInput, setAuthDaemonPortInput] = useState(() => formAuthDaemonPort != null ? String(formAuthDaemonPort) : "22123" ); const isEditLayout = layout === "default"; const [sshServerMode, setSshServerMode] = useState<"standard" | "native">( () => (authDaemonMode === "native" ? "native" : "standard") ); const isNative = isNativeSshProp ?? (isEditLayout ? authDaemonMode === "native" : sshServerMode === "native"); const useMultiSiteTargetForm = getSshUseMultiSiteTargetForm( isNative, authDaemonMode, pamMode ); function trimSitesToFirst() { if (selectedSites.length <= 1) return; const first = selectedSites.slice(0, 1); onSelectedSitesChange(first); setValue( "siteIds", first.map((s: Selectedsite) => s.siteId), { shouldValidate: true } ); } function handlePamModeChange(value: "passthrough" | "push") { if (disabled) return; setValue("pamMode", value, { shouldValidate: true }); if (value === "passthrough") { setValue("authDaemonPort", null, { shouldValidate: true }); setAuthDaemonPortInput("22123"); return; } if (standardDaemonLocation !== "remote" && selectedSites.length > 1) { trimSitesToFirst(); } } function handleDaemonLocationChange(value: "site" | "remote") { if (disabled) return; setValue("standardDaemonLocation", value, { shouldValidate: true }); setValue("authDaemonMode", value, { shouldValidate: true }); if (value === "site") { setValue("authDaemonPort", null, { shouldValidate: true }); setAuthDaemonPortInput("22123"); trimSitesToFirst(); } } function handleAuthDaemonPortChange(value: string) { if (disabled) return; setAuthDaemonPortInput(value); const trimmed = value.trim(); setValue("authDaemonPort", trimmed ? Number(trimmed) : null, { shouldValidate: true }); } function handleServerModeChange(mode: "standard" | "native") { if (disabled) return; setSshServerMode(mode); if (mode === "native") { setValue("authDaemonMode", "native", { shouldValidate: true }); setValue("authDaemonPort", null, { shouldValidate: true }); setValue("destination", null, { shouldValidate: true }); setValue("destinationPort", null, { shouldValidate: true }); setAuthDaemonPortInput("22123"); trimSitesToFirst(); return; } setValue("authDaemonMode", standardDaemonLocation, { shouldValidate: true }); setValue("destinationPort", 22, { shouldValidate: true }); } const aliasField = hideAlias ? null : ( ); const standardSshTargetRow = orgId && !isNative ? (
( {t(destinationLabelKey)} field.onChange( e.target.value === "" ? null : e.target.value ) } /> )} /> ( {t(destinationPortLabelKey)} { const raw = e.target.value; if (raw === "") { field.onChange(null); return; } const n = Number(raw); field.onChange( Number.isFinite(n) ? n : null ); }} /> )} />
) : null; const sshSettingsFields = showSshSettings ? ( ) : null; const destinationSection = ( <> {t("sshServerDestination")} {t("sshServerDestinationDescription")} {isNative && orgId ? ( <> ) : null} {!isNative && orgId ? ( {standardSshTargetRow} ) : null} {!isNative && !hideAlias ? ( {aliasField} ) : null} ); const content: ReactNode = ( <> {showPaidFeaturesAlert && layout === "default" && ( )} {sshSettingsFields} {destinationSection} ); if (embedInParentGrid) { return content; } return {content}; }