forked from opensearch-project/dashboards-flow-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflows.tsx
124 lines (112 loc) · 3.54 KB
/
workflows.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useEffect, useState } from 'react';
import { RouteComponentProps, useLocation } from 'react-router-dom';
import {
EuiPageHeader,
EuiTitle,
EuiPage,
EuiPageBody,
EuiPageContent,
EuiSpacer,
} from '@elastic/eui';
import queryString from 'query-string';
import { useSelector } from 'react-redux';
import { BREADCRUMBS } from '../../utils';
import { getCore } from '../../services';
import { WorkflowList } from './workflow_list';
import { NewWorkflow } from './new_workflow';
import { AppState } from '../../store';
export interface WorkflowsRouterProps {}
interface WorkflowsProps extends RouteComponentProps<WorkflowsRouterProps> {}
enum WORKFLOWS_TAB {
MANAGE = 'manage',
CREATE = 'create',
}
const ACTIVE_TAB_PARAM = 'tab';
function replaceActiveTab(activeTab: string, props: WorkflowsProps) {
props.history.replace({
...history,
search: queryString.stringify({
[ACTIVE_TAB_PARAM]: activeTab,
}),
});
}
/**
* The base workflows page. From here, users can toggle between views to access
* existing created workflows, or explore the library of workflow templates
* to get started on a new workflow.
*/
export function Workflows(props: WorkflowsProps) {
const { workflows } = useSelector((state: AppState) => state.workflows);
const tabFromUrl = queryString.parse(useLocation().search)[
ACTIVE_TAB_PARAM
] as WORKFLOWS_TAB;
const [selectedTabId, setSelectedTabId] = useState<WORKFLOWS_TAB>(tabFromUrl);
// If there is no selected tab or invalid tab, default to a tab depending
// on if user has existing created workflows or not.
useEffect(() => {
if (
!selectedTabId ||
!Object.values(WORKFLOWS_TAB).includes(selectedTabId)
) {
if (workflows?.length > 0) {
setSelectedTabId(WORKFLOWS_TAB.MANAGE);
replaceActiveTab(WORKFLOWS_TAB.MANAGE, props);
} else {
setSelectedTabId(WORKFLOWS_TAB.CREATE);
replaceActiveTab(WORKFLOWS_TAB.CREATE, props);
}
}
}, [selectedTabId, workflows]);
useEffect(() => {
getCore().chrome.setBreadcrumbs([
BREADCRUMBS.FLOW_FRAMEWORK,
BREADCRUMBS.WORKFLOWS,
]);
});
return (
<EuiPage>
<EuiPageBody>
<EuiPageHeader
pageTitle="Workflows"
tabs={[
{
id: WORKFLOWS_TAB.MANAGE,
label: 'Manage workflows',
isSelected: selectedTabId === WORKFLOWS_TAB.MANAGE,
onClick: () => {
setSelectedTabId(WORKFLOWS_TAB.MANAGE);
replaceActiveTab(WORKFLOWS_TAB.MANAGE, props);
},
},
{
id: WORKFLOWS_TAB.CREATE,
label: 'New workflow',
isSelected: selectedTabId === WORKFLOWS_TAB.CREATE,
onClick: () => {
setSelectedTabId(WORKFLOWS_TAB.CREATE);
replaceActiveTab(WORKFLOWS_TAB.CREATE, props);
},
},
]}
bottomBorder={true}
/>
<EuiPageContent>
<EuiTitle>
<h2>
{selectedTabId === WORKFLOWS_TAB.MANAGE && 'Workflows'}
{selectedTabId === WORKFLOWS_TAB.CREATE &&
'Create a new workflow'}
</h2>
</EuiTitle>
<EuiSpacer size="m" />
{selectedTabId === WORKFLOWS_TAB.MANAGE && <WorkflowList />}
{selectedTabId === WORKFLOWS_TAB.CREATE && <NewWorkflow />}
</EuiPageContent>
</EuiPageBody>
</EuiPage>
);
}