diff --git a/tdrive/frontend/src/app/features/users/hooks/use-user-quota.ts b/tdrive/frontend/src/app/features/users/hooks/use-user-quota.ts index 2f71d7351..bfde33bbe 100644 --- a/tdrive/frontend/src/app/features/users/hooks/use-user-quota.ts +++ b/tdrive/frontend/src/app/features/users/hooks/use-user-quota.ts @@ -1,7 +1,17 @@ -import { useCallback, useEffect, useState } from "react"; +import { useCallback, useEffect } from "react"; import { UserQuota } from '@features/users/types/user-quota'; import UserAPIClient from '@features/users/api/user-api-client'; import { useCurrentUser } from "features/users/hooks/use-current-user"; +import { atom, useRecoilState } from "recoil"; + +export const QuotaState = atom({ + key: 'QuotaState', + default: { + used: 0, + remaining: 1, + total: 1 + }, +}); export const useUserQuota = () => { const nullQuota = { @@ -10,8 +20,7 @@ export const useUserQuota = () => { total: 1 } const {user } = useCurrentUser(); - const [quota, setQuota] = useState(nullQuota); - const [used, setUsed] = useState(0); + const [quota, setQuota] = useRecoilState(QuotaState); const getQuota = useCallback(async () => { let data: UserQuota = nullQuota; @@ -21,7 +30,6 @@ export const useUserQuota = () => { data = nullQuota; } setQuota(data) - setUsed(Math.random()); }, []); useEffect(() => { @@ -29,5 +37,5 @@ export const useUserQuota = () => { }, []); - return { quota, used, getQuota }; + return { quota, getQuota }; }; diff --git a/tdrive/frontend/src/app/views/client/common/disk-usage.tsx b/tdrive/frontend/src/app/views/client/common/disk-usage.tsx index eabf4a18b..a1d90a5da 100644 --- a/tdrive/frontend/src/app/views/client/common/disk-usage.tsx +++ b/tdrive/frontend/src/app/views/client/common/disk-usage.tsx @@ -1,34 +1,38 @@ -import { Base, Title } from "@atoms/text"; +import { Base } from "@atoms/text"; import { formatBytesToInt } from "@features/drive/utils"; import Languages from "features/global/services/languages-service"; import { useUserQuota } from "@features/users/hooks/use-user-quota"; import RouterServices from "features/router/services/router-service"; import { useEffect, useState } from "react"; import FeatureTogglesService, { FeatureNames } from "@features/global/services/feature-toggles-service"; +import { useDriveItem } from "features/drive/hooks/use-drive-item"; -export default () => { +const DiskUsage = () => { const { viewId } = RouterServices.getStateFromRoute(); console.log("VIEW-iD::" + viewId); - const { quota, used } = useUserQuota() - // const [used, setUsed] = useState( - // Math.round(quota.used / quota.total * 100) - // ) - // - // useEffect(() => { - // console.log("SETUSED::") - // setUsed(Math.round(quota.used / quota.total * 100)) - // }, [quota]); - // - // useEffect(() => { - // console.log("USED::" + used); - // }, [used]); + const [used, setUsed] = useState(0); + const [usedBytes, setUsedBytes] = useState(0); + const [totalBytes, setTotalBytes] = useState(0); + + if (FeatureTogglesService.isActiveFeatureName(FeatureNames.COMPANY_USER_QUOTA)) { + const { quota } = useUserQuota() + useEffect(() => { + setUsed(Math.round(quota.used / quota.total * 100)) + setUsedBytes(quota.used); + setTotalBytes(quota.total); + }, [quota]); + } else if (viewId) { + const { item } = useDriveItem(viewId); + useEffect(() => { + setUsedBytes(item?.size || 0); + }, [viewId, item]) + } return ( <> - {used} - {!FeatureTogglesService.isActiveFeatureName(FeatureNames.COMPANY_USER_QUOTA) && ( + {FeatureTogglesService.isActiveFeatureName(FeatureNames.COMPANY_USER_QUOTA) && (
@@ -46,20 +50,20 @@ export default () => {
{/*
*/} - {formatBytesToInt(quota?.used || 0)} + {formatBytesToInt(usedBytes)} { Languages.t('components.disk_usage.of')} - {formatBytesToInt(quota?.total || 0)} + {formatBytesToInt(totalBytes || 0)} { Languages.t('components.disk_usage.used')} {/*{formatBytes(trash?.size || 0)} {Languages.t('components.disk_usage.in_trash')}*/}
)} - {FeatureTogglesService.isActiveFeatureName(FeatureNames.COMPANY_USER_QUOTA) && ( + {!FeatureTogglesService.isActiveFeatureName(FeatureNames.COMPANY_USER_QUOTA) && (
- {formatBytesToInt(quota?.used || 0)} + {formatBytesToInt(usedBytes)} { Languages.t('components.disk_usage.used')}
@@ -67,4 +71,6 @@ export default () => { )} ); -}; \ No newline at end of file +}; + +export default DiskUsage; \ No newline at end of file