mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-03 05:53:40 +01:00
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
@ -549,7 +549,9 @@
|
|||||||
"cpuload": "Cpu Load",
|
"cpuload": "Cpu Load",
|
||||||
"memoryusage": "Memory Usage",
|
"memoryusage": "Memory Usage",
|
||||||
"freespace": "Free Space",
|
"freespace": "Free Space",
|
||||||
"activeusers": "Active Users"
|
"activeusers": "Active Users",
|
||||||
|
"numfiles": "Files",
|
||||||
|
"numshares": "Shared Items"
|
||||||
},
|
},
|
||||||
"kopia": {
|
"kopia": {
|
||||||
"status": "Status",
|
"status": "Status",
|
||||||
|
@ -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,6 +11,25 @@ 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} />;
|
||||||
}
|
}
|
||||||
@ -17,23 +37,27 @@ export default function Component({ service }) {
|
|||||||
if (!nextcloudData) {
|
if (!nextcloudData) {
|
||||||
return (
|
return (
|
||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<Block label="nextcloud.cpuload" />
|
{showCpuLoad && <Block label="nextcloud.cpuload" />}
|
||||||
<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" />
|
||||||
|
<Block label="nextcloud.numfiles" />
|
||||||
|
<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 (
|
||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<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] })} />}
|
||||||
<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.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>
|
</Container>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user