|
1
|
"use client" |
|
2
|
|
|
3
|
import clsx from "clsx" |
|
4
|
import { |
|
5
|
CodeBlock, |
|
6
|
CodeBlockProps, |
|
7
|
HeadlineTags, |
|
8
|
ShadedBlock, |
|
9
|
useMobile, |
|
10
|
useScrollController, |
|
11
|
} from "docs-ui" |
|
12
|
import React, { useLayoutEffect, useRef, useState } from "react" |
|
13
|
import { HomepageHorizontalDottedSeparator } from "../DottedSeparator/Horizontal" |
|
14
|
import HomepageEdges from "../Edges" |
|
15
|
|
|
16
|
type Tab = { |
|
17
|
title: string |
|
18
|
code: CodeBlockProps |
|
19
|
textSection: { |
|
20
|
content: string |
|
21
|
link: { |
|
22
|
title: string |
|
23
|
link: string |
|
24
|
} |
|
25
|
} |
|
26
|
} |
|
27
|
|
|
28
|
const HomepageCodeTabs = () => { |
|
29
|
const [selectedTabIndex, setSelectedTabIndex] = useState(0) |
|
30
|
const { isMobile } = useMobile() |
|
31
|
const tabRefs = useRef<(HTMLButtonElement | null)[]>([]) |
|
32
|
const { scrollableElement, getScrolledTop } = useScrollController() |
|
33
|
|
|
34
|
const handleTabClick = (index: number) => { |
|
35
|
setSelectedTabIndex(index) |
|
36
|
} |
|
37
|
|
|
38
|
useLayoutEffect(() => { |
|
39
|
if (!isMobile || !tabRefs.current[selectedTabIndex]) { |
|
40
|
return |
|
41
|
} |
|
42
|
const element = tabRefs.current[selectedTabIndex] |
|
43
|
if (element) { |
|
44
|
const elementPosition = element.getBoundingClientRect().top |
|
45
|
const scrollTop = getScrolledTop() |
|
46
|
const offsetPosition = elementPosition + scrollTop - 52 |
|
47
|
|
|
48
|
scrollableElement?.scrollTo({ |
|
49
|
top: offsetPosition, |
|
50
|
behavior: "auto", |
|
51
|
}) |
|
52
|
} |
|
53
|
}, [isMobile, selectedTabIndex, getScrolledTop, scrollableElement]) |
|
54
|
|
|
55
|
const tabs: Tab[] = [ |
|
56
|
{ |
|
57
|
title: "Create API Route", |
|
58
|
textSection: { |
|
59
|
content: |
|
60
|
"Expose custom features with REST API routes, then consume them from your client applications.", |
|
61
|
link: { |
|
62
|
title: "API Routes", |
|
63
|
link: "/learn/fundamentals/api-routes", |
|
64
|
}, |
|
65
|
}, |
|
66
|
code: { |
|
67
|
lang: "ts", |
|
68
|
source: `export async function GET( |
|
69
|
req: MedusaRequest, |
|
70
|
res: MedusaResponse |
|
71
|
) { |
|
72
|
const query = req.scope.resolve("query") |
|
73
|
|
|
74
|
const { data } = await query.graph({ |
|
75
|
entity: "company", |
|
76
|
fields: ["id", "name"], |
|
77
|
filters: { name: "ACME" }, |
|
78
|
}) |
|
79
|
|
|
80
|
res.json({ |
|
81
|
companies: data |
|
82
|
}) |
|
83
|
}`, |
|
84
|
highlights: [ |
|
85
|
["1", "GET", "Create a GET endpoint."], |
|
86
|
[ |
|
87
|
"5", |
|
88
|
`query`, |
|
89
|
"use Query that retrieves data from a graph of\nall data models and their relations.", |
|
90
|
], |
|
91
|
["8", `"company"`, "Retrieve records of the `company` data model"], |
|
92
|
["13", "res.json", "Return a JSON response"], |
|
93
|
], |
|
94
|
}, |
|
95
|
}, |
|
96
|
{ |
|
97
|
title: "Build Workflows", |
|
98
|
textSection: { |
|
99
|
content: |
|
100
|
"Build flows as a series of steps, with retry mechanisms and tracking of each steps' status.", |
|
101
|
link: { |
|
102
|
title: "Workflows", |
|
103
|
link: "/learn/fundamentals/workflows", |
|
104
|
}, |
|
105
|
}, |
|
106
|
code: { |
|
107
|
lang: "ts", |
|
108
|
source: `const handleDeliveryWorkflow = createWorkflow( |
|
109
|
"handle-delivery", |
|
110
|
function (input: WorkflowInput) { |
|
111
|
notifyRestaurantStep(input.delivery_id) |
|
112
|
|
|
113
|
const order = createOrderStep(input.delivery_id) |
|
114
|
|
|
115
|
createFulfillmentStep(order) |
|
116
|
|
|
117
|
awaitDeliveryStep() |
|
118
|
|
|
119
|
return new WorkflowResponse("Delivery completed") |
|
120
|
} |
|
121
|
)`, |
|
122
|
highlights: [ |
|
123
|
[ |
|
124
|
"1", |
|
125
|
"createWorkflow", |
|
126
|
"Use the Workflows SDK to build a flow of a series of steps.", |
|
127
|
], |
|
128
|
["4", "notifyRestaurantStep", "Run steps in the worklfow"], |
|
129
|
[ |
|
130
|
"10", |
|
131
|
"awaitDeliveryStep", |
|
132
|
"Wait for background actions to finish execution\nbefore performing some steps.", |
|
133
|
], |
|
134
|
], |
|
135
|
}, |
|
136
|
}, |
|
137
|
{ |
|
138
|
title: "Add a Data Model", |
|
139
|
textSection: { |
|
140
|
content: |
|
141
|
"Create data models that represent tables in the database using Medusa's Data Model Language.", |
|
142
|
link: { |
|
143
|
title: "DML", |
|
144
|
link: "/learn/fundamentals/modules#1-create-data-model", |
|
145
|
}, |
|
146
|
}, |
|
147
|
code: { |
|
148
|
lang: "ts", |
|
149
|
source: `const DigitalProduct = model.define("digital_product", |
|
150
|
{ |
|
151
|
id: model.id().primaryKey(), |
|
152
|
name: model.text(), |
|
153
|
medias: model.hasMany(() => DigitalProductMedia, { |
|
154
|
mappedBy: "digitalProduct" |
|
155
|
}) |
|
156
|
}) |
|
157
|
.cascades({ |
|
158
|
delete: ["medias"] |
|
159
|
})`, |
|
160
|
highlights: [ |
|
161
|
[ |
|
162
|
"1", |
|
163
|
"model", |
|
164
|
"Use Medusa's Data Model Language to\nrepresent custom tables in the database.", |
|
165
|
], |
|
166
|
[ |
|
167
|
"4", |
|
168
|
"hasMany", |
|
169
|
"Create relations between models of the same module.", |
|
170
|
], |
|
171
|
], |
|
172
|
}, |
|
173
|
}, |
|
174
|
{ |
|
175
|
title: "Build a Custom Module", |
|
176
|
textSection: { |
|
177
|
content: |
|
178
|
"Build custom modules with commerce or architectural features and use them in API routes or workflows.", |
|
179
|
link: { |
|
180
|
title: "Modules", |
|
181
|
link: "/learn/fundamentals/modules", |
|
182
|
}, |
|
183
|
}, |
|
184
|
code: { |
|
185
|
lang: "ts", |
|
186
|
source: `class DigitalProductService extends MedusaService({ |
|
187
|
DigitalProduct, |
|
188
|
}) { |
|
189
|
async authorizeLicense() { |
|
190
|
console.log("License authorized!") |
|
191
|
} |
|
192
|
} |
|
193
|
|
|
194
|
export async function POST( |
|
195
|
req: MedusaRequest, |
|
196
|
res: MedusaResponse |
|
197
|
) { |
|
198
|
const moduleService = req.scope.resolve( |
|
199
|
"digitalProduct" |
|
200
|
) |
|
201
|
|
|
202
|
await moduleService.authorizeLicense() |
|
203
|
|
|
204
|
res.json({ success: true }) |
|
205
|
}`, |
|
206
|
highlights: [ |
|
207
|
[ |
|
208
|
"1", |
|
209
|
"DigitalProductModuleService", |
|
210
|
"Create a service that accesses\nthe database to manage data models.", |
|
211
|
], |
|
212
|
[ |
|
213
|
"1", |
|
214
|
"MedusaService", |
|
215
|
"Generate data-management methods\nfor your data models automatically.", |
|
216
|
], |
|
217
|
[ |
|
218
|
"13", |
|
219
|
"digitalProductModuleService", |
|
220
|
"Resolve the database from the Medusa container\nin routes and other resources.", |
|
221
|
], |
|
222
|
["17", "authorizeLicense", "Use the service's custom methods."], |
|
223
|
], |
|
224
|
}, |
|
225
|
}, |
|
226
|
{ |
|
227
|
title: "Link Data Models", |
|
228
|
textSection: { |
|
229
|
content: |
|
230
|
"Add custom properties to Medusa's data models using module links to build custom use cases.", |
|
231
|
link: { |
|
232
|
title: "Module Links", |
|
233
|
link: "/learn/fundamentals/module-links", |
|
234
|
}, |
|
235
|
}, |
|
236
|
code: { |
|
237
|
lang: "ts", |
|
238
|
source: `const DigitalProduct = model.define("digital_product", { |
|
239
|
id: model.id().primaryKey(), |
|
240
|
name: model.text(), |
|
241
|
}) |
|
242
|
|
|
243
|
export default defineLink( |
|
244
|
DigitalProductModule.linkable.digitalProduct, |
|
245
|
ProductModule.linkable.productVariant |
|
246
|
)`, |
|
247
|
highlights: [ |
|
248
|
[ |
|
249
|
"6", |
|
250
|
"defineLink", |
|
251
|
"Create a link between data models of different modules.", |
|
252
|
], |
|
253
|
], |
|
254
|
}, |
|
255
|
}, |
|
256
|
{ |
|
257
|
title: "Subscribe to Events", |
|
258
|
textSection: { |
|
259
|
content: |
|
260
|
"Handle events emitted by the Medusa application to perform custom actions.", |
|
261
|
link: { |
|
262
|
title: "Subscribers", |
|
263
|
link: "/learn/fundamentals/events-and-subscribers", |
|
264
|
}, |
|
265
|
}, |
|
266
|
code: { |
|
267
|
lang: "ts", |
|
268
|
source: `async function orderPlaced({ |
|
269
|
container, |
|
270
|
}: SubscriberArgs) { |
|
271
|
const moduleService = container.resolve( |
|
272
|
Modules.NOTIFICATION |
|
273
|
) |
|
274
|
|
|
275
|
await moduleService.createNotifications({ |
|
276
|
to: "customer@gmail.com", |
|
277
|
channel: "email", |
|
278
|
template: "order-placed" |
|
279
|
}) |
|
280
|
} |
|
281
|
|
|
282
|
export const config: SubscriberConfig = { |
|
283
|
event: "order.placed", |
|
284
|
}`, |
|
285
|
highlights: [ |
|
286
|
[ |
|
287
|
"1", |
|
288
|
"orderPlaced", |
|
289
|
"Define a subscriber that's\nexecuted when an event is emitted.", |
|
290
|
], |
|
291
|
[ |
|
292
|
"4", |
|
293
|
"notificationModuleService", |
|
294
|
"Resolve a module's main service\nto use its methods.", |
|
295
|
], |
|
296
|
["8", "createNotification", "Send an email to a customer."], |
|
297
|
[ |
|
298
|
"16", |
|
299
|
`"order.placed"`, |
|
300
|
"Execute the subscriber when an order is placed.", |
|
301
|
], |
|
302
|
], |
|
303
|
}, |
|
304
|
}, |
|
305
|
{ |
|
306
|
title: "Customize Admin", |
|
307
|
textSection: { |
|
308
|
content: |
|
309
|
"Inject widgets into predefined zones in the Medusa Admin, or add new pages.", |
|
310
|
link: { |
|
311
|
title: "Admin Widgets", |
|
312
|
link: "/learn/fundamentals/admin/widgets", |
|
313
|
}, |
|
314
|
}, |
|
315
|
code: { |
|
316
|
lang: "tsx", |
|
317
|
source: `const ProductBrandWidget = () => { |
|
318
|
const [brand, setBrand] = useState({ |
|
319
|
name: "Acme" |
|
320
|
}) |
|
321
|
|
|
322
|
return ( |
|
323
|
<Container> |
|
324
|
<Heading level="h2">Brand</Heading> |
|
325
|
{brand && <span>Name: {brand.name}</span>} |
|
326
|
</Container> |
|
327
|
) |
|
328
|
} |
|
329
|
|
|
330
|
export const config = defineWidgetConfig({ |
|
331
|
zone: "product.details.before", |
|
332
|
})`, |
|
333
|
highlights: [ |
|
334
|
[ |
|
335
|
"1", |
|
336
|
"ProductBrandWidget", |
|
337
|
"Create admin widgets as React components.", |
|
338
|
], |
|
339
|
[ |
|
340
|
"7", |
|
341
|
"Container", |
|
342
|
"Use Medusa's UI components in your customizations.", |
|
343
|
], |
|
344
|
[ |
|
345
|
"15", |
|
346
|
`"product.details.before"`, |
|
347
|
"Show the widget on the product details page.", |
|
348
|
], |
|
349
|
], |
|
350
|
}, |
|
351
|
}, |
|
352
|
{ |
|
353
|
title: "Integrate Systems", |
|
354
|
textSection: { |
|
355
|
content: |
|
356
|
"Build workflows around multiple systems to add more powerful features to Medusa.", |
|
357
|
link: { |
|
358
|
title: "Integrate Systems", |
|
359
|
link: "/learn/customization/integrate-systems", |
|
360
|
}, |
|
361
|
}, |
|
362
|
code: { |
|
363
|
lang: "tsx", |
|
364
|
source: `const syncBrandsFromSystemWorkflow = createWorkflow( |
|
365
|
"sync-brands-from-system", |
|
366
|
() => { |
|
367
|
const toCreate = retrieveBrandsFromSystemStep() |
|
368
|
|
|
369
|
const created = createBrandsInMedusaStep({ |
|
370
|
brands: toCreate |
|
371
|
}) |
|
372
|
|
|
373
|
return new WorkflowResponse({ |
|
374
|
created, |
|
375
|
}) |
|
376
|
} |
|
377
|
)`, |
|
378
|
highlights: [ |
|
379
|
["1", "createWorkflow", "Integrate systems using workflows."], |
|
380
|
[ |
|
381
|
"4", |
|
382
|
"retrieveBrandsFromSystemStep", |
|
383
|
"Retrieve data from an external system.", |
|
384
|
], |
|
385
|
["6", "createBrandsInMedusaStep", "Sync data to Medusa."], |
|
386
|
], |
|
387
|
}, |
|
388
|
}, |
|
389
|
] |
|
390
|
|
|
391
|
return ( |
|
392
|
<div |
|
393
|
className={clsx( |
|
394
|
"w-full border-b border-medusa-border-base", |
|
395
|
"flex gap-0 flex-col lg:flex-row" |
|
396
|
)} |
|
397
|
> |
|
398
|
<div className="w-full lg:w-1/2 flex flex-col gap-0 border-r border-medusa-border-base"> |
|
399
|
{tabs.map((tab, index) => ( |
|
400
|
<React.Fragment key={index}> |
|
401
|
<button |
|
402
|
ref={(el) => { |
|
403
|
tabRefs.current[index] = el |
|
404
|
}} |
|
405
|
className={clsx( |
|
406
|
"px-2 py-1.5 appearance-none text-left", |
|
407
|
"flex flex-col gap-0.5 group" |
|
408
|
)} |
|
409
|
onClick={() => handleTabClick(index)} |
|
410
|
> |
|
411
|
<div className="flex items-center gap-0.5"> |
|
412
|
<span |
|
413
|
className={clsx( |
|
414
|
"text-code-label group-hover:text-medusa-fg-interactive", |
|
415
|
index === selectedTabIndex && "text-medusa-fg-interactive", |
|
416
|
index !== selectedTabIndex && "text-medusa-fg-subtle" |
|
417
|
)} |
|
418
|
> |
|
419
|
[ {index + 1} ] |
|
420
|
</span> |
|
421
|
<span className="text-medium-plus text-medusa-fg-base"> |
|
422
|
{tab.title} |
|
423
|
</span> |
|
424
|
</div> |
|
425
|
{index === selectedTabIndex && ( |
|
426
|
<p className="text-medusa-fg-subtle text-medium"> |
|
427
|
{tab.textSection.content} |
|
428
|
</p> |
|
429
|
)} |
|
430
|
</button> |
|
431
|
{index !== tabs.length - 1 && <HomepageHorizontalDottedSeparator />} |
|
432
|
{isMobile && index === selectedTabIndex && ( |
|
433
|
<> |
|
434
|
<CodeSection tab={tab} /> |
|
435
|
<HomepageHorizontalDottedSeparator /> |
|
436
|
</> |
|
437
|
)} |
|
438
|
</React.Fragment> |
|
439
|
))} |
|
440
|
</div> |
|
441
|
{!isMobile && <CodeSection tab={tabs[selectedTabIndex]} />} |
|
442
|
</div> |
|
443
|
) |
|
444
|
} |
|
445
|
|
|
446
|
export default HomepageCodeTabs |
|
447
|
|
|
448
|
type CodeSectionProps = { |
|
449
|
tab: Tab |
|
450
|
} |
|
451
|
|
|
452
|
const CodeSection = ({ tab }: CodeSectionProps) => { |
|
453
|
return ( |
|
454
|
<div |
|
455
|
className={clsx( |
|
456
|
"w-full lg:w-1/2 p-2 flex flex-col gap-2 justify-center bg-medusa-bg-component relative" |
|
457
|
)} |
|
458
|
> |
|
459
|
<ShadedBlock className="!h-2" /> |
|
460
|
<CodeBlock |
|
461
|
{...tab.code} |
|
462
|
noCopy={true} |
|
463
|
noReport={true} |
|
464
|
noAskAi={true} |
|
465
|
forceNoTitle={true} |
|
466
|
wrapperClassName="h-full !rounded-none" |
|
467
|
className={clsx("overflow-auto h-full max-h-full !mb-0 !rounded-none")} |
|
468
|
innerClassName="h-full" |
|
469
|
animateTokenHighlights |
|
470
|
blockStyle="subtle" |
|
471
|
/> |
|
472
|
<ShadedBlock className="!h-2" /> |
|
473
|
<HeadlineTags |
|
474
|
tags={[ |
|
475
|
tab.textSection.link.title, |
|
476
|
{ |
|
477
|
text: "Learn more", |
|
478
|
link: tab.textSection.link.link, |
|
479
|
}, |
|
480
|
]} |
|
481
|
/> |
|
482
|
<HomepageEdges /> |
|
483
|
</div> |
|
484
|
) |
|
485
|
} |