-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDevfilePageProjects.tsx
217 lines (202 loc) · 6.9 KB
/
DevfilePageProjects.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import styles from './DevfilePageProjects.module.css';
import type { Project, DefaultProps, Devfile } from 'custom-types';
import { DevfilePageProjectDisplay } from '@src/components';
import { getUserRegion } from '@src/util/client';
import {
Alert,
AlertActionLink,
AlertActionCloseButton,
Card,
CardHeader,
CardTitle,
CardBody,
CardExpandableContent,
Button,
Text,
TextContent,
TextVariants,
} from '@patternfly/react-core';
import { useState } from 'react';
import { useRouter } from 'next/router';
import getConfig from 'next/config';
/**
* type for errorAlert variable in {@link DevPageProjects}
* specifically for catching errors in download
*
* @param name - name of alert
* @param error - error that occurred
* @param message - message to display
* @param alertType - type of alert
*/
export interface ErrorAlert {
name: string;
error: string;
message: string;
alertType: 'warning' | 'danger';
}
/**
* props for {@link DevPageProjects}
*
* @param starterProjects - list of starter projects
*/
export interface DevfilePageProjectsProps extends DefaultProps {
devfile: Devfile;
starterProjects: Project[];
}
/**
* component for expandable starter project select with functionality to download
*
* @remarks
* when hovering, should display hovered project description
* when not hovering, should display selected project description
* component only for stacks, won't be present for samples
*
* @param starterProjects - starter projects associated with respective devfile
*/
export const DevfilePageProjects: React.FC<DevfilePageProjectsProps> = ({
devfile,
starterProjects,
analytics,
}: DevfilePageProjectsProps) => {
const [expanded, setExpanded] = useState<boolean>(false);
const [downloading, setDownloading] = useState<boolean>(false);
const [selectedProject, setSelectedProject] = useState<Project>(starterProjects[0]);
const [currentlyHoveredProject, setCurrentlyHoveredProject] = useState<Project | null>(null); // null when not hovering over project list
const [errorAlert, setErrorAlert] = useState<null | ErrorAlert>(null);
const router = useRouter();
async function downloadStarterProject(devfile: Devfile, project: Project): Promise<void> {
setDownloading(true);
try {
window.open(`${devfile.registryLink}/starter-projects/${project.name}`);
if (analytics) {
const region = getUserRegion(router.locale);
const { publicRuntimeConfig } = getConfig();
analytics.track(
'Starter Project Downloaded',
{
client: publicRuntimeConfig.segmentClientId,
devfile: devfile.name,
starterProject: project.name,
},
{
context: { ip: '0.0.0.0', location: { country: region } },
userId: analytics.user().anonymousId(),
},
);
}
} catch (error) {
setErrorAlert({
name: 'Download Error',
error: (error as Error).toString(),
message:
'Internal error has occurred during download. Please try again or report as issue. \n',
alertType: 'danger',
});
if (analytics) {
const region = getUserRegion(router.locale);
const { publicRuntimeConfig } = getConfig();
analytics.track(
'Starter Project Download Failed',
{
client: publicRuntimeConfig.segmentClientId,
devfile: devfile.name,
starterProject: project.name,
},
{
context: { ip: '0.0.0.0', location: { country: region } },
userId: analytics.user().anonymousId(),
},
);
}
}
setDownloading(false);
}
return (
<Card data-testid="dev-page-projects" isExpanded={expanded} className={styles.card}>
<CardHeader
data-testid="toggle-button"
onClick={(): void => setExpanded(!expanded)}
isToggleRightAligned
className={styles.cardHeader}
toggleButtonProps={{
id: 'toggle-button',
'aria-label': 'Details',
'aria-labelledby': 'titleId toggle-button',
'aria-expanded': expanded,
}}
>
<CardTitle>Starter Projects</CardTitle>
</CardHeader>
<CardExpandableContent>
<CardBody>
<div className={styles.cardBody}>
<div
data-testid="projects-selector"
className={styles.select}
onMouseLeave={(): void => setCurrentlyHoveredProject(null)}
>
{starterProjects.map((project) => (
<div
key={project.name}
data-testid={`projects-selector-item-${project.name}`}
onMouseDown={(): void => setSelectedProject(project)}
onMouseEnter={(): void => setCurrentlyHoveredProject(project)}
className={
selectedProject.name === project.name ? styles.selected : styles.project
}
>
{project.name}
</div>
))}
</div>
<div className={styles.display}>
<DevfilePageProjectDisplay project={currentlyHoveredProject || selectedProject} />
<Button
data-testid="download-button"
className={styles.button}
isLoading={downloading}
onClick={(): Promise<void> => downloadStarterProject(devfile, selectedProject)}
variant="tertiary"
>
Download
</Button>
</div>
</div>
{errorAlert && (
<Alert
variant={errorAlert.alertType}
title={errorAlert.name}
actionClose={<AlertActionCloseButton onClose={(): void => setErrorAlert(null)} />}
actionLinks={
<>
<AlertActionLink
onClick={(): Window | null =>
window.open(
`https://github.com/devfile/api/issues/new?assignees=&labels=&template=bug_report.md&title=Registry+Viewer+${errorAlert.name.replace(
' ',
'+',
)}`,
)
}
>
Report Issue to Github
</AlertActionLink>
</>
}
>
<TextContent className={styles.displayedProject}>
<Text data-testid="alert-message">{errorAlert.message}</Text>
{errorAlert.alertType !== 'warning' && (
<Text component={TextVariants.blockquote}>
<code>{errorAlert.error}</code>
</Text>
)}
</TextContent>
</Alert>
)}
</CardBody>
</CardExpandableContent>
</Card>
);
};
DevfilePageProjects.displayName = 'DevfilePageProjects';