-
Notifications
You must be signed in to change notification settings - Fork 4
/
Stats.tsx
45 lines (40 loc) · 1.58 KB
/
Stats.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
import * as React from "react";
import { useGetStatsQuery } from "../features/stats/statsSlice";
import LoadingIndicator from "@thepalaceproject/web-opds-client/lib/components/LoadingIndicator";
import ErrorMessage from "./ErrorMessage";
import LibraryStats from "./LibraryStats";
import { FetchErrorData } from "@thepalaceproject/web-opds-client/lib/interfaces";
export interface StatsProps {
library?: string;
}
/**
* Displays statistics about patrons, licenses, and collections from the server.
* If a library prop is provided, then statistics for that
* library will be displayed.
* Otherwise, statistics for all authorized libraries will be displayed.
* @param {StatsProps} props
* @param {string} props.library - key (short name) of a library.
* */
export const Stats = (props: StatsProps) => {
const { library: targetLibraryKey } = props;
const { data: statisticsData, error, isLoading } = useGetStatsQuery();
// TODO: This is to overcome a type inference problem with a RTKQ hook.
const fetchError = error as FetchErrorData;
const summaryStatistics = statisticsData?.summaryStatistics;
const targetLibraryData = statisticsData?.libraries?.find(
(library) => library.key === targetLibraryKey
);
return (
<>
{targetLibraryData && (
<LibraryStats library={targetLibraryKey} stats={targetLibraryData} />
)}
{!targetLibraryData && summaryStatistics && (
<LibraryStats stats={summaryStatistics} />
)}
{error && <ErrorMessage error={fetchError} />}
{isLoading && <LoadingIndicator />}
</>
);
};
export default Stats;