mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 23:15:36 +00:00
feat: add a search modal using the database (wip)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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'],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user