Add computed entry stats to collection detail page

Show movies/series/seasons/episodes counts and estimated
total watch time inline — all from already-fetched data.
This commit is contained in:
X4Lo committed 2026-07-01 00:00:13 +01:00
1 parent e660b1ec87
commit 1aa0a84f4d
1 file changed
+50 -4
@@ -125,6 +125,52 @@ export function CollectionDetailsPage() {
} }
}; };
// ── Computed stats (from already-fetched data) ──
const stats = useMemo(() => {
const movies = entries.filter((e) => !e.is_tvseries);
const tvEntries = entries.filter((e) => e.is_tvseries);
const seasonCount = tvEntries.reduce(
(sum, e) => sum + (e.seasons?.length || 0),
0,
);
const episodeCount = tvEntries.reduce(
(sum, e) =>
sum +
(e.seasons?.reduce((s, season) => s + (season.episode_count || 0), 0) ||
0),
0,
);
const typeParts: string[] = [];
if (movies.length > 0) typeParts.push(`${movies.length} movies`);
if (tvEntries.length > 0) typeParts.push(`${tvEntries.length} series`);
const typeStr = typeParts.join(" & ");
const subParts: string[] = [];
if (seasonCount > 0) subParts.push(`${seasonCount} seasons`);
if (episodeCount > 0) subParts.push(`${episodeCount} episodes`);
const subStr = subParts.length > 0 ? ` (${subParts.join("/")})` : "";
const summary = `${entries.length} entries - ${typeStr}${subStr}`;
const watchMinutes = entries.reduce((sum, e) => {
if (e.is_tvseries) {
const eps =
e.seasons?.reduce(
(s, season) => s + (season.episode_count || 0),
0,
) || 0;
const epRuntime = e.runtime || 42;
return sum + eps * epRuntime;
}
return sum + (e.runtime || 0);
}, 0);
const hours = Math.floor(watchMinutes / 60);
const mins = watchMinutes % 60;
const watchTime = hours > 0 ? `${hours}h ${mins}m` : `${mins}m`;
return { summary, watchTime };
}, [entries]);
// Handle error redirect in useEffect (not during render) // Handle error redirect in useEffect (not during render)
useEffect(() => { useEffect(() => {
if (!isLoading && (isError || (!collection && !isLoading))) { if (!isLoading && (isError || (!collection && !isLoading))) {
@@ -221,10 +267,10 @@ export function CollectionDetailsPage() {
{collection.description} {collection.description}
</p> </p>
)} )}
<div className="flex items-center gap-3"> <div className="flex items-center gap-3 flex-wrap text-sm text-white/60">
<p className="text-sm text-white/60"> <span>
{entries.length} {entries.length === 1 ? "entry" : "entries"} {stats.summary} ~{stats.watchTime}
</p> </span>
{collection.user_rating != null && {collection.user_rating != null &&
collection.user_rating > 0 && ( collection.user_rating > 0 && (
<div className="flex items-center gap-1"> <div className="flex items-center gap-1">