mirror of
https://github.com/spacedriveapp/spacedrive.git
synced 2026-04-21 23:18:06 -04:00
* Replace `useExplorerStore` with `useSelector` * Remove `getExplorerStore() * Devtools names for all major anonymous components * referential equality * Undo mobile changes * remove debug log * Properly memoise `RenameTextBox` args * goodbye log * Fix a couple of bugs Thanks @ameer2468 for pointing out!
149 lines
3.8 KiB
TypeScript
149 lines
3.8 KiB
TypeScript
import { useInfiniteQuery } from '@tanstack/react-query';
|
|
import { useMemo } from 'react';
|
|
import {
|
|
ExplorerItem,
|
|
FilePathCursorVariant,
|
|
FilePathObjectCursor,
|
|
FilePathOrder,
|
|
FilePathSearchArgs,
|
|
useLibraryContext,
|
|
useNodes,
|
|
useNormalisedCache,
|
|
useRspcLibraryContext
|
|
} from '@sd/client';
|
|
|
|
import { explorerStore } from '../store';
|
|
import { UseExplorerInfiniteQueryArgs } from './useExplorerInfiniteQuery';
|
|
|
|
export function usePathsInfiniteQuery({
|
|
arg,
|
|
explorerSettings,
|
|
...args
|
|
}: UseExplorerInfiniteQueryArgs<FilePathSearchArgs, FilePathOrder>) {
|
|
const { library } = useLibraryContext();
|
|
const ctx = useRspcLibraryContext();
|
|
const settings = explorerSettings.useSettingsSnapshot();
|
|
const cache = useNormalisedCache();
|
|
|
|
if (settings.order) {
|
|
arg.orderAndPagination = { orderOnly: settings.order };
|
|
if (arg.orderAndPagination.orderOnly.field === 'sizeInBytes') delete arg.take;
|
|
}
|
|
|
|
const query = useInfiniteQuery({
|
|
queryKey: ['search.paths', { library_id: library.uuid, arg }] as const,
|
|
queryFn: async ({ pageParam, queryKey: [_, { arg }] }) => {
|
|
const cItem: Extract<ExplorerItem, { type: 'Path' }> = pageParam;
|
|
const { order } = settings;
|
|
|
|
let orderAndPagination: (typeof arg)['orderAndPagination'];
|
|
|
|
if (!cItem) {
|
|
if (order) orderAndPagination = { orderOnly: order };
|
|
} else {
|
|
let variant: FilePathCursorVariant | undefined;
|
|
|
|
if (!order) variant = 'none';
|
|
else if (cItem) {
|
|
switch (order.field) {
|
|
case 'name': {
|
|
const data = cItem.item.name;
|
|
if (data !== null)
|
|
variant = {
|
|
name: { order: order.value, data }
|
|
};
|
|
break;
|
|
}
|
|
case 'sizeInBytes': {
|
|
variant = { sizeInBytes: order.value };
|
|
break;
|
|
}
|
|
case 'dateCreated': {
|
|
const data = cItem.item.date_created;
|
|
if (data !== null)
|
|
variant = {
|
|
dateCreated: { order: order.value, data }
|
|
};
|
|
break;
|
|
}
|
|
case 'dateModified': {
|
|
const data = cItem.item.date_modified;
|
|
if (data !== null)
|
|
variant = {
|
|
dateModified: { order: order.value, data }
|
|
};
|
|
break;
|
|
}
|
|
case 'dateIndexed': {
|
|
const data = cItem.item.date_indexed;
|
|
if (data !== null)
|
|
variant = {
|
|
dateIndexed: { order: order.value, data }
|
|
};
|
|
break;
|
|
}
|
|
case 'object': {
|
|
const object = cItem.item.object;
|
|
if (!object) break;
|
|
|
|
let objectCursor: FilePathObjectCursor | undefined;
|
|
|
|
switch (order.value.field) {
|
|
case 'dateAccessed': {
|
|
const data = object.date_accessed;
|
|
if (data !== null)
|
|
objectCursor = {
|
|
dateAccessed: { order: order.value.value, data }
|
|
};
|
|
break;
|
|
}
|
|
case 'kind': {
|
|
const data = object.kind;
|
|
if (data !== null)
|
|
objectCursor = {
|
|
kind: { order: order.value.value, data }
|
|
};
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (objectCursor) variant = { object: objectCursor };
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (cItem.item.is_dir === null) throw new Error();
|
|
|
|
if (variant)
|
|
orderAndPagination = {
|
|
cursor: { cursor: { variant, isDir: cItem.item.is_dir }, id: cItem.item.id }
|
|
};
|
|
}
|
|
|
|
arg.orderAndPagination = orderAndPagination;
|
|
|
|
const result = await ctx.client.query(['search.paths', arg]);
|
|
cache.withNodes(result.nodes);
|
|
return result;
|
|
},
|
|
getNextPageParam: (lastPage) => {
|
|
if (arg.take === null || arg.take === undefined) return undefined;
|
|
if (lastPage.items.length < arg.take) return undefined;
|
|
else return lastPage.nodes[arg.take - 1];
|
|
},
|
|
onSuccess: () => explorerStore.resetNewThumbnails(),
|
|
...args
|
|
});
|
|
|
|
const nodes = useMemo(
|
|
() => query.data?.pages.flatMap((page) => page.nodes) ?? [],
|
|
[query.data?.pages]
|
|
);
|
|
|
|
useNodes(nodes);
|
|
|
|
return query;
|
|
}
|