feat: drag-to-reorder torrent provider tabs with localStorage persistence
Lets users customize the order of YTS / Pirate Bay / Toloka tabs in the torrent search modal via drag-and-drop; the saved order also determines which provider opens by default. dnd-kit is isolated to the modal's lazily-loaded chunk so it doesn't affect the main bundle. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
fef5838a23
commit
b31bd062e2
4 files changed
+178
-19
No files matched your search
@@ -1,5 +1,8 @@
|
||||
import React, { FormEvent, useState } from 'react';
|
||||
import React, { FC, FormEvent, useState } from 'react';
|
||||
import { create, InstanceProps } from 'react-modal-promise';
|
||||
import { DndContext } from '@dnd-kit/core';
|
||||
import { horizontalListSortingStrategy, SortableContext, useSortable } from '@dnd-kit/sortable';
|
||||
import { CSS } from '@dnd-kit/utilities';
|
||||
import { TabsContent } from '@radix-ui/react-tabs';
|
||||
import { Loader } from 'lucide-react';
|
||||
import { Search } from 'lucide-react';
|
||||
@@ -11,13 +14,35 @@ 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';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
import TorrentsTable from './components/TorrentsTable';
|
||||
import { providers } from './constants';
|
||||
import useProvidersOrder, { ProviderKey } from './useProvidersOrder';
|
||||
import useTorrents from './useTorrents';
|
||||
|
||||
type Props = InstanceProps<void> & Show & Partial<Details>;
|
||||
|
||||
const SortableTabsTrigger: FC<{ provider: (typeof providers)[ProviderKey]; isLoading: boolean }> = ({
|
||||
provider,
|
||||
isLoading
|
||||
}) => {
|
||||
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: provider.key });
|
||||
|
||||
return (
|
||||
<TabsTrigger
|
||||
ref={setNodeRef}
|
||||
value={provider.key}
|
||||
style={{ transform: CSS.Transform.toString(transform), transition }}
|
||||
className={cn('min-h-8 touch-none', isDragging && 'z-10 opacity-50')}
|
||||
{...attributes}
|
||||
{...listeners}
|
||||
>
|
||||
{isLoading ? <Loader className='animate-spin' size={14} /> : provider.label}
|
||||
</TabsTrigger>
|
||||
);
|
||||
};
|
||||
|
||||
const showTorrentsModal = create(({ onResolve, ...show }: Props) => {
|
||||
const [sort, setSort] = useState<Sort>(Sort.seeds);
|
||||
|
||||
@@ -48,20 +73,11 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => {
|
||||
imdbID: show.imdb_id!
|
||||
});
|
||||
|
||||
const tabs = [
|
||||
{
|
||||
query: yts,
|
||||
provider: providers.yts
|
||||
},
|
||||
{
|
||||
query: tpb,
|
||||
provider: providers.tpb
|
||||
},
|
||||
{
|
||||
query: tlk,
|
||||
provider: providers.tlk
|
||||
}
|
||||
];
|
||||
const queries = { yts, tpb, tlk };
|
||||
|
||||
const { order, ...dndProps } = useProvidersOrder();
|
||||
|
||||
const tabs = order.map((key) => ({ query: queries[key], provider: providers[key] }));
|
||||
|
||||
const handleSearch = (event: FormEvent) => {
|
||||
event.preventDefault();
|
||||
@@ -91,14 +107,16 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => {
|
||||
</Button>
|
||||
</ButtonsGroup>
|
||||
</form>
|
||||
<Tabs defaultValue={providers.yts.key} className='w-full rounded-none px-2'>
|
||||
<Tabs defaultValue={tabs[0].provider.key} className='w-full rounded-none px-2'>
|
||||
<DndContext {...dndProps}>
|
||||
<SortableContext items={order} strategy={horizontalListSortingStrategy}>
|
||||
<TabsList className='grid w-full grid-cols-3'>
|
||||
{tabs.map(({ provider, query }) => (
|
||||
<TabsTrigger key={provider.key} value={provider.key} className='min-h-8'>
|
||||
{query.isLoading ? <Loader className='animate-spin' size={14} /> : provider.label}
|
||||
</TabsTrigger>
|
||||
<SortableTabsTrigger key={provider.key} provider={provider} isLoading={query.isLoading} />
|
||||
))}
|
||||
</TabsList>
|
||||
</SortableContext>
|
||||
</DndContext>
|
||||
{tabs.map(({ provider, query }) => {
|
||||
const isEmpty = checkIsEmpty(query);
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { closestCenter, DragEndEvent, KeyboardSensor, PointerSensor, useSensor, useSensors } from '@dnd-kit/core';
|
||||
import { arrayMove, sortableKeyboardCoordinates } from '@dnd-kit/sortable';
|
||||
import { useAtom } from 'jotai';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
|
||||
import { providers } from './constants';
|
||||
|
||||
export type ProviderKey = keyof typeof providers;
|
||||
|
||||
const DEFAULT_ORDER = Object.keys(providers) as ProviderKey[];
|
||||
|
||||
const orderAtom = atomWithStorage<ProviderKey[]>('torrent-providers-order', DEFAULT_ORDER, undefined, {
|
||||
unstable_getOnInit: true
|
||||
});
|
||||
|
||||
const sanitize = (order: ProviderKey[]) => {
|
||||
const known = order.filter((key) => key in providers);
|
||||
const missing = DEFAULT_ORDER.filter((key) => !known.includes(key));
|
||||
|
||||
return [...known, ...missing];
|
||||
};
|
||||
|
||||
const useProvidersOrder = () => {
|
||||
const [order, setOrder] = useAtom(orderAtom);
|
||||
|
||||
const sensors = useSensors(
|
||||
useSensor(PointerSensor, { activationConstraint: { distance: 8 } }),
|
||||
useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates })
|
||||
);
|
||||
|
||||
const handleDragEnd = ({ active, over }: DragEndEvent) => {
|
||||
if (!over || active.id === over.id) return;
|
||||
|
||||
void setOrder((current) => {
|
||||
const activeIndex = current.indexOf(active.id as ProviderKey);
|
||||
const overIndex = current.indexOf(over.id as ProviderKey);
|
||||
|
||||
if (activeIndex === -1 || overIndex === -1) return current;
|
||||
|
||||
return arrayMove(current, activeIndex, overIndex);
|
||||
});
|
||||
};
|
||||
|
||||
return { order: sanitize(order), sensors, collisionDetection: closestCenter, onDragEnd: handleDragEnd };
|
||||
};
|
||||
|
||||
export default useProvidersOrder;
|
||||
Generated
+91
@@ -8,6 +8,9 @@
|
||||
"name": "movie-advisor",
|
||||
"version": "0.1.0",
|
||||
"dependencies": {
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/sortable": "^10.0.0",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"@radix-ui/react-dialog": "^1.1.1",
|
||||
"@radix-ui/react-dropdown-menu": "^2.1.2",
|
||||
"@radix-ui/react-tabs": "^1.0.4",
|
||||
@@ -1699,6 +1702,59 @@
|
||||
"integrity": "sha512-N8uEMrMPL0cu/bdboEWpQYb/0i2K5Qn8eCsxzOmxSggJbbQte7ljMRoXm917AbntqTGOzdTu+vP3KOOzoC70HQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@dnd-kit/accessibility": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/accessibility/-/accessibility-3.1.1.tgz",
|
||||
"integrity": "sha512-2P+YgaXF+gRsIihwwY1gCsQSYnu9Zyj2py8kY5fFvUM1qm2WA2u639R6YNVfU4GWr+ZM5mqEsfHZZLoRONbemw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@dnd-kit/core": {
|
||||
"version": "6.3.1",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/core/-/core-6.3.1.tgz",
|
||||
"integrity": "sha512-xkGBRQQab4RLwgXxoqETICr6S5JlogafbhNsidmrkVv2YRs5MLwpjoF2qpiGjQt8S9AoxtIV603s0GIUpY5eYQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@dnd-kit/accessibility": "^3.1.1",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.8.0",
|
||||
"react-dom": ">=16.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@dnd-kit/sortable": {
|
||||
"version": "10.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/sortable/-/sortable-10.0.0.tgz",
|
||||
"integrity": "sha512-+xqhmIIzvAYMGfBYYnbKuNicfSsk4RksY2XdmJhT+HAC01nix6fHCztU68jooFiMUB01Ky3F0FyOvhG/BZrWkg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@dnd-kit/core": "^6.3.0",
|
||||
"react": ">=16.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@dnd-kit/utilities": {
|
||||
"version": "3.2.2",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/utilities/-/utilities-3.2.2.tgz",
|
||||
"integrity": "sha512-+MKAJEOfaBe5SmV6t34p80MMKhjvUz0vRrvVJbPT0WElzaOJ/1xs+D+KDv+tD/NE5ujfrChEcshd4fLn0wpiqg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@eslint-community/eslint-utils": {
|
||||
"version": "4.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.4.0.tgz",
|
||||
@@ -13575,6 +13631,41 @@
|
||||
"resolved": "https://registry.npmjs.org/@corex/deepmerge/-/deepmerge-4.0.43.tgz",
|
||||
"integrity": "sha512-N8uEMrMPL0cu/bdboEWpQYb/0i2K5Qn8eCsxzOmxSggJbbQte7ljMRoXm917AbntqTGOzdTu+vP3KOOzoC70HQ=="
|
||||
},
|
||||
"@dnd-kit/accessibility": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/accessibility/-/accessibility-3.1.1.tgz",
|
||||
"integrity": "sha512-2P+YgaXF+gRsIihwwY1gCsQSYnu9Zyj2py8kY5fFvUM1qm2WA2u639R6YNVfU4GWr+ZM5mqEsfHZZLoRONbemw==",
|
||||
"requires": {
|
||||
"tslib": "^2.0.0"
|
||||
}
|
||||
},
|
||||
"@dnd-kit/core": {
|
||||
"version": "6.3.1",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/core/-/core-6.3.1.tgz",
|
||||
"integrity": "sha512-xkGBRQQab4RLwgXxoqETICr6S5JlogafbhNsidmrkVv2YRs5MLwpjoF2qpiGjQt8S9AoxtIV603s0GIUpY5eYQ==",
|
||||
"requires": {
|
||||
"@dnd-kit/accessibility": "^3.1.1",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"tslib": "^2.0.0"
|
||||
}
|
||||
},
|
||||
"@dnd-kit/sortable": {
|
||||
"version": "10.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/sortable/-/sortable-10.0.0.tgz",
|
||||
"integrity": "sha512-+xqhmIIzvAYMGfBYYnbKuNicfSsk4RksY2XdmJhT+HAC01nix6fHCztU68jooFiMUB01Ky3F0FyOvhG/BZrWkg==",
|
||||
"requires": {
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"tslib": "^2.0.0"
|
||||
}
|
||||
},
|
||||
"@dnd-kit/utilities": {
|
||||
"version": "3.2.2",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/utilities/-/utilities-3.2.2.tgz",
|
||||
"integrity": "sha512-+MKAJEOfaBe5SmV6t34p80MMKhjvUz0vRrvVJbPT0WElzaOJ/1xs+D+KDv+tD/NE5ujfrChEcshd4fLn0wpiqg==",
|
||||
"requires": {
|
||||
"tslib": "^2.0.0"
|
||||
}
|
||||
},
|
||||
"@eslint-community/eslint-utils": {
|
||||
"version": "4.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.4.0.tgz",
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
"node": "22.x"
|
||||
},
|
||||
"dependencies": {
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/sortable": "^10.0.0",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"@radix-ui/react-dialog": "^1.1.1",
|
||||
"@radix-ui/react-dropdown-menu": "^2.1.2",
|
||||
"@radix-ui/react-tabs": "^1.0.4",
|
||||
|
||||
Reference in new issue
Block a user