Files
spacedrive/interface/app/$libraryId/Explorer/util.ts
2023-09-21 11:10:56 +00:00

46 lines
1.2 KiB
TypeScript

import { useMemo } from 'react';
import { getExplorerItemData, type ExplorerItem } from '@sd/client';
import { ExplorerParamsSchema } from '~/app/route-schemas';
import { useZodSearchParams } from '~/hooks';
import { flattenThumbnailKey, useExplorerStore } from './store';
export function useExplorerSearchParams() {
return useZodSearchParams(ExplorerParamsSchema);
}
export function useExplorerItemData(explorerItem: ExplorerItem) {
const explorerStore = useExplorerStore();
const newThumbnail = !!(
explorerItem.thumbnail_key &&
explorerStore.newThumbnails.has(flattenThumbnailKey(explorerItem.thumbnail_key))
);
return useMemo(() => {
const itemData = getExplorerItemData(explorerItem);
if (!itemData.hasLocalThumbnail) {
itemData.hasLocalThumbnail = newThumbnail;
}
return itemData;
}, [explorerItem, newThumbnail]);
}
export const pubIdToString = (pub_id: number[]) =>
pub_id.map((b) => b.toString(16).padStart(2, '0')).join('');
export const uniqueId = (item: ExplorerItem | { pub_id: number[] }) => {
if ('pub_id' in item) return pubIdToString(item.pub_id);
const { type } = item;
switch (type) {
case 'NonIndexedPath':
return item.item.path;
default:
return pubIdToString(item.item.pub_id);
}
};