feat: open magnets in elementum
This commit is contained in:
1 parent
626586939a
commit
e4788ae384
6 files changed
+173
-9
No files matched your search
@@ -0,0 +1,16 @@
|
|||||||
|
import client from '@/network';
|
||||||
|
|
||||||
|
const API = {
|
||||||
|
search: {
|
||||||
|
byTitle({ query, page }: { query: string; page: number }): Promise<MovieDBResponse> {
|
||||||
|
return client.get('/search', {
|
||||||
|
params: {
|
||||||
|
query,
|
||||||
|
page
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default API;
|
||||||
@@ -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<NodeJS.Timeout>();
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
|
const { data: results, hasNextPage, fetchNextPage } = useSearch({ query });
|
||||||
|
|
||||||
|
const handleChangeTitle = ({ target: { value } }: ChangeEvent<HTMLInputElement>) => {
|
||||||
|
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 (
|
||||||
|
<>
|
||||||
|
<Button variant='outline' size='icon' onClick={() => setIsShow(true)}>
|
||||||
|
<SearchIcon />
|
||||||
|
</Button>
|
||||||
|
{isShow && (
|
||||||
|
<Dialog defaultOpen={true} onOpenChange={handleClose}>
|
||||||
|
<DialogContent className='block p-3 max-w-[950px]' onClose={() => setIsShow(false)}>
|
||||||
|
<div className='relative'>
|
||||||
|
<Input
|
||||||
|
ref={inputRef}
|
||||||
|
autoFocus
|
||||||
|
placeholder='Start entering title...'
|
||||||
|
value={title}
|
||||||
|
onChange={handleChangeTitle}
|
||||||
|
className='pr-10'
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
variant='outline'
|
||||||
|
size='icon'
|
||||||
|
onClick={handleReset}
|
||||||
|
disabled={!query.length}
|
||||||
|
className='absolute right-0 top-0'
|
||||||
|
>
|
||||||
|
<XCircle />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{Boolean(results?.pages.length) && (
|
||||||
|
<div className='mt-3'>
|
||||||
|
<List
|
||||||
|
pages={results!.pages}
|
||||||
|
fetchNextPage={fetchNextPage}
|
||||||
|
hasNextPage={hasNextPage}
|
||||||
|
withBottomGap={false}
|
||||||
|
onPreviewClose={handleClose}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Search;
|
||||||
@@ -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;
|
||||||
@@ -10,6 +10,8 @@ import { Button } from '@/components/ui/button';
|
|||||||
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||||
import { useTheme } from '@/hooks/useTheme';
|
import { useTheme } from '@/hooks/useTheme';
|
||||||
|
|
||||||
|
import Search from './components/Search';
|
||||||
|
|
||||||
const Header: FC = () => {
|
const Header: FC = () => {
|
||||||
const path = usePathname();
|
const path = usePathname();
|
||||||
const [theme, setTheme] = useTheme();
|
const [theme, setTheme] = useTheme();
|
||||||
@@ -20,16 +22,18 @@ const Header: FC = () => {
|
|||||||
<Link href='/'>
|
<Link href='/'>
|
||||||
<Badge className='whitespace-nowrap'>Movie advisor</Badge>
|
<Badge className='whitespace-nowrap'>Movie advisor</Badge>
|
||||||
</Link>
|
</Link>
|
||||||
|
<div className='flex items-center gap-2'>
|
||||||
<Button variant='outline' size='icon' onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}>
|
<Button variant='outline' size='icon' onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}>
|
||||||
{theme === 'light' ? <Moon /> : <Sun />}
|
{theme === 'light' ? <Moon /> : <Sun />}
|
||||||
</Button>
|
</Button>
|
||||||
|
<Search />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Tabs value={path} className='flex-grow mt-2'>
|
<Tabs value={path} className='flex-grow mt-2'>
|
||||||
<TabsList className='grid w-full grid-cols-4'>
|
<TabsList className='grid w-full grid-cols-3'>
|
||||||
{[
|
{[
|
||||||
['/', 'Random'],
|
['/', 'Random'],
|
||||||
['/top', 'Top'],
|
['/top', 'Top'],
|
||||||
['/search', 'Search'],
|
|
||||||
['/favorites', 'Favorites']
|
['/favorites', 'Favorites']
|
||||||
].map(([path, label]) => (
|
].map(([path, label]) => (
|
||||||
<Link href={path} className='w-full' key={path} prefetch>
|
<Link href={path} className='w-full' key={path} prefetch>
|
||||||
|
|||||||
@@ -9,9 +9,11 @@ interface Props {
|
|||||||
pages: Array<MovieDBResponse>;
|
pages: Array<MovieDBResponse>;
|
||||||
hasNextPage?: boolean;
|
hasNextPage?: boolean;
|
||||||
fetchNextPage?: () => void;
|
fetchNextPage?: () => void;
|
||||||
|
withBottomGap?: boolean;
|
||||||
|
onPreviewClose?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const List: FC<Props> = ({ pages, hasNextPage, fetchNextPage }) => {
|
const List: FC<Props> = ({ pages, hasNextPage, fetchNextPage, withBottomGap = true, onPreviewClose }) => {
|
||||||
const [movie, setMovie] = useState<Movie | null>(null);
|
const [movie, setMovie] = useState<Movie | null>(null);
|
||||||
const [loader, setLoader] = useState<HTMLLIElement>();
|
const [loader, setLoader] = useState<HTMLLIElement>();
|
||||||
|
|
||||||
@@ -52,7 +54,7 @@ const List: FC<Props> = ({ pages, hasNextPage, fetchNextPage }) => {
|
|||||||
</Fragment>
|
</Fragment>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
<div className='h-4 w-full' />
|
{withBottomGap && <div className='h-4 w-full' />}
|
||||||
{movie && (
|
{movie && (
|
||||||
<Dialog defaultOpen={true} onOpenChange={(isOpen) => !isOpen && setMovie(null)}>
|
<Dialog defaultOpen={true} onOpenChange={(isOpen) => !isOpen && setMovie(null)}>
|
||||||
<DialogContent className='block p-0' onClose={() => setMovie(null)}>
|
<DialogContent className='block p-0' onClose={() => setMovie(null)}>
|
||||||
@@ -63,6 +65,7 @@ const List: FC<Props> = ({ pages, hasNextPage, fetchNextPage }) => {
|
|||||||
onClose={() => {
|
onClose={() => {
|
||||||
setMovie(null);
|
setMovie(null);
|
||||||
window.scrollTo({ top: 0, behavior: 'smooth' });
|
window.scrollTo({ top: 0, behavior: 'smooth' });
|
||||||
|
onPreviewClose?.();
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
|
|||||||
@@ -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 { 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 useTorrents from '@/app/(site)/useTorrents';
|
||||||
import { ORDER } from '@/app/api/torrents/parsers/pirate-bay';
|
import { ORDER } from '@/app/api/torrents/parsers/pirate-bay';
|
||||||
import { Dialog, DialogContent } from '@/components/ui/dialog';
|
import { Dialog, DialogContent } from '@/components/ui/dialog';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -44,10 +47,22 @@ const TableHeadSortable = ({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const hostAtom = atomWithStorage<string>('magnet-host', '');
|
||||||
|
|
||||||
const Torrents: FC<Props> = ({ title, onClose }) => {
|
const Torrents: FC<Props> = ({ title, onClose }) => {
|
||||||
|
const [host, setHost] = useAtom(hostAtom);
|
||||||
|
const [isShowHostInput, setIsShowHostInput] = useState(false);
|
||||||
const [sorting, setSorting] = useState<Sorting>({ by: ORDER.seeders });
|
const [sorting, setSorting] = useState<Sorting>({ by: ORDER.seeders });
|
||||||
|
|
||||||
const { data: torrents, isLoading, isFetched } = useTorrents({ query: title, order: sorting.by });
|
const { data: torrents, isLoading, isFetched } = useTorrents({ query: title, order: sorting.by });
|
||||||
|
|
||||||
|
const handleCast = (magnet: string) => (event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
const link = window.open(`http://${host}:65220/playuri?uri=${magnet}`, '_blank');
|
||||||
|
setTimeout(() => link?.close(), 100);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog defaultOpen={true} onOpenChange={(isOpen) => !isOpen && onClose()}>
|
<Dialog defaultOpen={true} onOpenChange={(isOpen) => !isOpen && onClose()}>
|
||||||
<DialogContent className='p-0' onClose={onClose}>
|
<DialogContent className='p-0' onClose={onClose}>
|
||||||
@@ -57,7 +72,9 @@ const Torrents: FC<Props> = ({ title, onClose }) => {
|
|||||||
<TableHead className='px-2'>Title</TableHead>
|
<TableHead className='px-2'>Title</TableHead>
|
||||||
<TableHeadSortable title='Size' by={ORDER.size} value={sorting} onChange={setSorting} />
|
<TableHeadSortable title='Size' by={ORDER.size} value={sorting} onChange={setSorting} />
|
||||||
<TableHeadSortable title='Seeders' by={ORDER.seeders} value={sorting} onChange={setSorting} />
|
<TableHeadSortable title='Seeders' by={ORDER.seeders} value={sorting} onChange={setSorting} />
|
||||||
<TableHead className='px-2 text-center'>Magnet</TableHead>
|
<TableHead className='px-2 text-center' onClick={() => setIsShowHostInput(true)}>
|
||||||
|
Magnet
|
||||||
|
</TableHead>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHeader>
|
</TableHeader>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
@@ -69,12 +86,20 @@ const Torrents: FC<Props> = ({ title, onClose }) => {
|
|||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell className='text-center p-2'>{torrent.seeders}</TableCell>
|
<TableCell className='text-center p-2'>{torrent.seeders}</TableCell>
|
||||||
<TableCell className='text-center p-1'>
|
<TableCell className='text-center p-1'>
|
||||||
|
<div className='flex gap-2 m-auto justify-center'>
|
||||||
|
{host ? (
|
||||||
<a
|
<a
|
||||||
href={torrent.magnet}
|
onClick={handleCast(torrent.magnet)}
|
||||||
className='flex f-full items-center justify-center h-8 w-8 m-auto border rounded-lg'
|
className='flex items-center justify-center h-8 w-8 border rounded-lg'
|
||||||
>
|
>
|
||||||
|
<Cast size={20} />
|
||||||
|
</a>
|
||||||
|
) : (
|
||||||
|
<a href={torrent.magnet} className='flex items-center justify-center h-8 w-8 border rounded-lg'>
|
||||||
<Magnet size={20} />
|
<Magnet size={20} />
|
||||||
</a>
|
</a>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
))}
|
))}
|
||||||
@@ -98,6 +123,19 @@ const Torrents: FC<Props> = ({ title, onClose }) => {
|
|||||||
)}
|
)}
|
||||||
</TableBody>
|
</TableBody>
|
||||||
</Table>
|
</Table>
|
||||||
|
{isShowHostInput && (
|
||||||
|
<Dialog defaultOpen={true} onOpenChange={() => setIsShowHostInput(false)}>
|
||||||
|
<DialogContent className='flex p-3 max-w-[300px] gap-2' onClose={() => setIsShowHostInput(false)}>
|
||||||
|
<Input
|
||||||
|
autoFocus
|
||||||
|
placeholder='Enter host of Elementum'
|
||||||
|
value={host}
|
||||||
|
onChange={({ target: { value } }) => setHost(value)}
|
||||||
|
onKeyDown={({ key }) => key === 'Enter' && setIsShowHostInput(false)}
|
||||||
|
/>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
)}
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in new issue
Block a user