-
Notifications
You must be signed in to change notification settings - Fork 3
/
ResultsCard.tsx
142 lines (129 loc) · 3.47 KB
/
ResultsCard.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
import React, { ReactNode } from "react";
import { useTranslation } from "react-i18next";
import Card from "./Card.js";
import { useFunction } from "../hooks/useFunction.js";
import { styled } from "styled-components";
import Skeleton from "./Skeleton.js";
import { ProgressBar, ProgressBarWrapper } from "./ProgressBar.js";
import { ReportError } from "./ReportError.js";
import { GeoprocessingRequestParams } from "../types/service.js";
export interface ResultsCardProps<T> {
functionName: string;
children: (results: T) => ReactNode;
skeleton?: ReactNode;
title?: string | ReactNode;
titleStyle?: React.CSSProperties;
style?: object;
/** Assumes caller will provide card in children to use results (e.g. ToolbarCard with DataDownload). Shows a simple card until loading complete */
useChildCard?: boolean;
/** Additional runtime parameters from report client for geoprocessing function. */
extraParams?: GeoprocessingRequestParams;
}
const DefaultSkeleton = () => (
<div>
<Skeleton />
<Skeleton />
<Skeleton />
<Skeleton style={{ width: "25%" }} />
</div>
);
// styled-components are needed here to use the ::before pseudo selector
const ErrorIndicator = styled.div`
display: inline-block;
font-weight: bold;
font-size: 18px;
line-height: 1em;
background-color: #ea4848;
width: 20px;
height: 20px;
border-radius: 20px;
color: white;
text-align: center;
margin-right: 8px;
::before {
position: relative;
bottom: -1px;
content: "!";
}
`;
export const EstimateLabel = styled.div`
height: 20px;
margin-top: 5px;
padding-bottom: 15px;
margin-left: auto;
margin-right: auto;
font-style: italic;
width: 100%;
text-align: center;
display: none;
`;
export function ResultsCard<T>({
functionName,
skeleton,
children,
title,
titleStyle = {},
style = {},
useChildCard = false,
extraParams = {},
}: ResultsCardProps<T>) {
if (!functionName) {
throw new Error("No function specified for ResultsCard");
}
const { t } = useTranslation();
const resultsCardNoResultMsg = t(
"ResultsCard - no result message",
"Report run completed, but no results returned",
);
const cardProps = {
style,
title,
titleStyle,
};
const { task, loading, error } = useFunction(functionName, extraParams);
let theError = error;
let taskEstimate = 5;
if (task && task.estimate) {
taskEstimate = Math.round(task.estimate / 1000);
}
if (task && !task.data && !loading) {
if (task.error) {
theError = task.error;
} else {
theError = resultsCardNoResultMsg;
}
}
let contents: JSX.Element;
if (theError) {
contents = (
<Card {...cardProps}>
<div role="alert">
<ErrorIndicator />
{theError}
</div>
</Card>
);
} else if (loading) {
contents = (
<Card {...cardProps}>
{skeleton || <DefaultSkeleton />}
<ProgressBarWrapper>
<ProgressBar $duration={taskEstimate} />
</ProgressBarWrapper>
</Card>
);
} else if (task && task.data) {
const renderedChildren = children(task.data as T);
if (useChildCard) {
// Assume caller will provide card in children
contents = <>{renderedChildren}</>;
} else {
// Default card
contents = <Card {...cardProps}>{renderedChildren}</Card>;
}
} else {
throw new Error("ResultsCard error"); // trigger ReportError boundary
}
return <ReportError>{contents}</ReportError>;
}
export default ResultsCard;