fix(spotlight): deduplicate registered results
This commit is contained in:
1 parent
45368e11fc
commit
2b9aa0fdf1
2 files changed
+30
-3
No files matched your search
@@ -15,6 +15,18 @@ interface GroupActionsProps<TOption extends Record<string, unknown>> {
|
||||
setChildrenOptions: (options: inferSearchInteractionOptions<"children">) => void;
|
||||
}
|
||||
|
||||
const getUniqueOptions = <TOption extends Record<string, unknown>>(options: TOption[], keyPath: keyof TOption) => {
|
||||
const optionKeys = new Set<unknown>();
|
||||
|
||||
return options.filter((option) => {
|
||||
const optionKey = option[keyPath];
|
||||
if (optionKeys.has(optionKey)) return false;
|
||||
|
||||
optionKeys.add(optionKey);
|
||||
return true;
|
||||
});
|
||||
};
|
||||
|
||||
export const SpotlightGroupActions = <TOption extends Record<string, unknown>>({
|
||||
group,
|
||||
query,
|
||||
@@ -33,7 +45,7 @@ export const SpotlightGroupActions = <TOption extends Record<string, unknown>>({
|
||||
return null;
|
||||
}
|
||||
|
||||
const filteredOptions = options
|
||||
const filteredOptions = getUniqueOptions(options, group.keyPath)
|
||||
.filter((option) => ("filter" in group ? group.filter(query, option) : false))
|
||||
.sort((optionA, optionB) => {
|
||||
if ("sort" in group) {
|
||||
@@ -80,7 +92,7 @@ export const SpotlightGroupActions = <TOption extends Record<string, unknown>>({
|
||||
return null;
|
||||
}
|
||||
|
||||
return options.data.map((option) => (
|
||||
return getUniqueOptions(options.data, group.keyPath).map((option) => (
|
||||
<SpotlightGroupActionItem
|
||||
key={option[group.keyPath] as never}
|
||||
option={option}
|
||||
|
||||
@@ -63,7 +63,22 @@ const createSpotlightContext = (displayName: string) => {
|
||||
});
|
||||
}, []);
|
||||
|
||||
const items = useMemo(() => Array.from(itemsMap.values()).flatMap(({ items }) => items), [itemsMap]);
|
||||
const items = useMemo(() => {
|
||||
const uniqueItems: ContextSpecificItem[] = [];
|
||||
const itemKeys = new Set<string>();
|
||||
|
||||
for (const registration of itemsMap.values()) {
|
||||
for (const item of registration.items) {
|
||||
const itemKey = item.dedupeKey ?? item.id;
|
||||
if (itemKeys.has(itemKey)) continue;
|
||||
|
||||
itemKeys.add(itemKey);
|
||||
uniqueItems.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
return uniqueItems;
|
||||
}, [itemsMap]);
|
||||
const itemsContext = useMemo(() => ({ items }), [items]);
|
||||
const registration = useMemo(() => ({ registerItems, unregisterItems }), [registerItems, unregisterItems]);
|
||||
|
||||
|
||||
Reference in new issue
Block a user