2026-02-12 10:40:49 +00:00
|
|
|
import { useGetOneLogicFunction } from '@/logic-functions/hooks/useGetOneLogicFunction';
|
|
|
|
|
import { type Dispatch, type SetStateAction, useEffect, useState } from 'react';
|
2026-01-28 00:42:19 +00:00
|
|
|
import { isDefined } from 'twenty-shared/utils';
|
2026-03-13 13:59:46 +00:00
|
|
|
import { type LogicFunction } from '~/generated-metadata/graphql';
|
2026-02-12 10:40:49 +00:00
|
|
|
import { useGetLogicFunctionSourceCode } from '@/logic-functions/hooks/useGetLogicFunctionSourceCode';
|
2026-02-16 09:43:29 +00:00
|
|
|
import { DEFAULT_TOOL_INPUT_SCHEMA } from 'twenty-shared/logic-function';
|
2026-01-28 00:42:19 +00:00
|
|
|
|
2026-02-16 09:43:29 +00:00
|
|
|
export type LogicFunctionFormValues = {
|
2026-01-28 00:42:19 +00:00
|
|
|
name: string;
|
|
|
|
|
description: string;
|
2026-02-16 09:43:29 +00:00
|
|
|
isTool: boolean;
|
|
|
|
|
timeoutSeconds: number;
|
|
|
|
|
sourceHandlerCode: string;
|
|
|
|
|
toolInputSchema?: object;
|
2026-01-28 00:42:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type SetLogicFunctionFormValues = Dispatch<
|
|
|
|
|
SetStateAction<LogicFunctionFormValues>
|
|
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
export const useLogicFunctionUpdateFormState = ({
|
|
|
|
|
logicFunctionId,
|
|
|
|
|
}: {
|
|
|
|
|
logicFunctionId: string;
|
|
|
|
|
}): {
|
|
|
|
|
formValues: LogicFunctionFormValues;
|
|
|
|
|
logicFunction: LogicFunction | null;
|
|
|
|
|
setFormValues: SetLogicFunctionFormValues;
|
|
|
|
|
loading: boolean;
|
|
|
|
|
} => {
|
|
|
|
|
const [formValues, setFormValues] = useState<LogicFunctionFormValues>({
|
|
|
|
|
name: '',
|
|
|
|
|
description: '',
|
2026-02-16 09:43:29 +00:00
|
|
|
isTool: false,
|
|
|
|
|
sourceHandlerCode: '',
|
|
|
|
|
timeoutSeconds: 300,
|
|
|
|
|
toolInputSchema: DEFAULT_TOOL_INPUT_SCHEMA,
|
2026-01-28 00:42:19 +00:00
|
|
|
});
|
|
|
|
|
|
2026-02-16 09:43:29 +00:00
|
|
|
const { sourceHandlerCode, loading: logicFunctionSourceCodeLoading } =
|
2026-02-12 10:40:49 +00:00
|
|
|
useGetLogicFunctionSourceCode({
|
|
|
|
|
logicFunctionId,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 00:42:19 +00:00
|
|
|
const { logicFunction, loading: logicFunctionLoading } =
|
|
|
|
|
useGetOneLogicFunction({
|
|
|
|
|
id: logicFunctionId,
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-13 13:59:46 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (isDefined(logicFunction)) {
|
|
|
|
|
setFormValues((prevState) => ({
|
|
|
|
|
...prevState,
|
|
|
|
|
name: logicFunction.name || '',
|
|
|
|
|
description: logicFunction.description || '',
|
|
|
|
|
isTool: logicFunction.isTool ?? false,
|
|
|
|
|
timeoutSeconds: logicFunction.timeoutSeconds ?? 300,
|
|
|
|
|
toolInputSchema:
|
|
|
|
|
logicFunction.toolInputSchema || DEFAULT_TOOL_INPUT_SCHEMA,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}, [logicFunction]);
|
|
|
|
|
|
2026-02-12 10:40:49 +00:00
|
|
|
useEffect(() => {
|
2026-02-16 09:43:29 +00:00
|
|
|
if (isDefined(sourceHandlerCode)) {
|
|
|
|
|
setFormValues((prev) => ({
|
|
|
|
|
...prev,
|
|
|
|
|
sourceHandlerCode,
|
|
|
|
|
}));
|
2026-02-12 10:40:49 +00:00
|
|
|
}
|
2026-02-16 09:43:29 +00:00
|
|
|
}, [sourceHandlerCode]);
|
2026-02-12 10:40:49 +00:00
|
|
|
|
2026-01-28 00:42:19 +00:00
|
|
|
return {
|
|
|
|
|
formValues,
|
|
|
|
|
setFormValues,
|
|
|
|
|
logicFunction,
|
2026-02-12 10:40:49 +00:00
|
|
|
loading: logicFunctionLoading || logicFunctionSourceCodeLoading,
|
2026-01-28 00:42:19 +00:00
|
|
|
};
|
|
|
|
|
};
|