diff --git a/components/Header/components/Search/api.ts b/components/Header/components/Search/api.ts new file mode 100644 index 0000000..af69af7 --- /dev/null +++ b/components/Header/components/Search/api.ts @@ -0,0 +1,16 @@ +import client from '@/network'; + +const API = { + search: { + byTitle({ query, page }: { query: string; page: number }): Promise { + return client.get('/search', { + params: { + query, + page + } + }); + } + } +}; + +export default API; diff --git a/components/Header/components/Search/index.tsx b/components/Header/components/Search/index.tsx new file mode 100644 index 0000000..b503493 --- /dev/null +++ b/components/Header/components/Search/index.tsx @@ -0,0 +1,84 @@ +import { ChangeEvent, useRef, useState } from 'react'; +import { Search as SearchIcon, XCircle } from 'lucide-react'; + +import List from '@/components/Movie/List'; +import { Button } from '@/components/ui/button'; +import { Dialog, DialogContent } from '@/components/ui/dialog'; +import { Input } from '@/components/ui/input'; + +import useSearch from './useSearch'; + +const Search = () => { + const [isShow, setIsShow] = useState(false); + const [title, setTitle] = useState(''); + const [query, setQuery] = useState(''); + const timeout = useRef(); + const inputRef = useRef(null); + + const { data: results, hasNextPage, fetchNextPage } = useSearch({ query }); + + const handleChangeTitle = ({ target: { value } }: ChangeEvent) => { + setTitle(value); + + clearTimeout(timeout.current); + timeout.current = setTimeout(() => setQuery(value.trim()), 500); + }; + + const handleReset = () => { + setQuery(''); + setTitle(''); + inputRef.current?.focus(); + }; + + const handleClose = () => { + setIsShow(false); + handleReset(); + }; + + return ( + <> + + {isShow && ( + + setIsShow(false)}> +
+ + +
+ + {Boolean(results?.pages.length) && ( +
+ +
+ )} +
+
+ )} + + ); +}; + +export default Search; diff --git a/components/Header/components/Search/useSearch.ts b/components/Header/components/Search/useSearch.ts new file mode 100644 index 0000000..31514dc --- /dev/null +++ b/components/Header/components/Search/useSearch.ts @@ -0,0 +1,19 @@ +import { useInfiniteQuery, useQuery } from '@tanstack/react-query'; + +import API from './api'; + +const useSearch = ({ query }: { query: string }) => { + return useInfiniteQuery({ + queryKey: ['search', query], + queryFn: ({ pageParam: page }) => API.search.byTitle({ query, page }), + enabled: Boolean(query), + defaultPageParam: 1, + getNextPageParam({ page, total_pages }) { + if (page === total_pages) return undefined; + + return page + 1; + } + }); +}; + +export default useSearch; diff --git a/components/Header/index.tsx b/components/Header/index.tsx index a7cfc1f..37c8ca2 100644 --- a/components/Header/index.tsx +++ b/components/Header/index.tsx @@ -10,6 +10,8 @@ import { Button } from '@/components/ui/button'; import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { useTheme } from '@/hooks/useTheme'; +import Search from './components/Search'; + const Header: FC = () => { const path = usePathname(); const [theme, setTheme] = useTheme(); @@ -20,16 +22,18 @@ const Header: FC = () => { Movie advisor - +
+ + +
- + {[ ['/', 'Random'], ['/top', 'Top'], - ['/search', 'Search'], ['/favorites', 'Favorites'] ].map(([path, label]) => ( diff --git a/components/Movie/List.tsx b/components/Movie/List.tsx index 4b3d601..f878fbe 100644 --- a/components/Movie/List.tsx +++ b/components/Movie/List.tsx @@ -9,9 +9,11 @@ interface Props { pages: Array; hasNextPage?: boolean; fetchNextPage?: () => void; + withBottomGap?: boolean; + onPreviewClose?: () => void; } -const List: FC = ({ pages, hasNextPage, fetchNextPage }) => { +const List: FC = ({ pages, hasNextPage, fetchNextPage, withBottomGap = true, onPreviewClose }) => { const [movie, setMovie] = useState(null); const [loader, setLoader] = useState(); @@ -52,7 +54,7 @@ const List: FC = ({ pages, hasNextPage, fetchNextPage }) => { ))} -
+ {withBottomGap &&
} {movie && ( !isOpen && setMovie(null)}> setMovie(null)}> @@ -63,6 +65,7 @@ const List: FC = ({ pages, hasNextPage, fetchNextPage }) => { onClose={() => { setMovie(null); window.scrollTo({ top: 0, behavior: 'smooth' }); + onPreviewClose?.(); }} /> diff --git a/components/Movie/components/Torrents/index.tsx b/components/Movie/components/Torrents/index.tsx index 43fb097..16e50e7 100644 --- a/components/Movie/components/Torrents/index.tsx +++ b/components/Movie/components/Torrents/index.tsx @@ -1,10 +1,13 @@ -import { Dispatch, FC, SetStateAction, useState } from 'react'; +import React, { Dispatch, FC, SetStateAction, useState } from 'react'; import { ArrowDownIcon, CaretSortIcon } from '@radix-ui/react-icons'; -import { Loader, Magnet } from 'lucide-react'; +import { useAtom } from 'jotai'; +import { atomWithStorage } from 'jotai/utils'; +import { Cast, Loader, Magnet } from 'lucide-react'; import useTorrents from '@/app/(site)/useTorrents'; import { ORDER } from '@/app/api/torrents/parsers/pirate-bay'; import { Dialog, DialogContent } from '@/components/ui/dialog'; +import { Input } from '@/components/ui/input'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'; interface Props { @@ -44,10 +47,22 @@ const TableHeadSortable = ({ ); }; +const hostAtom = atomWithStorage('magnet-host', ''); + const Torrents: FC = ({ title, onClose }) => { + const [host, setHost] = useAtom(hostAtom); + const [isShowHostInput, setIsShowHostInput] = useState(false); const [sorting, setSorting] = useState({ by: ORDER.seeders }); + const { data: torrents, isLoading, isFetched } = useTorrents({ query: title, order: sorting.by }); + const handleCast = (magnet: string) => (event: React.MouseEvent) => { + event.preventDefault(); + + const link = window.open(`http://${host}:65220/playuri?uri=${magnet}`, '_blank'); + setTimeout(() => link?.close(), 100); + }; + return ( !isOpen && onClose()}> @@ -57,7 +72,9 @@ const Torrents: FC = ({ title, onClose }) => { Title - Magnet + setIsShowHostInput(true)}> + Magnet + @@ -69,12 +86,20 @@ const Torrents: FC = ({ title, onClose }) => { {torrent.seeders} - - - +
+ {host ? ( + + + + ) : ( + + + + )} +
))} @@ -98,6 +123,19 @@ const Torrents: FC = ({ title, onClose }) => { )}
+ {isShowHostInput && ( + setIsShowHostInput(false)}> + setIsShowHostInput(false)}> + setHost(value)} + onKeyDown={({ key }) => key === 'Enter' && setIsShowHostInput(false)} + /> + + + )}
);