Way more explicit render rules

This commit is contained in:
Georges-Antoine Assi 2023-05-03 22:19:14 -04:00
parent d680a8656d
commit 358939df3a
No known key found for this signature in database
GPG Key ID: E01F01B06E816D51

View File

@ -1,4 +1,5 @@
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { useMemo } from "react";
import Container from "components/services/widget/container"; import Container from "components/services/widget/container";
import Block from "components/services/widget/block"; import Block from "components/services/widget/block";
@ -10,15 +11,28 @@ export default function Component({ service }) {
const { widget } = service; const { widget } = service;
const { data: nextcloudData, error: nextcloudError } = useWidgetAPI(widget, "serverinfo"); const { data: nextcloudData, error: nextcloudError } = useWidgetAPI(widget, "serverinfo");
// Support for deprecated fields (cpuload, memoryusage)
const [showCpuLoad, showMemoryUsage] = useMemo(() => {
// Default values if fields is not set
if (!widget.fields) return [false, false];
// Allows for backwards compatibility with existing values of fields
if (widget.fields.length <= 4) return [true, true];
// If all fields are enabled, drop cpuload and memoryusage
if (widget.fields.length === 6) return [false, false];
const hasCpuLoad = widget.fields?.includes('cpuload');
const hasMemoryUsage = widget.fields?.includes('memoryusage');
// If (for some reason) 5 fields are set, drop memoryusage
if (hasCpuLoad && hasMemoryUsage) return [true, false];
return [!hasCpuLoad, !hasMemoryUsage]
}, [widget.fields]);
if (nextcloudError) { if (nextcloudError) {
return <Container service={service} error={nextcloudError} />; return <Container service={service} error={nextcloudError} />;
} }
// cpuload & memoryusage are deprecated, so limit to 4 fields
const showCpuLoad = widget.fields?.includes('cpuload');
const showMemoryUsage = widget.fields?.includes('memoryusage');
const showNumFiles = !showCpuLoad || !showMemoryUsage; // at least 1 deprecated field is hidden
const showNumShares = !showCpuLoad && !showMemoryUsage; // both deprecated fields are hidden
if (!nextcloudData) { if (!nextcloudData) {
return ( return (
@ -27,13 +41,13 @@ export default function Component({ service }) {
{showMemoryUsage && <Block label="nextcloud.memoryusage" />} {showMemoryUsage && <Block label="nextcloud.memoryusage" />}
<Block label="nextcloud.freespace" /> <Block label="nextcloud.freespace" />
<Block label="nextcloud.activeusers" /> <Block label="nextcloud.activeusers" />
{showNumFiles && <Block label="nextcloud.numfiles" />} <Block label="nextcloud.numfiles" />
{showNumShares && <Block label="nextcloud.numshares" />} <Block label="nextcloud.numshares" />
</Container> </Container>
); );
} }
const nextcloudInfo = nextcloudData.ocs.data.nextcloud; const { nextcloud: nextcloudInfo, activeUsers } = nextcloudData.ocs.data;
const memoryUsage = 100 * ((parseFloat(nextcloudInfo.system.mem_total) - parseFloat(nextcloudInfo.system.mem_free)) / parseFloat(nextcloudInfo.system.mem_total)); const memoryUsage = 100 * ((parseFloat(nextcloudInfo.system.mem_total) - parseFloat(nextcloudInfo.system.mem_free)) / parseFloat(nextcloudInfo.system.mem_total));
return ( return (
@ -41,9 +55,9 @@ export default function Component({ service }) {
{showCpuLoad && <Block label="nextcloud.cpuload" value={t("common.percent", { value: nextcloudInfo.system.cpuload[0] })} />} {showCpuLoad && <Block label="nextcloud.cpuload" value={t("common.percent", { value: nextcloudInfo.system.cpuload[0] })} />}
{showMemoryUsage && <Block label="nextcloud.memoryusage" value={t("common.percent", { value:memoryUsage })} />} {showMemoryUsage && <Block label="nextcloud.memoryusage" value={t("common.percent", { value:memoryUsage })} />}
<Block label="nextcloud.freespace" value={t("common.bbytes", { value: nextcloudInfo.system.freespace, maximumFractionDigits: 1 })} /> <Block label="nextcloud.freespace" value={t("common.bbytes", { value: nextcloudInfo.system.freespace, maximumFractionDigits: 1 })} />
<Block label="nextcloud.activeusers" value={t("common.number", { value: nextcloudData.ocs.data.activeUsers.last24hours })} /> <Block label="nextcloud.activeusers" value={t("common.number", { value: activeUsers.last24hours })} />
{showNumFiles && <Block label="nextcloud.numfiles" value={t("common.number", { value: nextcloudInfo.storage.num_files })} />} <Block label="nextcloud.numfiles" value={t("common.number", { value: nextcloudInfo.storage.num_files })} />
{showNumShares && <Block label="nextcloud.numshares" value={t("common.number", { value: nextcloudInfo.shares.num_shares })} />} <Block label="nextcloud.numshares" value={t("common.number", { value: nextcloudInfo.shares.num_shares })} />
</Container> </Container>
); );
} }