Skip to content

Commit 80328c4

Browse files
authored
Merge pull request framer#402 from framer/global-search/order-results
Global Search: Order Results and Search Menu items
2 parents 03c5607 + 753df04 commit 80328c4

3 files changed

Lines changed: 20 additions & 9 deletions

File tree

plugins/global-search/src/components/SearchScene.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { framer, type MenuItem } from "framer-plugin"
22
import { useCallback, useDeferredValue, useEffect, useMemo, useState } from "react"
33
import { FocusScope } from "react-aria"
44
import { cn } from "../utils/className"
5+
import { compareRootNodeTypeByPriority } from "../utils/filter/group-results"
56
import { useAsyncFilter } from "../utils/filter/useAsyncFilter"
67
import type { RootNodeType } from "../utils/indexer/types"
78
import { useIndexer } from "../utils/indexer/useIndexer"
@@ -88,11 +89,13 @@ const optionsMenuLabels = {
8889
Collection: "Collections",
8990
} as const satisfies Record<RootNodeType, string>
9091

92+
const sortedRootNodeTypes = entries(optionsEnabled).sort(([a], [b]) => compareRootNodeTypeByPriority(a, b))
93+
9194
function useOptionsMenuItems() {
9295
const [searchOptions, setSearchOptions] = useState<readonly RootNodeType[]>(defaultSearchOptions)
9396

9497
const optionsMenuItems = useMemo((): MenuItem[] => {
95-
return entries(optionsEnabled).map(([rootNode, enabled]) => ({
98+
return sortedRootNodeTypes.map(([rootNode, enabled]) => ({
9699
id: rootNode,
97100
label: optionsMenuLabels[rootNode],
98101
enabled,

plugins/global-search/src/components/ui/Menu.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export const Menu = memo(function Menu({ items, children, className }: MenuProps
2222
.showContextMenu(items, {
2323
location: { x: buttonBounds.right - 5, y: buttonBounds.bottom },
2424
placement: "bottom-left",
25-
width: 200,
2625
})
2726
.catch((error: unknown) => {
2827
framer.notify(

plugins/global-search/src/utils/filter/group-results.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { RootNodeType } from "../indexer/types"
12
import type { Result } from "./types"
23

34
export interface EntryResult {
@@ -21,11 +22,19 @@ export function groupResults(items: readonly Result[]): readonly EntryResult[] {
2122
}
2223
}
2324

24-
return Array.from(entryMap.values()).map((results): EntryResult => {
25-
const [first] = results
26-
return {
27-
entry: first.entry,
28-
results,
29-
}
30-
})
25+
return Array.from(entryMap.values())
26+
.map((results): EntryResult => {
27+
const [first] = results
28+
return {
29+
entry: first.entry,
30+
results,
31+
}
32+
})
33+
.sort((a, b) => compareRootNodeTypeByPriority(a.entry.rootNodeType, b.entry.rootNodeType))
34+
}
35+
36+
const orderOfResults = ["WebPageNode", "Collection", "ComponentNode"] satisfies RootNodeType[]
37+
38+
export function compareRootNodeTypeByPriority(a: RootNodeType, b: RootNodeType): number {
39+
return orderOfResults.indexOf(a) - orderOfResults.indexOf(b)
3140
}

0 commit comments

Comments
 (0)