import {
ConfigurableValue,
ConfigurableFieldDefinition,
PartialEditorPorts,
} from "@flyde/core";
import { ConfigurableValueBaseEditor } from "./ConfigurableValueBaseEditor";
import { useCallback, useState, useEffect } from "react";
import { convertValue } from "./convertValues";
import {
DropdownMenu,
DropdownMenuTrigger, Button, Settings, DropdownMenuContent, DropdownMenuItem, Check, AiGenerate, Expand, TooltipProvider, Tooltip, TooltipTrigger, Info, TooltipContent, Dialog, DialogTrigger, DialogContent
} from "../..";
function FieldContent({
config,
value,
onChange,
handleDialogToggle,
isExpanded,
onTypeChange,
nodeId,
insId,
}: {
config: ConfigurableFieldDefinition;
value: ConfigurableValue;
onChange: (value: ConfigurableValue) => void;
handleDialogToggle?: () => void;
isExpanded?: boolean;
onTypeChange?: (type: ConfigurableValue["type"]) => void;
nodeId: string;
insId?: string;
}) {
const shouldShowExpand =
!isExpanded &&
(value.type === "json" ||
(value.type === "string" && config.type === "longtext"));
const typeOptions: ConfigurableValue["type"][] = [
"dynamic",
"number",
"boolean",
"json",
"string",
];
return (
{config.label}:
{config.typeConfigurable !== false && onTypeChange && (
{value.type}
{typeOptions.map((type) => (
onTypeChange(type)}>
{type}
{value.type === type && }
))}
)}
{config.aiCompletion && (value.type === "json" || value.type === "string") && (
{
try {
const parsed = JSON.parse(text);
onChange({
...value,
value: parsed,
} as ConfigurableValue);
} catch {
onChange({ ...value, value: text } as ConfigurableValue);
}
}}
/>
)}
{shouldShowExpand && handleDialogToggle && (
)}
);
}
export function ConfigurableFieldEditor(props: {
value: ConfigurableValue;
onChange: (value: ConfigurableValue) => void;
config: ConfigurableFieldDefinition;
ports: PartialEditorPorts;
nodeId: string;
insId?: string;
}) {
const { config, value, onChange, ports, nodeId, insId } = props;
const [isDialogOpen, setIsDialogOpen] = useState(false);
const [rawJsonData, setRawJsonData] = useState(
value.type === "json" ? JSON.stringify(value.value, null, 2) : ""
);
useEffect(() => {
if (value.type === "json" && typeof value.value !== "string") {
setRawJsonData(JSON.stringify(value.value, null, 2));
}
}, [value]);
const changeType = useCallback(
(newType: ConfigurableValue["type"]) => {
const newValue = convertValue(value.type, newType, value.value);
onChange({ type: newType, value: newValue });
},
[value, onChange]
);
const handleDialogToggle = () => {
setIsDialogOpen(!isDialogOpen);
};
const isTemplateSupported =
((value.type === "string" && config.type !== 'secret') || value.type === "json") &&
config.templateSupport !== false
return (
{config.description ? (
{config.description}
) : (
)}
{isTemplateSupported && (
Variables supported
Use {{ }} to insert variables. For example {{myVar}} or {{myVar.someProp}}. Each distinct variable will be exposed as an input and can be used to receive dynamic values from other nodes.
)}
Open Dialog
{config.description ? (
{config.description}
) : (
)}
{isTemplateSupported && (
Variables supported
Use {{ }} to insert variables. For example {{myVar}} or {{myVar.someProp}}
)}
setIsDialogOpen(false)}>Close
);
}