|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
import { |
|
3
|
Avatar, |
|
4
|
clx, |
|
5
|
Container, |
|
6
|
Heading, |
|
7
|
Skeleton, |
|
8
|
Text, |
|
9
|
Tooltip, |
|
10
|
} from "@medusajs/ui" |
|
11
|
import { Collapsible } from "radix-ui" |
|
12
|
import { ReactNode, useMemo, useState } from "react" |
|
13
|
import { Link } from "react-router-dom" |
|
14
|
import { useUser } from "../../hooks/api/users" |
|
15
|
import { getFullDate, getRelativeDate } from "../../lib/utils/date-utils" |
|
16
|
|
|
17
|
interface ActivitySectionProps { |
|
18
|
order: HttpTypes.AdminOrder |
|
19
|
changes: HttpTypes.AdminOrderChange[] |
|
20
|
} |
|
21
|
|
|
22
|
export const ActivitySection = ({ order, changes }: ActivitySectionProps) => { |
|
23
|
const activityItems = useMemo( |
|
24
|
() => getActivityItems(order, changes), |
|
25
|
[order, changes] |
|
26
|
) |
|
27
|
|
|
28
|
return ( |
|
29
|
<Container className="overflow-hidden p-0"> |
|
30
|
<div className="px-6 py-4"> |
|
31
|
<Heading>Activity</Heading> |
|
32
|
</div> |
|
33
|
<ActivityItemList items={activityItems} /> |
|
34
|
</Container> |
|
35
|
) |
|
36
|
} |
|
37
|
|
|
38
|
interface ActivityItemListProps { |
|
39
|
items: ActivityItem[] |
|
40
|
} |
|
41
|
|
|
42
|
const ActivityItemList = ({ items }: ActivityItemListProps) => { |
|
43
|
if (items.length <= 3) { |
|
44
|
return ( |
|
45
|
<div className="flex flex-col gap-y-0.5 px-6 pb-6"> |
|
46
|
{items.map((item, idx) => ( |
|
47
|
<ActivityItem |
|
48
|
key={idx} |
|
49
|
item={item} |
|
50
|
isFirst={idx === items.length - 1} |
|
51
|
/> |
|
52
|
))} |
|
53
|
</div> |
|
54
|
) |
|
55
|
} |
|
56
|
|
|
57
|
const lastItems = items.slice(0, 2) |
|
58
|
const collapsibleItems = items.slice(2, items.length - 1) |
|
59
|
const firstItem = items[items.length - 1] |
|
60
|
|
|
61
|
return ( |
|
62
|
<div className="flex flex-col gap-y-0.5 px-6 pb-6"> |
|
63
|
{lastItems.map((item, idx) => ( |
|
64
|
<ActivityItem key={idx} item={item} /> |
|
65
|
))} |
|
66
|
<CollapsibleActivityItemList items={collapsibleItems} /> |
|
67
|
<ActivityItem key={items.length - 1} item={firstItem} isFirst /> |
|
68
|
</div> |
|
69
|
) |
|
70
|
} |
|
71
|
|
|
72
|
interface CollapsibleActivityItemListProps { |
|
73
|
items: ActivityItem[] |
|
74
|
} |
|
75
|
|
|
76
|
const CollapsibleActivityItemList = ({ |
|
77
|
items, |
|
78
|
}: CollapsibleActivityItemListProps) => { |
|
79
|
const [open, setOpen] = useState(false) |
|
80
|
|
|
81
|
return ( |
|
82
|
<Collapsible.Root open={open} onOpenChange={setOpen}> |
|
83
|
{!open && ( |
|
84
|
<div className="grid grid-cols-[20px_1fr] items-start gap-2"> |
|
85
|
<div className="flex size-full flex-col items-center"> |
|
86
|
<div className="border-ui-border-strong w-px flex-1 bg-[linear-gradient(var(--border-strong)_33%,rgba(255,255,255,0)_0%)] bg-[length:1px_3px] bg-clip-content bg-right bg-repeat-y" /> |
|
87
|
</div> |
|
88
|
<Collapsible.Trigger className="text-ui-fg-muted hover:text-ui-fg-base focus:text-ui-fg-base m-0 p-0 pb-4 text-left outline-none transition-colors"> |
|
89
|
<Text size="small" leading="compact" weight="plus"> |
|
90
|
{`Show ${items.length} more ${ |
|
91
|
items.length === 1 ? "activity" : "activities" |
|
92
|
}`} |
|
93
|
</Text> |
|
94
|
</Collapsible.Trigger> |
|
95
|
</div> |
|
96
|
)} |
|
97
|
<Collapsible.Content> |
|
98
|
<div className="flex flex-col gap-y-0.5"> |
|
99
|
{items.map((item, idx) => { |
|
100
|
return <ActivityItem key={idx} item={item} /> |
|
101
|
})} |
|
102
|
</div> |
|
103
|
</Collapsible.Content> |
|
104
|
</Collapsible.Root> |
|
105
|
) |
|
106
|
} |
|
107
|
|
|
108
|
interface ActivityItem { |
|
109
|
label: string |
|
110
|
content?: ReactNode |
|
111
|
timestamp: string |
|
112
|
userId?: string | null |
|
113
|
} |
|
114
|
|
|
115
|
interface ActivityItemProps { |
|
116
|
item: ActivityItem |
|
117
|
isFirst?: boolean |
|
118
|
} |
|
119
|
|
|
120
|
const ActivityItem = ({ item, isFirst = false }: ActivityItemProps) => { |
|
121
|
const { user, isPending, isError, error } = useUser(item.userId!, undefined, { |
|
122
|
enabled: !!item.userId, |
|
123
|
}) |
|
124
|
|
|
125
|
if (isError) { |
|
126
|
throw error |
|
127
|
} |
|
128
|
|
|
129
|
const isUserLoaded = !isPending && !!user && !!item.userId |
|
130
|
|
|
131
|
return ( |
|
132
|
<div |
|
133
|
className={clx("grid w-full grid-cols-[20px_1fr] items-start gap-x-2")} |
|
134
|
> |
|
135
|
<div className="flex h-full flex-col items-center gap-0.5"> |
|
136
|
<div className="flex size-5 items-center justify-center"> |
|
137
|
<div className="shadow-borders-base flex size-2.5 items-center justify-center rounded-full"> |
|
138
|
<div className="bg-ui-tag-neutral-icon size-1.5 rounded-full" /> |
|
139
|
</div> |
|
140
|
</div> |
|
141
|
{!isFirst && ( |
|
142
|
<div className="flex flex-1 items-center justify-center"> |
|
143
|
<div className="bg-ui-border-base h-full w-px" /> |
|
144
|
</div> |
|
145
|
)} |
|
146
|
</div> |
|
147
|
<div className={clx("flex flex-col", !isFirst && "pb-4")}> |
|
148
|
<div className="flex items-center justify-between gap-x-2"> |
|
149
|
<Text size="small" weight="plus" leading="compact"> |
|
150
|
{item.label} |
|
151
|
</Text> |
|
152
|
<Tooltip |
|
153
|
content={getFullDate({ date: item.timestamp, includeTime: true })} |
|
154
|
> |
|
155
|
<Text size="small" leading="compact" className="cursor-default"> |
|
156
|
{getRelativeDate(item.timestamp)} |
|
157
|
</Text> |
|
158
|
</Tooltip> |
|
159
|
</div> |
|
160
|
{item.content && renderContent(item.content)} |
|
161
|
{item.userId && ( |
|
162
|
<div className="text-ui-fg-muted pt-2"> |
|
163
|
{isUserLoaded ? ( |
|
164
|
<Link to={`/settings/users/${user.id}`} className="w-fit"> |
|
165
|
<div className="flex w-fit items-center gap-x-1.5"> |
|
166
|
<Text size="small">By</Text> |
|
167
|
<Avatar |
|
168
|
size="2xsmall" |
|
169
|
fallback={[user.first_name, user.last_name] |
|
170
|
.filter(Boolean) |
|
171
|
.join("") |
|
172
|
.slice(0, 1)} |
|
173
|
/> |
|
174
|
<Text size="small"> |
|
175
|
{user.first_name} {user.last_name} |
|
176
|
</Text> |
|
177
|
</div> |
|
178
|
</Link> |
|
179
|
) : ( |
|
180
|
<div className="flex items-center gap-x-1.5"> |
|
181
|
<Text size="small">By</Text> |
|
182
|
<Skeleton className="h-5 w-5 rounded-full" /> |
|
183
|
<Skeleton className="h-4 w-[75px]" /> |
|
184
|
</div> |
|
185
|
)} |
|
186
|
</div> |
|
187
|
)} |
|
188
|
</div> |
|
189
|
</div> |
|
190
|
) |
|
191
|
} |
|
192
|
|
|
193
|
function renderContent(content: ReactNode) { |
|
194
|
if (typeof content === "string") { |
|
195
|
return ( |
|
196
|
<Text size="small" className="text-ui-fg-subtle"> |
|
197
|
{content} |
|
198
|
</Text> |
|
199
|
) |
|
200
|
} |
|
201
|
|
|
202
|
return content |
|
203
|
} |
|
204
|
|
|
205
|
function getEditActivityItems( |
|
206
|
change: HttpTypes.AdminOrderChange |
|
207
|
): ActivityItem[] { |
|
208
|
const activityItems: ActivityItem[] = [] |
|
209
|
const counts = { |
|
210
|
itemsAdded: 0, |
|
211
|
itemsRemoved: 0, |
|
212
|
shippingMethodsAdded: 0, |
|
213
|
shippingMethodsRemoved: 0, |
|
214
|
promotionsAdded: 0, |
|
215
|
promotionsRemoved: 0, |
|
216
|
} |
|
217
|
|
|
218
|
const orderedActions = change.actions.sort((a, b) => { |
|
219
|
return a.ordering - b.ordering |
|
220
|
}) |
|
221
|
|
|
222
|
const addedPromotionMap = new Map<string, true>() |
|
223
|
const removedPromotionMap = new Map<string, true>() |
|
224
|
|
|
225
|
for (const action of orderedActions) { |
|
226
|
if (!action.details) { |
|
227
|
continue |
|
228
|
} |
|
229
|
|
|
230
|
switch (action.action) { |
|
231
|
case "ITEM_ADD": |
|
232
|
counts.itemsAdded += action.details.quantity as number |
|
233
|
break |
|
234
|
case "ITEM_UPDATE": |
|
235
|
const diff = action.details.quantity_diff as number |
|
236
|
diff > 0 ? (counts.itemsAdded += diff) : (counts.itemsRemoved += diff) |
|
237
|
break |
|
238
|
case "SHIPPING_ADD": |
|
239
|
counts.shippingMethodsAdded += 1 |
|
240
|
break |
|
241
|
case "SHIPPING_REMOVE": |
|
242
|
counts.shippingMethodsRemoved += 1 |
|
243
|
break |
|
244
|
case "PROMOTION_ADD": { |
|
245
|
addedPromotionMap.set(action.reference_id!, true) |
|
246
|
break |
|
247
|
} |
|
248
|
case "PROMOTION_REMOVE": { |
|
249
|
if (addedPromotionMap.has(action.reference_id!)) { |
|
250
|
addedPromotionMap.delete(action.reference_id!) |
|
251
|
} else { |
|
252
|
removedPromotionMap.set(action.reference_id!, true) |
|
253
|
} |
|
254
|
break |
|
255
|
} |
|
256
|
} |
|
257
|
|
|
258
|
counts.promotionsAdded = addedPromotionMap.size |
|
259
|
counts.promotionsRemoved = removedPromotionMap.size |
|
260
|
} |
|
261
|
|
|
262
|
const createActivityItem = ( |
|
263
|
type: "items" | "shipping" | "promotions", |
|
264
|
added: number, |
|
265
|
removed: number |
|
266
|
) => { |
|
267
|
if (added === 0 && removed === 0) return |
|
268
|
|
|
269
|
const getText = (count: number, singular: string, plural: string) => |
|
270
|
count === 1 ? `${count} ${singular}` : `${count} ${plural}` |
|
271
|
|
|
272
|
const addedText = getText( |
|
273
|
added, |
|
274
|
type === "items" |
|
275
|
? "item" |
|
276
|
: type === "shipping" |
|
277
|
? "shipping method" |
|
278
|
: "promotion", |
|
279
|
type === "items" |
|
280
|
? "items" |
|
281
|
: type === "shipping" |
|
282
|
? "shipping methods" |
|
283
|
: "promotions" |
|
284
|
) |
|
285
|
const removedText = getText( |
|
286
|
Math.abs(removed), |
|
287
|
type === "items" |
|
288
|
? "item" |
|
289
|
: type === "shipping" |
|
290
|
? "shipping method" |
|
291
|
: "promotion", |
|
292
|
type === "items" |
|
293
|
? "items" |
|
294
|
: type === "shipping" |
|
295
|
? "shipping methods" |
|
296
|
: "promotions" |
|
297
|
) |
|
298
|
|
|
299
|
const content = |
|
300
|
added && removed |
|
301
|
? `Added ${addedText}, removed ${removedText}` |
|
302
|
: added |
|
303
|
? `Added ${addedText}` |
|
304
|
: `Removed ${removedText}` |
|
305
|
|
|
306
|
const label = |
|
307
|
added && removed |
|
308
|
? `${ |
|
309
|
type === "items" |
|
310
|
? "Items" |
|
311
|
: type === "shipping" |
|
312
|
? "Shipping methods" |
|
313
|
: "Promotions" |
|
314
|
} updated` |
|
315
|
: added |
|
316
|
? `${ |
|
317
|
type === "items" |
|
318
|
? "Items" |
|
319
|
: type === "shipping" |
|
320
|
? "Shipping methods" |
|
321
|
: "Promotions" |
|
322
|
} added` |
|
323
|
: `${ |
|
324
|
type === "items" |
|
325
|
? "Items" |
|
326
|
: type === "shipping" |
|
327
|
? "Shipping methods" |
|
328
|
: "Promotions" |
|
329
|
} removed` |
|
330
|
|
|
331
|
activityItems.push({ |
|
332
|
label, |
|
333
|
content, |
|
334
|
timestamp: new Date(change.created_at).toISOString(), |
|
335
|
userId: change.confirmed_by, |
|
336
|
}) |
|
337
|
} |
|
338
|
|
|
339
|
createActivityItem("items", counts.itemsAdded, counts.itemsRemoved) |
|
340
|
createActivityItem( |
|
341
|
"shipping", |
|
342
|
counts.shippingMethodsAdded, |
|
343
|
counts.shippingMethodsRemoved |
|
344
|
) |
|
345
|
createActivityItem( |
|
346
|
"promotions", |
|
347
|
counts.promotionsAdded, |
|
348
|
counts.promotionsRemoved |
|
349
|
) |
|
350
|
|
|
351
|
return activityItems |
|
352
|
} |
|
353
|
|
|
354
|
function getTransferActivityItem(change: HttpTypes.AdminOrderChange) { |
|
355
|
return { |
|
356
|
label: "Transferred", |
|
357
|
content: "Draft order transferred", |
|
358
|
timestamp: new Date(change.created_at).toISOString(), |
|
359
|
} |
|
360
|
} |
|
361
|
|
|
362
|
function getUpdateOrderActivityItem(change: HttpTypes.AdminOrderChange) { |
|
363
|
const { details } = change.actions?.[0] || {} |
|
364
|
|
|
365
|
if (!details) { |
|
366
|
return null |
|
367
|
} |
|
368
|
|
|
369
|
switch (details.type) { |
|
370
|
case "customer_id": |
|
371
|
return { |
|
372
|
label: "Customer updated", |
|
373
|
timestamp: new Date(change.created_at).toISOString(), |
|
374
|
userId: change.confirmed_by, |
|
375
|
} |
|
376
|
case "sales_channel_id": |
|
377
|
return { |
|
378
|
label: "Sales channel updated", |
|
379
|
timestamp: new Date(change.created_at).toISOString(), |
|
380
|
userId: change.confirmed_by, |
|
381
|
} |
|
382
|
case "billing_address": |
|
383
|
return { |
|
384
|
label: "Billing address updated", |
|
385
|
timestamp: new Date(change.created_at).toISOString(), |
|
386
|
userId: change.confirmed_by, |
|
387
|
} |
|
388
|
case "shipping_address": |
|
389
|
return { |
|
390
|
label: "Shipping address updated", |
|
391
|
timestamp: new Date(change.created_at).toISOString(), |
|
392
|
userId: change.confirmed_by, |
|
393
|
} |
|
394
|
case "email": |
|
395
|
return { |
|
396
|
label: "Email updated", |
|
397
|
timestamp: new Date(change.created_at).toISOString(), |
|
398
|
userId: change.confirmed_by, |
|
399
|
} |
|
400
|
|
|
401
|
default: |
|
402
|
return null |
|
403
|
} |
|
404
|
} |
|
405
|
|
|
406
|
function getActivityItems( |
|
407
|
order: HttpTypes.AdminOrder, |
|
408
|
changes: HttpTypes.AdminOrderChange[] |
|
409
|
) { |
|
410
|
const items: ActivityItem[] = [] |
|
411
|
|
|
412
|
if (order.created_at) { |
|
413
|
items.push({ |
|
414
|
label: "Created", |
|
415
|
content: "Draft order created", |
|
416
|
timestamp: new Date(order.created_at).toISOString(), |
|
417
|
}) |
|
418
|
} |
|
419
|
|
|
420
|
changes.forEach((change) => { |
|
421
|
if (!change.change_type || !change.confirmed_at) { |
|
422
|
return |
|
423
|
} |
|
424
|
|
|
425
|
switch (change.change_type) { |
|
426
|
case "edit": { |
|
427
|
items.push(...getEditActivityItems(change)) |
|
428
|
break |
|
429
|
} |
|
430
|
case "transfer": |
|
431
|
items.push(getTransferActivityItem(change)) |
|
432
|
break |
|
433
|
case "update_order": { |
|
434
|
const item = getUpdateOrderActivityItem(change) |
|
435
|
|
|
436
|
if (item) { |
|
437
|
items.push(item) |
|
438
|
} |
|
439
|
break |
|
440
|
} |
|
441
|
default: |
|
442
|
break |
|
443
|
} |
|
444
|
}) |
|
445
|
|
|
446
|
return items.sort( |
|
447
|
(a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime() |
|
448
|
) |
|
449
|
} |