@nuvler/cli 0.5.15 → 0.5.17

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.
@@ -0,0 +1,230 @@
1
+ "use client";
2
+
3
+ import type { Dispatch, SetStateAction } from "react";
4
+ import type { ThemeKitGroup } from "@nuvler/theme-sdk";
5
+ import { ThemeImageView } from "./shared.js";
6
+ import { getKitGroupSelection, kitSelectionKey } from "./kit-selection.js";
7
+
8
+ export function KitGroupSelector({
9
+ group,
10
+ grouped,
11
+ quantities,
12
+ setQuantities,
13
+ initiallyOpen = true,
14
+ collapsible = grouped,
15
+ }: {
16
+ group: ThemeKitGroup;
17
+ grouped: boolean;
18
+ quantities: Record<string, string>;
19
+ setQuantities: Dispatch<SetStateAction<Record<string, string>>>;
20
+ initiallyOpen?: boolean;
21
+ collapsible?: boolean;
22
+ }) {
23
+ const selection = getKitGroupSelection(group, grouped, quantities);
24
+ const shortcuts = [5, 10, 15].filter((step) => step <= group.quantity);
25
+ const header = (
26
+ <>
27
+ {group.image?.url && (
28
+ <ThemeImageView
29
+ image={group.image}
30
+ fallbackAlt={group.name}
31
+ className="kit-group-image"
32
+ sizes="48px"
33
+ fallbackWidth={96}
34
+ fallbackHeight={96}
35
+ />
36
+ )}
37
+ <span className="kit-group-title">
38
+ <span className="kit-group-name">
39
+ {grouped ? group.name : "Monte seu kit"}
40
+ </span>
41
+ <span className="kit-group-preview">
42
+ {selection.summary ||
43
+ `Escolha ${group.quantity} ${group.quantity === 1 ? "unidade" : "unidades"}`}
44
+ </span>
45
+ </span>
46
+ <span
47
+ className="kit-group-status"
48
+ data-complete={selection.complete || undefined}
49
+ >
50
+ <span>
51
+ {selection.total}/{group.quantity}
52
+ </span>
53
+ <span>
54
+ {selection.complete
55
+ ? "Completo"
56
+ : !selection.valid
57
+ ? "Revisar"
58
+ : "Pendente"}
59
+ </span>
60
+ </span>
61
+ {collapsible && (
62
+ <svg
63
+ className="kit-group-chevron"
64
+ viewBox="0 0 24 24"
65
+ aria-hidden="true"
66
+ >
67
+ <path d="m6 9 6 6 6-6" />
68
+ </svg>
69
+ )}
70
+ </>
71
+ );
72
+
73
+ const content = (
74
+ <div className="kit-group-body">
75
+ <p className="kit-group-instructions">
76
+ {grouped
77
+ ? "Distribua as quantidades entre as opções deste grupo."
78
+ : "Escolha os sabores e distribua as quantidades do seu jeito."}
79
+ </p>
80
+ {group.options.map((item) => {
81
+ const selectionKey = kitSelectionKey(group.id, item.id, grouped);
82
+ const inputId = `kit-${grouped ? `${encodeURIComponent(group.id)}-` : ""}${encodeURIComponent(item.id)}`;
83
+ const value = quantities[selectionKey] ?? "0";
84
+ const amount = Number(value) || 0;
85
+ const available = item.active && Boolean(item.id);
86
+ const changeAmount = (quantity: number) =>
87
+ setQuantities((current) => ({
88
+ ...current,
89
+ [selectionKey]: String(quantity),
90
+ }));
91
+ return (
92
+ <div
93
+ className="kit-flavor"
94
+ data-selected={(available && amount > 0) || undefined}
95
+ data-unavailable={!available || undefined}
96
+ key={item.id}
97
+ >
98
+ <label htmlFor={inputId}>
99
+ {item.name}
100
+ {!available && <small>Indisponível</small>}
101
+ </label>
102
+ <div className="kit-controls">
103
+ <div className="kit-stepper">
104
+ <button
105
+ type="button"
106
+ aria-label={`Diminuir ${item.name}`}
107
+ disabled={!available || amount <= 0}
108
+ onClick={() => changeAmount(Math.max(0, amount - 1))}
109
+ >
110
+ −
111
+ </button>
112
+ <input
113
+ id={inputId}
114
+ type="number"
115
+ min="0"
116
+ step="1"
117
+ max={amount + selection.remaining}
118
+ value={value}
119
+ disabled={!available}
120
+ aria-label={`Quantidade de ${item.name}`}
121
+ onChange={(event) =>
122
+ setQuantities((current) => ({
123
+ ...current,
124
+ [selectionKey]: event.target.value,
125
+ }))
126
+ }
127
+ />
128
+ <button
129
+ type="button"
130
+ aria-label={`Aumentar ${item.name}`}
131
+ disabled={
132
+ !available || selection.remaining < 1 || !selection.valid
133
+ }
134
+ onClick={() => changeAmount(amount + 1)}
135
+ >
136
+ +
137
+ </button>
138
+ </div>
139
+ {shortcuts.length > 0 && (
140
+ <div
141
+ className="kit-shortcuts"
142
+ role="group"
143
+ aria-label={`Ajustar unidades de ${item.name}`}
144
+ >
145
+ {shortcuts.map((step) => (
146
+ <button
147
+ key={`remove-${step}`}
148
+ type="button"
149
+ disabled={
150
+ !available ||
151
+ !Number.isSafeInteger(amount) ||
152
+ amount < step
153
+ }
154
+ aria-label={`Retirar ${step} de ${item.name}`}
155
+ onClick={() => changeAmount(amount - step)}
156
+ >
157
+ −{step}
158
+ </button>
159
+ ))}
160
+ {shortcuts.map((step) => (
161
+ <button
162
+ key={`add-${step}`}
163
+ type="button"
164
+ disabled={
165
+ !available ||
166
+ selection.remaining < step ||
167
+ !selection.valid
168
+ }
169
+ aria-label={`Adicionar ${step} de ${item.name}`}
170
+ onClick={() => changeAmount(amount + step)}
171
+ >
172
+ +{step}
173
+ </button>
174
+ ))}
175
+ </div>
176
+ )}
177
+ </div>
178
+ </div>
179
+ );
180
+ })}
181
+ <div
182
+ className="kit-group-progress"
183
+ data-complete={selection.complete || undefined}
184
+ >
185
+ <progress
186
+ className="kit-progress"
187
+ max={group.quantity}
188
+ value={Math.min(group.quantity, selection.total)}
189
+ aria-label={`Progresso de ${group.name}`}
190
+ />
191
+ <p role="status" aria-live="polite">
192
+ Selecionados: {selection.total} de {group.quantity} · Faltam:{" "}
193
+ {selection.remaining}
194
+ </p>
195
+ </div>
196
+ {!selection.valid && (
197
+ <p role="alert">
198
+ Informe quantidades inteiras positivas ou zero, sem ultrapassar{" "}
199
+ {group.quantity} unidades.
200
+ </p>
201
+ )}
202
+ {group.options.every((item) => !item.active) && (
203
+ <p>Não há opções disponíveis neste grupo.</p>
204
+ )}
205
+ </div>
206
+ );
207
+
208
+ if (collapsible) {
209
+ return (
210
+ <details
211
+ className="kit-selector kit-group-accordion"
212
+ open={initiallyOpen}
213
+ data-complete={selection.complete || undefined}
214
+ aria-label={`Montagem: ${group.name}`}
215
+ >
216
+ <summary className="kit-group-header">{header}</summary>
217
+ {content}
218
+ </details>
219
+ );
220
+ }
221
+ return (
222
+ <section
223
+ className="kit-selector kit-group-static"
224
+ aria-label={grouped ? `Montagem: ${group.name}` : "Montagem do kit"}
225
+ >
226
+ <div className="kit-group-header">{header}</div>
227
+ {content}
228
+ </section>
229
+ );
230
+ }
@@ -0,0 +1,39 @@
1
+ import type { ThemeKitGroup } from "@nuvler/theme-sdk";
2
+
3
+ export function kitSelectionKey(
4
+ groupId: string,
5
+ optionId: string,
6
+ grouped: boolean,
7
+ ): string {
8
+ return grouped ? JSON.stringify([groupId, optionId]) : optionId;
9
+ }
10
+
11
+ export function getKitGroupSelection(
12
+ group: ThemeKitGroup,
13
+ grouped: boolean,
14
+ quantities: Record<string, string>,
15
+ ) {
16
+ const values = group.options.map((option) => ({
17
+ option,
18
+ raw: quantities[kitSelectionKey(group.id, option.id, grouped)] ?? "0",
19
+ }));
20
+ const selected = values.filter(
21
+ ({ option, raw }) => option.active && Number(raw) > 0,
22
+ );
23
+ const total = selected.reduce((sum, { raw }) => sum + Number(raw), 0);
24
+ const valid =
25
+ values.every(
26
+ ({ raw }) =>
27
+ raw === "" || (Number.isSafeInteger(Number(raw)) && Number(raw) >= 0),
28
+ ) && total <= group.quantity;
29
+ return {
30
+ selected,
31
+ total,
32
+ remaining: Math.max(0, group.quantity - total),
33
+ valid,
34
+ complete: valid && total === group.quantity,
35
+ summary: selected
36
+ .map(({ option, raw }) => `${Number(raw)} × ${option.name}`)
37
+ .join(" · "),
38
+ };
39
+ }