feat: add a search modal using the database (wip)

This commit is contained in:
Sonny
2024-05-25 03:40:08 +02:00
committed by Sonny
parent b28499a69a
commit 56c05f1bf6
21 changed files with 535 additions and 621 deletions

View File

@@ -1,43 +0,0 @@
import { buildSearchItem, formatSearchItem } from 'lib/search';
import { useMemo } from 'react';
import useCollections from '~/hooks/use_collections';
import { SearchItem, SearchResult } from '~/types/search';
export default function useSearchItem({
searchTerm = '',
disableLinks = false,
disableCollections = false,
}: {
searchTerm: string;
disableLinks?: boolean;
disableCollections?: boolean;
}) {
const { collections } = useCollections();
const itemsSearch: SearchItem[] = useMemo<SearchItem[]>(() => {
return collections.reduce((acc, collection) => {
const collectionItem = buildSearchItem(collection, 'collection');
const items: SearchItem[] = collection.links.map((link) =>
buildSearchItem(link, 'link')
);
return [...acc, ...items, collectionItem];
}, [] as SearchItem[]);
}, [collections]);
const itemsCompletion: SearchResult[] = useMemo(() => {
return itemsSearch.reduce((acc, item) => {
const formattedItem = formatSearchItem(item, searchTerm);
if (
(!disableLinks && item.type === 'link') ||
(!disableCollections && item.type === 'collection')
) {
return formattedItem ? [...acc, formattedItem] : acc;
}
return acc;
}, [] as SearchResult[]);
}, [itemsSearch, searchTerm, disableLinks, disableCollections]);
return itemsCompletion;
}

View File

@@ -2,18 +2,27 @@ import KEYS from '#constants/keys';
import { useHotkeys } from 'react-hotkeys-hook';
import useGlobalHotkeys from '~/hooks/use_global_hotkeys';
type ShortcutOptions = { ignoreGlobalHotkeysStatus?: boolean };
type ShortcutOptions = {
enabled?: boolean;
};
export default function useShortcut(
key: keyof typeof KEYS,
cb: () => void,
options: ShortcutOptions = {
ignoreGlobalHotkeysStatus: false,
{ enabled }: ShortcutOptions = {
enabled: true,
}
) {
const { globalHotkeysEnabled } = useGlobalHotkeys();
return useHotkeys(KEYS[key], cb, {
enabled: !options.ignoreGlobalHotkeysStatus ? globalHotkeysEnabled : true,
enableOnFormTags: ['INPUT'],
});
return useHotkeys(
KEYS[key],
(event) => {
event.preventDefault();
cb();
},
{
enabled: key === 'ESCAPE_KEY' ? true : enabled && globalHotkeysEnabled,
enableOnFormTags: ['INPUT'],
}
);
}