|
| 1 | +import { useMemo } from 'react'; |
| 2 | +import { FILTERS } from '../constants'; |
| 3 | +import { Filters, FilterWithParam } from '../types'; |
| 4 | + |
| 5 | +type SuggestionGroup = { |
| 6 | + label: string; |
| 7 | + filters: Filters[]; |
| 8 | +}; |
| 9 | + |
| 10 | +export function useSuggestedFilters(variableName: string, currentFilters: FilterWithParam[]): SuggestionGroup[] { |
| 11 | + return useMemo(() => { |
| 12 | + const currentFilterValues = new Set(currentFilters.map((f) => f.value)); |
| 13 | + const suggestedFilters: Filters[] = []; |
| 14 | + |
| 15 | + const addSuggestions = (filterValues: string[]) => { |
| 16 | + const newFilters = FILTERS.filter((f) => filterValues.includes(f.value) && !currentFilterValues.has(f.value)); |
| 17 | + |
| 18 | + suggestedFilters.push(...newFilters); |
| 19 | + }; |
| 20 | + |
| 21 | + if (isStepsEventsPattern(variableName)) { |
| 22 | + addSuggestions(['digest']); |
| 23 | + } |
| 24 | + |
| 25 | + if (isDateVariable(variableName)) { |
| 26 | + addSuggestions(['date']); |
| 27 | + } |
| 28 | + |
| 29 | + if (isNumberVariable(variableName)) { |
| 30 | + addSuggestions(['round', 'floor', 'ceil', 'abs', 'plus', 'minus', 'times', 'divided_by']); |
| 31 | + } |
| 32 | + |
| 33 | + if (isArrayVariable(variableName)) { |
| 34 | + addSuggestions(['first', 'last', 'join', 'map', 'where', 'size']); |
| 35 | + } |
| 36 | + |
| 37 | + if (isTextVariable(variableName)) { |
| 38 | + addSuggestions(['upcase', 'downcase', 'capitalize', 'truncate', 'truncatewords']); |
| 39 | + } |
| 40 | + |
| 41 | + return suggestedFilters.length > 0 ? [{ label: 'Suggested', filters: suggestedFilters }] : []; |
| 42 | + }, [variableName, currentFilters]); |
| 43 | +} |
| 44 | + |
| 45 | +function isDateVariable(name: string): boolean { |
| 46 | + const datePatterns = ['date', 'time', 'created', 'updated', 'timestamp', 'scheduled']; |
| 47 | + |
| 48 | + return datePatterns.some((pattern) => name.toLowerCase().includes(pattern)); |
| 49 | +} |
| 50 | + |
| 51 | +function isNumberVariable(name: string): boolean { |
| 52 | + const numberPatterns = ['count', 'amount', 'total', 'price', 'quantity', 'number', 'sum', 'age']; |
| 53 | + |
| 54 | + return numberPatterns.some((pattern) => name.toLowerCase().includes(pattern)); |
| 55 | +} |
| 56 | + |
| 57 | +function isArrayVariable(name: string): boolean { |
| 58 | + const arrayPatterns = ['list', 'array', 'items', 'collection', 'set', 'group', 'events']; |
| 59 | + |
| 60 | + return arrayPatterns.some((pattern) => name.toLowerCase().includes(pattern)); |
| 61 | +} |
| 62 | + |
| 63 | +function isTextVariable(name: string): boolean { |
| 64 | + const textPatterns = ['name', 'title', 'description', 'text', 'message', 'content', 'label']; |
| 65 | + |
| 66 | + return textPatterns.some((pattern) => name.toLowerCase().includes(pattern)); |
| 67 | +} |
| 68 | + |
| 69 | +function isStepsEventsPattern(name: string): boolean { |
| 70 | + return /^steps\..*\.events$/.test(name); |
| 71 | +} |
0 commit comments