-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Discover.tsx
111 lines (96 loc) · 3.11 KB
/
Discover.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
/**
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import React from 'react';
import { Prompt } from 'react-router-dom';
import { Box } from 'design';
import { Navigation } from 'teleport/components/Wizard/Navigation';
import { FeatureBox } from 'teleport/components/Layout';
import { SelectResource } from 'teleport/Discover/SelectResource/SelectResource';
import cfg from 'teleport/config';
import { findViewAtIndex } from 'teleport/components/Wizard/flow';
import { EViewConfigs } from './types';
import { DiscoverProvider, useDiscover } from './useDiscover';
import { DiscoverIcon } from './SelectResource/icons';
function DiscoverContent() {
const {
currentStep,
viewConfig,
onSelectResource,
indexedViews,
...agentProps
} = useDiscover();
let content;
const hasSelectedResource = Boolean(viewConfig);
if (hasSelectedResource) {
const view = findViewAtIndex(indexedViews, currentStep);
const Component = view.component;
content = <Component {...agentProps} />;
if (viewConfig.wrapper) {
content = viewConfig.wrapper(content);
}
} else {
content = (
<SelectResource onSelect={resource => onSelectResource(resource)} />
);
}
return (
<>
<FeatureBox>
{hasSelectedResource && (
<Box mt={2} mb={7}>
<Navigation
currentStep={currentStep}
views={indexedViews}
startWithIcon={{
title: agentProps.resourceSpec.name,
component: <DiscoverIcon name={agentProps.resourceSpec.icon} />,
}}
/>
</Box>
)}
<Box>{content}</Box>
</FeatureBox>
{hasSelectedResource && (
<Prompt
message={nextLocation => {
if (nextLocation.pathname === cfg.routes.discover) return true;
return 'Are you sure you want to exit the "Enroll New Resource” workflow? You’ll have to start from the beginning next time.';
}}
when={
viewConfig.shouldPrompt
? viewConfig.shouldPrompt(currentStep, agentProps.resourceSpec)
: true
}
/>
)}
</>
);
}
export function DiscoverComponent({ eViewConfigs = [] }: Props) {
return (
<DiscoverProvider eViewConfigs={eViewConfigs}>
<DiscoverContent />
</DiscoverProvider>
);
}
export function Discover() {
return <DiscoverComponent />;
}
type Props = {
eViewConfigs?: EViewConfigs;
};