mirror of
https://github.com/sussy-code/smov.git
synced 2025-01-01 16:37:39 +01:00
Update BookmarksPart.tsx (Fixed Error Code)
This commit is contained in:
parent
42d12cfb96
commit
f6348e94ad
1 changed files with 34 additions and 25 deletions
|
@ -17,39 +17,48 @@ export function BookmarksPart({
|
||||||
onItemsChange: (hasItems: boolean) => void;
|
onItemsChange: (hasItems: boolean) => void;
|
||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const progressItems = useProgressStore((s) => s.items);
|
const progressItems = useProgressStore((state) => state.items);
|
||||||
const bookmarks = useBookmarkStore((s) => s.bookmarks);
|
const bookmarks = useBookmarkStore((state) => state.bookmarks);
|
||||||
const removeBookmark = useBookmarkStore((s) => s.removeBookmark);
|
const removeBookmark = useBookmarkStore((state) => state.removeBookmark);
|
||||||
const [editing, setEditing] = useState(false);
|
const [editing, setEditing] = useState(false);
|
||||||
const [gridRef] = useAutoAnimate<HTMLDivElement>();
|
const [gridRef] = useAutoAnimate<HTMLDivElement>();
|
||||||
|
|
||||||
const items = useMemo(() => {
|
const items = useMemo(() => {
|
||||||
let output: MediaItem[] = [];
|
// Transform bookmarks object into an array of MediaItem
|
||||||
Object.entries(bookmarks).forEach((entry) => {
|
const transformedItems: MediaItem[] = Object.keys(bookmarks).map((id) => {
|
||||||
output.push({
|
const { title, year, poster, type, updatedAt } = bookmarks[id];
|
||||||
id: entry[0],
|
return {
|
||||||
...entry[1],
|
id,
|
||||||
});
|
title,
|
||||||
|
year,
|
||||||
|
poster,
|
||||||
|
type,
|
||||||
|
updatedAt,
|
||||||
|
seasons: type === "show" ? [] : undefined, // Ensure seasons is defined for 'show' type
|
||||||
|
};
|
||||||
});
|
});
|
||||||
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);
|
// Sort items based on the latest update time
|
||||||
const dateB = Math.max(bookmarkB.updatedAt, progressB?.updatedAt ?? 0);
|
transformedItems.sort((a, b) => {
|
||||||
|
const aUpdatedAt = Math.max(
|
||||||
return dateB - dateA;
|
bookmarks[a.id].updatedAt,
|
||||||
|
progressItems[a.id]?.updatedAt ?? 0,
|
||||||
|
);
|
||||||
|
const bUpdatedAt = Math.max(
|
||||||
|
bookmarks[b.id].updatedAt,
|
||||||
|
progressItems[b.id]?.updatedAt ?? 0,
|
||||||
|
);
|
||||||
|
return bUpdatedAt - aUpdatedAt;
|
||||||
});
|
});
|
||||||
return output;
|
|
||||||
|
return transformedItems;
|
||||||
}, [bookmarks, progressItems]);
|
}, [bookmarks, progressItems]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
onItemsChange(items.length > 0);
|
onItemsChange(items.length > 0); // Notify parent component if there are items
|
||||||
}, [items, onItemsChange]);
|
}, [items, onItemsChange]);
|
||||||
|
|
||||||
if (items.length === 0) return null;
|
if (items.length === 0) return null; // If there are no items, return null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
@ -60,12 +69,12 @@ export function BookmarksPart({
|
||||||
<EditButton editing={editing} onEdit={setEditing} />
|
<EditButton editing={editing} onEdit={setEditing} />
|
||||||
</SectionHeading>
|
</SectionHeading>
|
||||||
<MediaGrid ref={gridRef}>
|
<MediaGrid ref={gridRef}>
|
||||||
{items.map((v) => (
|
{items.map((item) => (
|
||||||
<WatchedMediaCard
|
<WatchedMediaCard
|
||||||
key={v.id}
|
key={item.id}
|
||||||
media={v}
|
media={item}
|
||||||
closable={editing}
|
closable={editing}
|
||||||
onClose={() => removeBookmark(v.id)}
|
onClose={() => removeBookmark(item.id)}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</MediaGrid>
|
</MediaGrid>
|
||||||
|
|
Loading…
Reference in a new issue