"use client"; import type { Dispatch, SetStateAction } from "react"; import { useEffect, useMemo, useState } from "react"; import { ActionIcon, Box, Center, Group, Image, ScrollArea, Stack, Text, UnstyledButton } from "@mantine/core"; import { useReducedMotion } from "@mantine/hooks"; import { IconAlertCircle, IconCalendar, IconChevronLeft, IconChevronRight, IconPlayerPause, IconPlayerPlay, } from "@tabler/icons-react"; import { clientApi } from "@homarr/api/client"; import { useI18n, useScopedI18n } from "@homarr/translation/client"; import { WidgetEmptyState } from "../../common/empty-state"; import type { WidgetComponentProps } from "../../definition"; import classes from "./component.module.css"; import { ALL_PHOTOS_ALBUM_ID } from "./constants"; export default function ImmichAlbumCarouselWidget({ integrationIds, options, displayMode = "compact", widgetStateRef, }: WidgetComponentProps<"immich-albumCarousel">) { const [currentPhotoIndex, setCurrentPhotoIndex] = useState(0); const [paused, setPaused] = useState(false); const { data: album } = clientApi.widget.immich.getAlbum.useQuery( { integrationId: integrationIds[0] ?? "", albumId: options.albumId && options.albumId !== ALL_PHOTOS_ALBUM_ID ? options.albumId : undefined, }, { enabled: integrationIds.length > 0 }, ); const photoAssets = useMemo(() => { const assets = album?.assets.filter((asset) => asset.type === "IMAGE") ?? []; return options.randomizePhotos ? shuffle(assets) : assets; }, [album?.assets, options.randomizePhotos]); useEffect(() => { if (photoAssets.length === 0) return; setCurrentPhotoIndex((current) => Math.min(current, photoAssets.length - 1)); }, [photoAssets.length]); useEffect(() => { if (!widgetStateRef) return; const clearActions = () => { if (!widgetStateRef.current) return; delete widgetStateRef.current.previousPhoto; delete widgetStateRef.current.nextPhoto; delete widgetStateRef.current.toggleSlideshow; }; if (photoAssets.length <= 1) { clearActions(); return; } const previousPhoto = () => setCurrentPhotoIndex((current) => (current - 1 + photoAssets.length) % photoAssets.length); const nextPhoto = () => setCurrentPhotoIndex((current) => (current + 1) % photoAssets.length); const toggleSlideshow = () => setPaused((value) => !value); widgetStateRef.current = { ...widgetStateRef.current, previousPhoto, nextPhoto, toggleSlideshow, }; return clearActions; }, [photoAssets.length, widgetStateRef]); if (!album) return ; if (album.assets.length === 0) { return ; } if (photoAssets.length === 0) { return ; } return ( ); } function shuffle(items: T[]) { const shuffled = [...items]; for (let index = shuffled.length - 1; index > 0; index--) { const randomIndex = Math.floor(Math.random() * (index + 1)); const current = shuffled[index]; const random = shuffled[randomIndex]; if (current === undefined || random === undefined) continue; shuffled[index] = random; shuffled[randomIndex] = current; } return shuffled; } interface CarouselProps { assets: { id: string; fileModifiedAt: string; publicLink: string; }[]; currentIndex: number; setCurrentIndex: Dispatch>; rotationInterval: number; showPhotoInfo: boolean; albumName: string; advanced: boolean; paused: boolean; setPaused: Dispatch>; } function Carousel({ assets, currentIndex, setCurrentIndex, rotationInterval, showPhotoInfo, albumName, advanced, paused, setPaused, }: CarouselProps) { const t = useScopedI18n("widget.immich-albumCarousel"); const reduceMotion = useReducedMotion(); useEffect(() => { if (paused || reduceMotion || assets.length <= 1) return; const interval = setInterval(() => { setCurrentIndex((prev) => (prev + 1) % assets.length); }, rotationInterval * 1000); return () => clearInterval(interval); }, [assets.length, paused, reduceMotion, rotationInterval, setCurrentIndex]); const safeCurrentIndex = Math.min(currentIndex, assets.length - 1); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const currentAsset = assets[safeCurrentIndex]!; const move = (offset: number) => setCurrentIndex((current) => (current + offset + assets.length) % assets.length); return ( {t("albumPhoto")} {advanced && ( <> move(-1)} > setPaused((value) => !value)} > {paused ? : } move(1)} > )} {(showPhotoInfo || advanced) && ( {albumName && ( {albumName} )} {new Date(currentAsset.fileModifiedAt).toLocaleDateString()} {safeCurrentIndex + 1} / {assets.length} )} {advanced && ( {assets.slice(0, 24).map((asset, index) => ( setCurrentIndex(index)} aria-label={t("actions.photo", { number: index + 1 })} aria-pressed={index === safeCurrentIndex} aria-current={index === safeCurrentIndex ? "true" : undefined} > ))} )} ); } function NoPhotosInAlbum() { const t = useI18n(); return (
{t("widget.immich-albumCarousel.noPhotos")}
); }