# Introduction Avoid having multiple `isDefined` definition across our pacakges Also avoid importing `isDefined` from `twenty-ui` which exposes a huge barrel for a such little util function ## In a nutshell Removed own `isDefined.ts` definition from `twenty-ui` `twenty-front` and `twenty-server` to move it to `twenty-shared`. Updated imports for each packages, and added explicit dependencies to `twenty-shared` if not already in place Related PR https://github.com/twentyhq/twenty/pull/9941
53 lines
1.4 KiB
TypeScript
53 lines
1.4 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-shared';
|
|
|
|
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]);
|
|
};
|