Skip to content

Commit da52434

Browse files
committed
Simplify CurrentUptime
1 parent 6f9575a commit da52434

2 files changed

Lines changed: 25 additions & 34 deletions

File tree

Plan/react/dashboard/src/components/datapoint/CurrentUptime.tsx

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import React from 'react';
22
import {useTranslation} from "react-i18next";
33
import {faPowerOff} from "@fortawesome/free-solid-svg-icons";
4-
import {faQuestionCircle} from "@fortawesome/free-regular-svg-icons";
5-
import Datapoint, {Datapoint as DatapointComponent} from "./Datapoint";
6-
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
7-
import FormattedTime from "../text/FormattedTime.jsx";
8-
import {isNumber} from "../../util/isNumber.js";
94
import {GenericFilter} from "../../dataHooks/model/GenericFilter";
10-
import {useAuth} from "../../hooks/authenticationHook";
11-
import {calculatePermission, useDatapointQuery} from "./QueryDatapoint";
5+
import {QueryDatapoint} from "./QueryDatapoint";
126
import {DatapointType} from "../../dataHooks/model/datapoint/Datapoint";
13-
import {DatapointLoader} from "../navigation/Loader";
147

158
type Props = {
169
filter: GenericFilter;
@@ -19,31 +12,10 @@ type Props = {
1912
const CurrentUptime = ({filter}: Props) => {
2013
const {t} = useTranslation();
2114

22-
const {hasPermission} = useAuth();
23-
const isAllowed = hasPermission(calculatePermission(DatapointType.CURRENT_UPTIME, undefined, filter))
24-
const {data, isFetching, error} = useDatapointQuery(isAllowed, DatapointType.CURRENT_UPTIME, filter);
25-
26-
if (!isAllowed) return null;
27-
28-
const infoBubble = error?.status === 404
29-
? <span title={t('html.description.noUptimeCalculation')}><FontAwesomeIcon icon={faQuestionCircle}/></span>
30-
: undefined;
31-
32-
if (error && error.status !== 404) {
33-
console.error(error);
34-
return <DatapointComponent
35-
icon={faPowerOff} color={'uptime'}
36-
name={t('html.label.currentUptime')}
37-
value={error.message}
38-
/>
39-
}
40-
4115
return (
42-
<Datapoint icon={faPowerOff} color={'uptime'}
43-
name={t('html.label.currentUptime')}
44-
value={(data || error?.status === 404) && !isFetching ? (isNumber(data?.value) &&
45-
<FormattedTime timeMs={data?.value}/> || 'plugin.generic.unavailable') : <DatapointLoader/>}
46-
valueLabel={infoBubble}/>
16+
<QueryDatapoint dataType={DatapointType.CURRENT_UPTIME} icon={faPowerOff} color={'uptime'}
17+
name={t('html.label.currentUptime')} filter={filter}
18+
fallbackUnavailableExplanation={t('html.description.noUptimeCalculation')}/>
4719
)
4820
};
4921

Plan/react/dashboard/src/components/datapoint/QueryDatapoint.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {GenericFilter} from "../../dataHooks/model/GenericFilter";
1010
import {useNavigation} from "../../hooks/navigationHook";
1111
import {useQuery} from "@tanstack/react-query";
1212
import {queryRetry} from "../../dataHooks/queryRetry";
13-
import {useEffect} from "react";
13+
import React, {useEffect} from "react";
1414
import {Datapoint as DatapointComponent, DatapointProps} from "./Datapoint";
1515
import FormattedTime from "../text/FormattedTime";
1616
import {DatapointLoader} from "../navigation/Loader";
@@ -22,11 +22,14 @@ import {useTranslation} from "react-i18next";
2222
import {DateObj, isDateObj} from "../../dataHooks/model/datapoint/DateObj";
2323
import FormattedDate from "../text/FormattedDate";
2424
import BigTrend from "../trend/BigTrend";
25+
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
26+
import {faQuestionCircle} from "@fortawesome/free-regular-svg-icons";
2527

2628
type Props<K extends DatapointType> = {
2729
dataType: K;
2830
filter?: GenericFilter;
2931
permission?: string;
32+
fallbackUnavailableExplanation?: string
3033
} & Omit<DatapointProps, 'value'>;
3134

3235
type ValueProps<K extends DatapointType> = Omit<Props<K>, 'color' | 'name' | 'icon'>;
@@ -67,6 +70,8 @@ function Format<K extends DatapointType>({value, formatType}: FormatProps<K>) {
6770
return <FormattedTime timeMs={value}/>
6871
case "PERCENTAGE":
6972
return formatDecimals(value as number * 100) + '%'
73+
case "DECIMAL":
74+
return formatDecimals(value as number)
7075
case "SPECIAL":
7176
if (isOutOf(value)) {
7277
return <FormattedOutOf outOf={value as OutOf}/>
@@ -94,13 +99,27 @@ export function calculatePermission(dataType: DatapointType, permission?: string
9499
return 'data.network.' + asPermission;
95100
}
96101

97-
export function QueryDatapoint<K extends DatapointType>({permission, dataType, filter, ...props}: Props<K>) {
102+
export function QueryDatapoint<K extends DatapointType>({
103+
permission,
104+
dataType,
105+
filter,
106+
fallbackUnavailableExplanation,
107+
...props
108+
}: Props<K>) {
98109
const {hasPermission} = useAuth();
99110
const allowed = hasPermission(calculatePermission(dataType, permission, filter));
100111
const {data, isFetching, error} = useDatapointQuery(allowed, dataType, filter);
101112

102113
if (!allowed) return null;
103114
if (error) {
115+
if (error.status === 404) {
116+
return <DatapointComponent
117+
{...props}
118+
value={'plugin.generic.unavailable'}
119+
valueLabel={<span
120+
title={fallbackUnavailableExplanation}><FontAwesomeIcon icon={faQuestionCircle}/></span>}
121+
/>
122+
}
104123
console.error(error);
105124
return <DatapointComponent
106125
{...props}

0 commit comments

Comments
 (0)