medusa-analytics 0.0.3 → 0.0.4

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.
@@ -5,6 +5,39 @@ const adminSdk = require("@medusajs/admin-sdk");
5
5
  const icons = require("@medusajs/icons");
6
6
  const ui = require("@medusajs/ui");
7
7
  const recharts = require("recharts");
8
+ function OrdersOverTimeTooltip({
9
+ active,
10
+ payload,
11
+ label
12
+ }) {
13
+ var _a, _b;
14
+ if (!active || !(payload == null ? void 0 : payload.length)) return null;
15
+ const row = (_a = payload[0]) == null ? void 0 : _a.payload;
16
+ const displayLabel = (row == null ? void 0 : row.label) ?? (label ? new Date(label).toLocaleDateString() : label);
17
+ const value = ((_b = payload[0]) == null ? void 0 : _b.value) ?? 0;
18
+ return /* @__PURE__ */ jsxRuntime.jsxs(
19
+ "div",
20
+ {
21
+ style: {
22
+ backgroundColor: "rgb(254, 233, 233)",
23
+ color: "rgba(5, 0, 0, 0)",
24
+ border: "1px solidrgb(0, 0, 0)",
25
+ borderRadius: "8px",
26
+ padding: "12px 16px",
27
+ boxShadow: "0 4px 14px rgba(102, 102, 102, 0.99)",
28
+ fontSize: "14px",
29
+ lineHeight: 1.5
30
+ },
31
+ children: [
32
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontWeight: 600, marginBottom: "6px", color: "#111827" }, children: displayLabel }),
33
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { color: "#374151" }, children: [
34
+ "Orders: ",
35
+ /* @__PURE__ */ jsxRuntime.jsx("strong", { style: { color: "#1f2937" }, children: value })
36
+ ] })
37
+ ]
38
+ }
39
+ );
40
+ }
8
41
  const STATUS_COLORS = {
9
42
  pending: "#F59E0B",
10
43
  confirmed: "#3B82F6",
@@ -36,9 +69,9 @@ const SUMMARY_PERIODS = [
36
69
  { value: "90", label: "Last 90 days" }
37
70
  ];
38
71
  const OVER_TIME_PERIODS = [
39
- { value: "1d", label: "1 day" },
40
- { value: "7d", label: "1 week" },
41
- { value: "30d", label: "1 month" }
72
+ { value: "daily", label: "Daily" },
73
+ { value: "weekly", label: "Weekly" },
74
+ { value: "monthly", label: "Monthly" }
42
75
  ];
43
76
  function OrdersDashboard() {
44
77
  const [data, setData] = react.useState(null);
@@ -46,7 +79,7 @@ function OrdersDashboard() {
46
79
  const [error, setError] = react.useState(null);
47
80
  const [filter, setFilter] = react.useState("7");
48
81
  const [dailyOrders, setDailyOrders] = react.useState([]);
49
- const [overTimePeriod, setOverTimePeriod] = react.useState("7d");
82
+ const [overTimePeriod, setOverTimePeriod] = react.useState("weekly");
50
83
  const [overTimeLoading, setOverTimeLoading] = react.useState(true);
51
84
  const [overTimeError, setOverTimeError] = react.useState(null);
52
85
  react.useEffect(() => {
@@ -212,27 +245,301 @@ function OrdersDashboard() {
212
245
  {
213
246
  dataKey: "date",
214
247
  tick: { fontSize: 12 },
215
- tickFormatter: (v) => {
248
+ tickFormatter: (v, index) => {
249
+ const row = dailyOrders[index];
250
+ if (row == null ? void 0 : row.label) return row.label;
216
251
  const d = new Date(v);
217
252
  return `${d.getUTCMonth() + 1}/${d.getUTCDate()}`;
218
253
  }
219
254
  }
220
255
  ),
221
256
  /* @__PURE__ */ jsxRuntime.jsx(recharts.YAxis, { tick: { fontSize: 12 } }),
257
+ /* @__PURE__ */ jsxRuntime.jsx(recharts.Tooltip, { content: /* @__PURE__ */ jsxRuntime.jsx(OrdersOverTimeTooltip, {}) }),
258
+ /* @__PURE__ */ jsxRuntime.jsx(
259
+ recharts.Line,
260
+ {
261
+ type: "monotone",
262
+ dataKey: "orders_count",
263
+ name: "Orders",
264
+ stroke: "var(--medusa-color-ui-fg-interactive)",
265
+ strokeWidth: 2,
266
+ dot: { r: 3 }
267
+ }
268
+ )
269
+ ]
270
+ }
271
+ ) }) })
272
+ ] })
273
+ ] });
274
+ }
275
+ function CustomersOverTimeTooltip({
276
+ active,
277
+ payload,
278
+ label
279
+ }) {
280
+ var _a, _b, _c;
281
+ if (!active || !(payload == null ? void 0 : payload.length)) return null;
282
+ const row = (_a = payload[0]) == null ? void 0 : _a.payload;
283
+ const displayLabel = (row == null ? void 0 : row.label) ?? (label ? new Date(label).toLocaleDateString() : label);
284
+ const registered = Math.floor(((_b = payload.find((p) => p.name === "Registered")) == null ? void 0 : _b.value) ?? 0);
285
+ const guest = Math.floor(((_c = payload.find((p) => p.name === "Guest")) == null ? void 0 : _c.value) ?? 0);
286
+ return /* @__PURE__ */ jsxRuntime.jsxs(
287
+ "div",
288
+ {
289
+ style: {
290
+ backgroundColor: "rgb(254, 254, 254)",
291
+ color: "#111827",
292
+ border: "1px solid rgb(229, 231, 235)",
293
+ borderRadius: "8px",
294
+ padding: "12px 16px",
295
+ boxShadow: "0 4px 14px rgba(0, 0, 0, 0.08)",
296
+ fontSize: "14px",
297
+ lineHeight: 1.5
298
+ },
299
+ children: [
300
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontWeight: 600, marginBottom: "6px", color: "#111827" }, children: displayLabel }),
301
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { color: "#374151" }, children: [
302
+ "Registered: ",
303
+ /* @__PURE__ */ jsxRuntime.jsx("strong", { style: { color: "#1f2937" }, children: registered })
304
+ ] }),
305
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { color: "#374151" }, children: [
306
+ "Guest: ",
307
+ /* @__PURE__ */ jsxRuntime.jsx("strong", { style: { color: "#1f2937" }, children: guest })
308
+ ] })
309
+ ]
310
+ }
311
+ );
312
+ }
313
+ const COUNT_DAY_PRESETS = [
314
+ { value: "3", label: "Last 3 days" },
315
+ { value: "5", label: "Last 5 days" },
316
+ { value: "7", label: "Last 7 days" },
317
+ { value: "9", label: "Last 9 days" },
318
+ { value: "14", label: "Last 14 days" },
319
+ { value: "30", label: "Last 30 days" },
320
+ { value: "90", label: "Last 90 days" }
321
+ ];
322
+ const GRAPH_PERIODS = [
323
+ { value: "daily", label: "Daily" },
324
+ { value: "weekly", label: "Weekly" },
325
+ { value: "monthly", label: "Monthly" },
326
+ { value: "yearly", label: "Yearly" }
327
+ ];
328
+ const REGISTERED_COLOR = "var(--medusa-color-ui-fg-interactive)";
329
+ const GUEST_COLOR = "#6B7280";
330
+ function CustomersDashboard() {
331
+ const [data, setData] = react.useState(null);
332
+ const [loading, setLoading] = react.useState(true);
333
+ const [error, setError] = react.useState(null);
334
+ const [countDays, setCountDays] = react.useState("7");
335
+ const [series, setSeries] = react.useState([]);
336
+ const [graphPeriod, setGraphPeriod] = react.useState("weekly");
337
+ const [graphLoading, setGraphLoading] = react.useState(true);
338
+ const [graphError, setGraphError] = react.useState(null);
339
+ react.useEffect(() => {
340
+ let cancelled = false;
341
+ setLoading(true);
342
+ setError(null);
343
+ const url = `/admin/analytics/customers-summary?days=${countDays}`;
344
+ fetch(url).then((res) => {
345
+ if (!res.ok) throw new Error(res.statusText);
346
+ return res.json();
347
+ }).then((body) => {
348
+ if (!cancelled) setData(body);
349
+ }).catch((e) => {
350
+ if (!cancelled) setError(e instanceof Error ? e.message : String(e));
351
+ }).finally(() => {
352
+ if (!cancelled) setLoading(false);
353
+ });
354
+ return () => {
355
+ cancelled = true;
356
+ };
357
+ }, [countDays]);
358
+ react.useEffect(() => {
359
+ let cancelled = false;
360
+ setGraphLoading(true);
361
+ setGraphError(null);
362
+ const url = `/admin/analytics/customers-over-time?period=${graphPeriod}`;
363
+ fetch("http://127.0.0.1:7242/ingest/47f434f8-0643-48e5-bd1a-edfe05973288", {
364
+ method: "POST",
365
+ headers: { "Content-Type": "application/json", "X-Debug-Session-Id": "4a7f37" },
366
+ body: JSON.stringify({
367
+ sessionId: "4a7f37",
368
+ location: "CustomersDashboard.tsx:fetch",
369
+ message: "customers-over-time fetch start",
370
+ data: { graphPeriod, url },
371
+ timestamp: Date.now(),
372
+ hypothesisId: "H1-H2"
373
+ })
374
+ }).catch(() => {
375
+ });
376
+ fetch(url).then((res) => {
377
+ if (!res.ok) throw new Error(res.statusText);
378
+ return res.json();
379
+ }).then((body) => {
380
+ var _a, _b;
381
+ const received = body.series ?? [];
382
+ fetch("http://127.0.0.1:7242/ingest/47f434f8-0643-48e5-bd1a-edfe05973288", {
383
+ method: "POST",
384
+ headers: { "Content-Type": "application/json", "X-Debug-Session-Id": "4a7f37" },
385
+ body: JSON.stringify({
386
+ sessionId: "4a7f37",
387
+ location: "CustomersDashboard.tsx:response",
388
+ message: "customers-over-time response",
389
+ data: { graphPeriod, seriesLength: received.length, firstDate: (_a = received[0]) == null ? void 0 : _a.date, lastDate: (_b = received[received.length - 1]) == null ? void 0 : _b.date, cancelled },
390
+ timestamp: Date.now(),
391
+ hypothesisId: "H2-H5"
392
+ })
393
+ }).catch(() => {
394
+ });
395
+ if (!cancelled) setSeries(received);
396
+ }).catch((e) => {
397
+ if (!cancelled) setGraphError(e instanceof Error ? e.message : String(e));
398
+ }).finally(() => {
399
+ if (!cancelled) setGraphLoading(false);
400
+ });
401
+ return () => {
402
+ cancelled = true;
403
+ };
404
+ }, [graphPeriod]);
405
+ if (loading) {
406
+ return /* @__PURE__ */ jsxRuntime.jsx(
407
+ "div",
408
+ {
409
+ className: "flex items-center justify-center min-h-[320px]",
410
+ role: "status",
411
+ "aria-label": "Loading analytics",
412
+ children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-muted", children: "Loading…" })
413
+ }
414
+ );
415
+ }
416
+ if (error || !data) {
417
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { className: "p-6", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-danger", children: error ?? "Failed to load analytics" }) });
418
+ }
419
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-8", children: [
420
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-wrap items-center justify-between gap-4", children: [
421
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h1", children: "Customers Analytics" }),
422
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center gap-4", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
423
+ /* @__PURE__ */ jsxRuntime.jsx(
424
+ ui.Label,
425
+ {
426
+ htmlFor: "customers-count-period",
427
+ className: "text-ui-fg-muted text-sm",
428
+ children: "Period (counts)"
429
+ }
430
+ ),
431
+ /* @__PURE__ */ jsxRuntime.jsx(
432
+ "select",
433
+ {
434
+ id: "customers-count-period",
435
+ value: countDays,
436
+ onChange: (e) => setCountDays(e.target.value),
437
+ className: "h-9 rounded-md border border-ui-border-base bg-transparent px-3 text-sm text-ui-fg-base outline-none transition focus:ring-2 focus:ring-ui-fg-interactive",
438
+ children: COUNT_DAY_PRESETS.map((p) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: p.value, children: p.label }, p.value))
439
+ }
440
+ )
441
+ ] }) })
442
+ ] }),
443
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-1 sm:grid-cols-3 gap-4", children: [
444
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { className: "p-4", children: [
445
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-muted text-sm", children: "Guest customers" }),
446
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h2", children: Math.floor(data.guestCount).toLocaleString() })
447
+ ] }),
448
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { className: "p-4", children: [
449
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-muted text-sm", children: "Registered customers" }),
450
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h2", children: Math.floor(data.registeredCount).toLocaleString() })
451
+ ] }),
452
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { className: "p-4", children: [
453
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-muted text-sm", children: "Overall customers" }),
454
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h2", children: Math.floor(data.totalCount).toLocaleString() })
455
+ ] })
456
+ ] }),
457
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { className: "p-6", children: [
458
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-wrap items-center justify-between gap-4 mb-4", children: [
459
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h3", children: "Customers over time" }),
460
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
461
+ /* @__PURE__ */ jsxRuntime.jsx(
462
+ ui.Label,
463
+ {
464
+ htmlFor: "customers-graph-period",
465
+ className: "text-ui-fg-muted text-sm",
466
+ children: "Range"
467
+ }
468
+ ),
469
+ /* @__PURE__ */ jsxRuntime.jsx(
470
+ "select",
471
+ {
472
+ id: "customers-graph-period",
473
+ value: graphPeriod,
474
+ onChange: (e) => setGraphPeriod(e.target.value),
475
+ className: "h-9 rounded-md border border-ui-border-base bg-transparent px-3 text-sm text-ui-fg-base outline-none transition focus:ring-2 focus:ring-ui-fg-interactive",
476
+ children: GRAPH_PERIODS.map((p) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: p.value, children: p.label }, p.value))
477
+ }
478
+ )
479
+ ] })
480
+ ] }),
481
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "h-72", children: graphLoading ? /* @__PURE__ */ jsxRuntime.jsx(
482
+ "div",
483
+ {
484
+ className: "flex items-center justify-center h-full",
485
+ role: "status",
486
+ "aria-label": "Loading customers over time",
487
+ children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-muted", children: "Loading chart…" })
488
+ }
489
+ ) : graphError ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-center h-full", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-danger", children: graphError }) }) : /* @__PURE__ */ jsxRuntime.jsx(recharts.ResponsiveContainer, { width: "100%", height: "100%", children: /* @__PURE__ */ jsxRuntime.jsxs(
490
+ recharts.LineChart,
491
+ {
492
+ data: series,
493
+ margin: { top: 8, right: 8, left: 8, bottom: 8 },
494
+ children: [
495
+ /* @__PURE__ */ jsxRuntime.jsx(
496
+ recharts.CartesianGrid,
497
+ {
498
+ strokeDasharray: "3 3",
499
+ className: "stroke-ui-border-base"
500
+ }
501
+ ),
502
+ /* @__PURE__ */ jsxRuntime.jsx(
503
+ recharts.XAxis,
504
+ {
505
+ dataKey: "date",
506
+ tick: { fontSize: 12 },
507
+ tickFormatter: (v, index) => {
508
+ const row = series[index];
509
+ if (row == null ? void 0 : row.label) return row.label;
510
+ const d = new Date(v);
511
+ return `${d.getUTCMonth() + 1}/${d.getUTCDate()}`;
512
+ }
513
+ }
514
+ ),
222
515
  /* @__PURE__ */ jsxRuntime.jsx(
223
- recharts.Tooltip,
516
+ recharts.YAxis,
224
517
  {
225
- labelFormatter: (v) => new Date(v).toLocaleDateString(),
226
- formatter: (value) => [value ?? 0, "Orders"]
518
+ tick: { fontSize: 12 },
519
+ tickFormatter: (v) => String(Math.floor(Number(v))),
520
+ allowDecimals: false
227
521
  }
228
522
  ),
523
+ /* @__PURE__ */ jsxRuntime.jsx(recharts.Tooltip, { content: /* @__PURE__ */ jsxRuntime.jsx(CustomersOverTimeTooltip, {}) }),
524
+ /* @__PURE__ */ jsxRuntime.jsx(recharts.Legend, {}),
229
525
  /* @__PURE__ */ jsxRuntime.jsx(
230
526
  recharts.Line,
231
527
  {
232
528
  type: "monotone",
233
- dataKey: "orders_count",
234
- name: "Orders",
235
- stroke: "var(--medusa-color-ui-fg-interactive)",
529
+ dataKey: "registered_count",
530
+ name: "Registered",
531
+ stroke: REGISTERED_COLOR,
532
+ strokeWidth: 2,
533
+ dot: { r: 3 }
534
+ }
535
+ ),
536
+ /* @__PURE__ */ jsxRuntime.jsx(
537
+ recharts.Line,
538
+ {
539
+ type: "monotone",
540
+ dataKey: "guest_count",
541
+ name: "Guest",
542
+ stroke: GUEST_COLOR,
236
543
  strokeWidth: 2,
237
544
  dot: { r: 3 }
238
545
  }
@@ -243,7 +550,10 @@ function OrdersDashboard() {
243
550
  ] })
244
551
  ] });
245
552
  }
