Replace fields in Nextcloud widget with file count and shared item count (#1455)
* New file and share count fields for Nextcloud * Support "deprecated fields" for nextcloud widget * Move to explicit checks * Way more explicit render rules --------- Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com>
This commit is contained in:
parent
27837c6db8
commit
14a6ae4523
2 changed files with 33 additions and 7 deletions
|
@ -549,7 +549,9 @@
|
|||
"cpuload": "Cpu Load",
|
||||
"memoryusage": "Memory Usage",
|
||||
"freespace": "Free Space",
|
||||
"activeusers": "Active Users"
|
||||
"activeusers": "Active Users",
|
||||
"numfiles": "Files",
|
||||
"numshares": "Shared Items"
|
||||
},
|
||||
"kopia": {
|
||||
"status": "Status",
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
import { useMemo } from "react";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
|
@ -10,6 +11,25 @@ export default function Component({ service }) {
|
|||
const { widget } = service;
|
||||
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) {
|
||||
return <Container service={service} error={nextcloudError} />;
|
||||
}
|
||||
|
@ -17,23 +37,27 @@ export default function Component({ service }) {
|
|||
if (!nextcloudData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="nextcloud.cpuload" />
|
||||
<Block label="nextcloud.memoryusage" />
|
||||
{showCpuLoad && <Block label="nextcloud.cpuload" />}
|
||||
{showMemoryUsage && <Block label="nextcloud.memoryusage" />}
|
||||
<Block label="nextcloud.freespace" />
|
||||
<Block label="nextcloud.activeusers" />
|
||||
<Block label="nextcloud.numfiles" />
|
||||
<Block label="nextcloud.numshares" />
|
||||
</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));
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="nextcloud.cpuload" value={t("common.percent", { value: nextcloudInfo.system.cpuload[0] })} />
|
||||
<Block label="nextcloud.memoryusage" value={t("common.percent", { value:memoryUsage })} />
|
||||
{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 })} />}
|
||||
<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.last5minutes })} />
|
||||
<Block label="nextcloud.activeusers" value={t("common.number", { value: activeUsers.last24hours })} />
|
||||
<Block label="nextcloud.numfiles" value={t("common.number", { value: nextcloudInfo.storage.num_files })} />
|
||||
<Block label="nextcloud.numshares" value={t("common.number", { value: nextcloudInfo.shares.num_shares })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue