大声道撒旦

This commit is contained in:
dengqichen 2024-12-30 16:17:07 +08:00
parent 4ac633d295
commit 678f6a1809

View File

@ -1,4 +1,4 @@
import React from 'react'; import React, { useEffect } from 'react';
import {Tabs, TabsProps} from 'antd'; import {Tabs, TabsProps} from 'antd';
import {Cell} from '@antv/x6'; import {Cell} from '@antv/x6';
import {NodeDefinitionResponse} from "@/pages/Workflow/NodeDesign/types"; import {NodeDefinitionResponse} from "@/pages/Workflow/NodeDesign/types";
@ -10,18 +10,8 @@ import {
SheetFooter, SheetFooter,
} from "@/components/ui/sheet"; } from "@/components/ui/sheet";
import {Button} from "@/components/ui/button"; import {Button} from "@/components/ui/button";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import {Input} from "@/components/ui/input"; import {Input} from "@/components/ui/input";
import {useForm} from "react-hook-form"; import {Label} from "@/components/ui/label";
import {zodResolver} from "@hookform/resolvers/zod";
import * as z from "zod";
interface NodeConfigDrawerProps { interface NodeConfigDrawerProps {
visible: boolean; visible: boolean;
@ -42,100 +32,90 @@ const NodeConfigDrawer: React.FC<NodeConfigDrawerProps> = ({
onOk, onOk,
onCancel, onCancel,
}) => { }) => {
// 创建动态的 schema const [panelValues, setPanelValues] = React.useState<Variables>({});
const createFormSchema = (schema: any) => { const [localValues, setLocalValues] = React.useState<Variables>({});
if (!schema?.properties) return z.object({});
// 初始化表单值,包括默认值
const initializeFormValues = (schema: any) => {
if (!schema?.properties) return {};
const schemaFields: Record<string, any> = {}; const initialValues: Variables = {};
Object.entries(schema.properties).forEach(([key, property]: [string, any]) => { Object.entries(schema.properties).forEach(([key, property]: [string, any]) => {
switch (property.type) { if (property.default !== undefined) {
case 'string': initialValues[key] = property.default;
schemaFields[key] = property.required ? z.string() : z.string().optional();
break;
case 'number':
schemaFields[key] = property.required ? z.number() : z.number().optional();
break;
case 'boolean':
schemaFields[key] = property.required ? z.boolean() : z.boolean().optional();
break;
default:
schemaFields[key] = z.any();
} }
}); });
return z.object(schemaFields); return initialValues;
}; };
const panelFormSchema = createFormSchema(nodeDefinition?.panelVariablesSchema); useEffect(() => {
const localFormSchema = createFormSchema(nodeDefinition?.localVariablesSchema); if (nodeDefinition) {
// 合并默认值和已有值
const panelForm = useForm<z.infer<typeof panelFormSchema>>({ const panelDefaults = initializeFormValues(nodeDefinition.panelVariablesSchema);
resolver: zodResolver(panelFormSchema), const localDefaults = initializeFormValues(nodeDefinition.localVariablesSchema);
defaultValues: nodeDefinition?.panelVariables || {},
}); setPanelValues({
...panelDefaults,
const localForm = useForm<z.infer<typeof localFormSchema>>({ ...(nodeDefinition.panelVariables || {})
resolver: zodResolver(localFormSchema), });
defaultValues: nodeDefinition?.localVariables || {}, setLocalValues({
}); ...localDefaults,
...(nodeDefinition.localVariables || {})
const handleSubmit = async () => { });
try {
const panelValues = await panelForm.handleSubmit((data) => data)();
const localValues = await localForm.handleSubmit((data) => data)();
const updatedNodeDefinition = {
...nodeDefinition,
panelVariables: panelValues,
localVariables: localValues
};
onOk(updatedNodeDefinition);
} catch (error) {
console.error('Validation failed:', error);
} }
}, [nodeDefinition]);
const handleSubmit = () => {
const updatedNodeDefinition = {
...nodeDefinition,
panelVariables: panelValues,
localVariables: localValues
};
onOk(updatedNodeDefinition);
}; };
const renderFormFields = (schema: any, form: any) => { const renderFormFields = (schema: any, values: Variables, onChange: (key: string, value: any) => void) => {
if (!schema?.properties) return null; if (!schema?.properties) return null;
return Object.entries(schema.properties).map(([key, property]: [string, any]) => ( return Object.entries(schema.properties).map(([key, property]: [string, any]) => (
<FormField <div key={key} className="space-y-2">
key={key} <Label>{property.title || key}</Label>
control={form.control} <Input
name={key} value={values[key] || ''}
render={({field}) => ( placeholder={property.description}
<FormItem> onChange={(e) => onChange(key, e.target.value)}
<FormLabel>{property.title || key}</FormLabel> readOnly={property.readOnly}
<FormControl> className={property.readOnly ? "bg-muted" : ""}
<Input {...field} placeholder={property.description} /> />
</FormControl> </div>
<FormMessage />
</FormItem>
)}
/>
)); ));
}; };
const handlePanelChange = (key: string, value: any) => {
setPanelValues(prev => ({...prev, [key]: value}));
};
const handleLocalChange = (key: string, value: any) => {
setLocalValues(prev => ({...prev, [key]: value}));
};
const items: TabsProps['items'] = [ const items: TabsProps['items'] = [
nodeDefinition?.panelVariablesSchema && { nodeDefinition?.panelVariablesSchema && {
key: 'panel', key: 'panel',
label: '面板变量', label: '面板变量',
children: ( children: (
<Form {...panelForm}> <div className="space-y-4">
<form className="space-y-4"> {renderFormFields(nodeDefinition.panelVariablesSchema, panelValues, handlePanelChange)}
{renderFormFields(nodeDefinition.panelVariablesSchema, panelForm)} </div>
</form>
</Form>
) )
}, },
nodeDefinition?.localVariablesSchema && { nodeDefinition?.localVariablesSchema && {
key: 'local', key: 'local',
label: '环境变量', label: '环境变量',
children: ( children: (
<Form {...localForm}> <div className="space-y-4">
<form className="space-y-4"> {renderFormFields(nodeDefinition.localVariablesSchema, localValues, handleLocalChange)}
{renderFormFields(nodeDefinition.localVariablesSchema, localForm)} </div>
</form>
</Form>
) )
} }
].filter(Boolean) as TabsProps['items']; ].filter(Boolean) as TabsProps['items'];
@ -143,20 +123,24 @@ const NodeConfigDrawer: React.FC<NodeConfigDrawerProps> = ({
return ( return (
<Sheet open={visible} onOpenChange={(open) => !open && onCancel()}> <Sheet open={visible} onOpenChange={(open) => !open && onCancel()}>
<SheetContent side="right" className="w-[480px] sm:w-[480px]"> <SheetContent side="right" className="w-[480px] sm:w-[480px]">
<SheetHeader> <div className="flex flex-col h-full">
<SheetTitle> - {nodeDefinition?.nodeName || ''}</SheetTitle> <SheetHeader>
</SheetHeader> <SheetTitle> - {nodeDefinition?.nodeName || ''}</SheetTitle>
<div className="flex-1 overflow-y-auto py-6"> </SheetHeader>
<Tabs items={items}/> <div className="flex-1 overflow-y-auto py-6">
<Tabs items={items} className="border-none"/>
</div>
<div className="shrink-0 border-t bg-background p-4">
<div className="flex justify-end gap-2">
<Button variant="outline" onClick={onCancel}>
</Button>
<Button onClick={handleSubmit}>
</Button>
</div>
</div>
</div> </div>
<SheetFooter>
<Button variant="outline" onClick={onCancel}>
</Button>
<Button onClick={handleSubmit}>
</Button>
</SheetFooter>
</SheetContent> </SheetContent>
</Sheet> </Sheet>
); );