Skip to content

Commit

Permalink
Add the possibility to run workflows with manual trigger from the com…
Browse files Browse the repository at this point in the history
  • Loading branch information
bosiraphael authored Nov 6, 2024
1 parent ac7d740 commit 7f51eb8
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 76 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { WorkflowRunActionEffect } from '@/action-menu/actions/global-actions/workflow-run-actions/components/WorkflowRunActionEffect';
import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled';

export const GlobalActionMenuEntriesSetter = () => {
const isWorkflowEnabled = useIsFeatureEnabled('IS_WORKFLOW_ENABLED');

return <>{isWorkflowEnabled && <WorkflowRunActionEffect />}</>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { useActionMenuEntries } from '@/action-menu/hooks/useActionMenuEntries';
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { useAllActiveWorkflowVersions } from '@/workflow/hooks/useAllActiveWorkflowVersions';
import { useRunWorkflowVersion } from '@/workflow/hooks/useRunWorkflowVersion';

import { useTheme } from '@emotion/react';
import { useEffect } from 'react';
import { IconSettingsAutomation } from 'twenty-ui';
import { capitalize } from '~/utils/string/capitalize';

export const WorkflowRunActionEffect = () => {
const { addActionMenuEntry, removeActionMenuEntry } = useActionMenuEntries();

const { records: activeWorkflowVersions } = useAllActiveWorkflowVersions({
triggerType: 'MANUAL',
});

const { runWorkflowVersion } = useRunWorkflowVersion();

const { enqueueSnackBar } = useSnackBar();

const theme = useTheme();

useEffect(() => {
for (const [
index,
activeWorkflowVersion,
] of activeWorkflowVersions.entries()) {
addActionMenuEntry({
type: 'workflow-run',
key: `workflow-run-${activeWorkflowVersion.id}`,
label: capitalize(activeWorkflowVersion.workflow.name),
position: index,
Icon: IconSettingsAutomation,
onClick: async () => {
await runWorkflowVersion(activeWorkflowVersion.id);

enqueueSnackBar('', {
variant: SnackBarVariant.Success,
title: `${capitalize(activeWorkflowVersion.workflow.name)} starting...`,
icon: (
<IconSettingsAutomation
size={16}
color={theme.snackBar.success.color}
/>
),
});
},
});
}

return () => {
for (const activeWorkflowVersion of activeWorkflowVersions) {
removeActionMenuEntry(`workflow-run-${activeWorkflowVersion.id}`);
}
};
}, [
activeWorkflowVersions,
addActionMenuEntry,
enqueueSnackBar,
removeActionMenuEntry,
runWorkflowVersion,
theme.snackBar.success.color,
]);

return null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { contextStoreCurrentObjectMetadataIdComponentState } from '@/context-sto
import { contextStoreNumberOfSelectedRecordsComponentState } from '@/context-store/states/contextStoreNumberOfSelectedRecordsComponentState';
import { useObjectMetadataItemById } from '@/object-metadata/hooks/useObjectMetadataItemById';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled';

const globalRecordActionEffects = [ExportRecordsActionEffect];

Expand All @@ -29,6 +30,8 @@ export const RecordActionMenuEntriesSetter = () => {
objectId: contextStoreCurrentObjectMetadataId ?? '',
});

const isWorkflowEnabled = useIsFeatureEnabled('IS_WORKFLOW_ENABLED');

if (!objectMetadataItem) {
throw new Error(
`Object metadata item not found for id ${contextStoreCurrentObjectMetadataId}`,
Expand Down Expand Up @@ -56,7 +59,7 @@ export const RecordActionMenuEntriesSetter = () => {
objectMetadataItem={objectMetadataItem}
/>
))}
{contextStoreNumberOfSelectedRecords === 1 && (
{contextStoreNumberOfSelectedRecords === 1 && isWorkflowEnabled && (
<WorkflowRunRecordActionEffect
objectMetadataItem={objectMetadataItem}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { recordStoreFamilyState } from '@/object-record/record-store/states/reco
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { useAllActiveWorkflowVersionsForObject } from '@/workflow/hooks/useAllActiveWorkflowVersionsForObject';
import { useAllActiveWorkflowVersions } from '@/workflow/hooks/useAllActiveWorkflowVersions';
import { useRunWorkflowVersion } from '@/workflow/hooks/useRunWorkflowVersion';

import { useTheme } from '@emotion/react';
Expand Down Expand Up @@ -34,11 +34,10 @@ export const WorkflowRunRecordActionEffect = ({
recordStoreFamilyState(selectedRecordId ?? ''),
);

const { records: activeWorkflowVersions } =
useAllActiveWorkflowVersionsForObject({
objectNameSingular: objectMetadataItem.nameSingular,
triggerType: 'MANUAL',
});
const { records: activeWorkflowVersions } = useAllActiveWorkflowVersions({
objectMetadataItem,
triggerType: 'MANUAL',
});

const { runWorkflowVersion } = useRunWorkflowVersion();

Expand All @@ -57,7 +56,7 @@ export const WorkflowRunRecordActionEffect = ({
] of activeWorkflowVersions.entries()) {
addActionMenuEntry({
type: 'workflow-run',
key: `workflow-run-${activeWorkflowVersion.workflow.name}`,
key: `workflow-run-${activeWorkflowVersion.id}`,
label: capitalize(activeWorkflowVersion.workflow.name),
position: index,
Icon: IconSettingsAutomation,
Expand All @@ -84,9 +83,7 @@ export const WorkflowRunRecordActionEffect = ({

return () => {
for (const activeWorkflowVersion of activeWorkflowVersions) {
removeActionMenuEntry(
`workflow-run-${activeWorkflowVersion.workflow.name}`,
);
removeActionMenuEntry(`workflow-run-${activeWorkflowVersion.id}`);
}
};
}, [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GlobalActionMenuEntriesSetter } from '@/action-menu/actions/global-actions/components/GlobalActionMenuEntriesSetter';
import { RecordActionMenuEntriesSetter } from '@/action-menu/actions/record-actions/components/RecordActionMenuEntriesSetter';
import { ActionMenuConfirmationModals } from '@/action-menu/components/ActionMenuConfirmationModals';
import { RecordIndexActionMenuBar } from '@/action-menu/components/RecordIndexActionMenuBar';
Expand Down Expand Up @@ -27,6 +28,7 @@ export const RecordIndexActionMenu = () => {
<ActionMenuConfirmationModals />
<RecordIndexActionMenuEffect />
<RecordActionMenuEntriesSetter />
<GlobalActionMenuEntriesSetter />
</ActionMenuContext.Provider>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GlobalActionMenuEntriesSetter } from '@/action-menu/actions/global-actions/components/GlobalActionMenuEntriesSetter';
import { RecordActionMenuEntriesSetter } from '@/action-menu/actions/record-actions/components/RecordActionMenuEntriesSetter';
import { ActionMenuConfirmationModals } from '@/action-menu/components/ActionMenuConfirmationModals';
import { ActionMenuContext } from '@/action-menu/contexts/ActionMenuContext';
Expand Down Expand Up @@ -47,6 +48,7 @@ export const RecordShowActionMenu = ({
/>
<ActionMenuConfirmationModals />
<RecordActionMenuEntriesSetter />
<GlobalActionMenuEntriesSetter />
</ActionMenuContext.Provider>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GlobalActionMenuEntriesSetter } from '@/action-menu/actions/global-actions/components/GlobalActionMenuEntriesSetter';
import { RecordActionMenuEntriesSetter } from '@/action-menu/actions/record-actions/components/RecordActionMenuEntriesSetter';
import { ActionMenuConfirmationModals } from '@/action-menu/components/ActionMenuConfirmationModals';
import { RecordShowRightDrawerActionMenuBar } from '@/action-menu/components/RecordShowRightDrawerActionMenuBar';
Expand All @@ -23,6 +24,7 @@ export const RecordShowRightDrawerActionMenu = () => {
<RecordShowRightDrawerActionMenuBar />
<ActionMenuConfirmationModals />
<RecordActionMenuEntriesSetter />
<GlobalActionMenuEntriesSetter />
</ActionMenuContext.Provider>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ export const RecordShowRightDrawerActionMenuBar = () => {
const actionMenuEntries = useRecoilComponentValueV2(
actionMenuEntriesComponentSelector,
);

const standardActionMenuEntries = actionMenuEntries.filter(
(actionMenuEntry) => actionMenuEntry.type === 'standard',
);

return (
<>
{actionMenuEntries.map((actionMenuEntry) => (
{standardActionMenuEntries.map((actionMenuEntry) => (
<RecordShowActionMenuBarEntry entry={actionMenuEntry} />
))}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { generateDepthOneRecordGqlFields } from '@/object-record/graphql/utils/generateDepthOneRecordGqlFields';
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import {
Workflow,
WorkflowTriggerType,
WorkflowVersion,
} from '@/workflow/types/Workflow';
import { isDefined } from 'twenty-ui';

export const useAllActiveWorkflowVersions = ({
objectMetadataItem,
triggerType,
}: {
objectMetadataItem?: ObjectMetadataItem;
triggerType: WorkflowTriggerType;
}) => {
const filters = [
{
status: {
eq: 'ACTIVE',
},
},
{
trigger: {
like: `%"type": "${triggerType}"%`,
},
},
];

if (isDefined(objectMetadataItem)) {
filters.push({
trigger: {
like: `%"objectType": "${objectMetadataItem.nameSingular}"%`,
},
});
}

const { objectMetadataItem: workflowVersionObjectMetadataItem } =
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
});

const { records } = useFindManyRecords<
WorkflowVersion & { workflow: Workflow }
>({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
filter: {
and: filters,
},
recordGqlFields: {
...generateDepthOneRecordGqlFields({
objectMetadataItem: workflowVersionObjectMetadataItem,
}),
workflow: true,
},
});

// TODO: refactor when we can use 'not like' in the RawJson filter
if (!isDefined(objectMetadataItem)) {
return {
records: records.filter(
(record) => !isDefined(record.trigger?.settings.objectType),
),
};
}

return { records };
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const useRunWorkflowVersion = () => {

const runWorkflowVersion = async (
workflowVersionId: string,
payload: Record<string, any>,
payload?: Record<string, any>,
) => {
await mutate({
variables: { input: { workflowVersionId, payload } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type WorkflowDatabaseEventTrigger = BaseTrigger & {
eventName: string;
input?: object;
outputSchema: object;
objectType?: string;
};
};

Expand Down

0 comments on commit 7f51eb8

Please sign in to comment.