1 import { TFunction } from "i18next"
2 import { DynamicSearchResultItem } from "../../components/search/types"
3 import type { Keys } from "../../providers/keybind-provider/types"
4 import { DEFAULT_SEARCH_ENTITIES } from "./default-search-entities"
5
6 export type SearchEntityTransform<TItem = any> = (
7 item: TItem
8 ) => DynamicSearchResultItem
9
10 /**
11 * The `t` signature handed to `groupLabel` functions. Intentionally wider than
12 * i18next's `TFunction`: custom entities translate with keys the dashboard's
13 * typed resources don't know about.
14 */
15 export type SearchLabelTranslator = (
16 key: string,
17 options?: Record<string, unknown>
18 ) => string
19
20 /**
21 * A "Jump to" entry for a search entity: a key sequence bound by the admin
22 * shell that also appears in the search palette's navigation group.
23 */
24 export type SearchEntityShortcut = {
25 keys: Keys
26 /**
27 * Label for the "Jump to" entry. Falls back to the entity's group label.
28 */
29 label?: string | ((t: SearchLabelTranslator) => string)
30 /**
31 * Whether the entry is grouped with the page or the settings shortcuts.
32 * Defaults to `pageShortcut`.
33 */
34 type?: "pageShortcut" | "settingShortcut"
35 to: string
36 }
37
38 export type SearchEntityDefinition<TItem = any> = {
39 /**
40 * Group heading shown in the search palette.
41 * - omitted → `t("app.search.groups.<name>")`, falling back to the entity name
42 * - string → used as-is (plain label)
43 * - function → called with the i18n `t` function
44 */
45 groupLabel?: string | ((t: SearchLabelTranslator) => string)
46 /**
47 * Optional "Jump to" navigation entry for the entity's list page. Omit it
48 * for entities that have no page of their own (e.g. nested entities).
49 */
50 shortcut?: SearchEntityShortcut
51 /**
52 * Maps a result from `/admin/search` to a palette item. Omit it for entries
53 * that only contribute a `shortcut`, i.e. pages that are navigable but hold
54 * nothing worth matching on (e.g. reservations). Such entries are skipped
55 * when the palette decides what to query and which areas to offer.
56 */
57 transform?: SearchEntityTransform<TItem>
58 }
59
60 /**
61 * Seeded with the built-in commerce set when this module is evaluated.
62 * Registering an entity requires importing this module, so the defaults are in
63 * place before any app or plugin registration runs — regardless of how the app
64 * entry was bundled. `defineSearchEntity` replaces in place and
65 * `clearSearchEntities` empties the map, with no ordering caveats.
66 */
67 const searchEntities = new Map<string, SearchEntityDefinition>(
68 Object.entries(DEFAULT_SEARCH_ENTITIES)
69 )
70
71 let customized = false
72
73 /**
74 * Registers (or replaces) a searchable entity for the admin global search.
75 *
76 * Apps and plugins can call this from `src/admin/search-entities.tsx` to add
77 * custom entities or override built-in transforms. Use {@link clearSearchEntities}
78 * first when replacing the default commerce set entirely.
79 *
80 * @example
81 * ```tsx
82 * import { defineSearchEntity } from "@medusajs/dashboard/lib"
83 *
84 * defineSearchEntity("organization", {
85 * groupLabel: "Organizations",
86 * shortcut: {
87 * keys: { Mac: ["G", "Z"] },
88 * label: "Go to Organizations",
89 * to: "/organizations",
90 * },
91 * transform: (org) => ({
92 * id: org.id,
93 * title: org.name,
94 * subtitle: org.billing_email,
95 * to: `/organizations/${org.id}`,
96 * value: `organization:${org.id}`,
97 * }),
98 * })
99 * ```
100 */
101 export function defineSearchEntity<TItem = any>(
102 name: string,
103 definition: SearchEntityDefinition<TItem>
104 ) {
105 searchEntities.set(name, definition)
106 customized = true
107 }
108
109 /**
110 * Removes every registered search entity, including the built-in commerce set.
111 * Call before re-registering a custom set when the default entities should not
112 * appear in admin search.
113 */
114 export function clearSearchEntities() {
115 searchEntities.clear()
116 customized = true
117 }
118
119 /**
120 * Whether an app or plugin has changed the registry. While untouched, the
121 * search UI lets `/admin/search` decide what "all areas" covers (engine-only
122 * when the Search Module is enabled) instead of enumerating the defaults.
123 */
124 export function isSearchRegistryCustomized(): boolean {
125 return customized
126 }
127
128 export function getSearchEntity(
129 name: string
130 ): SearchEntityDefinition | undefined {
131 return searchEntities.get(name)
132 }
133
134 /**
135 * The entities that can actually be searched, in registration order.
136 * Shortcut-only entries are left out — there is nothing to query for them.
137 */
138 export function getSearchEntityNames(): string[] {
139 return [...searchEntities.entries()]
140 .filter(([, definition]) => !!definition.transform)
141 .map(([name]) => name)
142 }
143
144 /**
145 * Whether `name` is registered as a searchable entity. False for shortcut-only
146 * entries.
147 */
148 export function hasSearchEntity(name: string): boolean {
149 return !!searchEntities.get(name)?.transform
150 }
151
152 export function resolveSearchEntityGroupLabel(
153 name: string,
154 t: TFunction | SearchLabelTranslator
155 ): string {
156 const translate = t as SearchLabelTranslator
157 const definition = searchEntities.get(name)
158 const label = definition?.groupLabel
159
160 if (typeof label === "function") {
161 return label(translate)
162 }
163
164 if (typeof label === "string") {
165 return label
166 }
167
168 // Custom entities usually have no translation for the conventional key, so
169 // show the entity name rather than the raw key.
170 return translate(`app.search.groups.${name}`, { defaultValue: name })
171 }
172
173 /**
174 * The "Jump to" entries of every registered entity that declares one, in
175 * registration order.
176 */
177 export function getSearchEntityShortcuts(): {
178 entity: string
179 shortcut: SearchEntityShortcut
180 }[] {
181 return [...searchEntities.entries()]
182 .filter(([, definition]) => !!definition.shortcut)
183 .map(([entity, definition]) => ({
184 entity,
185 shortcut: definition.shortcut!,
186 }))
187 }
188
189 export function resolveSearchEntityShortcutLabel(
190 name: string,
191 t: TFunction | SearchLabelTranslator
192 ): string {
193 const translate = t as SearchLabelTranslator
194 const label = searchEntities.get(name)?.shortcut?.label
195
196 if (typeof label === "function") {
197 return label(translate)
198 }
199
200 if (typeof label === "string") {
201 return label
202 }
203
204 return resolveSearchEntityGroupLabel(name, t)
205 }