create-brainerce-store 1.71.0 → 1.73.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -10
- package/dist/index.js +179 -107
- package/messages/en.json +14 -1
- package/messages/he.json +14 -1
- package/package.json +1 -1
- package/templates/nextjs/base/TRANSLATIONS.md +207 -200
- package/templates/nextjs/base/src/app/checkout/page.tsx +1074 -1018
- package/templates/nextjs/base/src/app/order-confirmation/page.tsx +21 -2
- package/templates/nextjs/base/src/components/account/order-history.tsx +371 -385
- package/templates/nextjs/base/src/components/account/order-status-timeline.tsx +85 -66
- package/templates/nextjs/base/src/core/hooks/use-cart-page.ts +127 -58
- package/templates/nextjs/base/src/core/lib/auth.ts +32 -39
- package/templates/nextjs/base/src/core/lib/brainerce.ts.ejs +5 -16
- package/templates/nextjs/base/src/core/providers/store-provider.tsx.ejs +3 -6
- package/templates/nextjs/base/src/ui/cart/cart-item.tsx +164 -146
- package/templates/nextjs/base/src/ui/cart/cart-view.tsx +176 -140
- package/templates/nextjs/base/src/ui/cart/reservation-countdown.tsx +137 -95
- package/templates/nextjs/base/src/ui/product/review-form.tsx +33 -11
- package/templates/nextjs/designs/atelier/ui/cart/cart-drawer.tsx +177 -163
- package/templates/nextjs/designs/atelier/ui/cart/cart-item.tsx +158 -140
- package/templates/nextjs/designs/atelier/ui/cart/cart-view.tsx +184 -147
- package/templates/nextjs/designs/atelier/ui/cart/reservation-countdown.tsx +131 -89
- package/templates/nextjs/designs/atelier/ui/product/review-form.tsx +30 -10
- package/templates/nextjs/ui-canvas/cart/cart-item.tsx +137 -123
- package/templates/nextjs/ui-canvas/cart/cart-view.tsx +140 -106
- package/templates/nextjs/ui-canvas/cart/reservation-countdown.tsx +124 -81
- package/templates/nextjs/ui-canvas/product/review-form.tsx +9 -1
|
@@ -1,95 +1,137 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useState, useEffect, useCallback } from 'react';
|
|
4
|
-
import { Clock } from 'lucide-react';
|
|
5
|
-
import type { ReservationInfo } from 'brainerce';
|
|
6
|
-
import { useTranslations } from '@/core/lib/translations';
|
|
7
|
-
import { cn } from '@/core/lib/utils';
|
|
8
|
-
|
|
9
|
-
interface ReservationCountdownProps {
|
|
10
|
-
reservation: ReservationInfo;
|
|
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
|
-
const
|
|
45
|
-
const
|
|
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
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState, useEffect, useCallback, useRef } from 'react';
|
|
4
|
+
import { Clock } from 'lucide-react';
|
|
5
|
+
import type { ReservationInfo } from 'brainerce';
|
|
6
|
+
import { useTranslations } from '@/core/lib/translations';
|
|
7
|
+
import { cn } from '@/core/lib/utils';
|
|
8
|
+
|
|
9
|
+
interface ReservationCountdownProps {
|
|
10
|
+
reservation: ReservationInfo;
|
|
11
|
+
/**
|
|
12
|
+
* Fired once per reservation window when the timer reaches zero, including
|
|
13
|
+
* when the page opens on an already-expired reservation. The handler is
|
|
14
|
+
* expected to refresh the cart and gate checkout: this component only
|
|
15
|
+
* displays, it never decides what is still purchasable. Wire it to
|
|
16
|
+
* `useCartPage().onReservationExpired`.
|
|
17
|
+
*/
|
|
18
|
+
onExpire?: () => void;
|
|
19
|
+
className?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function ReservationCountdown({
|
|
23
|
+
reservation,
|
|
24
|
+
onExpire,
|
|
25
|
+
className,
|
|
26
|
+
}: ReservationCountdownProps) {
|
|
27
|
+
const t = useTranslations('reservation');
|
|
28
|
+
// `null` means "not measured yet". Server-rendered HTML must not compute a
|
|
29
|
+
// time, or it disagrees with the client on hydration; starting at 0 instead
|
|
30
|
+
// would render the expired banner for one frame on a perfectly live
|
|
31
|
+
// reservation, and would fire onExpire on every mount.
|
|
32
|
+
const [remainingSeconds, setRemainingSeconds] = useState<number | null>(null);
|
|
33
|
+
|
|
34
|
+
const calculateRemaining = useCallback(() => {
|
|
35
|
+
if (!reservation.expiresAt) return 0;
|
|
36
|
+
const expiresAtMs = new Date(reservation.expiresAt).getTime();
|
|
37
|
+
const nowMs = Date.now();
|
|
38
|
+
return Math.max(0, Math.floor((expiresAtMs - nowMs) / 1000));
|
|
39
|
+
}, [reservation.expiresAt]);
|
|
40
|
+
|
|
41
|
+
// Report each expiry window at most once. Keyed on `expiresAt` rather than a
|
|
42
|
+
// boolean, so the cart refresh that expiry triggers (which remounts this
|
|
43
|
+
// component) cannot bounce straight back into a second report.
|
|
44
|
+
const expiresAt = reservation.expiresAt;
|
|
45
|
+
const reportedExpiryRef = useRef<string | null>(null);
|
|
46
|
+
const onExpireRef = useRef(onExpire);
|
|
47
|
+
// Kept in a ref, and synced in an effect rather than during render, so a
|
|
48
|
+
// caller passing an inline arrow does not restart the timer every render.
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
onExpireRef.current = onExpire;
|
|
51
|
+
}, [onExpire]);
|
|
52
|
+
|
|
53
|
+
const reportExpired = useCallback(() => {
|
|
54
|
+
if (!expiresAt) return;
|
|
55
|
+
if (reportedExpiryRef.current === expiresAt) return;
|
|
56
|
+
reportedExpiryRef.current = expiresAt;
|
|
57
|
+
onExpireRef.current?.();
|
|
58
|
+
}, [expiresAt]);
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
const initial = calculateRemaining();
|
|
62
|
+
setRemainingSeconds(initial);
|
|
63
|
+
if (initial <= 0) reportExpired();
|
|
64
|
+
|
|
65
|
+
const interval = setInterval(() => {
|
|
66
|
+
const remaining = calculateRemaining();
|
|
67
|
+
setRemainingSeconds(remaining);
|
|
68
|
+
|
|
69
|
+
if (remaining <= 0) {
|
|
70
|
+
clearInterval(interval);
|
|
71
|
+
reportExpired();
|
|
72
|
+
}
|
|
73
|
+
}, 1000);
|
|
74
|
+
|
|
75
|
+
return () => clearInterval(interval);
|
|
76
|
+
}, [calculateRemaining, reportExpired]);
|
|
77
|
+
|
|
78
|
+
if (!reservation.hasReservation) return null;
|
|
79
|
+
|
|
80
|
+
const displaySeconds = remainingSeconds ?? 0;
|
|
81
|
+
const minutes = Math.floor(displaySeconds / 60);
|
|
82
|
+
const seconds = displaySeconds % 60;
|
|
83
|
+
const isExpired = remainingSeconds !== null && remainingSeconds <= 0;
|
|
84
|
+
const isUrgent = displaySeconds > 0 && displaySeconds < 120;
|
|
85
|
+
|
|
86
|
+
const displayMessage = reservation.countdownMessage
|
|
87
|
+
? reservation.countdownMessage.replace(
|
|
88
|
+
'{time}',
|
|
89
|
+
`${minutes}:${seconds.toString().padStart(2, '0')}`
|
|
90
|
+
)
|
|
91
|
+
: null;
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<div
|
|
95
|
+
className={cn(
|
|
96
|
+
'flex items-center gap-3 rounded-lg px-4 py-3 text-sm',
|
|
97
|
+
isExpired
|
|
98
|
+
? 'bg-destructive/10 border-destructive/20 text-destructive border'
|
|
99
|
+
: isUrgent
|
|
100
|
+
? 'border border-orange-200 bg-orange-50 text-orange-800 dark:border-orange-800 dark:bg-orange-950/30 dark:text-orange-300'
|
|
101
|
+
: 'bg-primary/5 border-primary/20 text-foreground border',
|
|
102
|
+
className
|
|
103
|
+
)}
|
|
104
|
+
>
|
|
105
|
+
<Clock
|
|
106
|
+
className={cn(
|
|
107
|
+
'h-5 w-5 flex-shrink-0',
|
|
108
|
+
isExpired
|
|
109
|
+
? 'text-destructive'
|
|
110
|
+
: isUrgent
|
|
111
|
+
? 'text-orange-600 dark:text-orange-400'
|
|
112
|
+
: 'text-primary'
|
|
113
|
+
)}
|
|
114
|
+
aria-hidden="true"
|
|
115
|
+
/>
|
|
116
|
+
|
|
117
|
+
<div className="flex-1">
|
|
118
|
+
{isExpired ? (
|
|
119
|
+
<>
|
|
120
|
+
<p className="font-medium">{t('expired')}</p>
|
|
121
|
+
<p className="text-xs opacity-90">{t('expiredHint')}</p>
|
|
122
|
+
</>
|
|
123
|
+
) : displayMessage ? (
|
|
124
|
+
<p>{displayMessage}</p>
|
|
125
|
+
) : (
|
|
126
|
+
<p>
|
|
127
|
+
{isUrgent ? `${t('hurry')} ` : ''}
|
|
128
|
+
{t('reservedFor')}{' '}
|
|
129
|
+
<span className="font-semibold tabular-nums">
|
|
130
|
+
{minutes}:{seconds.toString().padStart(2, '0')}
|
|
131
|
+
</span>
|
|
132
|
+
</p>
|
|
133
|
+
)}
|
|
134
|
+
</div>
|
|
135
|
+
</div>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import { useEffect, useState } from 'react';
|
|
4
|
-
import { Star, X } from 'lucide-react';
|
|
4
|
+
import { ImagePlus, Star, X } from 'lucide-react';
|
|
5
5
|
import { getClient } from '@/core/lib/brainerce';
|
|
6
6
|
import { checkAuthStatus } from '@/core/lib/auth';
|
|
7
7
|
import { Link } from '@/core/lib/navigation';
|
|
@@ -371,7 +371,7 @@ function ReviewEditor({
|
|
|
371
371
|
setUploads((prev) => prev.filter((item) => item.key !== upload.key))
|
|
372
372
|
}
|
|
373
373
|
aria-label={t('removePhoto')}
|
|
374
|
-
className="
|
|
374
|
+
className="absolute end-1 top-1 rounded-full bg-black/60 p-1 text-white"
|
|
375
375
|
>
|
|
376
376
|
<X className="h-3 w-3" aria-hidden="true" />
|
|
377
377
|
</button>
|
|
@@ -380,16 +380,38 @@ function ReviewEditor({
|
|
|
380
380
|
</ul>
|
|
381
381
|
)}
|
|
382
382
|
|
|
383
|
-
<input
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
383
|
+
{/* A <label> wrapping a visually hidden input, not a bare file control.
|
|
384
|
+
The browser's own "Choose Files" button cannot be styled and looks
|
|
385
|
+
nothing like the rest of the form. Two things this has to do that the
|
|
386
|
+
native control did for free: `disabled` on a hidden input greys
|
|
387
|
+
NOTHING, so the label paints its own disabled state, and the line
|
|
388
|
+
underneath says WHY it is disabled rather than leaving a dead button.
|
|
389
|
+
The input stays a real input (no ref.click()) so keyboard and
|
|
390
|
+
assistive tech reach it, and focus-within restores the focus ring. */}
|
|
391
|
+
<Button asChild variant="outline" size="sm" className="w-fit">
|
|
392
|
+
<label
|
|
393
|
+
className={`focus-within:ring-ring inline-flex cursor-pointer items-center gap-2 focus-within:ring-2 ${
|
|
394
|
+
uploading || roomLeft <= 0 ? 'pointer-events-none opacity-50' : ''
|
|
395
|
+
}`}
|
|
396
|
+
>
|
|
397
|
+
<ImagePlus className="h-4 w-4" aria-hidden="true" />
|
|
398
|
+
{uploading ? t('photoUploading') : t('choosePhotos')}
|
|
399
|
+
<input
|
|
400
|
+
type="file"
|
|
401
|
+
accept="image/jpeg,image/png,image/webp,image/gif"
|
|
402
|
+
multiple
|
|
403
|
+
disabled={uploading || roomLeft <= 0}
|
|
404
|
+
onChange={handleFiles}
|
|
405
|
+
className="sr-only"
|
|
406
|
+
/>
|
|
407
|
+
</label>
|
|
408
|
+
</Button>
|
|
391
409
|
|
|
392
|
-
|
|
410
|
+
<p className="text-muted-foreground text-xs">
|
|
411
|
+
{roomLeft <= 0
|
|
412
|
+
? t('photoLimitReached', { max: String(photos.maxPerReview) })
|
|
413
|
+
: t('photoFormats', { mb: String(Math.round(photos.maxBytes / (1024 * 1024))) })}
|
|
414
|
+
</p>
|
|
393
415
|
{/* Say it BEFORE they submit. A shopper who uploads a photo, submits, and
|
|
394
416
|
then cannot see it will conclude the site is broken. */}
|
|
395
417
|
{photos.requiresApproval && (
|
|
@@ -1,163 +1,177 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Mini-cart drawer — a slide-over sheet anchored to the header cart side
|
|
5
|
-
* (logical `end`, so it hugs the cart icon in both RTL and LTR).
|
|
6
|
-
*
|
|
7
|
-
* Opens via `openCartDrawer()` (fired after a successful add-to-cart);
|
|
8
|
-
* the header cart icon itself still navigates to /cart — the drawer never
|
|
9
|
-
* hijacks that. Closes on overlay click, Escape, the X, or any CTA.
|
|
10
|
-
*
|
|
11
|
-
* Stays mounted so the slide transition runs both ways; when closed it is
|
|
12
|
-
* `inert` + aria-hidden so nothing inside is focusable.
|
|
13
|
-
*/
|
|
14
|
-
import { useEffect, useRef, useState } from 'react';
|
|
15
|
-
import { Link } from '@/core/lib/navigation';
|
|
16
|
-
import { formatPrice } from 'brainerce';
|
|
17
|
-
import { useCart } from '@/core/providers/store-provider';
|
|
18
|
-
import { useTranslations } from '@/core/lib/translations';
|
|
19
|
-
import { useCurrency } from '@/core/lib/use-currency';
|
|
20
|
-
import { CartItem } from '@/ui/cart/cart-item';
|
|
21
|
-
import { FreeShippingBar } from '@/ui/cart/free-shipping-bar';
|
|
22
|
-
import { cn } from '@/core/lib/utils';
|
|
23
|
-
import { IconBag, IconX } from '@/ui/shared/icons';
|
|
24
|
-
|
|
25
|
-
const OPEN_EVENT = 'cart-drawer:open';
|
|
26
|
-
|
|
27
|
-
/** Ask the mounted drawer to slide open (no-op during SSR). */
|
|
28
|
-
export function openCartDrawer() {
|
|
29
|
-
if (typeof window !== 'undefined') {
|
|
30
|
-
window.dispatchEvent(new Event(OPEN_EVENT));
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function CartDrawer() {
|
|
35
|
-
const [open, setOpen] = useState(false);
|
|
36
|
-
const { cart, itemCount, totals, refreshCart } = useCart();
|
|
37
|
-
const t = useTranslations('cart');
|
|
38
|
-
const tc = useTranslations('common');
|
|
39
|
-
const currency = useCurrency();
|
|
40
|
-
const closeRef = useRef<HTMLButtonElement>(null);
|
|
41
|
-
|
|
42
|
-
useEffect(() => {
|
|
43
|
-
const onOpen = () => setOpen(true);
|
|
44
|
-
window.addEventListener(OPEN_EVENT, onOpen);
|
|
45
|
-
return () => window.removeEventListener(OPEN_EVENT, onOpen);
|
|
46
|
-
}, []);
|
|
47
|
-
|
|
48
|
-
// Open state side-effects: Escape to close, scroll lock, focus the X.
|
|
49
|
-
useEffect(() => {
|
|
50
|
-
if (!open) return;
|
|
51
|
-
const onKey = (e: KeyboardEvent) => {
|
|
52
|
-
if (e.key === 'Escape') setOpen(false);
|
|
53
|
-
};
|
|
54
|
-
document.addEventListener('keydown', onKey);
|
|
55
|
-
const prevOverflow = document.body.style.overflow;
|
|
56
|
-
document.body.style.overflow = 'hidden';
|
|
57
|
-
closeRef.current?.focus();
|
|
58
|
-
return () => {
|
|
59
|
-
document.removeEventListener('keydown', onKey);
|
|
60
|
-
document.body.style.overflow = prevOverflow;
|
|
61
|
-
};
|
|
62
|
-
}, [open]);
|
|
63
|
-
|
|
64
|
-
const close = () => setOpen(false);
|
|
65
|
-
const items = cart?.items ?? [];
|
|
66
|
-
|
|
67
|
-
return (
|
|
68
|
-
<div
|
|
69
|
-
aria-hidden={!open}
|
|
70
|
-
inert={!open}
|
|
71
|
-
className={cn('fixed inset-0 z-50', !open && 'pointer-events-none')}
|
|
72
|
-
>
|
|
73
|
-
{/* Overlay */}
|
|
74
|
-
<div
|
|
75
|
-
onClick={close}
|
|
76
|
-
aria-hidden="true"
|
|
77
|
-
className={cn(
|
|
78
|
-
'bg-foreground/35 absolute inset-0 backdrop-blur-[2px] transition-opacity duration-300 motion-reduce:transition-none',
|
|
79
|
-
open ? 'opacity-100' : 'opacity-0'
|
|
80
|
-
)}
|
|
81
|
-
/>
|
|
82
|
-
|
|
83
|
-
{/* Panel — anchored at the logical end (same side as the cart icon) */}
|
|
84
|
-
<aside
|
|
85
|
-
role="dialog"
|
|
86
|
-
aria-modal="true"
|
|
87
|
-
aria-label={t('title')}
|
|
88
|
-
className={cn(
|
|
89
|
-
'bg-background absolute inset-y-0 end-0 flex w-full max-w-md flex-col shadow-2xl transition-transform duration-300 ease-out motion-reduce:transition-none sm:rounded-s-3xl sm:border-s',
|
|
90
|
-
open ? 'translate-x-0' : 'ltr:translate-x-full rtl:-translate-x-full'
|
|
91
|
-
)}
|
|
92
|
-
>
|
|
93
|
-
<header className="flex items-center gap-3 border-b px-5 py-4">
|
|
94
|
-
<h2 className="font-display text-foreground text-xl font-semibold">{t('title')}</h2>
|
|
95
|
-
{itemCount > 0 && (
|
|
96
|
-
<span className="bg-primary text-primary-foreground flex h-6 min-w-6 items-center justify-center rounded-full px-1.5 text-xs font-bold">
|
|
97
|
-
{itemCount}
|
|
98
|
-
</span>
|
|
99
|
-
)}
|
|
100
|
-
<button
|
|
101
|
-
ref={closeRef}
|
|
102
|
-
type="button"
|
|
103
|
-
onClick={close}
|
|
104
|
-
aria-label={tc('close')}
|
|
105
|
-
className="text-foreground hover:bg-secondary ms-auto flex h-10 w-10 items-center justify-center rounded-full transition-colors"
|
|
106
|
-
>
|
|
107
|
-
<IconX size={20} />
|
|
108
|
-
</button>
|
|
109
|
-
</header>
|
|
110
|
-
|
|
111
|
-
{items.length === 0 ? (
|
|
112
|
-
<div className="flex flex-1 flex-col items-center justify-center gap-3 px-6 text-center">
|
|
113
|
-
<span className="bg-secondary text-muted-foreground flex h-16 w-16 items-center justify-center rounded-full">
|
|
114
|
-
<IconBag size={24} />
|
|
115
|
-
</span>
|
|
116
|
-
<p className="font-display text-foreground text-lg font-semibold">{t('emptyTitle')}</p>
|
|
117
|
-
<p className="text-muted-foreground max-w-xs text-sm">{t('emptySubtitle')}</p>
|
|
118
|
-
<Link href="/products" onClick={close} className="btn-primary btn-sm mt-2">
|
|
119
|
-
{tc('continueShopping')}
|
|
120
|
-
</Link>
|
|
121
|
-
</div>
|
|
122
|
-
) : (
|
|
123
|
-
<>
|
|
124
|
-
<div className="flex-1 overflow-y-auto px-5 py-4">
|
|
125
|
-
<FreeShippingBar className="mb-4" />
|
|
126
|
-
<ul className="divide-y">
|
|
127
|
-
{items.map((item) => (
|
|
128
|
-
<li key={item.id} className="py-4 first:pt-0">
|
|
129
|
-
<CartItem item={item} onUpdate={refreshCart} />
|
|
130
|
-
</li>
|
|
131
|
-
))}
|
|
132
|
-
</ul>
|
|
133
|
-
</div>
|
|
134
|
-
|
|
135
|
-
<footer className="bg-secondary/40 border-t px-5 py-4">
|
|
136
|
-
<div className="flex items-center justify-between text-sm">
|
|
137
|
-
<span className="text-muted-foreground">{tc('subtotal')}</span>
|
|
138
|
-
<strong className="text-foreground text-base">
|
|
139
|
-
{formatPrice(totals.subtotal, { currency }) as string}
|
|
140
|
-
</strong>
|
|
141
|
-
</div>
|
|
142
|
-
{totals.discount > 0 && (
|
|
143
|
-
<div className="text-primary mt-1 flex items-center justify-between text-sm">
|
|
144
|
-
<span>{tc('discount')}</span>
|
|
145
|
-
<span>-{formatPrice(totals.discount, { currency }) as string}</span>
|
|
146
|
-
</div>
|
|
147
|
-
)}
|
|
148
|
-
<p className="text-muted-foreground mt-1 text-xs">{t('shippingAtCheckout')}</p>
|
|
149
|
-
<div className="mt-4 grid gap-2">
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Mini-cart drawer — a slide-over sheet anchored to the header cart side
|
|
5
|
+
* (logical `end`, so it hugs the cart icon in both RTL and LTR).
|
|
6
|
+
*
|
|
7
|
+
* Opens via `openCartDrawer()` (fired after a successful add-to-cart);
|
|
8
|
+
* the header cart icon itself still navigates to /cart — the drawer never
|
|
9
|
+
* hijacks that. Closes on overlay click, Escape, the X, or any CTA.
|
|
10
|
+
*
|
|
11
|
+
* Stays mounted so the slide transition runs both ways; when closed it is
|
|
12
|
+
* `inert` + aria-hidden so nothing inside is focusable.
|
|
13
|
+
*/
|
|
14
|
+
import { useEffect, useRef, useState } from 'react';
|
|
15
|
+
import { Link } from '@/core/lib/navigation';
|
|
16
|
+
import { formatPrice } from 'brainerce';
|
|
17
|
+
import { useCart } from '@/core/providers/store-provider';
|
|
18
|
+
import { useTranslations } from '@/core/lib/translations';
|
|
19
|
+
import { useCurrency } from '@/core/lib/use-currency';
|
|
20
|
+
import { CartItem } from '@/ui/cart/cart-item';
|
|
21
|
+
import { FreeShippingBar } from '@/ui/cart/free-shipping-bar';
|
|
22
|
+
import { cn } from '@/core/lib/utils';
|
|
23
|
+
import { IconBag, IconX } from '@/ui/shared/icons';
|
|
24
|
+
|
|
25
|
+
const OPEN_EVENT = 'cart-drawer:open';
|
|
26
|
+
|
|
27
|
+
/** Ask the mounted drawer to slide open (no-op during SSR). */
|
|
28
|
+
export function openCartDrawer() {
|
|
29
|
+
if (typeof window !== 'undefined') {
|
|
30
|
+
window.dispatchEvent(new Event(OPEN_EVENT));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function CartDrawer() {
|
|
35
|
+
const [open, setOpen] = useState(false);
|
|
36
|
+
const { cart, itemCount, totals, refreshCart } = useCart();
|
|
37
|
+
const t = useTranslations('cart');
|
|
38
|
+
const tc = useTranslations('common');
|
|
39
|
+
const currency = useCurrency();
|
|
40
|
+
const closeRef = useRef<HTMLButtonElement>(null);
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
const onOpen = () => setOpen(true);
|
|
44
|
+
window.addEventListener(OPEN_EVENT, onOpen);
|
|
45
|
+
return () => window.removeEventListener(OPEN_EVENT, onOpen);
|
|
46
|
+
}, []);
|
|
47
|
+
|
|
48
|
+
// Open state side-effects: Escape to close, scroll lock, focus the X.
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!open) return;
|
|
51
|
+
const onKey = (e: KeyboardEvent) => {
|
|
52
|
+
if (e.key === 'Escape') setOpen(false);
|
|
53
|
+
};
|
|
54
|
+
document.addEventListener('keydown', onKey);
|
|
55
|
+
const prevOverflow = document.body.style.overflow;
|
|
56
|
+
document.body.style.overflow = 'hidden';
|
|
57
|
+
closeRef.current?.focus();
|
|
58
|
+
return () => {
|
|
59
|
+
document.removeEventListener('keydown', onKey);
|
|
60
|
+
document.body.style.overflow = prevOverflow;
|
|
61
|
+
};
|
|
62
|
+
}, [open]);
|
|
63
|
+
|
|
64
|
+
const close = () => setOpen(false);
|
|
65
|
+
const items = cart?.items ?? [];
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<div
|
|
69
|
+
aria-hidden={!open}
|
|
70
|
+
inert={!open}
|
|
71
|
+
className={cn('fixed inset-0 z-50', !open && 'pointer-events-none')}
|
|
72
|
+
>
|
|
73
|
+
{/* Overlay */}
|
|
74
|
+
<div
|
|
75
|
+
onClick={close}
|
|
76
|
+
aria-hidden="true"
|
|
77
|
+
className={cn(
|
|
78
|
+
'bg-foreground/35 absolute inset-0 backdrop-blur-[2px] transition-opacity duration-300 motion-reduce:transition-none',
|
|
79
|
+
open ? 'opacity-100' : 'opacity-0'
|
|
80
|
+
)}
|
|
81
|
+
/>
|
|
82
|
+
|
|
83
|
+
{/* Panel — anchored at the logical end (same side as the cart icon) */}
|
|
84
|
+
<aside
|
|
85
|
+
role="dialog"
|
|
86
|
+
aria-modal="true"
|
|
87
|
+
aria-label={t('title')}
|
|
88
|
+
className={cn(
|
|
89
|
+
'bg-background absolute inset-y-0 end-0 flex w-full max-w-md flex-col shadow-2xl transition-transform duration-300 ease-out motion-reduce:transition-none sm:rounded-s-3xl sm:border-s',
|
|
90
|
+
open ? 'translate-x-0' : 'ltr:translate-x-full rtl:-translate-x-full'
|
|
91
|
+
)}
|
|
92
|
+
>
|
|
93
|
+
<header className="flex items-center gap-3 border-b px-5 py-4">
|
|
94
|
+
<h2 className="font-display text-foreground text-xl font-semibold">{t('title')}</h2>
|
|
95
|
+
{itemCount > 0 && (
|
|
96
|
+
<span className="bg-primary text-primary-foreground flex h-6 min-w-6 items-center justify-center rounded-full px-1.5 text-xs font-bold">
|
|
97
|
+
{itemCount}
|
|
98
|
+
</span>
|
|
99
|
+
)}
|
|
100
|
+
<button
|
|
101
|
+
ref={closeRef}
|
|
102
|
+
type="button"
|
|
103
|
+
onClick={close}
|
|
104
|
+
aria-label={tc('close')}
|
|
105
|
+
className="text-foreground hover:bg-secondary ms-auto flex h-10 w-10 items-center justify-center rounded-full transition-colors"
|
|
106
|
+
>
|
|
107
|
+
<IconX size={20} />
|
|
108
|
+
</button>
|
|
109
|
+
</header>
|
|
110
|
+
|
|
111
|
+
{items.length === 0 ? (
|
|
112
|
+
<div className="flex flex-1 flex-col items-center justify-center gap-3 px-6 text-center">
|
|
113
|
+
<span className="bg-secondary text-muted-foreground flex h-16 w-16 items-center justify-center rounded-full">
|
|
114
|
+
<IconBag size={24} />
|
|
115
|
+
</span>
|
|
116
|
+
<p className="font-display text-foreground text-lg font-semibold">{t('emptyTitle')}</p>
|
|
117
|
+
<p className="text-muted-foreground max-w-xs text-sm">{t('emptySubtitle')}</p>
|
|
118
|
+
<Link href="/products" onClick={close} className="btn-primary btn-sm mt-2">
|
|
119
|
+
{tc('continueShopping')}
|
|
120
|
+
</Link>
|
|
121
|
+
</div>
|
|
122
|
+
) : (
|
|
123
|
+
<>
|
|
124
|
+
<div className="flex-1 overflow-y-auto px-5 py-4">
|
|
125
|
+
<FreeShippingBar className="mb-4" />
|
|
126
|
+
<ul className="divide-y">
|
|
127
|
+
{items.map((item) => (
|
|
128
|
+
<li key={item.id} className="py-4 first:pt-0">
|
|
129
|
+
<CartItem item={item} onUpdate={refreshCart} />
|
|
130
|
+
</li>
|
|
131
|
+
))}
|
|
132
|
+
</ul>
|
|
133
|
+
</div>
|
|
134
|
+
|
|
135
|
+
<footer className="bg-secondary/40 border-t px-5 py-4">
|
|
136
|
+
<div className="flex items-center justify-between text-sm">
|
|
137
|
+
<span className="text-muted-foreground">{tc('subtotal')}</span>
|
|
138
|
+
<strong className="text-foreground text-base">
|
|
139
|
+
{formatPrice(totals.subtotal, { currency }) as string}
|
|
140
|
+
</strong>
|
|
141
|
+
</div>
|
|
142
|
+
{totals.discount > 0 && (
|
|
143
|
+
<div className="text-primary mt-1 flex items-center justify-between text-sm">
|
|
144
|
+
<span>{tc('discount')}</span>
|
|
145
|
+
<span>-{formatPrice(totals.discount, { currency }) as string}</span>
|
|
146
|
+
</div>
|
|
147
|
+
)}
|
|
148
|
+
<p className="text-muted-foreground mt-1 text-xs">{t('shippingAtCheckout')}</p>
|
|
149
|
+
<div className="mt-4 grid gap-2">
|
|
150
|
+
{/* Same gate as the cart page: a line the server marked
|
|
151
|
+
unavailable blocks checkout. A disabled-looking anchor
|
|
152
|
+
would still navigate, so use a real disabled button. */}
|
|
153
|
+
{items.some((item) => item.isAvailable === false) ? (
|
|
154
|
+
<>
|
|
155
|
+
<button type="button" disabled className="btn-primary btn-lg w-full opacity-50">
|
|
156
|
+
{t('proceedToCheckout')}
|
|
157
|
+
</button>
|
|
158
|
+
<p className="text-destructive text-center text-xs">
|
|
159
|
+
{t('unavailableItemsHint')}
|
|
160
|
+
</p>
|
|
161
|
+
</>
|
|
162
|
+
) : (
|
|
163
|
+
<Link href="/checkout" onClick={close} className="btn-primary btn-lg w-full">
|
|
164
|
+
{t('proceedToCheckout')}
|
|
165
|
+
</Link>
|
|
166
|
+
)}
|
|
167
|
+
<Link href="/cart" onClick={close} className="btn-outline btn-lg w-full">
|
|
168
|
+
{t('viewCart')}
|
|
169
|
+
</Link>
|
|
170
|
+
</div>
|
|
171
|
+
</footer>
|
|
172
|
+
</>
|
|
173
|
+
)}
|
|
174
|
+
</aside>
|
|
175
|
+
</div>
|
|
176
|
+
);
|
|
177
|
+
}
|