-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDashboardGalleryScreen.tsx
85 lines (78 loc) · 3.07 KB
/
DashboardGalleryScreen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Navigate, useLoaderData, useNavigate } from 'react-router';
import { ProfileArrayCols } from '../../types';
import {
useGetProfileArray,
useProfileActions,
useProfileContext,
useSessionContext,
} from '../../hooks';
import { Button, ShowPoster } from '../../components';
import { DashboardGalleryLoader } from '../loaders';
import Typ from '@mui/material/Typography';
import ArrowBack from '@mui/icons-material/ArrowBack';
import DashboardEmptyGallery from './DashboardEmptyGallery';
export async function loader({ request }: { request: Request }): Promise<string> {
// get the end of the path from the URL
const url = new URL(request.url);
const path = url.pathname.split('/').pop();
if (!path) {
throw new Response('Bad Request', { status: 400 });
}
return path as ProfileArrayCols;
}
const DashboardGalleryScreen: React.FC = () => {
const { profile, setProfile } = useProfileContext();
const { session } = useSessionContext();
const profileActions = useProfileActions(profile, setProfile);
const navigate = useNavigate();
const path: ProfileArrayCols = useLoaderData() as ProfileArrayCols;
const { data, loading } = useGetProfileArray(path);
// If the user is not logged in, redirect to login
if (!session) {
return <Navigate to={'/login'} replace />;
}
// TODO: If profile does not return after a few seconds,
// we should assume the user is not logged in and redirect to an auth page
if (!profile || loading) return <DashboardGalleryLoader path={path} />;
/**
* Return the page title based on the URL path
*/
const getTitle = (path: ProfileArrayCols): string => {
switch (path) {
case 'queue':
return 'watch queue';
case 'favorites':
return 'favorites';
case 'watched':
return 'watched list';
}
};
return (
<div className='m-4' data-testid='dashboard-gallery-screen'>
<Typ variant='h5' sx={{ padding: 2 }}>{`${profile.username}'s ${getTitle(path)}`}</Typ>
<Button
title='Dashboard'
StartIcon={ArrowBack}
onClick={() => navigate('/dashboard')}
/>
{data && data.length > 0 ? (
<div className='grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 p-6'>
{data.map((item, i) => (
<ShowPoster
key={i}
details={item}
profile={profile}
profileActions={profileActions}
showQueueButton={path === 'queue'}
showFavoritesButton={path === 'favorites'}
showWatchedButton={path === 'watched'}
/>
))}
</div>
) : (
<DashboardEmptyGallery title={getTitle(path)} />
)}
</div>
);
};
export default DashboardGalleryScreen;