-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathTerritoryMetrics.tsx
More file actions
88 lines (83 loc) · 2.33 KB
/
TerritoryMetrics.tsx
File metadata and controls
88 lines (83 loc) · 2.33 KB
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
import { useParams } from "react-router-dom";
import { Row, Col, Spinner, CodeSnippet } from "@canonical/react-components";
import { useEffect } from "react";
import { renderTerritoriesMetrics } from "./metrics/metrics";
import useCountryMetrics from "../../hooks/useCountryMetrics";
export const TerritoryMetrics = ({
isEmpty,
onDataLoad,
}: {
isEmpty: boolean;
onDataLoad: (dataLength: number | undefined) => void;
}): React.JSX.Element => {
const { snapId } = useParams();
const {
status,
data: countryInfo,
isFetching,
}: {
status: string;
data:
| {
active_devices: {
[key: string]: {
code: string;
color_rgb: string;
name: string;
number_of_users: number;
percentage_of_users: number;
};
};
territories_total: number;
}
| undefined;
isFetching: boolean;
} = useCountryMetrics(snapId);
useEffect(() => {
if (countryInfo) {
if (countryInfo.active_devices) {
renderTerritoriesMetrics({
selector: "#territories",
metrics: countryInfo.active_devices,
});
}
// @ts-expect-error Type clash
onDataLoad(countryInfo.active_devices?.length);
}
}, [countryInfo]);
return (
<section className={`p-strip is-shallow ${isEmpty ? "is-empty" : ""}`}>
<Row>
<Col size={12} key="activeServices">
<h1 className="u-float-left p-heading--4">Territories</h1>
<div className="p-heading--4 u-float-right u-no-margin--top">
<strong>{countryInfo?.territories_total}</strong>
</div>
</Col>
<Col size={12} key="territoriesSeparator">
<hr />
</Col>
{isFetching ? (
<Spinner />
) : (
<>
{isEmpty && <div>No data found.</div>}
{status === "error" && (
<CodeSnippet
blocks={[
{
code: <div>An error occurred. Please try again.</div>,
wrapLines: true,
},
]}
/>
)}
</>
)}
<Col size={12} key="territories">
<div id="territories" className="snapcraft-territories"></div>
</Col>
</Row>
</section>
);
};