Raw
1 ---
2 sidebar_label: "Search Entities"
3 ---
4
5 import { Table, TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `Search Entity Utilities - Admin Components`,
9 }
10
11 # {metadata.title}
12
13 <Note>
14
15 These utilities are available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0).
16
17 </Note>
18
19 The `@medusajs/dashboard/lib` subpath exports the utilities that manage the registry of entities in the Medusa Admin's global search. Use them to add your custom entities to the search palette, or to override the entities that Medusa registers by default.
20
21 ```tsx
22 import {
23 clearSearchEntities,
24 defineSearchEntity,
25 getSearchEntity,
26 getSearchEntityNames,
27 hasSearchEntity,
28 } from "@medusajs/dashboard/lib"
29 ```
30
31 Apps and plugins register their entities from the file `src/admin/search-entities.tsx`. The Medusa Admin seeds the registry with its built-in commerce entities before it runs your registrations, so you can override a built-in entity by registering it again under the same name.
32
33 ---
34
35 ## defineSearchEntity
36
37 Registers or replaces a searchable entity in the Medusa Admin's global search. The entity's definition sets the group heading in the search palette, an optional keyboard shortcut, and how to turn a search result into a palette item.
38
39 ```tsx title="src/admin/search-entities.tsx"
40 import { defineSearchEntity } from "@medusajs/dashboard/lib"
41
42 defineSearchEntity("organization", {
43 groupLabel: "Organizations",
44 shortcut: {
45 keys: { Mac: ["G", "Z"] },
46 label: "Go to Organizations",
47 to: "/organizations",
48 },
49 transform: (org) => ({
50 id: org.id,
51 title: org.name,
52 subtitle: org.billing_email,
53 to: `/organizations/${org.id}`,
54 value: `organization:${org.id}`,
55 }),
56 })
57 ```
58
59 ### Parameters
60
61 <TypeList
62 types={[
63 {
64 name: "name",
65 type: "`string`",
66 optional: false,
67 description: "The entity's name, which the Medusa Admin uses as the search area.",
68 },
69 {
70 name: "definition",
71 type: "`object`",
72 optional: false,
73 description: "The entity's definition.",
74 children: [
75 {
76 name: "groupLabel",
77 type: "`string` \\| `((t: SearchLabelTranslator) => string)`",
78 optional: true,
79 description: "The group heading shown in the search palette. Pass a string to use it as-is, or a function to build it from the translation function. When you omit it, the Medusa Admin translates the key `app.search.groups.<name>` and falls back to the entity's name.",
80 },
81 {
82 name: "shortcut",
83 type: "`object`",
84 optional: true,
85 description: `A "Jump to" entry that navigates to the entity's list page. Omit it for entities that have no page of their own.`,
86 children: [
87 {
88 name: "keys",
89 type: "`Keys`",
90 optional: false,
91 description: `The key sequence that triggers the shortcut, keyed by platform, such as \`{ Mac: ["G", "Z"] }\`.`,
92 },
93 {
94 name: "to",
95 type: "`string`",
96 optional: false,
97 description: "The path the shortcut navigates to.",
98 },
99 {
100 name: "label",
101 type: "`string` \\| `((t: SearchLabelTranslator) => string)`",
102 optional: true,
103 description: "The shortcut's label. Falls back to the entity's group label.",
104 },
105 {
106 name: "type",
107 type: `\`"pageShortcut"\` \\| \`"settingShortcut"\``,
108 optional: true,
109 defaultValue: "pageShortcut",
110 description: "Whether the entry is grouped with the page or the settings shortcuts.",
111 },
112 ],
113 },
114 {
115 name: "transform",
116 type: "`(item: TItem) => object`",
117 optional: true,
118 description: "Maps a result from the Medusa backend's search route to a palette item. Omit it for entries that only contribute a shortcut. Entities without it aren't queried and aren't offered as a search area. It returns an object with the following properties.",
119 children: [
120 {
121 name: "id",
122 type: "`string`",
123 optional: false,
124 description: "The item's ID.",
125 },
126 {
127 name: "title",
128 type: "`string`",
129 optional: false,
130 description: "The item's main label in the palette.",
131 },
132 {
133 name: "to",
134 type: "`string`",
135 optional: false,
136 description: "The path the item navigates to.",
137 },
138 {
139 name: "value",
140 type: "`string`",
141 optional: false,
142 description: "A unique value the palette uses to identify the item.",
143 },
144 {
145 name: "subtitle",
146 type: "`string`",
147 optional: true,
148 description: "A secondary label shown under the title.",
149 },
150 {
151 name: "thumbnail",
152 type: "`string`",
153 optional: true,
154 description: "The URL of an image shown next to the item.",
155 },
156 ],
157 },
158 ],
159 },
160 ]}
161 />
162
163 ---
164
165 ## clearSearchEntities
166
167 Removes every registered search entity, including the commerce entities that Medusa registers by default. Call it before registering your own set when the default entities shouldn't appear in the Medusa Admin's search.
168
169 ```tsx title="src/admin/search-entities.tsx"
170 import {
171 clearSearchEntities,
172 defineSearchEntity,
173 } from "@medusajs/dashboard/lib"
174
175 clearSearchEntities()
176
177 defineSearchEntity("brand", {
178 groupLabel: "Brands",
179 transform: (brand) => ({
180 id: brand.id,
181 title: brand.name,
182 to: `/brands/${brand.id}`,
183 value: `brand:${brand.id}`,
184 }),
185 })
186 ```
187
188 ---
189
190 ## getSearchEntity
191
192 Returns a registered entity's definition by its name.
193
194 ```tsx
195 import { getSearchEntity } from "@medusajs/dashboard/lib"
196
197 const definition = getSearchEntity("brand")
198 ```
199
200 ### Parameters
201
202 <TypeList
203 types={[
204 {
205 name: "name",
206 type: "`string`",
207 optional: false,
208 description: "The entity's name.",
209 },
210 ]}
211 />
212
213 ### Returns
214
215 Returns the `SearchEntityDefinition`, or `undefined` when no entity is registered under that name.
216
217 ---
218
219 ## getSearchEntityNames
220
221 Returns the names of the entities that can be searched, in registration order. Entities that only contribute a shortcut are left out, since there's nothing to query for them.
222
223 ```tsx
224 import {
225 getSearchEntityNames,
226 } from "@medusajs/dashboard/lib"
227
228 const names = getSearchEntityNames()
229 ```
230
231 ### Returns
232
233 Returns a `string[]` of the searchable entities' names.
234
235 ---
236
237 ## hasSearchEntity
238
239 Returns whether a name is registered as a searchable entity. It returns `false` for entries that only contribute a shortcut.
240
241 ```tsx
242 import { hasSearchEntity } from "@medusajs/dashboard/lib"
243
244 if (hasSearchEntity("brand")) {
245 // ...
246 }
247 ```
248
249 ### Parameters
250
251 <TypeList
252 types={[
253 {
254 name: "name",
255 type: "`string`",
256 optional: false,
257 description: "The entity's name.",
258 },
259 ]}
260 />
261
262 ### Returns
263
264 Returns a `boolean` that is `true` when the entity is registered and has a `transform` function.
265
266 ---
267
268 ## Types
269
270 <Table>
271 <Table.Header>
272 <Table.Row>
273 <Table.HeaderCell>
274 Type
275 </Table.HeaderCell>
276 <Table.HeaderCell>
277 Description
278 </Table.HeaderCell>
279 </Table.Row>
280 </Table.Header>
281 <Table.Body>
282 <Table.Row>
283 <Table.Cell>
284
285 `SearchEntityDefinition<TItem>`
286
287 </Table.Cell>
288 <Table.Cell>
289
290 An entity's definition, holding its `groupLabel`, `shortcut`, and `transform`.
291
292 </Table.Cell>
293 </Table.Row>
294 <Table.Row>
295 <Table.Cell>
296
297 `SearchEntityShortcut`
298
299 </Table.Cell>
300 <Table.Cell>
301
302 A "Jump to" entry with its key sequence, label, type, and path.
303
304 </Table.Cell>
305 </Table.Row>
306 <Table.Row>
307 <Table.Cell>
308
309 `SearchEntityTransform<TItem>`
310
311 </Table.Cell>
312 <Table.Cell>
313
314 The signature of a `transform` function, which maps a search result to a palette item.
315
316 </Table.Cell>
317 </Table.Row>
318 <Table.Row>
319 <Table.Cell>
320
321 `SearchLabelTranslator`
322
323 </Table.Cell>
324 <Table.Cell>
325
326 The translation function handed to a `groupLabel` or `label` function. It accepts a translation key and options, and returns a string.
327
328 </Table.Cell>
329 </Table.Row>
330 </Table.Body>
331 </Table>