Files
twenty/packages/twenty-front/src/modules/workflow/hooks/useWorkflowWithCurrentVersion.ts
T
Raphaël Bosi 32194a88b3 Implement contextual actions for the workflows (#8814)
Implemented the following actions for the workflows:
- Test Workflow
- Discard Draft
- Activate Draft
- Activate Workflow Last Published Version
- Deactivate Workflow
- See Workflow Executions
- See Workflow Active Version
- See Workflow Versions History

And the following actions for the workflow versions:
- Use As Draft
- See Workflow Versions History
- See Workflow Executions

Video example:


https://github.com/user-attachments/assets/016956a6-6f2e-4de5-9605-d6e14526cbb3

A few of these actions are links to the relations of the workflow object
(link to a filtered table). To generalize this behavior, I will create
an hook named `useSeeRelationsActionSingleRecordAction` to automatically
generate links to a showPage if the relation is a Many To One or to a
filtered table if the relation is a One to Many for all the record
types.
I will also create actions which will allow to create a relation.

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
2024-12-03 12:09:51 +01:00

53 lines
1.3 KiB
TypeScript

import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useFindOneRecord } from '@/object-record/hooks/useFindOneRecord';
import {
Workflow,
WorkflowWithCurrentVersion,
} from '@/workflow/types/Workflow';
import { useMemo } from 'react';
import { isDefined } from 'twenty-ui';
export const useWorkflowWithCurrentVersion = (
workflowId: string | undefined,
): WorkflowWithCurrentVersion | undefined => {
const { record: workflow } = useFindOneRecord<Workflow>({
objectNameSingular: CoreObjectNameSingular.Workflow,
objectRecordId: workflowId,
recordGqlFields: {
id: true,
name: true,
statuses: true,
lastPublishedVersionId: true,
versions: true,
},
skip: !isDefined(workflowId),
});
return useMemo(() => {
if (!isDefined(workflow)) {
return undefined;
}
const draftVersion = workflow.versions.find(
(workflowVersion) => workflowVersion.status === 'DRAFT',
);
const workflowVersions = [...workflow.versions];
workflowVersions.sort((a, b) => (a.createdAt > b.createdAt ? -1 : 1));
const latestVersion = workflowVersions[0];
const currentVersion = draftVersion ?? latestVersion;
if (!isDefined(currentVersion)) {
return undefined;
}
return {
...workflow,
currentVersion,
};
}, [workflow]);
};