forked from opensearch-project/dashboards-flow-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow_detail.tsx
171 lines (154 loc) · 4.87 KB
/
workflow_detail.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useEffect, useState } from 'react';
import { RouteComponentProps, useLocation } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { ReactFlowProvider } from 'reactflow';
import queryString from 'query-string';
import { EuiPage, EuiPageBody } from '@elastic/eui';
import { BREADCRUMBS } from '../../utils';
import { getCore } from '../../services';
import { WorkflowDetailHeader } from './components';
import {
AppState,
getWorkflow,
searchModels,
useAppDispatch,
} from '../../store';
import { ResizableWorkspace } from './workspace';
import {
DEFAULT_NEW_WORKFLOW_NAME,
FETCH_ALL_QUERY_BODY,
NEW_WORKFLOW_ID_URL,
} from '../../../common';
import { Resources } from './resources';
// styling
import './workflow-detail-styles.scss';
export interface WorkflowDetailRouterProps {
workflowId: string;
}
interface WorkflowDetailProps
extends RouteComponentProps<WorkflowDetailRouterProps> {}
enum WORKFLOW_DETAILS_TAB {
EDITOR = 'editor',
// TODO: temporarily adding a resources tab until UX is finalized.
// This gives clarity into what has been done on the cluster on behalf
// of the frontend provisioning workflows.
RESOURCES = 'resources',
}
const ACTIVE_TAB_PARAM = 'tab';
function replaceActiveTab(activeTab: string, props: WorkflowDetailProps) {
props.history.replace({
...history,
search: queryString.stringify({
[ACTIVE_TAB_PARAM]: activeTab,
}),
});
}
/**
* The workflow details page. This is where users will configure, create, and
* test their created workflows. Additionally, can be used to load existing workflows
* to view details and/or make changes to them.
* New, unsaved workflows are cached in the redux store and displayed here.
*/
export function WorkflowDetail(props: WorkflowDetailProps) {
const dispatch = useAppDispatch();
const { workflows, cachedWorkflow, errorMessage } = useSelector(
(state: AppState) => state.workflows
);
// selected workflow state
const workflowId = props.match?.params?.workflowId;
const isNewWorkflow = workflowId === NEW_WORKFLOW_ID_URL;
const workflow = isNewWorkflow ? cachedWorkflow : workflows[workflowId];
const workflowName = workflow
? workflow.name
: isNewWorkflow && !workflow
? DEFAULT_NEW_WORKFLOW_NAME
: '';
// tab state
const tabFromUrl = queryString.parse(useLocation().search)[
ACTIVE_TAB_PARAM
] as WORKFLOW_DETAILS_TAB;
const [selectedTabId, setSelectedTabId] = useState<WORKFLOW_DETAILS_TAB>(
tabFromUrl
);
// Default to editor tab if there is none or invalid tab ID specified via url.
useEffect(() => {
if (
!selectedTabId ||
!Object.values(WORKFLOW_DETAILS_TAB).includes(selectedTabId)
) {
setSelectedTabId(WORKFLOW_DETAILS_TAB.EDITOR);
replaceActiveTab(WORKFLOW_DETAILS_TAB.EDITOR, props);
}
}, []);
useEffect(() => {
getCore().chrome.setBreadcrumbs([
BREADCRUMBS.FLOW_FRAMEWORK,
BREADCRUMBS.WORKFLOWS,
{ text: workflowName },
]);
});
// On initial load:
// - fetch workflow, if there is an existing workflow ID
// - fetch available models as their IDs may be used when building flows
useEffect(() => {
if (!isNewWorkflow) {
dispatch(getWorkflow(workflowId));
}
dispatch(searchModels(FETCH_ALL_QUERY_BODY));
}, []);
// Show a toast if an error message exists in state
useEffect(() => {
if (errorMessage) {
console.error(errorMessage);
getCore().notifications.toasts.addDanger(errorMessage);
}
}, [errorMessage]);
const tabs = [
{
id: WORKFLOW_DETAILS_TAB.EDITOR,
label: 'Editor',
isSelected: selectedTabId === WORKFLOW_DETAILS_TAB.EDITOR,
onClick: () => {
setSelectedTabId(WORKFLOW_DETAILS_TAB.EDITOR);
replaceActiveTab(WORKFLOW_DETAILS_TAB.EDITOR, props);
},
},
{
id: WORKFLOW_DETAILS_TAB.RESOURCES,
label: 'Resources',
isSelected: selectedTabId === WORKFLOW_DETAILS_TAB.RESOURCES,
onClick: () => {
setSelectedTabId(WORKFLOW_DETAILS_TAB.RESOURCES);
replaceActiveTab(WORKFLOW_DETAILS_TAB.RESOURCES, props);
},
},
];
return (
<ReactFlowProvider>
<EuiPage>
<EuiPageBody className="workflow-detail">
<WorkflowDetailHeader
workflow={workflow}
isNewWorkflow={isNewWorkflow}
tabs={tabs}
/>
{selectedTabId === WORKFLOW_DETAILS_TAB.EDITOR && (
<ReactFlowProvider>
<ResizableWorkspace
isNewWorkflow={isNewWorkflow}
workflow={workflow}
/>
</ReactFlowProvider>
)}
{selectedTabId === WORKFLOW_DETAILS_TAB.RESOURCES && (
<Resources workflow={workflow} />
)}
</EuiPageBody>
</EuiPage>
</ReactFlowProvider>
);
}