From 20202d2216c216ed9dc3e854081c6c4b3b9aef62 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Thu, 28 Mar 2024 13:39:18 +0000 Subject: [PATCH] Add logic for same year not released and fix language string --- src/assets/locales/en.json | 1 + src/backend/metadata/getmeta.ts | 4 +-- src/backend/metadata/tmdb.ts | 9 ++++--- src/backend/metadata/types/tmdb.ts | 2 +- src/components/media/MediaCard.tsx | 42 +++++++++++++++++++++--------- src/utils/mediaTypes.ts | 1 + 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/assets/locales/en.json b/src/assets/locales/en.json index ab128219..bc042462 100644 --- a/src/assets/locales/en.json +++ b/src/assets/locales/en.json @@ -148,6 +148,7 @@ }, "media": { "episodeDisplay": "S{{season}} E{{episode}}", + "unreleased": "Unreleased", "types": { "movie": "Movie", "show": "Show" diff --git a/src/backend/metadata/getmeta.ts b/src/backend/metadata/getmeta.ts index add1089e..3da79ac2 100644 --- a/src/backend/metadata/getmeta.ts +++ b/src/backend/metadata/getmeta.ts @@ -43,7 +43,7 @@ export function formatTMDBMetaResult( title: movie.title, object_type: mediaTypeToTMDB(type), poster: getMediaPoster(movie.poster_path) ?? undefined, - original_release_year: new Date(movie.release_date).getFullYear(), + original_release_date: new Date(movie.release_date), }; } if (type === MWMediaType.SERIES) { @@ -58,7 +58,7 @@ export function formatTMDBMetaResult( title: v.name, })), poster: getMediaPoster(show.poster_path) ?? undefined, - original_release_year: new Date(show.first_air_date).getFullYear(), + original_release_date: new Date(show.first_air_date), }; } diff --git a/src/backend/metadata/tmdb.ts b/src/backend/metadata/tmdb.ts index b143b312..67c7d56f 100644 --- a/src/backend/metadata/tmdb.ts +++ b/src/backend/metadata/tmdb.ts @@ -66,7 +66,7 @@ export function formatTMDBMeta( return { title: media.title, id: media.id.toString(), - year: media.original_release_year?.toString(), + year: media.original_release_date?.getFullYear()?.toString(), poster: media.poster, type, seasons: seasons as any, @@ -94,7 +94,8 @@ export function formatTMDBMetaToMediaItem(media: TMDBMediaResult): MediaItem { return { title: media.title, id: media.id.toString(), - year: media.original_release_year ?? 0, + year: media.original_release_date?.getFullYear() ?? 0, + release_date: media.original_release_date, poster: media.poster, type, }; @@ -260,7 +261,7 @@ export function formatTMDBSearchResult( title: show.name, poster: getMediaPoster(show.poster_path), id: show.id, - original_release_year: new Date(show.first_air_date).getFullYear(), + original_release_date: new Date(show.first_air_date), object_type: mediatype, }; } @@ -271,7 +272,7 @@ export function formatTMDBSearchResult( title: movie.title, poster: getMediaPoster(movie.poster_path), id: movie.id, - original_release_year: new Date(movie.release_date).getFullYear(), + original_release_date: new Date(movie.release_date), object_type: mediatype, }; } diff --git a/src/backend/metadata/types/tmdb.ts b/src/backend/metadata/types/tmdb.ts index 1071d96c..5d082f55 100644 --- a/src/backend/metadata/types/tmdb.ts +++ b/src/backend/metadata/types/tmdb.ts @@ -20,7 +20,7 @@ export type TMDBMediaResult = { title: string; poster?: string; id: number; - original_release_year?: number; + original_release_date?: Date; object_type: TMDBContentTypes; seasons?: TMDBSeasonShort[]; }; diff --git a/src/components/media/MediaCard.tsx b/src/components/media/MediaCard.tsx index 563e4d9b..17ada085 100644 --- a/src/components/media/MediaCard.tsx +++ b/src/components/media/MediaCard.tsx @@ -1,4 +1,5 @@ import classNames from "classnames"; +import { useCallback } from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; @@ -24,6 +25,20 @@ export interface MediaCardProps { onClose?: () => void; } +function checkReleased(media: MediaItem): boolean { + const isReleasedYear = Boolean( + media.year && media.year <= new Date().getFullYear(), + ); + const isReleasedDate = Boolean( + media.release_date && media.release_date <= new Date(), + ); + + // If the media has a release date, use that, otherwise use the year + const isReleased = media.release_date ? isReleasedDate : isReleasedYear; + + return isReleased; +} + function MediaCardContent({ media, linkable, @@ -35,16 +50,17 @@ function MediaCardContent({ const { t } = useTranslation(); const percentageString = `${Math.round(percentage ?? 0).toFixed(0)}%`; - const currentYear = new Date().getFullYear(); - const canLink = - linkable && !closable && media.year && media.year <= currentYear; + const isReleased = useCallback(() => checkReleased(media), [media]); + + const canLink = linkable && !closable && isReleased(); const dotListContent = [t(`media.types.${media.type}`)]; - if (media.year && media.year > currentYear) { - dotListContent.push(`${media.year}`, t("media.unreleased")); - } else if (media.year) { + + if (media.year) { dotListContent.push(media.year.toFixed()); - } else { + } + + if (!isReleased()) { dotListContent.push(t("media.unreleased")); } @@ -150,12 +166,12 @@ function MediaCardContent({ export function MediaCard(props: MediaCardProps) { const content = ; - const currentYear = new Date().getFullYear(); - const canLink = - props.linkable && - !props.closable && - props.media.year && - props.media.year <= currentYear; + const isReleased = useCallback( + () => checkReleased(props.media), + [props.media], + ); + + const canLink = props.linkable && !props.closable && isReleased(); let link = canLink ? `/media/${encodeURIComponent(mediaItemToId(props.media))}` diff --git a/src/utils/mediaTypes.ts b/src/utils/mediaTypes.ts index f577ca5f..c81b1ac0 100644 --- a/src/utils/mediaTypes.ts +++ b/src/utils/mediaTypes.ts @@ -2,6 +2,7 @@ export interface MediaItem { id: string; title: string; year?: number; + release_date?: Date; poster?: string; type: "show" | "movie"; }