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 | 117x 117x 2x 2x 2x 117x 717x 717x 717x 115x 115x 1x 115x 1x 1x 115x 1x 1x 1x 1x 115x 717x 121x 717x | import { useMemo } from "react";
import { toUnixDateFormat } from "@utils/date.helpers";
import { useForm, useWatch } from "react-hook-form";
import { FilterValuesType } from "../types";
import { SelectorOption } from "@common/TableFilters/SelectorOption";
const FILTER_DEFAULT_VALUES = {
user: [],
category: [],
date: undefined,
};
const generateFilterQuery = (key: string, list: SelectorOption[]) => {
return list.reduce((acc, { value }) => {
const formattedValue = `${key}:"${value}"`;
return acc.length === 0 ? formattedValue : `(${acc},${formattedValue})`;
}, "");
};
const useChangelogFilters = () => {
const filtersForm = useForm<FilterValuesType>({
mode: "onChange",
defaultValues: FILTER_DEFAULT_VALUES,
});
const filters = useWatch<FilterValuesType>({ control: filtersForm.control });
const filtersQuery = useMemo(() => {
let query = "";
if (filters?.category && filters.category?.length > 0) {
query = generateFilterQuery(
"resourceTypeDisplayName",
filters.category as SelectorOption[],
);
}
if (filters?.user && filters.user?.length > 0) {
const prefixedValue = query.length > 0 ? `${query};` : query;
query = `${prefixedValue}${generateFilterQuery(
"userAccID",
filters.user as SelectorOption[],
)}`;
}
if (filters?.date) {
const prefixedValue = query.length > 0 ? `${query};` : query;
const startDate = toUnixDateFormat(
new Date(filters.date.startDate || ""),
);
const endDate = toUnixDateFormat(
new Date(filters.date.endDate?.setHours(23, 59, 59, 0) || ""),
);
query = `${prefixedValue}(changeDate:>=d${startDate};changeDate:<=d${endDate})`;
}
return query;
}, [filters]);
const resetFilters = () => {
Iif (filtersQuery) {
filtersForm.reset();
}
};
return {
filtersQuery,
filtersForm,
resetFilters,
};
};
export default useChangelogFilters;
|