import type { ProbeShellFormSchema } from "@/types/schema"; import { InfoIcon, ServerIcon } from "lucide-react"; import { useCallback, useEffect, useMemo } from "react"; import { Controller, type UseFormReturn } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Field, FieldContent, FieldError, FieldLabel } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Separator } from "@/components/ui/separator"; import { Switch } from "@/components/ui/switch"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; // Hoisted static JSX to avoid recreation on each render (rendering-hoist-jsx) const infoIcon = ; const PROBE_OPTIONS = [ { value: "Server" as const, label: "server" }, { value: "JDK" as const, label: "jdk" }, { value: "Command" as const, label: "command" }, { value: "Bytecode" as const, label: "bytecode" }, { value: "ScriptEngine" as const, label: "script" }, { value: "Filter" as const, label: "filter" }, ] as const; const MIDDLEWARE_OPTIONS = [ { value: "Tomcat", label: "Tomcat" }, { value: "Jetty", label: "Jetty" }, { value: "Undertow", label: "Undertow" }, { value: "Resin", label: "Resin" }, { value: "JBoss", label: "JBoss" }, { value: "GlassFish", label: "GlassFish" }, { value: "BES", label: "BES" }, { value: "TongWeb", label: "TongWeb" }, { value: "InforSuite", label: "InforSuite" }, { value: "Apusic", label: "Apusic" }, { value: "SpringWebFlux", label: "SpringWebFlux" }, { value: "WebLogic", label: "WebLogic" }, { value: "WebSphere", label: "WebSphere" }, ] as const; const PROBE_METHOD_OPTIONS = [ { value: "ResponseBody", label: "ResponseBody" }, { value: "DNSLog", label: "DNSLog" }, { value: "Sleep", label: "Sleep" }, ] as const; const DEFAULT_FORM_VALUES = { sleepServer: "Tomcat", seconds: 5, } as const; interface MainConfigCardProps { readonly form: UseFormReturn; readonly responseBodyServers?: string[]; } export default function MainConfigCard({ form, responseBodyServers }: MainConfigCardProps) { const { t } = useTranslation(["common", "probeshell"]); const watchedProbeMethod = form.watch("probeMethod"); const watchedProbeContent = form.watch("probeContent"); const filteredOptions = useMemo(() => { const filterMap = { ResponseBody: ["Command", "Bytecode", "ScriptEngine", "Filter"], DNSLog: ["JDK", "Server"], Sleep: ["Server"], } as const; const allowedValues = filterMap[watchedProbeMethod as keyof typeof filterMap]; if (!allowedValues) return PROBE_OPTIONS; return PROBE_OPTIONS.filter((opt) => allowedValues.includes(opt.value as never)); }, [watchedProbeMethod]); const resetFormValues = useCallback(() => { if (filteredOptions.length === 0) return; const currentValues = form.getValues(); form.reset({ ...currentValues, probeMethod: watchedProbeMethod, probeContent: filteredOptions[0].value, ...DEFAULT_FORM_VALUES, }); }, [form, watchedProbeMethod, filteredOptions]); useEffect(() => { resetFormValues(); }, [resetFormValues]); const isBodyMethod = watchedProbeMethod === "ResponseBody"; const isCommandBody = watchedProbeContent === "Command"; const isFilter = watchedProbeContent === "Filter"; const needParam = !isFilter && (isCommandBody || watchedProbeContent === "Bytecode" || watchedProbeContent === "ScriptEngine"); const isSleepMethod = watchedProbeMethod === "Sleep"; const isServerContent = watchedProbeContent === "Server"; return ( {t("mainConfig.title")} ( {t("probeshell:probeMethod")} {fieldState.error && } )} /> {watchedProbeMethod === "ResponseBody" && ( ( {t("server")} {fieldState.error && } )} /> )} {watchedProbeMethod === "DNSLog" && ( ( {t("probeshell:dnslog.host")} {fieldState.error && } )} /> )} {watchedProbeMethod && ( ( {t("probeshell:probeContent")} {fieldState.error && } )} /> )}
(

{t("common:debug.description")}

)} /> (

{t("common:byPassJavaModule.description")}

)} /> (

{t("common:lambdaSuffix.description")}

)} /> (

{t("common:shrink.description")}

)} /> (

{t("common:staticInitialize.description")}

)} />
{isBodyMethod && needParam && (
(
{t("common:paramName")} {t("common:optional")} {infoIcon}

{t("common:paramName.description")}

{fieldState.error && }
)} />
)} {isBodyMethod && isCommandBody && ( ( {t("common:commandTemplate")} {t("common:optional")}

{t("common:commandTemplate.description")}

)} /> )} {isSleepMethod && isServerContent && (
( {t("probeshell:sleepServer")} {fieldState.error && } )} /> ( {t("probeshell:sleepSeconds")} field.onChange(+event.target.value)} /> {fieldState.error && } )} />
)} ( {t("probeshell:shellClassName")} {t("optional")} )} />
); }