-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathintegration.patch
More file actions
581 lines (573 loc) · 18 KB
/
Copy pathintegration.patch
File metadata and controls
581 lines (573 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
diff --git a/core/.env.example b/core/.env.example
index b0f4ee46..e962f421 100644
--- a/core/.env.example
+++ b/core/.env.example
@@ -1,3 +1,7 @@
+# Makeswift Site API Key
+# In the Makeswift builder, go to Settings > Host and copy the API key for the site.
+MAKESWIFT_SITE_API_KEY=
+
# The hash visible in the subject store's URL when signed in to the store control panel.
# The control panel URL is of the form `https://store-{hash}.mybigcommerce.com`.
BIGCOMMERCE_STORE_HASH=
diff --git a/core/app/[locale]/(default)/[...rest]/page.tsx b/core/app/[locale]/(default)/[...rest]/page.tsx
index 71d40507..2d20fa4f 100644
--- a/core/app/[locale]/(default)/[...rest]/page.tsx
+++ b/core/app/[locale]/(default)/[...rest]/page.tsx
@@ -1,5 +1,46 @@
+import { Page as MakeswiftPage } from '@makeswift/runtime/next';
+import { getSiteVersion } from '@makeswift/runtime/next/server';
import { notFound } from 'next/navigation';
-export default function CatchAllPage() {
- notFound();
+import { defaultLocale, locales } from '~/i18n/routing';
+import { client } from '~/lib/makeswift/client';
+import { MakeswiftProvider } from '~/lib/makeswift/provider';
+
+interface CatchAllParams {
+ locale: string;
+ rest: string[];
+}
+
+export async function generateStaticParams() {
+ const pages = await client.getPages().toArray();
+
+ return pages
+ .filter((page) => page.path !== '/')
+ .flatMap((page) =>
+ locales.map((locale) => ({
+ rest: page.path.split('/').filter((segment) => segment !== ''),
+ // Remove eslint disable once more locales are added
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+ locale: locale === defaultLocale ? undefined : locale,
+ })),
+ );
+}
+
+export default async function CatchAllPage({ params }: { params: CatchAllParams }) {
+ const path = `/${params.rest.join('/')}`;
+
+ const snapshot = await client.getPageSnapshot(path, {
+ siteVersion: getSiteVersion(),
+ locale: params.locale === defaultLocale ? undefined : params.locale,
+ });
+
+ if (snapshot == null) return notFound();
+
+ return (
+ <MakeswiftProvider>
+ <MakeswiftPage snapshot={snapshot} />
+ </MakeswiftProvider>
+ );
}
diff --git a/core/app/[locale]/layout.tsx b/core/app/[locale]/layout.tsx
index faf15d53..a66fb02b 100644
--- a/core/app/[locale]/layout.tsx
+++ b/core/app/[locale]/layout.tsx
@@ -1,3 +1,4 @@
+import { DraftModeScript } from '@makeswift/runtime/next/server';
import { Analytics } from '@vercel/analytics/react';
import { SpeedInsights } from '@vercel/speed-insights/next';
import type { Metadata } from 'next';
@@ -73,6 +74,9 @@ export default function RootLayout({ children, params: { locale } }: Props) {
return (
<html className={`${inter.variable} font-sans`} lang={locale}>
+ <head>
+ <DraftModeScript />
+ </head>
<body className="flex h-screen min-w-[375px] flex-col">
<Notifications />
<NextIntlClientProvider locale={locale} messages={messages}>
diff --git a/core/app/api/makeswift/[...makeswift]/route.ts b/core/app/api/makeswift/[...makeswift]/route.ts
new file mode 100644
index 00000000..52186316
--- /dev/null
+++ b/core/app/api/makeswift/[...makeswift]/route.ts
@@ -0,0 +1,14 @@
+import { MakeswiftApiHandler } from '@makeswift/runtime/next/server';
+import { strict } from 'assert';
+
+import { runtime } from '~/lib/makeswift/runtime';
+
+strict(process.env.MAKESWIFT_SITE_API_KEY, 'MAKESWIFT_SITE_API_KEY is required');
+
+const handler = MakeswiftApiHandler(process.env.MAKESWIFT_SITE_API_KEY, {
+ runtime,
+ apiOrigin: process.env.MAKESWIFT_API_ORIGIN,
+ appOrigin: process.env.MAKESWIFT_APP_ORIGIN,
+});
+
+export { handler as GET, handler as POST };
diff --git a/core/app/api/makeswift/draft-mode/route.ts b/core/app/api/makeswift/draft-mode/route.ts
new file mode 100644
index 00000000..4013f3ff
--- /dev/null
+++ b/core/app/api/makeswift/draft-mode/route.ts
@@ -0,0 +1,10 @@
+import { draftMode } from 'next/headers';
+import { NextRequest } from 'next/server';
+
+export const GET = (request: NextRequest) => {
+ if (request.headers.get('x-makeswift-api-key') === process.env.MAKESWIFT_SITE_API_KEY) {
+ draftMode().enable();
+ }
+
+ return new Response(null);
+};
diff --git a/core/app/api/product-card-carousel/[type]/route.ts b/core/app/api/product-card-carousel/[type]/route.ts
new file mode 100644
index 00000000..bea1b56f
--- /dev/null
+++ b/core/app/api/product-card-carousel/[type]/route.ts
@@ -0,0 +1,52 @@
+import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client';
+import { NextRequest, NextResponse } from 'next/server';
+
+import { getSessionCustomerAccessToken } from '~/auth';
+import { client } from '~/client';
+import { graphql } from '~/client/graphql';
+import { ProductCardCarouselFragment } from '~/components/product-card-carousel/fragment';
+
+const GetProductCardCarousel = graphql(
+ `
+ query GetProductCardCarousel {
+ site {
+ newestProducts(first: 12) {
+ edges {
+ node {
+ ...ProductCardCarouselFragment
+ }
+ }
+ }
+ featuredProducts(first: 12) {
+ edges {
+ node {
+ ...ProductCardCarouselFragment
+ }
+ }
+ }
+ }
+ }
+ `,
+ [ProductCardCarouselFragment],
+);
+
+export const GET = async (
+ _request: NextRequest,
+ { params }: { params: { type: 'newest' | 'featured' } },
+) => {
+ const customerAccessToken = await getSessionCustomerAccessToken();
+ const { type } = params;
+
+ const { data } = await client.fetch({
+ document: GetProductCardCarousel,
+ customerAccessToken,
+ });
+
+ if (type === 'newest') {
+ return NextResponse.json(removeEdgesAndNodes(data.site.newestProducts));
+ }
+
+ return NextResponse.json(removeEdgesAndNodes(data.site.featuredProducts));
+};
diff --git a/core/components/ui/accordions/accordion.makeswift.tsx b/core/components/ui/accordions/accordion.makeswift.tsx
new file mode 100644
index 00000000..a70a94e8
--- /dev/null
+++ b/core/components/ui/accordions/accordion.makeswift.tsx
@@ -0,0 +1,35 @@
+import { List, Select, Shape, Slot, TextInput } from '@makeswift/runtime/controls';
+
+import { Accordions } from '~/components/ui/accordions';
+import { runtime } from '~/lib/makeswift/runtime';
+
+runtime.registerComponent(Accordions, {
+ type: 'catalyst-accordion',
+ label: 'Catalyst / Accordion',
+ props: {
+ accordions: List({
+ label: 'Accordions',
+ type: Shape({
+ type: {
+ content: Slot(),
+ title: TextInput({
+ label: 'Title',
+ defaultValue: 'Lorem Ipsum?',
+ placeholder: 'Unique value',
+ }),
+ },
+ }),
+ getItemLabel() {
+ return 'Slot';
+ },
+ }),
+ type: Select({
+ label: 'Type',
+ options: [
+ { label: 'Single', value: 'single' },
+ { label: 'Multiple', value: 'multiple' },
+ ],
+ defaultValue: 'single',
+ }),
+ },
+});
diff --git a/core/components/ui/carousel/carousel.makeswift.tsx b/core/components/ui/carousel/carousel.makeswift.tsx
new file mode 100644
index 00000000..c2c1d9a6
--- /dev/null
+++ b/core/components/ui/carousel/carousel.makeswift.tsx
@@ -0,0 +1,75 @@
+import { Select, Style, TextInput } from '@makeswift/runtime/controls';
+import { useEffect, useState } from 'react';
+
+import { ResultOf } from '~/client/graphql';
+import { ProductCardCarouselFragment } from '~/components/product-card-carousel/fragment';
+import { ProductCard } from '~/components/ui/product-card';
+import { runtime } from '~/lib/makeswift/runtime';
+
+import { Carousel } from './carousel';
+
+interface Props {
+ title: string;
+ type: 'newest' | 'featured';
+ className?: string;
+}
+
+type Product = ResultOf<typeof ProductCardCarouselFragment>;
+
+runtime.registerComponent(
+ function MakeswiftCarousel({ title, type, className }: Props) {
+ const [products, setProducts] = useState<Product[]>([]);
+
+ useEffect(() => {
+ const fetchProducts = async () => {
+ const response = await fetch(`/api/product-card-carousel/${type}`);
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
+ const data = (await response.json()) as Product[];
+
+ setProducts(data);
+ };
+
+ void fetchProducts();
+ }, [type]);
+
+ const items = products.map((product) => (
+ <ProductCard
+ href={product.path}
+ id={product.entityId.toString()}
+ image={
+ product.defaultImage
+ ? { src: product.defaultImage.url, altText: product.defaultImage.altText }
+ : undefined
+ }
+ imagePriority={false}
+ imageSize="square"
+ key={product.entityId}
+ name={product.name}
+ showCompare={false}
+ subtitle={product.brand?.name}
+ />
+ ));
+
+ return (
+ <div className={className}>
+ <Carousel className="mb-14" products={items} title={title} />
+ </div>
+ );
+ },
+ {
+ type: 'catalyst-carousel',
+ label: 'Catalyst / Carousel',
+ props: {
+ className: Style(),
+ title: TextInput({ label: 'Title', defaultValue: 'Carousel' }),
+ type: Select({
+ label: 'Type',
+ options: [
+ { label: 'Newest', value: 'newest' },
+ { label: 'Featured', value: 'featured' },
+ ],
+ defaultValue: 'newest',
+ }),
+ },
+ },
+);
diff --git a/core/components/ui/slideshow/slideshow.makeswift.tsx b/core/components/ui/slideshow/slideshow.makeswift.tsx
new file mode 100644
index 00000000..4010405b
--- /dev/null
+++ b/core/components/ui/slideshow/slideshow.makeswift.tsx
@@ -0,0 +1,83 @@
+import { List, Number, Shape, Style, TextInput } from '@makeswift/runtime/controls';
+import { StaticImageData } from 'next/image';
+
+import { runtime } from '~/lib/makeswift/runtime';
+
+import { Slideshow } from './slideshow';
+
+interface Link {
+ label: string;
+ href: string;
+}
+
+interface Image {
+ alt: string;
+ blurDataUrl?: string;
+ src: string | StaticImageData;
+}
+
+interface Slide {
+ cta?: Link;
+ description?: string;
+ image?: Image;
+ title: string;
+}
+
+interface Props {
+ className?: string;
+ interval?: number;
+ slides: Slide[];
+}
+
+runtime.registerComponent(
+ function MakeswiftSlideshow({ slides, interval, className }: Props) {
+ return (
+ <Slideshow
+ className={className}
+ interval={interval}
+ slides={slides.map((slide) => ({
+ cta: slide.cta ? { label: slide.cta.label, href: slide.cta.href } : undefined,
+ description: slide.description,
+ image: slide.image
+ ? {
+ altText: slide.image.alt,
+ blurDataUrl: slide.image.blurDataUrl,
+ src: slide.image.src,
+ }
+ : undefined,
+ title: slide.title,
+ }))}
+ />
+ );
+ },
+ {
+ type: 'catalyst-slideshow',
+ label: 'Catalyst / Slideshow',
+ props: {
+ className: Style(),
+ interval: Number({ label: 'Interval' }),
+ slides: List({
+ label: 'Slides',
+ type: Shape({
+ type: {
+ title: TextInput({ label: 'Title', defaultValue: 'Great Deals' }),
+ description: TextInput({
+ label: 'Description',
+ defaultValue:
+ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.',
+ }),
+ cta: Shape({
+ type: {
+ label: TextInput({ label: 'Label', defaultValue: 'Shop now' }),
+ href: TextInput({ label: 'Href', defaultValue: '/#' }),
+ },
+ }),
+ },
+ }),
+ getItemLabel(item) {
+ return item?.title ?? 'Slide';
+ },
+ }),
+ },
+ },
+);
diff --git a/core/components/ui/tabs/tabs.makeswift.tsx b/core/components/ui/tabs/tabs.makeswift.tsx
new file mode 100644
index 00000000..8f8135fd
--- /dev/null
+++ b/core/components/ui/tabs/tabs.makeswift.tsx
@@ -0,0 +1,32 @@
+import { List, Shape, Slot, Style, TextInput } from '@makeswift/runtime/controls';
+
+import { runtime } from '~/lib/makeswift/runtime';
+
+import { Tabs } from './tabs';
+
+runtime.registerComponent(Tabs, {
+ type: 'catalyst-tabs',
+ label: 'Catalyst / Tabs',
+ props: {
+ className: Style(),
+ tabs: List({
+ label: 'Tabs',
+ type: Shape({
+ type: {
+ content: Slot(),
+ title: Slot(),
+ value: TextInput({
+ label: 'Unique value',
+ defaultValue: Math.random().toString(36).substring(7),
+ placeholder: 'Unique value',
+ }),
+ },
+ }),
+ }),
+ label: TextInput({
+ label: 'Label',
+ defaultValue: 'Tabs',
+ placeholder: 'Label',
+ }),
+ },
+});
diff --git a/core/components/ui/tabs/tabs.tsx b/core/components/ui/tabs/tabs.tsx
index 2178a860..df24b2d4 100644
--- a/core/components/ui/tabs/tabs.tsx
+++ b/core/components/ui/tabs/tabs.tsx
@@ -10,7 +10,7 @@ interface Props {
className?: string;
defaultValue?: string;
label: string;
- onValueChange: (value: string) => void;
+ onValueChange?: (value: string) => void;
tabs: Tab[];
value?: string;
}
diff --git a/core/lib/makeswift/client.ts b/core/lib/makeswift/client.ts
new file mode 100644
index 00000000..c1dc2108
--- /dev/null
+++ b/core/lib/makeswift/client.ts
@@ -0,0 +1,11 @@
+import { Makeswift } from '@makeswift/runtime/next';
+import { strict } from 'assert';
+
+import { runtime } from '~/lib/makeswift/runtime';
+
+strict(process.env.MAKESWIFT_SITE_API_KEY, 'MAKESWIFT_SITE_API_KEY is required');
+
+export const client = new Makeswift(process.env.MAKESWIFT_SITE_API_KEY, {
+ runtime,
+ apiOrigin: process.env.MAKESWIFT_API_ORIGIN,
+});
diff --git a/core/lib/makeswift/components.ts b/core/lib/makeswift/components.ts
new file mode 100644
index 00000000..ed2a9db3
--- /dev/null
+++ b/core/lib/makeswift/components.ts
@@ -0,0 +1,4 @@
+import '~/components/ui/accordions/accordion.makeswift';
+import '~/components/ui/carousel/carousel.makeswift';
+import '~/components/ui/slideshow/slideshow.makeswift';
+import '~/components/ui/tabs/tabs.makeswift';
diff --git a/core/lib/makeswift/provider.tsx b/core/lib/makeswift/provider.tsx
new file mode 100644
index 00000000..674aab77
--- /dev/null
+++ b/core/lib/makeswift/provider.tsx
@@ -0,0 +1,14 @@
+'use client';
+
+import { ReactRuntimeProvider, RootStyleRegistry } from '@makeswift/runtime/next';
+
+import { runtime } from '~/lib/makeswift/runtime';
+import '~/lib/makeswift/components';
+
+export function MakeswiftProvider({ children }: { children: React.ReactNode }) {
+ return (
+ <ReactRuntimeProvider runtime={runtime}>
+ <RootStyleRegistry>{children}</RootStyleRegistry>
+ </ReactRuntimeProvider>
+ );
+}
diff --git a/core/lib/makeswift/runtime.ts b/core/lib/makeswift/runtime.ts
new file mode 100644
index 00000000..e988b5cf
--- /dev/null
+++ b/core/lib/makeswift/runtime.ts
@@ -0,0 +1,2 @@
+import { ReactRuntime } from '@makeswift/runtime/react';
+
diff --git a/core/middleware.ts b/core/middleware.ts
index c9928bdd..1724c1ea 100644
--- a/core/middleware.ts
+++ b/core/middleware.ts
@@ -2,9 +2,16 @@ import { composeMiddlewares } from './middlewares/compose-middlewares';
import { withAuth } from './middlewares/with-auth';
import { withChannelId } from './middlewares/with-channel-id';
import { withIntl } from './middlewares/with-intl';
+import { withMakeswift } from './middlewares/with-makeswift';
import { withRoutes } from './middlewares/with-routes';
-export const middleware = composeMiddlewares(withAuth, withIntl, withChannelId, withRoutes);
+export const middleware = composeMiddlewares(
+ withAuth,
+ withMakeswift,
+ withIntl,
+ withChannelId,
+ withRoutes,
+);
export const config = {
matcher: [
diff --git a/core/middlewares/with-makeswift.ts b/core/middlewares/with-makeswift.ts
new file mode 100644
index 00000000..e1eed268
--- /dev/null
+++ b/core/middlewares/with-makeswift.ts
@@ -0,0 +1,39 @@
+import { NextRequest } from 'next/server';
+import { parse as parseSetCookie } from 'set-cookie-parser';
+
+import { MiddlewareFactory } from './compose-middlewares';
+
+export const withMakeswift: MiddlewareFactory = (middleware) => {
+ return async (request, event) => {
+ const apiKey = request.nextUrl.searchParams.get('x-makeswift-draft-mode');
+
+ if (apiKey === process.env.MAKESWIFT_SITE_API_KEY) {
+ const response = await fetch(new URL('/api/makeswift/draft-mode', request.nextUrl.origin), {
+ headers: {
+ 'x-makeswift-api-key': apiKey,
+ },
+ });
+
+ const cookies = parseSetCookie(response.headers.get('set-cookie') || '');
+ const prerenderBypassValue = cookies.find((c) => c.name === '__prerender_bypass')?.value;
+
+ if (!prerenderBypassValue) {
+ return middleware(request, event);
+ }
+
+ // https://github.com/vercel/next.js/issues/52967#issuecomment-1644675602
+ // if we don't pass request twice, headers are stripped
+ const proxiedRequest = new NextRequest(request, request);
+
+ proxiedRequest.cookies.set('__prerender_bypass', prerenderBypassValue);
+ proxiedRequest.cookies.set(
+ 'x-makeswift-draft-data',
+ JSON.stringify({ makeswift: true, siteVersion: 'Working' }),
+ );
+
+ return middleware(proxiedRequest, event);
+ }
+
+ return middleware(request, event);
+ };
+};
diff --git a/core/next.config.js b/core/next.config.js
index de72984d..82c50cf3 100644
--- a/core/next.config.js
+++ b/core/next.config.js
@@ -1,6 +1,8 @@
// @ts-check
+const createWithMakeswift = require('@makeswift/runtime/next/plugin');
const createNextIntlPlugin = require('next-intl/plugin');
+const withMakeswift = createWithMakeswift({ previewMode: false });
const withNextIntl = createNextIntlPlugin();
const { cspHeader } = require('./lib/content-security-policy');
@@ -67,6 +69,9 @@ let nextConfig = {
// Apply withNextIntl to the config
nextConfig = withNextIntl(nextConfig);
+// Apply withMakeswift to the config
+nextConfig = withMakeswift(nextConfig);
+
if (process.env.ANALYZE === 'true') {
const withBundleAnalyzer = require('@next/bundle-analyzer')();