|
| 1 | +import type { FilterFn } from '@tanstack/vue-table' |
| 2 | +import type { Event } from '@/modules/events/types' |
| 3 | +import type { Field } from '@/modules/fields/types' |
| 4 | +import type { Tag } from '@/modules/tags/types' |
| 5 | + |
| 6 | +/** |
| 7 | + * Universal multi-field search function for any array of objects |
| 8 | + * @param item The item to check |
| 9 | + * @param fields Array of field names to search in (supports dot notation) |
| 10 | + * @param searchQuery The search query string |
| 11 | + * @returns true if item matches the search query |
| 12 | + */ |
| 13 | +export function searchMultiField<T>(item: T, fields: string[], searchQuery: string): boolean { |
| 14 | + if (!searchQuery || typeof searchQuery !== 'string') { |
| 15 | + return true |
| 16 | + } |
| 17 | + |
| 18 | + const searchText = searchQuery.toLowerCase().trim() |
| 19 | + if (!searchText) return true |
| 20 | + |
| 21 | + return fields.some(field => { |
| 22 | + const value = getNestedValue(item, field) |
| 23 | + if (value === null || value === undefined) return false |
| 24 | + |
| 25 | + const stringValue = String(value).toLowerCase() |
| 26 | + return stringValue.includes(searchText) |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Filter an array using multi-field search |
| 32 | + * @param data Array of items to filter |
| 33 | + * @param fields Array of field names to search in |
| 34 | + * @param searchQuery The search query string |
| 35 | + * @returns Filtered array |
| 36 | + */ |
| 37 | +export function filterMultiField<T>(data: T[], fields: string[], searchQuery: string): T[] { |
| 38 | + if (!searchQuery?.trim()) return data |
| 39 | + |
| 40 | + return data.filter(item => searchMultiField(item, fields, searchQuery)) |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Creates a TanStack Table filter function from multi-field search |
| 45 | + * @param fields Array of field names to search in |
| 46 | + * @returns TanStack Table filter function |
| 47 | + */ |
| 48 | +export function createTableFilter<TData>(fields: string[]): FilterFn<TData> { |
| 49 | + return (row, columnId, filterValue) => { |
| 50 | + return searchMultiField(row.original, fields, filterValue as string) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Gets a nested value from an object using dot notation |
| 56 | + * @param obj The object to get the value from |
| 57 | + * @param path The path to the value (e.g., 'user.name' or 'id') |
| 58 | + * @returns The value at the path, or undefined if not found |
| 59 | + */ |
| 60 | +function getNestedValue<T>(obj: T, path: string): unknown { |
| 61 | + return path.split('.').reduce((current: unknown, key: string) => { |
| 62 | + return (current as Record<string, unknown>)?.[key] |
| 63 | + }, obj) |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Pre-configured fields for different entities |
| 68 | + */ |
| 69 | +export const EVENTS_SEARCH_FIELDS = ['id', 'name', 'description'] |
| 70 | +export const FIELDS_SEARCH_FIELDS = ['id', 'name', 'description'] |
| 71 | +export const TAGS_SEARCH_FIELDS = ['id', 'description'] |
| 72 | + |
| 73 | +/** |
| 74 | + * Pre-configured TanStack Table filters |
| 75 | + */ |
| 76 | +export const eventsTableFilter = createTableFilter<Event>(EVENTS_SEARCH_FIELDS) |
| 77 | +export const fieldsTableFilter = createTableFilter<Field>(FIELDS_SEARCH_FIELDS) |
| 78 | +export const tagsTableFilter = createTableFilter<Tag>(TAGS_SEARCH_FIELDS) |
| 79 | + |
| 80 | +/** |
| 81 | + * Pre-configured array filter functions |
| 82 | + */ |
| 83 | +export const filterEvents = (data: Event[], searchQuery: string): Event[] => |
| 84 | + filterMultiField(data, EVENTS_SEARCH_FIELDS, searchQuery) |
| 85 | + |
| 86 | +export const filterFields = (data: Field[], searchQuery: string): Field[] => |
| 87 | + filterMultiField(data, FIELDS_SEARCH_FIELDS, searchQuery) |
| 88 | + |
| 89 | +export const filterTags = (data: Tag[], searchQuery: string): Tag[] => |
| 90 | + filterMultiField(data, TAGS_SEARCH_FIELDS, searchQuery) |
0 commit comments