246
- const ANALYTICS_MODULES = [{ id: "orders", label: "Orders" }];
553
+ const ANALYTICS_MODULES = [
554
+ { id: "orders", label: "Orders" },
555
+ { id: "customers", label: "Customers" }
556
+ ];
247
557
  const PLUGIN_VERSION = "0.2";
248
558
  const AnalyticsPage = () => {
249
559
  const [activeModule, setActiveModule] = react.useState("orders");
@@ -289,7 +599,10 @@ const AnalyticsPage = () => {
289
599
  children: "Analysis"
290
600
  }
291
601
  ) }),
292
- /* @__PURE__ */ jsxRuntime.jsx("main", { className: "flex-1 overflow-auto", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { className: "divide-y p-0", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "py-6 px-6", children: activeModule === "orders" && /* @__PURE__ */ jsxRuntime.jsx(OrdersDashboard, {}) }) }) })
602
+ /* @__PURE__ */ jsxRuntime.jsx("main", { className: "flex-1 overflow-auto", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { className: "divide-y p-0", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "py-6 px-6", children: [
603
+ activeModule === "orders" && /* @__PURE__ */ jsxRuntime.jsx(OrdersDashboard, {}),
604
+ activeModule === "customers" && /* @__PURE__ */ jsxRuntime.jsx(CustomersDashboard, {})
605
+ ] }) }) })
293
606
  ] });
294
607
  };
295
608
  const config = adminSdk.defineRouteConfig({