UNPKG

@mantine/spotlight

Version:

Command center components for react and Mantine

1 lines 3.37 kB
{"version":3,"file":"default-spotlight-filter.cjs","names":[],"sources":["../src/default-spotlight-filter.ts"],"sourcesContent":["import type {\n SpotlightActionData,\n SpotlightActionGroupData,\n SpotlightActions,\n SpotlightFilterFunction,\n} from './Spotlight';\n\nfunction getKeywords(keywords: string | string[] | undefined) {\n if (Array.isArray(keywords)) {\n return keywords\n .map((keyword) => keyword.trim())\n .join(',')\n .toLowerCase()\n .trim();\n }\n\n if (typeof keywords === 'string') {\n return keywords.toLowerCase().trim();\n }\n\n return '';\n}\n\nfunction getFlatActions(data: SpotlightActions[]) {\n return data.reduce<SpotlightActionData[]>((acc, item) => {\n if ('actions' in item) {\n return [...acc, ...item.actions.map((action) => ({ ...action, group: item.group }))];\n }\n\n return [...acc, item];\n }, []);\n}\n\nfunction flatActionsToGroups(data: SpotlightActionData[]) {\n const groups: Record<string, { pushed: boolean; data: SpotlightActionGroupData }> = {};\n const result: SpotlightActions[] = [];\n\n data.forEach((action) => {\n if (action.group) {\n if (!groups[action.group]) {\n groups[action.group] = { pushed: false, data: { group: action.group, actions: [] } };\n }\n\n groups[action.group].data.actions.push(action);\n\n if (!groups[action.group].pushed) {\n groups[action.group].pushed = true;\n result.push(groups[action.group].data);\n }\n } else {\n result.push(action);\n }\n });\n\n return result;\n}\n\nexport const defaultSpotlightFilter: SpotlightFilterFunction = (_query, data) => {\n const query = _query.trim().toLowerCase();\n const priorityMatrix: SpotlightActionData[][] = [[], []];\n const flatActions = getFlatActions(data);\n flatActions.forEach((item) => {\n if (item.label?.toLowerCase().includes(query)) {\n priorityMatrix[0].push(item);\n } else if (\n item.description?.toLowerCase().includes(query) ||\n getKeywords(item.keywords).includes(query)\n ) {\n priorityMatrix[1].push(item);\n }\n });\n\n return flatActionsToGroups(priorityMatrix.flat());\n};\n"],"mappings":";;AAOA,SAAS,YAAY,UAAyC;CAC5D,IAAI,MAAM,QAAQ,QAAQ,GACxB,OAAO,SAC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