import { ArrowCircleRightIcon } from '@heroicons/react/outline'; import Link from 'next/link'; import { useRouter } from 'next/router'; import { useCallback, useEffect, useState } from 'react'; import { defineMessages, FormattedNumber, useIntl } from 'react-intl'; import useSWR from 'swr'; import type { QuotaResponse, UserRequestsResponse, UserWatchDataResponse, } from '../../../server/interfaces/api/userInterfaces'; import type { MovieDetails } from '../../../server/models/Movie'; import type { TvDetails } from '../../../server/models/Tv'; import { Permission, UserType, useUser } from '../../hooks/useUser'; import Error from '../../pages/_error'; import ImageFader from '../Common/ImageFader'; import LoadingSpinner from '../Common/LoadingSpinner'; import PageTitle from '../Common/PageTitle'; import ProgressCircle from '../Common/ProgressCircle'; import RequestCard from '../RequestCard'; import Slider from '../Slider'; import TmdbTitleCard from '../TitleCard/TmdbTitleCard'; import ProfileHeader from './ProfileHeader'; const messages = defineMessages({ recentrequests: 'Recent Requests', norequests: 'No requests.', limit: '{remaining} of {limit}', requestsperdays: '{limit} remaining', unlimited: 'Unlimited', totalrequests: 'Total Requests', pastdays: '{type} (past {days} days)', movierequests: 'Movie Requests', seriesrequest: 'Series Requests', recentlywatched: 'Recently Watched', }); type MediaTitle = MovieDetails | TvDetails; const UserProfile = () => { const intl = useIntl(); const router = useRouter(); const { user, error } = useUser({ id: Number(router.query.userId), }); const { user: currentUser, hasPermission: currentHasPermission } = useUser(); const [availableTitles, setAvailableTitles] = useState< Record >({}); const { data: requests, error: requestError } = useSWR( user && (user.id === currentUser?.id || currentHasPermission( [Permission.MANAGE_REQUESTS, Permission.REQUEST_VIEW], { type: 'or' } )) ? `/api/v1/user/${user?.id}/requests?take=10&skip=0` : null ); const { data: quota } = useSWR( user && (user.id === currentUser?.id || currentHasPermission( [Permission.MANAGE_USERS, Permission.MANAGE_REQUESTS], { type: 'and' } )) ? `/api/v1/user/${user.id}/quota` : null ); const { data: watchData } = useSWR( user?.userType === UserType.PLEX && (user.id === currentUser?.id || currentHasPermission(Permission.ADMIN)) ? `/api/v1/user/${user.id}/watch_data` : null ); const updateAvailableTitles = useCallback( (requestId: number, mediaTitle: MediaTitle) => { setAvailableTitles((titles) => ({ ...titles, [requestId]: mediaTitle, })); }, [] ); useEffect(() => { setAvailableTitles({}); }, [user?.id]); if (!user && !error) { return ; } if (!user) { return ; } return ( <> {Object.keys(availableTitles).length > 0 && (
media.backdropPath) .map( (media) => `https://image.tmdb.org/t/p/w1920_and_h800_multi_faces/${media.backdropPath}` ) .slice(0, 6)} />
)} {quota && (user.id === currentUser?.id || currentHasPermission( [Permission.MANAGE_USERS, Permission.MANAGE_REQUESTS], { type: 'and' } )) && (
{intl.formatMessage(messages.totalrequests)}
{quota.movie.limit ? intl.formatMessage(messages.pastdays, { type: intl.formatMessage(messages.movierequests), days: quota?.movie.days, }) : intl.formatMessage(messages.movierequests)}
{quota.movie.limit ? ( <>
{intl.formatMessage(messages.requestsperdays, { limit: ( {intl.formatMessage(messages.limit, { remaining: quota.movie.remaining, limit: quota.movie.limit, })} ), })}
) : ( {intl.formatMessage(messages.unlimited)} )}
{quota.tv.limit ? intl.formatMessage(messages.pastdays, { type: intl.formatMessage(messages.seriesrequest), days: quota?.tv.days, }) : intl.formatMessage(messages.seriesrequest)}
{quota.tv.limit ? ( <>
{intl.formatMessage(messages.requestsperdays, { limit: ( {intl.formatMessage(messages.limit, { remaining: quota.tv.remaining, limit: quota.tv.limit, })} ), })}
) : ( {intl.formatMessage(messages.unlimited)} )}
)} {(user.id === currentUser?.id || currentHasPermission( [Permission.MANAGE_REQUESTS, Permission.REQUEST_VIEW], { type: 'or' } )) && ( <> ( ))} placeholder={} emptyMessage={intl.formatMessage(messages.norequests)} /> )} {(user.id === currentUser?.id || currentHasPermission(Permission.ADMIN)) && !!watchData?.recentlyWatched.length && ( <>
{intl.formatMessage(messages.recentlywatched)}
( ))} /> )} ); }; export default UserProfile;