Skip to content

Commit 997d492

Browse files
author
Roman Snapko
committed
Refactor action metadata logic to centralize in flows-utils and replace redundant implementations
1 parent 84bdedd commit 997d492

4 files changed

Lines changed: 55 additions & 47 deletions

File tree

packages/react-ui/src/app/features/builder/flow-canvas/nodes/step-node.tsx

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ import {
3232
FlowRunStatus,
3333
FlowVersion,
3434
isNil,
35-
RiskLevel,
3635
TriggerType,
3736
} from '@openops/shared';
3837

39-
import { getActionMetadata } from '@/app/features/flows/components/execute-risky-flow-dialog/utils';
38+
import { flowsHooks } from '@/app/features/flows/lib/flows-hooks';
4039
import { ShieldHalf } from 'lucide-react';
4140
import { CanvasContextMenu } from '../context-menu/canvas-context-menu';
4241
import { CollapsibleButton } from './collapsible-button';
@@ -130,26 +129,11 @@ const WorkflowStepNode = React.memo(
130129
return getStepStatus(data.step?.name, run, loopIndexes, flowVersion);
131130
}, [data.step?.name, run, loopIndexes, flowVersion]);
132131

133-
const isRiskyStep = useMemo(() => {
134-
const actionName = data.step?.settings.actionName;
135-
const blockName = data.step?.settings.blockName;
136-
137-
if (!actionsMetadata || !actionName || !blockName) {
138-
return false;
139-
}
140-
141-
const actionMetadata = getActionMetadata(
142-
actionsMetadata,
143-
blockName,
144-
actionName,
145-
);
146-
147-
return actionMetadata?.riskLevel === RiskLevel.HIGH;
148-
}, [
132+
const isRiskyStep = flowsHooks.useIsRiskyAction(
149133
actionsMetadata,
150-
data.step?.settings.actionName,
151134
data.step?.settings.blockName,
152-
]);
135+
data.step?.settings.actionName,
136+
);
153137

154138
const showRunningIcon =
155139
isNil(stepOutputStatus) && run?.status === FlowRunStatus.RUNNING;

packages/react-ui/src/app/features/flows/components/execute-risky-flow-dialog/utils.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,9 @@
1-
import { ActionBase } from '@openops/blocks-framework';
2-
import {
3-
BlockStepMetadataWithSuggestions,
4-
StepMetadataWithSuggestions,
5-
} from '@openops/components/ui';
1+
import { StepMetadataWithSuggestions } from '@openops/components/ui';
62
import { Action, ActionType, RiskLevel, Trigger } from '@openops/shared';
3+
import { flowsUtils } from '@/app/features/flows/lib/flows-utils';
74

85
type ActionOrTriggerWithIndex = (Action | Trigger) & { index: number };
96

