mirror of
https://github.com/blakeblackshear/frigate.git
synced 2025-12-06 21:44:13 +03:00
* Add ability to filter by time range * Cleanup * Handle input with tags * fix input for time_range filter * fix before and after filters * clean up * Ensure the default value works as expected * Handle time range in am/pm based on browser * Fix arrow * Fix text * Handle midnight case * fix width * Fix bg * Fix bg * Fix mobile spacing * y spacing * remove left padding --------- Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com>
62 lines
2.0 KiB
TypeScript
62 lines
2.0 KiB
TypeScript
import { FilterType, SavedSearchQuery, SearchFilter } from "@/types/search";
|
|
import { useCallback, useState } from "react";
|
|
|
|
// Custom hook for managing suggestions
|
|
export type UseSuggestionsType = (
|
|
filters: SearchFilter,
|
|
allSuggestions: { [K in keyof SearchFilter]: string[] },
|
|
searchHistory: SavedSearchQuery[],
|
|
) => ReturnType<typeof useSuggestions>;
|
|
|
|
// Define and export the useSuggestions hook
|
|
export default function useSuggestions(
|
|
filters: SearchFilter,
|
|
allSuggestions: { [K in keyof SearchFilter]: string[] },
|
|
searchHistory?: SavedSearchQuery[],
|
|
) {
|
|
const [suggestions, setSuggestions] = useState<string[]>([]);
|
|
|
|
const updateSuggestions = useCallback(
|
|
(value: string, currentFilterType: FilterType | null) => {
|
|
if (currentFilterType && currentFilterType in allSuggestions) {
|
|
const filterValue = value.split(":").pop() || "";
|
|
const currentFilterValues = filters[currentFilterType] || [];
|
|
setSuggestions(
|
|
allSuggestions[currentFilterType]?.filter(
|
|
(item) =>
|
|
item.toLowerCase().startsWith(filterValue.toLowerCase()) &&
|
|
!(currentFilterValues as (string | number)[]).includes(item),
|
|
) ?? [],
|
|
);
|
|
} else {
|
|
const availableFilters = Object.keys(allSuggestions).filter(
|
|
(filter) => {
|
|
const filterKey = filter as FilterType;
|
|
const filterValues = filters[filterKey];
|
|
const suggestionValues = allSuggestions[filterKey];
|
|
|
|
if (!filterValues) return true;
|
|
if (
|
|
Array.isArray(filterValues) &&
|
|
Array.isArray(suggestionValues)
|
|
) {
|
|
return filterValues.length < suggestionValues.length;
|
|
}
|
|
return false;
|
|
},
|
|
);
|
|
setSuggestions([
|
|
...(searchHistory?.map((search) => search.name) ?? []),
|
|
...availableFilters,
|
|
]);
|
|
}
|
|
},
|
|
[filters, allSuggestions, searchHistory],
|
|
);
|
|
|
|
return {
|
|
suggestions,
|
|
updateSuggestions,
|
|
};
|
|
}
|