-
-
Notifications
You must be signed in to change notification settings - Fork 930
feat(title-card): display ratings on media cards #3154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
u61d
wants to merge
5
commits into
seerr-team:develop
Choose a base branch
from
u61d:feat/title-card-ratings
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
52b9cb3
feat(title-card): display ratings on media cards
u61d 0c53a3a
fix(ratings): handle provider failures and reduce hover requests
u61d 0f6bd81
fix(title-card): limit ratings to movies and series
u61d 4e50292
fix(title-card): address rating review feedback
u61d 068c05b
fix(title-card): avoid duplicate rating announcements
u61d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import RTAudFresh from '@app/assets/rt_aud_fresh.svg'; | ||
| import RTAudRotten from '@app/assets/rt_aud_rotten.svg'; | ||
| import RTFresh from '@app/assets/rt_fresh.svg'; | ||
| import RTRotten from '@app/assets/rt_rotten.svg'; | ||
| import ImdbLogo from '@app/assets/services/imdb.svg'; | ||
| import TmdbLogo from '@app/assets/services/tmdb.svg'; | ||
| import defineMessages from '@app/utils/defineMessages'; | ||
| import type { RTRating } from '@server/api/rating/rottentomatoes'; | ||
| import type { RatingResponse } from '@server/api/ratings'; | ||
| import type { MediaType } from '@server/models/Search'; | ||
| import { useEffect, useState } from 'react'; | ||
| import { useIntl } from 'react-intl'; | ||
| import useSWR from 'swr'; | ||
|
|
||
| interface TitleCardRatingsProps { | ||
| id: number; | ||
| mediaType: MediaType; | ||
| userScore?: number; | ||
| visible: boolean; | ||
| } | ||
|
|
||
| const RATINGS_REQUEST_DELAY_MS = 300; | ||
|
|
||
| const messages = defineMessages('components.TitleCard.TitleCardRatings', { | ||
| ratings: 'Ratings', | ||
| rottenTomatoesAudienceScore: 'Rotten Tomatoes Audience Score', | ||
| rottenTomatoesCriticsScore: 'Rotten Tomatoes Critics Score', | ||
| imdbUserScore: 'IMDb User Score', | ||
| tmdbUserScore: 'TMDB User Score', | ||
| }); | ||
|
|
||
| const TitleCardRatings = ({ | ||
| id, | ||
| mediaType, | ||
| userScore, | ||
| visible, | ||
| }: TitleCardRatingsProps) => { | ||
| const intl = useIntl(); | ||
| const [ratingsRequested, setRatingsRequested] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| if (!visible || ratingsRequested) { | ||
| return; | ||
| } | ||
|
|
||
| const timeout = setTimeout(() => { | ||
| setRatingsRequested(true); | ||
| }, RATINGS_REQUEST_DELAY_MS); | ||
|
|
||
| return () => clearTimeout(timeout); | ||
| }, [ratingsRequested, visible]); | ||
|
|
||
| const { data: movieRatings } = useSWR<RatingResponse>( | ||
| ratingsRequested && mediaType === 'movie' | ||
| ? `/api/v1/movie/${id}/ratingscombined` | ||
| : null, | ||
| { | ||
| revalidateOnFocus: false, | ||
| shouldRetryOnError: false, | ||
| } | ||
| ); | ||
| const { data: tvRatings } = useSWR<RTRating>( | ||
| ratingsRequested && mediaType === 'tv' ? `/api/v1/tv/${id}/ratings` : null, | ||
| { | ||
| revalidateOnFocus: false, | ||
| shouldRetryOnError: false, | ||
| } | ||
| ); | ||
|
|
||
| const rtRating = | ||
| mediaType === 'movie' | ||
| ? movieRatings?.rt | ||
| : mediaType === 'tv' | ||
| ? tvRatings | ||
| : undefined; | ||
| const imdbRating = movieRatings?.imdb; | ||
| const hasRtAudienceScore = | ||
| typeof rtRating?.audienceScore === 'number' && | ||
| rtRating.audienceRating !== undefined; | ||
| const hasRtCriticsScore = | ||
| !hasRtAudienceScore && | ||
| typeof rtRating?.criticsScore === 'number' && | ||
| rtRating.criticsRating !== undefined; | ||
| const hasImdbScore = typeof imdbRating?.criticsScore === 'number'; | ||
| const hasTmdbScore = typeof userScore === 'number' && userScore > 0; | ||
|
|
||
| if ( | ||
| !hasRtAudienceScore && | ||
| !hasRtCriticsScore && | ||
| !hasImdbScore && | ||
| !hasTmdbScore | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| className="mt-1 flex min-h-4 min-w-0 items-center gap-1 whitespace-nowrap text-[10px] font-medium md:gap-2 md:text-xs" | ||
| aria-label={intl.formatMessage(messages.ratings)} | ||
| > | ||
| {hasRtAudienceScore && ( | ||
| <span className="flex min-w-0 items-center gap-0.5 md:gap-1"> | ||
| <span className="sr-only"> | ||
| {intl.formatMessage(messages.rottenTomatoesAudienceScore)}:{' '} | ||
| </span> | ||
| {rtRating.audienceRating === 'Spilled' ? ( | ||
| <RTAudRotten | ||
| className="h-3.5 w-3.5 shrink-0 md:h-4 md:w-4" | ||
| aria-hidden="true" | ||
| /> | ||
| ) : ( | ||
| <RTAudFresh | ||
| className="h-3.5 w-3.5 shrink-0 md:h-4 md:w-4" | ||
| aria-hidden="true" | ||
| /> | ||
| )} | ||
| <span>{rtRating.audienceScore}%</span> | ||
| </span> | ||
| )} | ||
| {hasRtCriticsScore && ( | ||
| <span className="flex min-w-0 items-center gap-0.5 md:gap-1"> | ||
| <span className="sr-only"> | ||
| {intl.formatMessage(messages.rottenTomatoesCriticsScore)}:{' '} | ||
| </span> | ||
| {rtRating.criticsRating === 'Rotten' ? ( | ||
| <RTRotten | ||
| className="h-3.5 w-3.5 shrink-0 md:h-4 md:w-4" | ||
| aria-hidden="true" | ||
| /> | ||
| ) : ( | ||
| <RTFresh | ||
| className="h-3.5 w-3.5 shrink-0 md:h-4 md:w-4" | ||
| aria-hidden="true" | ||
| /> | ||
| )} | ||
| <span>{rtRating.criticsScore}%</span> | ||
| </span> | ||
| )} | ||
| {hasImdbScore && ( | ||
| <span className="flex min-w-0 items-center gap-0.5 md:gap-1"> | ||
| <span className="sr-only"> | ||
| {intl.formatMessage(messages.imdbUserScore)}:{' '} | ||
| </span> | ||
| <ImdbLogo | ||
| className="h-2.5 w-4 shrink-0 md:h-3 md:w-5" | ||
| aria-hidden="true" | ||
| /> | ||
| <span>{imdbRating.criticsScore}</span> | ||
| </span> | ||
| )} | ||
| {hasTmdbScore && ( | ||
| <span className="flex min-w-0 items-center gap-0.5 md:gap-1"> | ||
| <span className="sr-only"> | ||
| {intl.formatMessage(messages.tmdbUserScore)}:{' '} | ||
| </span> | ||
| <TmdbLogo | ||
| className="h-3.5 w-4 shrink-0 md:h-4 md:w-5" | ||
| aria-hidden="true" | ||
| /> | ||
| <span>{Math.round(userScore * 10)}%</span> | ||
| </span> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default TitleCardRatings; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.