@ -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" ) ;
/ / S u p p o r t f o r d e p r e c a t e d f i e l d s ( c p u l o a d , m e m o r y u s a g e )
const [ showCpuLoad , showMemoryUsage ] = useMemo ( ( ) => {
/ / D e f a u l t v a l u e s i f f i e l d s i s n o t s e t
if ( ! widget . fields ) return [ false , false ] ;
/ / A l l o w s f o r b a c k w a r d s c o m p a t i b i l i t y w i t h e x i s t i n g v a l u e s o f f i e l d s
if ( widget . fields . length <= 4 ) return [ true , true ] ;
/ / I f a l l f i e l d s a r e e n a b l e d , d r o p c p u l o a d a n d m e m o r y u s a g e
if ( widget . fields . length === 6 ) return [ false , false ] ;
const hasCpuLoad = widget . fields ? . includes ( 'cpuload' ) ;
const hasMemoryUsage = widget . fields ? . includes ( 'memoryusage' ) ;
/ / I f ( f o r s o m e r e a s o n ) 5 f i e l d s a r e s e t , d r o p m e m o r y u s a g e
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 >
) ;
}