10-
export const getActionMetadata = (
11-
metadata: StepMetadataWithSuggestions[] | undefined,
12-
blockName: string,
13-
actionName: string | undefined,
14-
): ActionBase | undefined => {
15-
const blockStepMetadata = metadata?.find(
16-
(stepMetadata: StepMetadataWithSuggestions) =>
17-
stepMetadata.type === ActionType.BLOCK &&
18-
(stepMetadata as BlockStepMetadataWithSuggestions).blockName ===
19-
blockName,
20-
) as BlockStepMetadataWithSuggestions | undefined;
21-
22-
return blockStepMetadata?.suggestedActions?.find(
23-
(suggestedAction) => suggestedAction.name === actionName,
24-
);
25-
};
26-
277
export const getRiskyActionFormattedNames = (
288
allSteps: (Action | Trigger)[],
299
metadata: StepMetadataWithSuggestions[] | undefined,
@@ -35,7 +15,7 @@ export const getRiskyActionFormattedNames = (
3515
.map((action) => {
3616
return {
3717
action,
38-
metadata: getActionMetadata(
18+
metadata: flowsUtils.getActionMetadata(
3919
metadata,
4020
action.settings.blockName,
4121
action.settings.actionName,

packages/react-ui/src/app/features/flows/lib/flows-hooks.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { INTERNAL_ERROR_TOAST, toast } from '@openops/components/ui';
2-
import { ListFlowsRequest, PopulatedFlow } from '@openops/shared';
1+
import {
2+
INTERNAL_ERROR_TOAST,
3+
StepMetadataWithSuggestions,
4+
toast,
5+
} from '@openops/components/ui';
6+
import { ListFlowsRequest, PopulatedFlow, RiskLevel } from '@openops/shared';
37
import { useMutation, useQuery } from '@tanstack/react-query';
48
import { t } from 'i18next';
5-
import { useState } from 'react';
9+
import { useMemo, useState } from 'react';
610
import { NavigateFunction } from 'react-router-dom';
711

12+
import { flowsUtils } from '@/app/features/flows/lib/flows-utils';
813
import { flowsApi } from './flows-api';
914

1015
import { userSettingsHooks } from '@/app/common/hooks/user-settings-hooks';
@@ -65,6 +70,22 @@ export const flowsHooks = {
6570
setSearchTerm,
6671
};
6772
},
73+
useIsRiskyAction: (
74+
metadata: StepMetadataWithSuggestions[] | undefined,
75+
76+
blockName: string | undefined,
77+
actionName: string | undefined,
78+
) => {
79+
return useMemo(() => {
80+
if (!metadata || !blockName || !actionName) return false;
81+
const actionMetadata = flowsUtils.getActionMetadata(
82+
metadata,
83+
blockName,
84+
actionName,
85+
);
86+
return actionMetadata?.riskLevel === RiskLevel.HIGH;
87+
}, [metadata, blockName, actionName]);
88+
},
6889
useCreateFlow: (navigate: NavigateFunction) => {
6990
const { updateHomePageOperationalViewFlag } =
7091
userSettingsHooks.useHomePageOperationalView();

packages/react-ui/src/app/features/flows/lib/flows-utils.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import { ActionBase } from '@openops/blocks-framework';
2+
import {
3+
BlockStepMetadataWithSuggestions,
4+
StepMetadataWithSuggestions,
5+
} from '@openops/components/ui';
16
import cronstrue from 'cronstrue/i18n';
27
import { t } from 'i18next';
38
import { TimerReset, TriangleAlert, Zap } from 'lucide-react';
49

5-
import { Flow, FlowVersion, TriggerType } from '@openops/shared';
10+
import { ActionType, Flow, FlowVersion, TriggerType } from '@openops/shared';
611

712
import { flowsApi } from './flows-api';
813

@@ -29,8 +34,26 @@ const downloadFlow = async (flowId: string, versionId: string) => {
2934
downloadFile(JSON.stringify(template, null, 2), template.name, 'json');
3035
};
3136

37+
const getActionMetadata = (
38+
metadata: StepMetadataWithSuggestions[] | undefined,
39+
blockName: string,
40+
actionName: string | undefined,
41+
): ActionBase | undefined => {
42+
const blockStepMetadata = metadata?.find(
43+
(stepMetadata: StepMetadataWithSuggestions) =>
44+
stepMetadata.type === ActionType.BLOCK &&
45+
(stepMetadata as BlockStepMetadataWithSuggestions).blockName ===
46+
blockName,
47+
) as BlockStepMetadataWithSuggestions | undefined;
48+
49+
return blockStepMetadata?.suggestedActions?.find(
50+
(suggestedAction) => suggestedAction.name === actionName,
51+
);
52+
};
53+
3254
export const flowsUtils = {
3355
downloadFlow,
56+
getActionMetadata,
3457
flowStatusToolTipRenderer: (flow: Flow, version: FlowVersion) => {
3558
const trigger = version.trigger;
3659
switch (trigger.type) {

0 commit comments

Comments
 (0)