2023-08-20 17:59:46 +02:00
|
|
|
import { useAutoAnimate } from "@formkit/auto-animate/react";
|
|
|
|
import { useMemo, useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
|
|
import { EditButton } from "@/components/buttons/EditButton";
|
|
|
|
import { Icons } from "@/components/Icon";
|
|
|
|
import { SectionHeading } from "@/components/layout/SectionHeading";
|
|
|
|
import { MediaGrid } from "@/components/media/MediaGrid";
|
|
|
|
import { WatchedMediaCard } from "@/components/media/WatchedMediaCard";
|
2023-10-17 16:14:46 +02:00
|
|
|
import { useBookmarkStore } from "@/stores/bookmarks";
|
2023-10-17 17:04:03 +02:00
|
|
|
import { useProgressStore } from "@/stores/progress";
|
2023-10-17 16:14:46 +02:00
|
|
|
import { MediaItem } from "@/utils/mediaTypes";
|
2023-08-20 17:59:46 +02:00
|
|
|
|
|
|
|
export function BookmarksPart() {
|
|
|
|
const { t } = useTranslation();
|
2023-10-17 17:04:03 +02:00
|
|
|
const progressItems = useProgressStore((s) => s.items);
|
2023-10-17 16:14:46 +02:00
|
|
|
const bookmarks = useBookmarkStore((s) => s.bookmarks);
|
|
|
|
const removeBookmark = useBookmarkStore((s) => s.removeBookmark);
|
2023-08-20 17:59:46 +02:00
|
|
|
const [editing, setEditing] = useState(false);
|
|
|
|
const [gridRef] = useAutoAnimate<HTMLDivElement>();
|
|
|
|
|
2023-10-17 16:14:46 +02:00
|
|
|
const items = useMemo(() => {
|
2023-10-17 17:04:03 +02:00
|
|
|
let output: MediaItem[] = [];
|
2023-10-17 16:14:46 +02:00
|
|
|
Object.entries(bookmarks).forEach((entry) => {
|
|
|
|
output.push({
|
|
|
|
id: entry[0],
|
|
|
|
...entry[1],
|
|
|
|
});
|
|
|
|
});
|
2023-10-17 17:04:03 +02:00
|
|
|
output = output.sort((a, b) => {
|
|
|
|
const bookmarkA = bookmarks[a.id];
|
|
|
|
const bookmarkB = bookmarks[b.id];
|
|
|
|
const progressA = progressItems[a.id];
|
|
|
|
const progressB = progressItems[b.id];
|
|
|
|
|
|
|
|
const dateA = Math.max(bookmarkA.updatedAt, progressA?.updatedAt ?? 0);
|
|
|
|
const dateB = Math.max(bookmarkB.updatedAt, progressB?.updatedAt ?? 0);
|
|
|
|
|
|
|
|
return dateB - dateA;
|
|
|
|
});
|
2023-10-17 16:14:46 +02:00
|
|
|
return output;
|
2023-10-17 17:04:03 +02:00
|
|
|
}, [bookmarks, progressItems]);
|
2023-08-20 17:59:46 +02:00
|
|
|
|
2023-10-17 16:14:46 +02:00
|
|
|
if (items.length === 0) return null;
|
2023-08-20 17:59:46 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<SectionHeading
|
2023-11-27 20:19:03 +01:00
|
|
|
title={t("home.bookmarks.sectionTitle") || "Bookmarks"}
|
2023-08-20 17:59:46 +02:00
|
|
|
icon={Icons.BOOKMARK}
|
|
|
|
>
|
|
|
|
<EditButton editing={editing} onEdit={setEditing} />
|
|
|
|
</SectionHeading>
|
|
|
|
<MediaGrid ref={gridRef}>
|
2023-10-17 16:14:46 +02:00
|
|
|
{items.map((v) => (
|
|
|
|
<WatchedMediaCard
|
|
|
|
key={v.id}
|
|
|
|
media={v}
|
|
|
|
closable={editing}
|
|
|
|
onClose={() => removeBookmark(v.id)}
|
|
|
|
/>
|
2023-08-20 17:59:46 +02:00
|
|
|
))}
|
|
|
|
</MediaGrid>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|