import { useFindOneRecord } from '@/object-record/hooks/useFindOneRecord'; import { useEffectiveDraftVersionId } from '@/workflow/hooks/useEffectiveDraftVersionId'; import { type Workflow, type WorkflowVersion, type WorkflowWithCurrentVersion, } from '@/workflow/types/Workflow'; import { CoreObjectNameSingular } from 'twenty-shared/types'; import { isDefined } from 'twenty-shared/utils'; type WorkflowWithAllVersions = Omit & { versions: Array< Pick >; }; export const useWorkflowWithCurrentVersion = ( workflowId: string | undefined, ): WorkflowWithCurrentVersion | undefined => { const { record: workflow } = useFindOneRecord({ objectNameSingular: CoreObjectNameSingular.Workflow, objectRecordId: workflowId, recordGqlFields: { id: true, name: true, statuses: true, lastPublishedVersionId: true, versions: { id: true, status: true, name: true, createdAt: true, }, }, skip: !isDefined(workflowId), }); const draftVersionFromServer = workflow?.versions.find( (workflowVersion) => workflowVersion.status === 'DRAFT', ); const { effectiveDraftId, lastDiscardedDraftId } = useEffectiveDraftVersionId( draftVersionFromServer, ); const workflowVersions = [...(workflow?.versions ?? [])] .filter((version) => version.id !== lastDiscardedDraftId) .sort((a, b) => (a.createdAt > b.createdAt ? -1 : 1)); const latestVersion = workflowVersions[0]; const currentVersionId = effectiveDraftId ?? latestVersion?.id; const { record: currentVersionWithSteps } = useFindOneRecord( { objectNameSingular: CoreObjectNameSingular.WorkflowVersion, objectRecordId: currentVersionId, recordGqlFields: { id: true, name: true, status: true, workflowId: true, createdAt: true, updatedAt: true, }, skip: !isDefined(currentVersionId), }, ); if (!isDefined(workflow) || !isDefined(currentVersionWithSteps)) { return undefined; } return { ...workflow, currentVersion: currentVersionWithSteps, }; };