Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | import {
ChangeEventHandler,
FormEventHandler,
useCallback,
useMemo,
useState,
Dispatch,
SetStateAction,
} from "react";
import { useHistory } from "react-router-dom";
import { CatalogSearchSort } from "../../api/catalog-search/constants";
import { CDKType } from "../../constants/constructs";
import { Language } from "../../constants/languages";
import { getSearchPath } from "../../util/url";
export interface UseCatalogSearchParams {
defaultCdkMajor?: number;
defaultCdkType?: CDKType;
defaultQuery?: string;
defaultKeywords?: UseCatalogSearchReturn["keywords"];
defaultLanguages?: UseCatalogSearchReturn["languages"];
defaultSort?: UseCatalogSearchReturn["sort"];
defaultTags?: UseCatalogSearchReturn["tags"];
}
interface NavigationParams {
replace?: boolean;
}
export interface UseCatalogSearchReturn {
/**
* The CDK Type's major version to filter by
*/
cdkMajor?: number;
/**
* The CDK Type to filter by
*/
cdkType?: CDKType;
/**
* The list of user-defined keywords to filter by
*/
keywords: string[];
/**
* The list of languages being filtered
*/
languages: Language[];
/**
* The list of tags being filtered
*/
tags: string[];
/**
* Input ChangeEventHandler which wraps the setQuery state setter
*/
onQueryChange: ChangeEventHandler<HTMLInputElement>;
/**
* Navigates to the search query url
*/
onSearch: (p?: NavigationParams) => void;
/**
* FormEventHandler to handle query submission
*/
onSubmit: FormEventHandler<HTMLFormElement>;
/**
* The query state for this search
*/
query: string;
/**
* CDK Major state setter
*/
setCdkMajor: Dispatch<SetStateAction<UseCatalogSearchReturn["cdkMajor"]>>;
/**
* CDK Type state setter
*/
setCdkType: Dispatch<SetStateAction<UseCatalogSearchReturn["cdkType"]>>;
/**
* Keywords list state setter
*/
setKeywords: Dispatch<SetStateAction<UseCatalogSearchReturn["keywords"]>>;
/**
* Languages list state setter
*/
setLanguages: Dispatch<SetStateAction<UseCatalogSearchReturn["languages"]>>;
/**
* Tags list state setter
*/
setTags: Dispatch<SetStateAction<UseCatalogSearchReturn["tags"]>>;
/**
* Query state setter
*/
setQuery: Dispatch<SetStateAction<UseCatalogSearchReturn["query"]>>;
setSort: Dispatch<SetStateAction<UseCatalogSearchReturn["sort"]>>;
sort?: CatalogSearchSort;
}
/**
* This hook provides all of the methods required to implement the functionality
* of a client-side catalog search component. It additionally exposes lower-level methods
* for custom components which may want to access parts of the search API
*/
export const useCatalogSearch = (
options: UseCatalogSearchParams = {}
): UseCatalogSearchReturn => {
const [query, setQuery] = useState(options.defaultQuery ?? "");
const [cdkType, setCdkType] = useState<UseCatalogSearchReturn["cdkType"]>(
options.defaultCdkType
);
const [cdkMajor, setCdkMajor] = useState<UseCatalogSearchReturn["cdkMajor"]>(
options.defaultCdkMajor
);
const [languages, setLanguages] = useState<
UseCatalogSearchReturn["languages"]
>(options.defaultLanguages ?? []);
const [tags, setTags] = useState<UseCatalogSearchReturn["tags"]>(
options.defaultTags ?? []
);
const [keywords, setKeywords] = useState<UseCatalogSearchReturn["keywords"]>(
options.defaultKeywords ?? []
);
const [sort, setSort] = useState<UseCatalogSearchReturn["sort"]>(
options.defaultSort
);
const { push, replace } = useHistory();
const onQueryChange: UseCatalogSearchReturn["onQueryChange"] = (e) => {
e.preventDefault();
setQuery(e.target.value);
};
const onSearch: UseCatalogSearchReturn["onSearch"] = useCallback(
(opts) => {
const navigate = opts?.replace ? replace : push;
navigate(
getSearchPath({
cdkType,
cdkMajor,
keywords,
languages,
query,
sort,
tags,
})
);
},
[replace, push, cdkType, cdkMajor, keywords, languages, query, sort, tags]
);
const onSubmit: UseCatalogSearchReturn["onSubmit"] = useCallback(
(e) => {
e?.preventDefault();
onSearch();
},
[onSearch]
);
return useMemo(
() => ({
cdkMajor,
cdkType,
keywords,
languages,
onQueryChange,
onSearch,
onSubmit,
query,
setCdkMajor,
setCdkType,
setKeywords,
setLanguages,
setTags,
setQuery,
setSort,
sort,
tags,
}),
[
cdkMajor,
cdkType,
keywords,
languages,
onSearch,
onSubmit,
query,
sort,
tags,
]
);
};
|