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 | 104x 162x 160x 104x 7x 6x 6x 6x 6x 76x 6x 104x 378x 95x 104x | import { useMutation, UseMutationOptions } from "@tanstack/react-query";
import { API } from "aws-amplify";
import type { opensearch, ReactQueryApiError } from "shared-types";
import {
aggQueryBuilder,
filterQueryBuilder,
paginationQueryBuilder,
sortQueryBuilder,
} from "@/components";
type QueryProps<T> = {
index: opensearch.Index;
filters: opensearch.QueryState<T>["filters"];
sort?: opensearch.QueryState<T>["sort"];
pagination: opensearch.QueryState<T>["pagination"];
aggs?: opensearch.AggQuery<T>[];
};
export const getOsData = async <TProps, TResponse extends opensearch.Response<any>>(
props: QueryProps<TProps>,
): Promise<TResponse> => {
const searchData = await API.post("os", `/search/${props.index}`, {
body: {
...filterQueryBuilder(props.filters),
...paginationQueryBuilder(props.pagination),
...(!!props.sort && sortQueryBuilder(props.sort)),
...(!!props.aggs && aggQueryBuilder(props.aggs)),
track_total_hits: true,
},
});
return searchData;
};
export const getMainExportData = async (
filters?: opensearch.main.Filterable[],
sort?: opensearch.main.State["sort"],
) => {
if (!filters) return [];
const recursiveSearch = async (startPage: number): Promise<opensearch.main.Document[]> => {
Iif (startPage * 1000 >= 10000) {
return [];
}
const searchData = await API.post("os", "/search/main", {
body: {
...filterQueryBuilder(filters),
...(!!sort && sortQueryBuilder(sort)),
...paginationQueryBuilder({ number: startPage, size: 1000 }),
},
});
Eif (searchData?.hits.hits.length < 1000) {
return searchData.hits.hits.map((hit: any) => ({ ...hit._source })) || [];
}
return searchData.hits.hits
.map((hit: any) => ({
...hit._source,
}))
.concat(await recursiveSearch(startPage + 1));
};
return await recursiveSearch(0);
};
export const useOsSearch = <TProps, TResponse>(
options?: UseMutationOptions<TResponse, ReactQueryApiError, QueryProps<TProps>>,
) => {
//@ts-expect-error
return useMutation<TResponse, ReactQueryApiError, QueryProps<TProps>>(
(props) => getOsData(props),
options,
);
};
export const useChangelogSearch = (
options?: UseMutationOptions<
opensearch.changelog.Response,
ReactQueryApiError,
QueryProps<opensearch.changelog.Field>
>,
) => {
return useMutation<
opensearch.changelog.Response,
ReactQueryApiError,
QueryProps<opensearch.changelog.Field>
>((props) => getOsData(props), options);
};
|