Files
Showy/react_frontend/src/lib/media-utils.ts
T
X4Lo f88dfdae44 Unify person page for local and TMDB people
- Merge local and TMDB person views into a single PersonDetailsPage
- Remove unused PersonTmdbRedirectPage
- Show age tooltip on birthdays (person page and people list)
2026-11-03 18:58:42 +00:00

161 lines
4.2 KiB
TypeScript

import { getMediaUrl, getTmdbImageUrl } from "@/lib/api";
import type { Entry, Collection, Media } from "@/types";
const DEFAULT_POSTER = "/images/default-poster.png";
export function getPosterUrl(entry: Entry): string {
// Not in library, use TMDB poster
if (!entry.isInLibrary) {
return entry.poster_path
? getTmdbImageUrl(entry.poster_path, "w342")
: DEFAULT_POSTER;
}
// In library: try local selected poster
if (entry.medias) {
const selectedPoster = entry.medias.find(
(m: Media) => m.type === "poster" && m.is_selected,
);
if (selectedPoster?.path) {
return getMediaUrl("entries", "posters", "preview", selectedPoster.path);
}
}
// Fallback to TMDB poster
if (entry.poster_path) {
return getTmdbImageUrl(entry.poster_path, "w300");
}
return DEFAULT_POSTER;
}
/** Same source as getPosterUrl, but at original resolution. */
export function getFullPosterUrl(entry: Entry): string {
const selectedPoster = entry.isInLibrary
? entry.medias?.find((m: Media) => m.type === "poster" && m.is_selected)
: undefined;
if (selectedPoster?.path) {
return getMediaUrl("entries", "posters", "full", selectedPoster.path);
}
if (entry.poster_path) {
return getTmdbImageUrl(entry.poster_path, "original");
}
return DEFAULT_POSTER;
}
export function getBackdropUrl(entry: Entry): string {
if (entry.medias) {
const selectedBackdrop = entry.medias.find(
(m: Media) => m.type === "backdrop" && m.is_selected,
);
if (selectedBackdrop?.path) {
return getMediaUrl("entries", "backdrops", "full", selectedBackdrop.path);
}
}
if (entry.banner_path) {
return getTmdbImageUrl(entry.banner_path, "w1280");
}
return "";
}
export function getTmdbPosterUrl(
posterPath: string | null,
size = "w342",
): string {
if (!posterPath) return DEFAULT_POSTER;
return getTmdbImageUrl(posterPath, size);
}
export function getCollectionBackdropUrl(collection: Collection): string {
if (collection.medias) {
const selectedBackdrop = collection.medias.find(
(m: Media) => m.type === "backdrop" && m.is_selected,
);
if (selectedBackdrop?.path) {
return getMediaUrl(
"collections",
"backdrops",
"full",
selectedBackdrop.path,
);
}
}
if (collection.backdrop_path) {
return getTmdbImageUrl(collection.backdrop_path, "w780");
}
return getCollectionPosterUrl(collection);
}
export function getCollectionPosterUrl(collection: Collection): string {
if (collection.medias) {
const selectedPoster = collection.medias.find(
(m: Media) => m.type === "poster" && m.is_selected,
);
if (selectedPoster?.path) {
return getMediaUrl(
"collections",
"posters",
"preview",
selectedPoster.path,
);
}
}
if (collection.poster_path) {
return getTmdbImageUrl(collection.poster_path, "w342");
}
return DEFAULT_POSTER;
}
export function getProfileUrl(profilePath: string | null): string {
if (!profilePath) return "/images/default-profile.png";
return getTmdbImageUrl(profilePath, "w185");
}
export function formatRuntime(minutes: number | null): string {
if (!minutes) return "";
const h = Math.floor(minutes / 60);
const m = minutes % 60;
return h > 0 ? `${h}h ${m}m` : `${m}m`;
}
export function formatDate(dateStr: string | null): string {
if (!dateStr) return "";
return new Date(dateStr).toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
});
}
export function getYear(dateStr: string | null): string {
if (!dateStr) return "";
return new Date(dateStr).getFullYear().toString();
}
export function calculateAge(
birthday: string | null | undefined,
deathday?: string | null,
): number | null {
if (!birthday) return null;
const birth = new Date(birthday);
if (isNaN(birth.getTime())) return null;
let end = deathday ? new Date(deathday) : new Date();
if (deathday && isNaN(end.getTime())) end = new Date();
let age = end.getFullYear() - birth.getFullYear();
const monthDiff = end.getMonth() - birth.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && end.getDate() < birth.getDate())) {
age--;
}
return age;
}