-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
tutorial_directory.js
273 lines (238 loc) · 7.74 KB
/
tutorial_directory.js
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import { EuiFlexItem, EuiFlexGrid, EuiFlexGroup, EuiLink } from '@elastic/eui';
import { injectI18n, FormattedMessage } from '@kbn/i18n-react';
import { SampleDataTab } from '@kbn/home-sample-data-tab';
import { i18n } from '@kbn/i18n';
import { Synopsis } from './synopsis';
import { getServices } from '../kibana_services';
import { KibanaPageTemplate } from '@kbn/shared-ux-page-kibana-template';
import { getTutorials } from '../load_tutorials';
const SAMPLE_DATA_TAB_ID = 'sampleData';
const integrationsTitle = i18n.translate('home.breadcrumbs.integrationsAppTitle', {
defaultMessage: 'Integrations',
});
class TutorialDirectoryUi extends React.Component {
constructor(props) {
super(props);
const extraTabs = getServices().addDataService.getAddDataTabs();
this.tabs = [
{
id: SAMPLE_DATA_TAB_ID,
name: this.props.intl.formatMessage({
id: 'home.tutorial.tabs.sampleDataTitle',
defaultMessage: 'Sample data',
}),
content: <SampleDataTab />,
},
...extraTabs.map(({ id, name, getComponent }) => ({
id,
name,
content: getComponent(),
})),
];
let openTab = SAMPLE_DATA_TAB_ID;
if (
props.openTab &&
this.tabs.some((tab) => {
return tab.id === props.openTab;
})
) {
openTab = props.openTab;
}
this.state = {
selectedTabId: openTab,
tutorialCards: [],
};
}
componentWillUnmount() {
this._isMounted = false;
}
async componentDidMount() {
this._isMounted = true;
this.setBreadcrumbs();
const tutorialConfigs = await getTutorials();
if (!this._isMounted) {
return;
}
let tutorialCards = tutorialConfigs.map((tutorialConfig) => {
// add base path to SVG based icons
let icon = tutorialConfig.euiIconType;
if (icon && icon.includes('/')) {
icon = this.props.addBasePath(icon);
}
return {
id: tutorialConfig.id,
category: tutorialConfig.category,
icon: icon,
name: tutorialConfig.name,
description: tutorialConfig.shortDescription,
url: this.props.addBasePath(`#/tutorial/${tutorialConfig.id}`),
elasticCloud: tutorialConfig.elasticCloud,
// Beta label is skipped on the tutorial overview page for now. Too many beta labels.
//isBeta: tutorialConfig.isBeta,
};
});
// Add card for sample data that only gets show in "all" tab
tutorialCards.push({
id: 'sample_data',
name: this.props.intl.formatMessage({
id: 'home.tutorial.card.sampleDataTitle',
defaultMessage: 'Sample Data',
}),
description: this.props.intl.formatMessage({
id: 'home.tutorial.card.sampleDataDescription',
defaultMessage: 'Get started exploring Kibana with these "one click" data sets.',
}),
url: this.props.addBasePath('#/tutorial_directory/sampleData'),
elasticCloud: true,
onClick: this.onSelectedTabChanged.bind(null, SAMPLE_DATA_TAB_ID),
});
if (this.props.isCloudEnabled) {
tutorialCards = tutorialCards.filter((tutorial) => {
return _.has(tutorial, 'elasticCloud');
});
}
tutorialCards.sort((a, b) => {
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
});
this.setState({
// eslint-disable-line react/no-did-mount-set-state
tutorialCards: tutorialCards,
});
}
componentDidUpdate(prevProps, prevState) {
if (prevState.selectedTabId !== this.state.selectedTabId) {
this.setBreadcrumbs();
}
}
setBreadcrumbs = () => {
const tab = this.getSelectedTab();
const breadcrumbs = [
{
text: integrationsTitle,
href: this.props.addBasePath(`/app/integrations/browse`),
},
];
if (tab?.name) {
breadcrumbs.push({
text: tab.name,
});
}
getServices().chrome.setBreadcrumbs(breadcrumbs);
};
onSelectedTabChanged = (id) => {
this.setState({
selectedTabId: id,
});
};
getTabs = () => {
return this.tabs.map((tab) => ({
label: tab.name,
onClick: () => this.onSelectedTabChanged(tab.id),
isSelected: tab.id === this.state.selectedTabId,
'data-test-subj': `homeTab-${tab.id}`,
}));
};
getSelectedTab = () => {
return this.tabs.find(({ id }) => id === this.state.selectedTabId);
};
renderTabContent = () => {
const tab = this.getSelectedTab();
if (tab?.content) {
return tab.content;
}
return (
<EuiFlexGrid columns={4}>
{this.state.tutorialCards
.filter((tutorial) => {
return (
this.state.selectedTabId === SAMPLE_DATA_TAB_ID ||
this.state.selectedTabId === tutorial.category
);
})
.map((tutorial) => {
return (
<EuiFlexItem data-test-subj={`homeTab-${tutorial.name}`} key={tutorial.name}>
<Synopsis
id={tutorial.id}
iconType={tutorial.icon}
description={tutorial.description}
title={tutorial.name}
wrapInPanel
url={tutorial.url}
onClick={tutorial.onClick}
isBeta={tutorial.isBeta}
/>
</EuiFlexItem>
);
})}
</EuiFlexGrid>
);
};
renderHeaderLinks = () => {
const headerLinks = getServices().tutorialService.getDirectoryHeaderLinks();
return headerLinks.length ? (
<EuiFlexGroup gutterSize="m" alignItems="center">
{headerLinks.map((HeaderLink, index) => (
<EuiFlexItem key={index}>
<HeaderLink />
</EuiFlexItem>
))}
</EuiFlexGroup>
) : null;
};
render() {
const headerLinks = this.renderHeaderLinks();
const tabs = this.getTabs();
return (
<KibanaPageTemplate
restrictWidth={1200}
template="empty"
pageHeader={{
pageTitle: (
<FormattedMessage
id="home.tutorial.addDataToKibanaTitle"
defaultMessage="More ways to add data"
/>
),
description: (
<FormattedMessage
id="home.tutorial.addDataToKibanaDescription"
defaultMessage="In addition to adding {integrationsLink}, you can try our sample data or upload your own data."
values={{
integrationsLink: (
<EuiLink href={this.props.addBasePath(`/app/integrations/browse`)}>
<FormattedMessage
id="home.tutorial.addDataToKibanaDescription.integrations"
defaultMessage="integrations"
/>
</EuiLink>
),
}}
/>
),
tabs,
rightSideItems: headerLinks ? [headerLinks] : [],
}}
>
<KibanaPageTemplate.Section>{this.renderTabContent()}</KibanaPageTemplate.Section>
</KibanaPageTemplate>
);
}
}
TutorialDirectoryUi.propTypes = {
addBasePath: PropTypes.func.isRequired,
openTab: PropTypes.string,
isCloudEnabled: PropTypes.bool.isRequired,
};
export const TutorialDirectory = injectI18n(TutorialDirectoryUi);