feat(web): use ui meter component for storage (#27459)

This commit is contained in:
Mees Frensel
2026-04-03 16:36:22 +02:00
committed by GitHub
parent 8e414e42f3
commit a19b7148e5
2 changed files with 40 additions and 80 deletions
@@ -4,38 +4,18 @@
import { userInteraction } from '$lib/stores/user.svelte'; import { userInteraction } from '$lib/stores/user.svelte';
import { requestServerInfo } from '$lib/utils/auth'; import { requestServerInfo } from '$lib/utils/auth';
import { getByteUnitString } from '$lib/utils/byte-units'; import { getByteUnitString } from '$lib/utils/byte-units';
import { LoadingSpinner } from '@immich/ui'; import { LoadingSpinner, Meter } from '@immich/ui';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { t } from 'svelte-i18n'; import { t } from 'svelte-i18n';
let usageClasses = $state('');
let hasQuota = $derived($user?.quotaSizeInBytes !== null); let hasQuota = $derived($user?.quotaSizeInBytes !== null);
let availableBytes = $derived((hasQuota ? $user?.quotaSizeInBytes : userInteraction.serverInfo?.diskSizeRaw) || 0); let availableBytes = $derived((hasQuota ? $user?.quotaSizeInBytes : userInteraction.serverInfo?.diskSizeRaw) || 0);
let usedBytes = $derived((hasQuota ? $user?.quotaUsageInBytes : userInteraction.serverInfo?.diskUseRaw) || 0); let usedBytes = $derived((hasQuota ? $user?.quotaUsageInBytes : userInteraction.serverInfo?.diskUseRaw) || 0);
let usedPercentage = $derived(Math.min(Math.round((usedBytes / availableBytes) * 100), 100));
const onUpdate = () => { const thresholds = [
usageClasses = getUsageClass(); { from: 0.8, className: 'bg-warning' },
}; { from: 0.95, className: 'bg-danger' },
];
const getUsageClass = () => {
if (usedPercentage >= 95) {
return 'bg-red-500';
}
if (usedPercentage > 80) {
return 'bg-yellow-500';
}
return 'bg-primary';
};
$effect(() => {
if ($user) {
onUpdate();
}
});
onMount(async () => { onMount(async () => {
if (userInteraction.serverInfo && $user) { if (userInteraction.serverInfo && $user) {
@@ -46,7 +26,7 @@
</script> </script>
<div <div
class="storage-status p-4 bg-gray-100 dark:bg-immich-dark-primary/10 ms-4 rounded-lg text-sm min-w-52" class="p-4 bg-light-100 ms-4 rounded-lg text-sm min-w-52"
title={$t('storage_usage', { title={$t('storage_usage', {
values: { values: {
used: getByteUnitString(usedBytes, $locale, 3), used: getByteUnitString(usedBytes, $locale, 3),
@@ -54,24 +34,23 @@
}, },
})} })}
> >
<p class="font-medium text-immich-dark-gray dark:text-white mb-2">{$t('storage')}</p>
{#if userInteraction.serverInfo} {#if userInteraction.serverInfo}
<p class="text-gray-500 dark:text-gray-300"> <Meter
{$t('storage_usage', { size="tiny"
class="bg-light-200"
containerClass="gap-2 leading-6"
label={$t('storage')}
valueLabel={$t('storage_usage', {
values: { values: {
used: getByteUnitString(usedBytes, $locale), used: getByteUnitString(usedBytes, $locale),
available: getByteUnitString(availableBytes, $locale), available: getByteUnitString(availableBytes, $locale),
}, },
})} })}
</p> value={usedBytes / availableBytes}
{thresholds}
<div class="mt-4 h-1.75 w-full rounded-full bg-gray-200 dark:bg-gray-700"> />
<div class="h-1.75 rounded-full {usageClasses}" style="width: {usedPercentage}%"></div>
</div>
{:else} {:else}
<div class="mt-2"> <p class="font-medium text-immich-dark-gray dark:text-white mb-4">{$t('storage')}</p>
<LoadingSpinner /> <LoadingSpinner />
</div>
{/if} {/if}
</div> </div>
+23 -42
View File
@@ -23,6 +23,7 @@
Heading, Heading,
Icon, Icon,
MenuItemType, MenuItemType,
Meter,
Stack, Stack,
Text, Text,
} from '@immich/ui'; } from '@immich/ui';
@@ -49,30 +50,21 @@
const { children, data }: Props = $props(); const { children, data }: Props = $props();
const { user, userPreferences, userStatistics, userSessions } = $derived(data); const { user, userPreferences, userStatistics, userSessions } = $derived(data);
const TiB = 1024 ** 4;
const usage = $derived(user.quotaUsageInBytes ?? 0);
let [statsUsage, statsUsageUnit] = $derived(getBytesWithUnit(usage, usage > TiB ? 2 : 0));
const usedBytes = $derived(user.quotaUsageInBytes ?? 0); const usedBytes = $derived(user.quotaUsageInBytes ?? 0);
const availableBytes = $derived(user.quotaSizeInBytes ?? 1); const availableBytes = $derived(user.quotaSizeInBytes ?? 0);
let usedPercentage = $derived(Math.min(Math.round((usedBytes / availableBytes) * 100), 100)); const TiB = 1024 ** 4;
const [statsUsage, statsUsageUnit] = $derived(getBytesWithUnit(usedBytes, usedBytes > TiB ? 2 : 0));
let editedLocale = $derived(findLocale($locale).code); let editedLocale = $derived(findLocale($locale).code);
let createAtDate: Date = $derived(new Date(user.createdAt)); let createAtDate = $derived(new Date(user.createdAt));
let updatedAtDate: Date = $derived(new Date(user.updatedAt)); let updatedAtDate = $derived(new Date(user.updatedAt));
let userCreatedAtDateAndTime: string = $derived(createDateFormatter(editedLocale).formatDateTime(createAtDate)); let userCreatedAtDateAndTime = $derived(createDateFormatter(editedLocale).formatDateTime(createAtDate));
let userUpdatedAtDateAndTime: string = $derived(createDateFormatter(editedLocale).formatDateTime(updatedAtDate)); let userUpdatedAtDateAndTime = $derived(createDateFormatter(editedLocale).formatDateTime(updatedAtDate));
const getUsageClass = () => { const storageUsageThresholds = [
if (usedPercentage >= 95) { { from: 0.8, className: 'bg-warning' },
return 'bg-red-500'; { from: 0.95, className: 'bg-danger' },
} ];
if (usedPercentage > 80) {
return 'bg-yellow-500';
}
return 'bg-primary';
};
const { ResetPassword, ResetPinCode, Update, Delete, Restore } = $derived(getUserAdminActions($t, user)); const { ResetPassword, ResetPinCode, Update, Delete, Restore } = $derived(getUserAdminActions($t, user));
@@ -170,37 +162,26 @@
<AdminCard icon={mdiChartPieOutline} title={$t('storage_quota')}> <AdminCard icon={mdiChartPieOutline} title={$t('storage_quota')}>
{#if user.quotaSizeInBytes !== null && user.quotaSizeInBytes >= 0} {#if user.quotaSizeInBytes !== null && user.quotaSizeInBytes >= 0}
<Text> <Meter
{$t('storage_usage', { size="small"
class="bg-gray-200 dark:bg-gray-700"
containerClass="p-4 gap-4 bg-gray-100 dark:bg-gray-800 rounded-lg leading-6"
label={$t('storage')}
valueLabel={$t('storage_usage', {
values: { values: {
used: getByteUnitString(usedBytes, $locale, 3), used: getByteUnitString(usedBytes, $locale, 2),
available: getByteUnitString(availableBytes, $locale, 3), available: getByteUnitString(availableBytes, $locale, 2),
}, },
})} })}
</Text> value={usedBytes / availableBytes}
thresholds={storageUsageThresholds}
/>
{:else} {:else}
<Text class="flex items-center gap-1"> <Text class="flex items-center gap-1">
<Icon icon={mdiCheckCircle} size="1.25rem" class="text-success" /> <Icon icon={mdiCheckCircle} size="1.25rem" class="text-success" />
{$t('unlimited')} {$t('unlimited')}
</Text> </Text>
{/if} {/if}
{#if user.quotaSizeInBytes !== null && user.quotaSizeInBytes >= 0}
<div
class="storage-status p-4 mt-4 bg-gray-100 dark:bg-immich-dark-primary/10 rounded-lg text-sm w-full"
title={$t('storage_usage', {
values: {
used: getByteUnitString(usedBytes, $locale, 3),
available: getByteUnitString(availableBytes, $locale, 3),
},
})}
>
<p class="font-medium text-immich-dark-gray dark:text-white mb-2">{$t('storage')}</p>
<div class="mt-4 h-1.75 w-full rounded-full bg-gray-200 dark:bg-gray-700">
<div class="h-1.75 rounded-full {getUsageClass()}" style="width: {usedPercentage}%"></div>
</div>
</div>
{/if}
</AdminCard> </AdminCard>
<AdminCard icon={mdiDevices} title={$t('authorized_devices')}> <AdminCard icon={mdiDevices} title={$t('authorized_devices')}>