The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { SettingsPath } from 'twenty-shared/types';
import { getSettingsPath, isDefined } from 'twenty-shared/utils';
import { usePersistLogicFunction } from '@/logic-functions/hooks/usePersistLogicFunction';
import { useToast } from 'twenty-ui/components/feedback';
import { useLingui } from '@lingui/react/macro';
export const useCreateTool = () => {
const { t } = useLingui();
const navigate = useNavigate();
const { enqueueToast } = useToast();
const { createLogicFunction } = usePersistLogicFunction();
const [isCreatingTool, setIsCreatingTool] = useState(false);
const handleCreateTool = async () => {
setIsCreatingTool(true);
try {
const result = await createLogicFunction({
input: {
name: 'new-tool',
toolTriggerSettings: {
inputSchema: { type: 'object', properties: {} },
},
},
});
// Failure path already surfaces its own toast from usePersistLogicFunction.
if (result.status !== 'successful' || !isDefined(result.response?.data)) {
return;
}
const newLogicFunction = result.response.data.createOneLogicFunction;
enqueueToast({ status: 'success', children: t`Tool created` });
const applicationId = newLogicFunction.applicationId;
if (isDefined(applicationId)) {
navigate(
getSettingsPath(SettingsPath.ApplicationLogicFunctionDetail, {
applicationId,
logicFunctionId: newLogicFunction.id,
}),
);
} else {
navigate(
getSettingsPath(SettingsPath.LogicFunctionDetail, {
logicFunctionId: newLogicFunction.id,
}),
);
}
} finally {
setIsCreatingTool(false);
}
};
return { handleCreateTool, isCreatingTool };
};