-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPluginPage.tsx
74 lines (68 loc) · 2.42 KB
/
PluginPage.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
import { Alert, AlertActionLink, AlertVariant, PageSection } from '@patternfly/react-core';
import React, { useContext } from 'react';
import { useHistory, useParams } from 'react-router-dom';
import { IPluginsContext, PluginsContext } from '../../context/PluginsContext';
// IPluginParams are the parameters for the PluginPage component. For the PluginPage we only require the name of the
// plugin.
interface IPluginParams {
name: string;
}
// PluginPage is the component to render the page component for a plugin. The plugin and component is selected from the
// PluginsContext by the name of the plugin. When the plugin name wasn't found (pluginDetails is undefined) or the
// Component is undefined, because the plugin doesn't support pages we render an Alert message.
export const PluginPage: React.FunctionComponent = () => {
const history = useHistory();
const params = useParams<IPluginParams>();
const pluginsContext = useContext<IPluginsContext>(PluginsContext);
const pluginDetails = pluginsContext.getPluginDetails(params.name);
const Component =
pluginDetails && pluginsContext.components.hasOwnProperty(pluginDetails.type)
? pluginsContext.components[pluginDetails.type].page
: undefined;
if (!pluginDetails) {
return (
<PageSection>
<Alert
variant={AlertVariant.danger}
title="Plugin was not found"
actionLinks={
<React.Fragment>
<AlertActionLink onClick={(): void => history.push('/')}>Home</AlertActionLink>
</React.Fragment>
}
>
<p>
The plugin <b>{params.name}</b> was not found.
</p>
</Alert>
</PageSection>
);
}
if (!Component) {
return (
<PageSection>
<Alert
variant={AlertVariant.info}
title="The Plugin doesn't have a page component"
actionLinks={
<React.Fragment>
<AlertActionLink onClick={(): void => history.push('/')}>Home</AlertActionLink>
</React.Fragment>
}
>
<p>
The plugin <b>{pluginDetails.displayName}</b> of tpye <b>{pluginDetails.type}</b> does not implements a page
component.
</p>
</Alert>
</PageSection>
);
}
return (
<Component
name={pluginDetails.name}
displayName={pluginDetails.displayName}
description={pluginDetails.description}
/>
);
};