-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathEmbedPage.tsx
82 lines (73 loc) · 2.81 KB
/
EmbedPage.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
import React, { useRef } from 'react';
import { IfFulfilled, IfPending, IfRejected } from 'react-async';
import { useParams } from 'react-router-dom';
import Loader from '../components/Loader';
import { useQueryWithAsyncEncoder } from '../helpers/use-query';
import { allWidgets } from '../widgets';
import { DecodedMergedWidgetProps } from '../widgets/Widget';
import styled from 'styled-components';
import { Alert, AlertVariant } from '../helpers/ui';
import { ExportManager, ExportManagerContext } from '../components/CombinedExport/ExportManager';
const host = process.env.REACT_APP_WEBSITE_HOST;
const Wrapper = styled.div`
height: 100vh;
display: flex;
flex-direction: column;
border: 1px solid darkgray;
padding: 15px;
border-radius: 10px;
`;
const Header = styled.div`
margin-bottom: 5px;
font-size: small;
text-align: right;
`;
export function EmbedPage() {
const widgetUrlName = (useParams() as any).widget as string; // TODO(voinovp) use add types for react-router params
const widget = allWidgets.find(w => w.urlName === widgetUrlName);
const asyncWidgetProps = useQueryWithAsyncEncoder<DecodedMergedWidgetProps>(widget?.mergedPropsEncoder);
// This export manager is effectively never used, since there is
// no "Export" button. It is here to prevent warnings about a
// missing "ExportManagerContext.Provider", since there may
// be things which use ExportManager.register() inside the widget.
const exportManagerRef = useRef(new ExportManager(false));
if (!widget) {
// TODO Redirect to a 404 page
return <Alert variant={AlertVariant.DANGER}>Widget is unspecified or unsupported</Alert>;
}
const renderHeader = (href: string | undefined) => (
<>
Find more information on{' '}
<a rel='noreferrer' target='_blank' href={href}>
<span style={{ color: 'orange', fontWeight: 'bold' }}>CoV-Spectrum</span>
</a>
.
</>
);
return (
<Wrapper>
<Header>
<IfPending state={asyncWidgetProps}>{renderHeader(host)}</IfPending>
<IfRejected state={asyncWidgetProps}>{renderHeader(host)}</IfRejected>
<IfFulfilled state={asyncWidgetProps}>
{({ shared }) => renderHeader(shared.originalPageUrl || host)}
</IfFulfilled>
</Header>
<div style={{ flexGrow: 1 }}>
<IfPending state={asyncWidgetProps}>
<Loader />
</IfPending>
<IfRejected state={asyncWidgetProps}>
<Alert variant={AlertVariant.DANGER}>Failed to load widget</Alert>
</IfRejected>
<IfFulfilled state={asyncWidgetProps}>
{({ specific }) => (
<ExportManagerContext.Provider value={exportManagerRef.current}>
<widget.DefaultComponent {...specific} />
</ExportManagerContext.Provider>
)}
</IfFulfilled>
</div>
</Wrapper>
);
}