Sort bookmarks based on last watch

This commit is contained in:
Jip Fr 2023-03-12 22:59:43 +01:00
parent 506c00960f
commit a34a644d07

View File

@ -9,7 +9,7 @@ import {
import { useWatchedContext } from "@/state/watched"; import { useWatchedContext } from "@/state/watched";
import { WatchedMediaCard } from "@/components/media/WatchedMediaCard"; import { WatchedMediaCard } from "@/components/media/WatchedMediaCard";
import { EditButton } from "@/components/buttons/EditButton"; import { EditButton } from "@/components/buttons/EditButton";
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useMemo, useState } from "react";
import { useAutoAnimate } from "@formkit/auto-animate/react"; import { useAutoAnimate } from "@formkit/auto-animate/react";
import { useHistory } from "react-router-dom"; import { useHistory } from "react-router-dom";
import { Modal, ModalCard } from "@/components/layout/Modal"; import { Modal, ModalCard } from "@/components/layout/Modal";
@ -22,6 +22,22 @@ function Bookmarks() {
const bookmarks = getFilteredBookmarks(); const bookmarks = getFilteredBookmarks();
const [editing, setEditing] = useState(false); const [editing, setEditing] = useState(false);
const [gridRef] = useAutoAnimate<HTMLDivElement>(); const [gridRef] = useAutoAnimate<HTMLDivElement>();
const { watched } = useWatchedContext();
const bookmarksSorted = useMemo(() => {
return bookmarks
.map((v) => {
return {
...v,
watched: watched.items
.sort((a, b) => b.watchedAt - a.watchedAt)
.find((watchedItem) => watchedItem.item.meta.id === v.id),
};
})
.sort(
(a, b) => (b.watched?.watchedAt || 0) - (a.watched?.watchedAt || 0)
);
}, [watched.items, bookmarks]);
if (bookmarks.length === 0) return null; if (bookmarks.length === 0) return null;
@ -34,7 +50,7 @@ function Bookmarks() {
<EditButton editing={editing} onEdit={setEditing} /> <EditButton editing={editing} onEdit={setEditing} />
</SectionHeading> </SectionHeading>
<MediaGrid ref={gridRef}> <MediaGrid ref={gridRef}>
{bookmarks.map((v) => ( {bookmarksSorted.map((v) => (
<WatchedMediaCard <WatchedMediaCard
key={v.id} key={v.id}
media={v} media={v}