+12








fcd7c979df
Co-authored-by: Artur <145776561+arturict@users.noreply.github.com> Co-authored-by: arturict <arturict@users.noreply.github.com> Co-authored-by: Thomas <thomas@ajnart.fr> Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com> Co-authored-by: Danura30082 <danura30082+github@gmail.com> Co-authored-by: Kushie <122986913+kushiemoon-dev@users.noreply.github.com> Co-authored-by: ajnart <thomas@ajnart.fr> Co-authored-by: ajnart <thomas@ajnart.dev> Co-authored-by: chenjun <chenjun@foxmail.com> Co-authored-by: Wikibear <wikibear@users.noreply.github.com> Co-authored-by: Bergasha <bergasha@gmail.com> Co-authored-by: leafyelin <43306196+leafyelin@users.noreply.github.com> Co-authored-by: Lucas Araujo <lucas.lra@gmail.com> Co-authored-by: GreenMonito <86085570+GreenMonito@users.noreply.github.com> Co-authored-by: Nerdy-Technician <roffo@nerdy-technician.social> Co-authored-by: Jun <34545896+jcstrong@users.noreply.github.com> Co-authored-by: Edmond Abraham <54119952+gitedmond@users.noreply.github.com> Co-authored-by: thrawnx <tight.bulb9384@fastmail.com> Co-authored-by: Chris <58918656+nordada@users.noreply.github.com> Co-authored-by: yi111 <153097222+Yi-111-a@users.noreply.github.com> Co-authored-by: Yanis <naamaneyanis@gmail.com> Co-authored-by: Wikibear <me@sej.de> Co-authored-by: alessiottavo <93441886+alessiottavo@users.noreply.github.com>
207 lines
6.8 KiB
TypeScript
207 lines
6.8 KiB
TypeScript
"use client";
|
|
|
|
import React from "react";
|
|
import { Group, Progress, ScrollArea, SimpleGrid, Stack, Text } from "@mantine/core";
|
|
import { IconDatabase, IconPhoto, IconUsers, IconVideo } from "@tabler/icons-react";
|
|
import { getQueryKey } from "@trpc/react-query";
|
|
|
|
import { clientApi } from "@homarr/api/client";
|
|
import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client";
|
|
import { useByteFormatter } from "@homarr/settings";
|
|
import { zoomCompensatedSize } from "@homarr/ui";
|
|
|
|
import { WidgetEmptyState } from "../../common/empty-state";
|
|
import type { WidgetComponentProps } from "../../definition";
|
|
import { getUsableWidgetQueryData, isInitialWidgetQueryPending } from "../../common/query-state";
|
|
import { WidgetQueryLoadingState } from "../../common/query-state-indicator";
|
|
import { useWidgetRuntimeQueries } from "../../runtime-hooks";
|
|
import classes from "./component.module.css";
|
|
|
|
const MAX_ADVANCED_ALBUMS = 50;
|
|
|
|
export default function ImmichServerStatsWidget({
|
|
integrationIds,
|
|
options,
|
|
displayMode = "compact",
|
|
width,
|
|
height,
|
|
widgetRuntimeRef,
|
|
}: WidgetComponentProps<"immich-serverStats">) {
|
|
const t = useI18n("widget.immich-serverStats");
|
|
const { formatBytes } = useByteFormatter();
|
|
const locale = useCurrentIntlLocale();
|
|
const input = { integrationId: integrationIds[0] ?? "" };
|
|
const albumsInput = { ...input, limit: MAX_ADVANCED_ALBUMS };
|
|
const isAdvanced = displayMode === "advanced";
|
|
const albumsEnabled = isAdvanced && integrationIds.length > 0;
|
|
const statsQuery = clientApi.widget.immich.getServerStats.useQuery(input);
|
|
const stats = getUsableWidgetQueryData(statsQuery);
|
|
const albumsQuery = clientApi.widget.immich.getAlbums.useQuery(albumsInput, {
|
|
enabled: albumsEnabled,
|
|
staleTime: 15 * 60 * 1000,
|
|
});
|
|
const albums = albumsQuery.data ?? [];
|
|
useWidgetRuntimeQueries(widgetRuntimeRef, [
|
|
getQueryKey(clientApi.widget.immich.getServerStats, input, "query"),
|
|
...(albumsEnabled ? [getQueryKey(clientApi.widget.immich.getAlbums, albumsInput, "query")] : []),
|
|
]);
|
|
|
|
if (isInitialWidgetQueryPending(statsQuery)) return <WidgetQueryLoadingState />;
|
|
if (!stats) return <WidgetEmptyState />;
|
|
|
|
const statVisibility = getImmichStatVisibility(options, isAdvanced);
|
|
const statCount =
|
|
Number(statVisibility.showUsers) +
|
|
Number(statVisibility.showPhotos) +
|
|
Number(statVisibility.showVideos) +
|
|
Number(statVisibility.showStorage);
|
|
const statsLayout = getImmichStatsLayout(width, height, statCount, isAdvanced);
|
|
|
|
const statItems = [
|
|
{
|
|
key: "users",
|
|
enabled: statVisibility.showUsers,
|
|
icon: <IconUsers style={zoomCompensatedSize(isAdvanced ? 18 : 20)} />,
|
|
label: t("users"),
|
|
value: stats.userCount.toLocaleString(locale),
|
|
},
|
|
{
|
|
key: "photos",
|
|
enabled: statVisibility.showPhotos,
|
|
icon: <IconPhoto style={zoomCompensatedSize(isAdvanced ? 18 : 20)} />,
|
|
label: t("photos"),
|
|
value: stats.photoCount.toLocaleString(locale),
|
|
},
|
|
{
|
|
key: "videos",
|
|
enabled: statVisibility.showVideos,
|
|
icon: <IconVideo style={zoomCompensatedSize(isAdvanced ? 18 : 20)} />,
|
|
label: t("videos"),
|
|
value: stats.videoCount.toLocaleString(locale),
|
|
},
|
|
{
|
|
key: "storage",
|
|
enabled: statVisibility.showStorage,
|
|
icon: <IconDatabase style={zoomCompensatedSize(isAdvanced ? 18 : 20)} />,
|
|
label: t("storage"),
|
|
value: formatBytes(stats.totalLibraryUsageInBytes),
|
|
},
|
|
]
|
|
.filter((item) => item.enabled)
|
|
.map(({ key, icon, label, value }) => (
|
|
<StatItem
|
|
key={key}
|
|
icon={icon}
|
|
label={label}
|
|
value={value}
|
|
dense={isAdvanced && statsLayout.dense}
|
|
compact={!isAdvanced}
|
|
/>
|
|
));
|
|
|
|
if (!isAdvanced) {
|
|
return (
|
|
<Stack gap="md" h="100%" p="md">
|
|
{statItems}
|
|
</Stack>
|
|
);
|
|
}
|
|
|
|
const statsContent = (
|
|
<SimpleGrid cols={statsLayout.columns} spacing={statsLayout.dense ? 4 : "sm"}>
|
|
{statItems}
|
|
</SimpleGrid>
|
|
);
|
|
const sortedAlbums = albums;
|
|
const maxAssets = sortedAlbums[0]?.assetCount ?? 1;
|
|
return (
|
|
<Stack gap="lg" h="100%" p="lg">
|
|
{statsContent}
|
|
<Group justify="space-between" wrap="nowrap">
|
|
<Text size="xs" c="dimmed">
|
|
{t("albumLimit", { count: MAX_ADVANCED_ALBUMS })}
|
|
</Text>
|
|
</Group>
|
|
<ScrollArea style={{ flex: 1, minHeight: 0 }}>
|
|
{isInitialWidgetQueryPending(albumsQuery) ? (
|
|
<WidgetQueryLoadingState />
|
|
) : albums.length === 0 ? (
|
|
<WidgetEmptyState />
|
|
) : (
|
|
<SimpleGrid cols={width >= 900 ? 2 : 1} spacing="xs">
|
|
{sortedAlbums.map((album) => (
|
|
<Stack key={album.id} gap={4} p="xs">
|
|
<Group justify="space-between" wrap="nowrap">
|
|
<Text size="sm" fw={600} truncate>
|
|
{album.albumName}
|
|
</Text>
|
|
<Text size="xs" c="dimmed">
|
|
{album.assetCount.toLocaleString(locale)}
|
|
</Text>
|
|
</Group>
|
|
<Progress value={(album.assetCount / Math.max(maxAssets, 1)) * 100} size="sm" />
|
|
</Stack>
|
|
))}
|
|
</SimpleGrid>
|
|
)}
|
|
</ScrollArea>
|
|
</Stack>
|
|
);
|
|
}
|
|
|
|
interface StatItemProps {
|
|
icon: React.ReactNode;
|
|
label: string;
|
|
value: string | number;
|
|
dense: boolean;
|
|
compact: boolean;
|
|
}
|
|
|
|
function StatItem({ icon, label, value, dense, compact }: StatItemProps) {
|
|
if (dense) {
|
|
return (
|
|
<Stack gap={1} align="center" justify="center" className={classes.statItemDense} title={`${label}: ${value}`}>
|
|
<Text size="sm" fw={700} truncate="end" w="100%" ta="center">
|
|
{value}
|
|
</Text>
|
|
<Text size="xs" c="dimmed" truncate="end" w="100%" ta="center">
|
|
{label}
|
|
</Text>
|
|
</Stack>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Group justify="space-between" align="center" className={compact ? classes.statItemCompact : classes.statItem}>
|
|
<Group gap="sm" align="center" wrap="nowrap" miw={0}>
|
|
{icon}
|
|
<Text size="sm" fw={500} truncate="end">
|
|
{label}
|
|
</Text>
|
|
</Group>
|
|
<Text size="sm" fw={700} c="var(--mantine-primary-color)" truncate="end" title={String(value)}>
|
|
{value}
|
|
</Text>
|
|
</Group>
|
|
);
|
|
}
|
|
|
|
export const getImmichStatsLayout = (width: number, _height: number, itemCount: number, isAdvanced = false) => {
|
|
const count = Math.max(1, itemCount);
|
|
if (isAdvanced) {
|
|
return { columns: width >= 720 ? Math.min(count, 4) : width >= 360 ? Math.min(count, 2) : 1, dense: false };
|
|
}
|
|
|
|
return { columns: 1, dense: false };
|
|
};
|
|
|
|
export const getImmichStatVisibility = (
|
|
options: WidgetComponentProps<"immich-serverStats">["options"],
|
|
isAdvanced: boolean,
|
|
) => ({
|
|
showUsers: isAdvanced || options.showUsers,
|
|
showPhotos: isAdvanced || options.showPhotos,
|
|
showVideos: isAdvanced || options.showVideos,
|
|
showStorage: isAdvanced || options.showStorage,
|
|
});
|