From 2bcc3e3419de9c83cd1330ba8518f9eacf776bfe Mon Sep 17 00:00:00 2001 From: TheCatLady <52870424+TheCatLady@users.noreply.github.com> Date: Thu, 18 Feb 2021 01:40:14 -0500 Subject: [PATCH] refactor(ui): Create PlayButton component (#946) --- src/components/Common/PlayButton/index.tsx | 68 +++++++++ src/components/MovieDetails/index.tsx | 153 ++++++-------------- src/components/TvDetails/index.tsx | 157 ++++++--------------- 3 files changed, 160 insertions(+), 218 deletions(-) create mode 100644 src/components/Common/PlayButton/index.tsx diff --git a/src/components/Common/PlayButton/index.tsx b/src/components/Common/PlayButton/index.tsx new file mode 100644 index 000000000..5a513d748 --- /dev/null +++ b/src/components/Common/PlayButton/index.tsx @@ -0,0 +1,68 @@ +import React from 'react'; +import ButtonWithDropdown from '../ButtonWithDropdown'; + +interface PlayButtonProps { + links: PlayButtonLink[]; +} + +export interface PlayButtonLink { + text: string; + url: string; +} + +const PlayButton: React.FC = ({ links }) => { + if (!links || !links.length) { + return null; + } + + return ( + + + + + + {links[0].text} + + } + onClick={() => { + window.open(links[0].url, '_blank'); + }} + > + {links.length > 1 && + links.slice(1).map((link, i) => { + return ( + { + window.open(link.url, '_blank'); + }} + buttonType="ghost" + > + {link.text} + + ); + })} + + ); +}; + +export default PlayButton; diff --git a/src/components/MovieDetails/index.tsx b/src/components/MovieDetails/index.tsx index 30159ec16..0f35c6c42 100644 --- a/src/components/MovieDetails/index.tsx +++ b/src/components/MovieDetails/index.tsx @@ -1,6 +1,5 @@ import React, { useState, useContext, useMemo } from 'react'; import { - FormattedMessage, defineMessages, FormattedNumber, FormattedDate, @@ -34,9 +33,9 @@ import RequestButton from '../RequestButton'; import MediaSlider from '../MediaSlider'; import ConfirmButton from '../Common/ConfirmButton'; import DownloadBlock from '../DownloadBlock'; -import ButtonWithDropdown from '../Common/ButtonWithDropdown'; import PageTitle from '../Common/PageTitle'; import useSettings from '../../hooks/useSettings'; +import PlayButton, { PlayButtonLink } from '../Common/PlayButton'; const messages = defineMessages({ releasedate: 'Release Date', @@ -110,11 +109,39 @@ const MovieDetails: React.FC = ({ movie }) => { return ; } + const mediaLinks: PlayButtonLink[] = []; + + if (data.mediaInfo?.plexUrl) { + mediaLinks.push({ + text: intl.formatMessage(messages.playonplex), + url: data.mediaInfo?.plexUrl, + }); + } + + if ( + data.mediaInfo?.plexUrl4k && + hasPermission([Permission.REQUEST_4K, Permission.REQUEST_4K_MOVIE], { + type: 'or', + }) + ) { + mediaLinks.push({ + text: intl.formatMessage(messages.play4konplex), + url: data.mediaInfo?.plexUrl4k, + }); + } + const trailerUrl = data.relatedVideos ?.filter((r) => r.type === 'Trailer') .sort((a, b) => a.size - b.size) .pop()?.url; + if (trailerUrl) { + mediaLinks.push({ + text: intl.formatMessage(messages.watchtrailer), + url: trailerUrl, + }); + } + const deleteMedia = async () => { if (data?.mediaInfo?.id) { await axios.delete(`/api/v1/media/${data?.mediaInfo?.id}`); @@ -395,95 +422,9 @@ const MovieDetails: React.FC = ({ movie }) => {
- {(trailerUrl || - data.mediaInfo?.plexUrl || - data.mediaInfo?.plexUrl4k) && ( -
- - - - - - - {data.mediaInfo?.plexUrl - ? intl.formatMessage(messages.playonplex) - : data.mediaInfo?.plexUrl4k && - (hasPermission(Permission.REQUEST_4K) || - hasPermission(Permission.REQUEST_4K_MOVIE)) - ? intl.formatMessage(messages.playonplex) - : intl.formatMessage(messages.watchtrailer)} - - - } - onClick={() => { - if (data.mediaInfo?.plexUrl) { - window.open(data.mediaInfo?.plexUrl, '_blank'); - } else if (data.mediaInfo?.plexUrl4k) { - window.open(data.mediaInfo?.plexUrl4k, '_blank'); - } else if (trailerUrl) { - window.open(trailerUrl, '_blank'); - } - }} - > - {( - trailerUrl - ? data.mediaInfo?.plexUrl || - (data.mediaInfo?.plexUrl4k && - (hasPermission(Permission.REQUEST_4K) || - hasPermission(Permission.REQUEST_4K_MOVIE))) - : data.mediaInfo?.plexUrl && - data.mediaInfo?.plexUrl4k && - (hasPermission(Permission.REQUEST_4K) || - hasPermission(Permission.REQUEST_4K_MOVIE)) - ) ? ( - <> - {data.mediaInfo?.plexUrl && - data.mediaInfo?.plexUrl4k && - (hasPermission(Permission.REQUEST_4K) || - hasPermission(Permission.REQUEST_4K_MOVIE)) && ( - { - window.open(data.mediaInfo?.plexUrl4k, '_blank'); - }} - buttonType="ghost" - > - {intl.formatMessage(messages.play4konplex)} - - )} - {trailerUrl && ( - { - window.open(trailerUrl, '_blank'); - }} - buttonType="ghost" - > - {intl.formatMessage(messages.watchtrailer)} - - )} - - ) : null} - -
- )} +
+ +
= ({ movie }) => {

- + {intl.formatMessage(messages.overview)}

{data.overview @@ -595,11 +536,11 @@ const MovieDetails: React.FC = ({ movie }) => {

)}
- {(data.voteCount || - (ratingData?.criticsRating && ratingData?.criticsScore) || - (ratingData?.audienceRating && ratingData?.audienceScore)) && ( + {(!!data.voteCount || + (ratingData?.criticsRating && !!ratingData?.criticsScore) || + (ratingData?.audienceRating && !!ratingData?.audienceScore)) && (
- {ratingData?.criticsRating && ratingData?.criticsScore && ( + {ratingData?.criticsRating && !!ratingData?.criticsScore && ( <> {ratingData.criticsRating === 'Rotten' ? ( @@ -613,7 +554,7 @@ const MovieDetails: React.FC = ({ movie }) => { )} - {ratingData?.audienceRating && ratingData?.audienceScore && ( + {ratingData?.audienceRating && !!ratingData?.audienceScore && ( <> {ratingData.audienceRating === 'Spilled' ? ( @@ -627,7 +568,7 @@ const MovieDetails: React.FC = ({ movie }) => { )} - {data.voteCount > 0 && ( + {!!data.voteCount && ( <> @@ -642,7 +583,7 @@ const MovieDetails: React.FC = ({ movie }) => { {data.releaseDate && (
- + {intl.formatMessage(messages.releasedate)} = ({ movie }) => { )}
- + {intl.formatMessage(messages.status)} {data.status} @@ -665,7 +606,7 @@ const MovieDetails: React.FC = ({ movie }) => { {data.revenue > 0 && (
- + {intl.formatMessage(messages.revenue)} = ({ movie }) => { {data.budget > 0 && (
- + {intl.formatMessage(messages.budget)} = ({ movie }) => { ) && (
- + {intl.formatMessage(messages.originallanguage)} { @@ -709,7 +650,7 @@ const MovieDetails: React.FC = ({ movie }) => { {data.productionCompanies[0] && (
- + {intl.formatMessage(messages.studio)} {data.productionCompanies[0]?.name} @@ -735,9 +676,7 @@ const MovieDetails: React.FC = ({ movie }) => {
- - - + {intl.formatMessage(messages.cast)} = ({ tv }) => { return ; } + const mediaLinks: PlayButtonLink[] = []; + + if (data.mediaInfo?.plexUrl) { + mediaLinks.push({ + text: intl.formatMessage(messages.playonplex), + url: data.mediaInfo?.plexUrl, + }); + } + + if ( + data.mediaInfo?.plexUrl4k && + hasPermission([Permission.REQUEST_4K, Permission.REQUEST_4K_TV], { + type: 'or', + }) + ) { + mediaLinks.push({ + text: intl.formatMessage(messages.play4konplex), + url: data.mediaInfo?.plexUrl4k, + }); + } + const trailerUrl = data.relatedVideos ?.filter((r) => r.type === 'Trailer') .sort((a, b) => a.size - b.size) .pop()?.url; + if (trailerUrl) { + mediaLinks.push({ + text: intl.formatMessage(messages.watchtrailer), + url: trailerUrl, + }); + } + const deleteMedia = async () => { if (data?.mediaInfo?.id) { await axios.delete(`/api/v1/media/${data?.mediaInfo?.id}`); @@ -423,95 +446,9 @@ const TvDetails: React.FC = ({ tv }) => {
- {(trailerUrl || - data.mediaInfo?.plexUrl || - data.mediaInfo?.plexUrl4k) && ( -
- - - - - - - {data.mediaInfo?.plexUrl - ? intl.formatMessage(messages.playonplex) - : data.mediaInfo?.plexUrl4k && - (hasPermission(Permission.REQUEST_4K) || - hasPermission(Permission.REQUEST_4K_TV)) - ? intl.formatMessage(messages.play4konplex) - : intl.formatMessage(messages.watchtrailer)} - - - } - onClick={() => { - if (data.mediaInfo?.plexUrl) { - window.open(data.mediaInfo?.plexUrl, '_blank'); - } else if (data.mediaInfo?.plexUrl4k) { - window.open(data.mediaInfo?.plexUrl4k, '_blank'); - } else if (trailerUrl) { - window.open(trailerUrl, '_blank'); - } - }} - > - {( - trailerUrl - ? data.mediaInfo?.plexUrl || - (data.mediaInfo?.plexUrl4k && - (hasPermission(Permission.REQUEST_4K) || - hasPermission(Permission.REQUEST_4K_TV))) - : data.mediaInfo?.plexUrl && - data.mediaInfo?.plexUrl4k && - (hasPermission(Permission.REQUEST_4K) || - hasPermission(Permission.REQUEST_4K_TV)) - ) ? ( - <> - {data.mediaInfo?.plexUrl && - data.mediaInfo?.plexUrl4k && - (hasPermission(Permission.REQUEST_4K) || - hasPermission(Permission.REQUEST_4K_TV)) ? ( - { - window.open(data.mediaInfo?.plexUrl4k, '_blank'); - }} - buttonType="ghost" - > - {intl.formatMessage(messages.play4konplex)} - - ) : null} - {trailerUrl ? ( - { - window.open(trailerUrl, '_blank'); - }} - buttonType="ghost" - > - {intl.formatMessage(messages.watchtrailer)} - - ) : null} - - ) : null} - -
- )} +
+ +
= ({ tv }) => {

- + {intl.formatMessage(messages.overview)}

{data.overview @@ -618,11 +555,11 @@ const TvDetails: React.FC = ({ tv }) => {

- {(data.voteCount || - (ratingData?.criticsRating && ratingData?.criticsScore) || - (ratingData?.audienceRating && ratingData?.audienceScore)) && ( + {(!!data.voteCount || + (ratingData?.criticsRating && !!ratingData?.criticsScore) || + (ratingData?.audienceRating && !!ratingData?.audienceScore)) && (
- {ratingData?.criticsRating && ratingData?.criticsScore && ( + {ratingData?.criticsRating && !!ratingData?.criticsScore && ( <> {ratingData.criticsRating === 'Rotten' ? ( @@ -636,7 +573,7 @@ const TvDetails: React.FC = ({ tv }) => { )} - {ratingData?.audienceRating && ratingData?.audienceScore && ( + {ratingData?.audienceRating && !!ratingData?.audienceScore && ( <> {ratingData.audienceRating === 'Spilled' ? ( @@ -650,7 +587,7 @@ const TvDetails: React.FC = ({ tv }) => { )} - {data.voteCount > 0 && ( + {!!data.voteCount && ( <> @@ -677,7 +614,7 @@ const TvDetails: React.FC = ({ tv }) => { {data.firstAirDate && (