-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathuseLinkValue.ts
More file actions
73 lines (63 loc) · 2.37 KB
/
useLinkValue.ts
File metadata and controls
73 lines (63 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { DEFAULT_INITIAL_SEARCH_DATA } from "@dashboard/config";
import useCategorySearch from "@dashboard/searches/useCategorySearch";
import useCollectionSearch from "@dashboard/searches/useCollectionSearch";
import usePageSearch from "@dashboard/searches/usePageSearch";
import { FetchMoreProps } from "@dashboard/types";
import { mapEdgesToItems } from "@dashboard/utils/maps";
import { MenuItemTypeWithOptions } from "../types";
export const useLinkValue = (linkType: MenuItemTypeWithOptions) => {
const categorySearch = useCategorySearch({
variables: DEFAULT_INITIAL_SEARCH_DATA,
skip: linkType !== "category",
});
const collectionSearch = useCollectionSearch({
variables: DEFAULT_INITIAL_SEARCH_DATA,
skip: linkType !== "collection",
});
const pageSearch = usePageSearch({
variables: DEFAULT_INITIAL_SEARCH_DATA,
skip: linkType !== "page",
});
const searches = {
category: categorySearch,
collection: collectionSearch,
page: pageSearch,
};
const selectedLinkTypeSearch = searches[linkType];
const handleQueryChange = (query: string) => {
if (selectedLinkTypeSearch) {
selectedLinkTypeSearch.search(query);
}
};
const fetchMoreProps: FetchMoreProps = {
hasMore: selectedLinkTypeSearch?.result?.data?.search?.pageInfo?.hasNextPage ?? false,
loading: selectedLinkTypeSearch?.result?.loading,
onFetchMore: selectedLinkTypeSearch?.loadMore,
};
const categories = mapEdgesToItems(categorySearch?.result?.data?.search) || [];
const collections = mapEdgesToItems(collectionSearch?.result?.data?.search) || [];
const pages = mapEdgesToItems(pageSearch?.result?.data?.search) || [];
const categoriesOptions = categories?.map(category => ({
value: (category as { id: string }).id,
label: (category as { name: string }).name,
}));
const collectionsOptions = collections?.map(collection => ({
value: (collection as { id: string }).id,
label: (collection as { name: string }).name,
}));
const pagesOptions = pages?.map(page => ({
value: (page as { id: string }).id,
label: (page as { title: string }).title,
}));
const options = {
category: categoriesOptions,
collection: collectionsOptions,
page: pagesOptions,
};
return {
options: options[linkType],
fetchMoreProps,
onQueryChange: handleQueryChange,
loading: selectedLinkTypeSearch?.result?.loading,
};
};