Files
Showy/react_frontend/src/lib/media-utils.ts
T

104 lines
2.6 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;
}
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 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();
}