feat: change torrents search query
This commit is contained in:
1 parent
731d658740
commit
a268f7c38d
12 files changed
+91
-122
No files matched your search
@@ -2,10 +2,11 @@
|
||||
|
||||
import { ChangeEvent, MutableRefObject, useRef, useState } from 'react';
|
||||
import { create } from 'react-modal-promise';
|
||||
import { Search as SearchIcon, XCircle } from 'lucide-react';
|
||||
import { Search as SearchIcon, X } from 'lucide-react';
|
||||
|
||||
import List from '@/components/List';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import ButtonsGroup from '@/components/ui/buttons-group';
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
@@ -46,25 +47,25 @@ export const showSearchModal = create(({ onResolve }) => {
|
||||
|
||||
return (
|
||||
<Modal className='block max-w-[932px] p-0' onClose={onResolve} scrollRef={scrollRef}>
|
||||
<div className='relative p-2'>
|
||||
<Input
|
||||
autoFocus
|
||||
ref={inputRef}
|
||||
placeholder='Start typing title...'
|
||||
value={title}
|
||||
onChange={handleChangeTitle}
|
||||
className='pr-10 focus-visible:ring-0'
|
||||
/>
|
||||
<Button
|
||||
variant='outline'
|
||||
type='button'
|
||||
size='icon'
|
||||
onClick={handleReset}
|
||||
disabled={!title.length}
|
||||
className='absolute right-2 top-2'
|
||||
>
|
||||
<XCircle />
|
||||
</Button>
|
||||
<div className='p-2'>
|
||||
<ButtonsGroup>
|
||||
<Input
|
||||
autoFocus
|
||||
ref={inputRef}
|
||||
placeholder='Start typing title...'
|
||||
value={title}
|
||||
onChange={handleChangeTitle}
|
||||
/>
|
||||
<Button
|
||||
type='button'
|
||||
size='icon'
|
||||
className='transition-all duration-200 hover:bg-secondary-foreground hover:shadow-lg hover:shadow-secondary-foreground/60'
|
||||
onClick={handleReset}
|
||||
disabled={!title.length}
|
||||
>
|
||||
<X />
|
||||
</Button>
|
||||
</ButtonsGroup>
|
||||
</div>
|
||||
{isFetchedRef.current && (
|
||||
<div className='px-2'>
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import React, { useState } from 'react';
|
||||
import React, { FormEvent, useState } from 'react';
|
||||
import { create, InstanceProps } from 'react-modal-promise';
|
||||
import { TabsContent } from '@radix-ui/react-tabs';
|
||||
import { Loader } from 'lucide-react';
|
||||
import { Search } from 'lucide-react';
|
||||
|
||||
import { Button } from '@/components/ui/button';
|
||||
import ButtonsGroup from '@/components/ui/buttons-group';
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import { Sort } from '@/data/parsers';
|
||||
|
||||
@@ -19,20 +22,31 @@ interface Props extends InstanceProps<void> {
|
||||
backdrop: string;
|
||||
}
|
||||
|
||||
const showTorrentsModal = create(({ title, year, imdbID, backdrop, onResolve }: Props) => {
|
||||
const showTorrentsModal = create(({ title: initialTitle, year, imdbID, backdrop, onResolve }: Props) => {
|
||||
const [sort, setSort] = useState<Sort>(Sort.seeds);
|
||||
const [withYear, setWithYear] = useState(true);
|
||||
|
||||
const yts = useTorrents({ query: title, queryFn: providers.yts.queryFn, sort, key: providers.yts.key, imdbID });
|
||||
const initialQuery = `${initialTitle} ${year}`;
|
||||
|
||||
const [title, setTitle] = useState(initialQuery);
|
||||
|
||||
const [query, setQuery] = useState(title);
|
||||
|
||||
const yts = useTorrents({
|
||||
query: initialTitle,
|
||||
queryFn: providers.yts.queryFn,
|
||||
sort,
|
||||
key: providers.yts.key,
|
||||
imdbID
|
||||
});
|
||||
const tpb = useTorrents({
|
||||
query: withYear ? `${title} ${year}` : title,
|
||||
query,
|
||||
queryFn: providers.tpb.queryFn,
|
||||
sort,
|
||||
key: providers.tpb.key,
|
||||
imdbID
|
||||
});
|
||||
const tlk = useTorrents({
|
||||
query: withYear ? `${title} ${year}` : title,
|
||||
query,
|
||||
queryFn: providers.tlk.queryFn,
|
||||
sort,
|
||||
key: providers.tlk.key,
|
||||
@@ -54,21 +68,33 @@ const showTorrentsModal = create(({ title, year, imdbID, backdrop, onResolve }:
|
||||
}
|
||||
];
|
||||
|
||||
const handleSearch = (event: FormEvent) => {
|
||||
event.preventDefault();
|
||||
|
||||
setQuery(title || initialQuery);
|
||||
};
|
||||
|
||||
const checkIsEmpty = (query: (typeof tabs)[number]['query']) =>
|
||||
query.isFetched && !query.isLoading && !query.data?.length;
|
||||
|
||||
return (
|
||||
<Modal className='rounded-xl p-0' onClose={onResolve}>
|
||||
<div className='grid grid-cols-[1fr_auto] items-center gap-3 p-2'>
|
||||
<span className='truncate text-base'>{title}</span>
|
||||
<Button className='h-6 p-2' variant={withYear ? 'default' : 'ghost'} onClick={() => setWithYear((w) => !w)}>
|
||||
{year}
|
||||
</Button>
|
||||
</div>
|
||||
<form className='p-2' onSubmit={handleSearch}>
|
||||
<ButtonsGroup>
|
||||
<Input value={title} onChange={({ target }) => setTitle(target.value)} placeholder='Start typing title...' />
|
||||
<Button
|
||||
type='submit'
|
||||
className='!px-3 transition-all duration-200 hover:bg-secondary-foreground hover:shadow-lg hover:shadow-secondary-foreground/60'
|
||||
disabled={initialQuery === title || tpb.isPending || tlk.isPending}
|
||||
>
|
||||
<Search size={14} />
|
||||
</Button>
|
||||
</ButtonsGroup>
|
||||
</form>
|
||||
<Tabs defaultValue={providers.yts.key} className='w-full rounded-none'>
|
||||
<TabsList className='grid w-full grid-cols-3'>
|
||||
{tabs.map(({ provider, query }) => (
|
||||
<TabsTrigger key={provider.key} value={provider.key}>
|
||||
<TabsTrigger key={provider.key} value={provider.key} className='min-h-8'>
|
||||
{query.isLoading ? <Loader className='animate-spin' size={14} /> : provider.label}
|
||||
</TabsTrigger>
|
||||
))}
|
||||
|
||||
+20
-1
@@ -1,10 +1,29 @@
|
||||
import * as React from 'react';
|
||||
import { useLayoutEffect, useRef } from 'react';
|
||||
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
export interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {}
|
||||
|
||||
const Input = React.forwardRef<HTMLInputElement, InputProps>(({ className, type, ...props }, ref) => {
|
||||
const inputRef = useRef<HTMLInputElement>();
|
||||
|
||||
const handleRef = (element: HTMLInputElement) => {
|
||||
if (!element) return;
|
||||
|
||||
inputRef.current = element;
|
||||
|
||||
if (!ref) return;
|
||||
|
||||
typeof ref === 'function' ? ref(element) : (ref.current = element);
|
||||
};
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!inputRef.current || props.autoFocus) return;
|
||||
|
||||
setTimeout(() => inputRef.current?.blur());
|
||||
}, [props.autoFocus]);
|
||||
|
||||
return (
|
||||
<input
|
||||
type={type}
|
||||
@@ -12,7 +31,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(({ className, type,
|
||||
'flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50',
|
||||
className
|
||||
)}
|
||||
ref={ref}
|
||||
ref={handleRef}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user