Refactor CommandMenu to prevent duplicate scripts across categories

This commit is contained in:
Bram Suurd
2025-06-25 17:04:05 +02:00
parent 5ad9323944
commit 319cf3167e

View File

@ -134,9 +134,27 @@ export default function CommandMenu() {
<CommandInput placeholder="Search for a script..." />
<CommandList>
<CommandEmpty>{isLoading ? "Loading..." : "No scripts found."}</CommandEmpty>
{links.map((category) => (
{(() => {
// Track seen scripts globally to avoid duplicates across all categories
const globalSeenScripts = new Set<string>();
return links.map((category) => {
const uniqueScripts = category.scripts.filter((script) => {
if (globalSeenScripts.has(script.slug)) {
return false;
}
globalSeenScripts.add(script.slug);
return true;
});
// Only render category if it has unique scripts
if (uniqueScripts.length === 0) {
return null;
}
return (
<CommandGroup key={`category:${category.name}`} heading={category.name}>
{category.scripts.map((script) => (
{uniqueScripts.map((script) => (
<CommandItem
key={`script:${script.slug}`}
value={`${script.slug}-${script.name}`}
@ -161,7 +179,9 @@ export default function CommandMenu() {
</CommandItem>
))}
</CommandGroup>
))}
);
});
})()}
</CommandList>
</CommandDialog>
</>