From ff506e755c9379889d5b1ffa8324787f65f7b458 Mon Sep 17 00:00:00 2001 From: ner Date: Sat, 19 Aug 2023 13:40:30 +0300 Subject: [PATCH] feat: filter can be hidden --- app/(site)/components/Filter/index.tsx | 4 +- app/(site)/components/Filter/useGenres.ts | 2 +- app/(site)/container.tsx | 21 ++++++++-- app/(site)/page.tsx | 8 +--- components/Header/index.tsx | 2 +- components/Movie/Card.tsx | 11 +++-- components/Movie/List.tsx | 3 +- components/Movie/Preview.tsx | 36 ++++------------ .../Movie/components/Torrents/index.tsx | 14 +------ components/Movie/components/Trailer/index.tsx | 2 +- components/ui/dialog.tsx | 41 ++++++++++--------- network/index.ts | 2 +- types.d.ts | 10 +++++ 13 files changed, 73 insertions(+), 83 deletions(-) diff --git a/app/(site)/components/Filter/index.tsx b/app/(site)/components/Filter/index.tsx index 1f18e07..cdcb68e 100644 --- a/app/(site)/components/Filter/index.tsx +++ b/app/(site)/components/Filter/index.tsx @@ -12,7 +12,7 @@ import useGenres from './useGenres'; export default function Filter() { const { filters, setFilters } = useFilters(); - const { data: genres } = useGenres(); + const { data: genres, isFetched } = useGenres(); const [years, setYears] = useState(filters.year || ['1990', new Date().getFullYear().toString()]); const [scores, setScores] = useState(filters.score || ['0', '10']); @@ -39,6 +39,8 @@ export default function Filter() { setter(value.map((value) => value.toString())); }; + if (!isFetched) return; + return ( <> diff --git a/app/(site)/components/Filter/useGenres.ts b/app/(site)/components/Filter/useGenres.ts index b4ee54e..5dd1356 100644 --- a/app/(site)/components/Filter/useGenres.ts +++ b/app/(site)/components/Filter/useGenres.ts @@ -5,7 +5,7 @@ import { useQuery } from '@tanstack/react-query'; import API from '@/app/(site)/api'; const useGenres = () => { return useQuery({ - suspense: true, + suspense: false, queryKey: ['genres'], queryFn: API.genres }); diff --git a/app/(site)/container.tsx b/app/(site)/container.tsx index 164b973..8bd8975 100644 --- a/app/(site)/container.tsx +++ b/app/(site)/container.tsx @@ -1,27 +1,42 @@ 'use client'; -import { ArrowLeft, ArrowRight } from 'lucide-react'; +import { useAtom } from 'jotai'; +import { atomWithStorage } from 'jotai/utils'; +import { ArrowLeft, ArrowRight, SlidersHorizontal } from 'lucide-react'; import Movie from '@/components/Movie/Preview'; import { Button } from '@/components/ui/button'; +import Filter from './components/Filter'; import useRandomMovie from './useRandomMovie'; +const isShowFilterAtom = atomWithStorage('is-show-filters', false); + export default function Container() { + const [isShowFilter, setIsShowFilter] = useAtom(isShowFilterAtom); const { movie, next, previous, hasPrevious } = useRandomMovie(); return ( <> -
+ {isShowFilter && } +
+
-
+
); } diff --git a/app/(site)/page.tsx b/app/(site)/page.tsx index 97b013e..0994b55 100644 --- a/app/(site)/page.tsx +++ b/app/(site)/page.tsx @@ -1,11 +1,5 @@ -import Filter from './components/Filter'; import Container from './container'; export default async function Random() { - return ( - <> - - - - ); + return ; } diff --git a/components/Header/index.tsx b/components/Header/index.tsx index cb46388..a7cfc1f 100644 --- a/components/Header/index.tsx +++ b/components/Header/index.tsx @@ -32,7 +32,7 @@ const Header: FC = () => { ['/search', 'Search'], ['/favorites', 'Favorites'] ].map(([path, label]) => ( - + {label} diff --git a/components/Movie/Card.tsx b/components/Movie/Card.tsx index 771f704..f5486a1 100644 --- a/components/Movie/Card.tsx +++ b/components/Movie/Card.tsx @@ -1,5 +1,4 @@ -import { FC } from 'react'; -import { Star } from 'lucide-react'; +import { FC, ReactNode } from 'react'; import Poster from '@/components/Movie/components/Poster'; import Rating from '@/components/Movie/components/Rating'; @@ -8,17 +7,17 @@ import Title from '@/components/Movie/components/Title'; interface Props { movie?: Movie; className?: string; + children?: ReactNode; } -const Card: FC = ({ movie, className }) => { +const Card: FC = ({ movie, className, children }) => { if (!movie) return null; return ( -
+
+ {children}
diff --git a/components/Movie/List.tsx b/components/Movie/List.tsx index 014822b..4b3d601 100644 --- a/components/Movie/List.tsx +++ b/components/Movie/List.tsx @@ -1,5 +1,4 @@ import { FC, Fragment, useLayoutEffect, useState } from 'react'; -import { InfiniteData, useInfiniteQuery, UseInfiniteQueryResult } from '@tanstack/react-query'; import Card from '@/components/Movie/Card'; import Preview from '@/components/Movie/Preview'; @@ -56,7 +55,7 @@ const List: FC = ({ pages, hasNextPage, fetchNextPage }) => {
{movie && ( !isOpen && setMovie(null)}> - + setMovie(null)}> = ({ movie, className, onClose }) => { return ( <>
-
- +
+ {trailer && (
setIsShowTrailer(true)} @@ -49,24 +40,9 @@ const Preview: FC = ({ movie, className, onClose }) => {
)} - +
-
-
- - - - </span> - {movie.release_date && <span className='ml-auto'>{new Date(movie.release_date).getFullYear()}</span>} - </span> - <Rating movie={movie} /> - </div> - </div> - <Separator className='my-2' /> - <div className='flex flex-col items-start grow justify-between'> - <p className='leading-7'>{movie.overview}</p> - </div> - <div className='flex w-full mt-2 gap-2'> + <div className='flex w-full gap-2'> <Button className='h-8 grow' onClick={() => setIsShowTorrents(true)}> Torrents </Button> @@ -80,6 +56,8 @@ const Preview: FC<Props> = ({ movie, className, onClose }) => { Similar </Button> </div> + <Separator className='my-2' /> + <p className='leading-7'>{movie.overview}</p> </div> </div> <Credits movieId={movie.id} onPersonClick={() => onClose?.()} /> diff --git a/components/Movie/components/Torrents/index.tsx b/components/Movie/components/Torrents/index.tsx index 20e9f21..43fb097 100644 --- a/components/Movie/components/Torrents/index.tsx +++ b/components/Movie/components/Torrents/index.tsx @@ -16,16 +16,6 @@ type Sorting = { by: ORDER; }; -function convertToBytes(size: string) { - const units: Record<string, number> = { B: 1, KiB: 1024, MiB: 1024 * 1024, GiB: 1024 * 1024 * 1024 }; - const [value, unit] = size.split(' '); - return parseFloat(value) * units[unit]; -} - -function compareSizes(size1: string, size2: string) { - return convertToBytes(size1) - convertToBytes(size2); -} - const TableHeadSortable = ({ title, by, @@ -60,8 +50,8 @@ const Torrents: FC<Props> = ({ title, onClose }) => { return ( <Dialog defaultOpen={true} onOpenChange={(isOpen) => !isOpen && onClose()}> - <DialogContent className='p-0'> - <Table> + <DialogContent className='p-0' onClose={onClose}> + <Table className='rounded-xl overflow-hidden'> <TableHeader> <TableRow> <TableHead className='px-2'>Title</TableHead> diff --git a/components/Movie/components/Trailer/index.tsx b/components/Movie/components/Trailer/index.tsx index 5d99e3b..21d40f0 100644 --- a/components/Movie/components/Trailer/index.tsx +++ b/components/Movie/components/Trailer/index.tsx @@ -11,7 +11,7 @@ const Trailer: FC<Props> = ({ trailerKey, onClose }) => { return ( trailerKey && ( <Dialog defaultOpen={true} onOpenChange={(isOpen) => !isOpen && onClose()}> - <DialogContent className='block p-0 min-h-[400px] aspect-w-16 aspect-h-9'> + <DialogContent className='block p-0 min-h-[400px] border-none aspect-w-16 aspect-h-9' onClose={onClose}> <iframe allow='autoplay' className='border-none rounded-lg' diff --git a/components/ui/dialog.tsx b/components/ui/dialog.tsx index ea3a4d3..e12d022 100644 --- a/components/ui/dialog.tsx +++ b/components/ui/dialog.tsx @@ -1,6 +1,7 @@ 'use client'; import * as React from 'react'; +import { useState } from 'react'; import * as DialogPrimitive from '@radix-ui/react-dialog'; import { cn } from '@/lib/utils'; @@ -21,7 +22,7 @@ const DialogOverlay = React.forwardRef< <DialogPrimitive.Overlay ref={ref} className={cn( - 'fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0', + 'fixed inset-0 z-50 bg-black/70 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0', className )} {...props} @@ -31,24 +32,26 @@ DialogOverlay.displayName = DialogPrimitive.Overlay.displayName; const DialogContent = React.forwardRef< React.ElementRef<typeof DialogPrimitive.Content>, - React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> ->(({ className, children, ...props }, ref) => ( - <DialogPortal> - <DialogOverlay className='overflow-auto flex justify-center'> - <DialogPrimitive.Content - ref={ref} - className={cn( - 'absolute top-[20vh] max-w-[1000px] w-[calc(100%-24px)] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 rounded-lg', - className - )} - {...props} - > - {children} - <div className='absolute w-full block h-[20vh] pointer-events-none' /> - </DialogPrimitive.Content> - </DialogOverlay> - </DialogPortal> -)); + React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & { onClose?: () => void } +>(({ className, children, ...props }, ref) => { + return ( + <DialogPortal> + <DialogOverlay className='overflow-auto flex justify-center'> + <DialogPrimitive.Content + ref={ref} + className={cn( + 'absolute top-[20vh] max-w-[1000px] w-[calc(100%-24px)] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 rounded-xl', + className + )} + {...props} + > + {children} + <div className='absolute w-full block h-[20vh] pointer-events-none' /> + </DialogPrimitive.Content> + </DialogOverlay> + </DialogPortal> + ); +}); DialogContent.displayName = DialogPrimitive.Content.displayName; const DialogHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => ( diff --git a/network/index.ts b/network/index.ts index b884aae..b21ef77 100644 --- a/network/index.ts +++ b/network/index.ts @@ -1,7 +1,7 @@ import axios, { AxiosResponse } from 'axios'; const client = axios.create({ - baseURL: `${process.env.NODE_ENV === 'development' ? 'http://' : 'https://'}${ + baseURL: `${process.env.NODE_ENV === 'development' ? 'http://' : 'http://'}${ process.env.NEXT_PUBLIC_MOVIE_ADVISOR_BASE }/api`, withCredentials: true diff --git a/types.d.ts b/types.d.ts index cb0656a..5bbb3a2 100644 --- a/types.d.ts +++ b/types.d.ts @@ -116,3 +116,13 @@ interface Category { id: string; name: string; } + +interface Poster { + aspect_ratio: number; + height: number; + iso_639_1: null; + file_path: string; + vote_average: number; + vote_count: number; + width: number; +}