@makoai/app-sdk 2.20.0 → 2.22.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 +54 -0
- package/components/alert.tsx +65 -0
- package/components/avatar.tsx +108 -0
- package/components/badge.tsx +47 -0
- package/components/button.tsx +63 -0
- package/components/card.tsx +91 -0
- package/components/collapsible.tsx +33 -0
- package/components/dialog.tsx +158 -0
- package/components/index.ts +31 -0
- package/components/input.tsx +20 -0
- package/components/label.tsx +23 -0
- package/components/popover.tsx +88 -0
- package/components/progress.tsx +30 -0
- package/components/select.tsx +189 -0
- package/components/separator.tsx +27 -0
- package/components/sheet.tsx +142 -0
- package/components/skeleton.tsx +13 -0
- package/components/slider.tsx +62 -0
- package/components/switch.tsx +34 -0
- package/components/table.tsx +115 -0
- package/components/tabs.tsx +90 -0
- package/components/toggle-group.tsx +83 -0
- package/components/toggle.tsx +46 -0
- package/components/tooltip.tsx +56 -0
- package/index.d.ts +38 -0
- package/index.js +2 -1
- package/lint.js +79 -0
- package/package.json +19 -2
- package/tailwind.css +60 -0
- package/time.js +110 -0
- package/ui.d.ts +3 -0
- package/ui.js +1 -0
package/time.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Times and dates (2.21). The SDK runs DuckDB in UTC, and that stays: display
|
|
2
|
+
// code decides the zone. Two kinds of value reach an app:
|
|
3
|
+
//
|
|
4
|
+
// - Instants (an event time, "updated at", a deploy): shown in the VIEWER's
|
|
5
|
+
// zone with a zone label, "14:05 CET". formatTime().
|
|
6
|
+
// - Day buckets (a dbt `date`, a UTC `date_trunc('day', …)`): a calendar day,
|
|
7
|
+
// printed with no shift. formatDate(). A stored day never goes through the
|
|
8
|
+
// viewer's zone, so 18 Nov stays 18 Nov in Los Angeles.
|
|
9
|
+
//
|
|
10
|
+
// toDayKey() is the one place a UTC "YYYY-MM-DD" key is built (URL state,
|
|
11
|
+
// joins, Map keys). Display code never calls getUTC*, never sets
|
|
12
|
+
// timeZone: "UTC" and never slices toISOString(): `mako-app-lint` flags it.
|
|
13
|
+
|
|
14
|
+
const DAY_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
15
|
+
// "2026-11-18 14:05:00", "2026-11-18T14:05:00.123": a DuckDB TIMESTAMP rendered
|
|
16
|
+
// without a zone. DuckDB runs in UTC, so it is a UTC wall time.
|
|
17
|
+
const NAIVE_RE = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?$/;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Any time value an app holds as a Date: a Date, epoch milliseconds, a bigint
|
|
21
|
+
* of epoch microseconds (DuckDB TIMESTAMP through Arrow), an ISO string with
|
|
22
|
+
* a zone, or a zone-less "YYYY-MM-DD hh:mm[:ss]" read as UTC. A bare
|
|
23
|
+
* "YYYY-MM-DD" is that day's UTC midnight. Null, "" and unparsable values
|
|
24
|
+
* give null.
|
|
25
|
+
*/
|
|
26
|
+
export function toInstant(value) {
|
|
27
|
+
if (value == null || value === "") return null;
|
|
28
|
+
if (value instanceof Date) return Number.isNaN(value.getTime()) ? null : value;
|
|
29
|
+
if (typeof value === "bigint") return new Date(Number(value / 1000n));
|
|
30
|
+
if (typeof value === "number") return Number.isFinite(value) ? new Date(value) : null;
|
|
31
|
+
const s = String(value).trim();
|
|
32
|
+
let m = DAY_RE.exec(s);
|
|
33
|
+
if (m) return new Date(Date.UTC(+m[1], +m[2] - 1, +m[3]));
|
|
34
|
+
m = NAIVE_RE.exec(s);
|
|
35
|
+
if (m) {
|
|
36
|
+
const ms = m[7] ? Math.round(Number(`0.${m[7]}`) * 1000) : 0;
|
|
37
|
+
return new Date(Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +(m[6] ?? 0), ms));
|
|
38
|
+
}
|
|
39
|
+
const d = new Date(s);
|
|
40
|
+
return Number.isNaN(d.getTime()) ? null : d;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The UTC calendar day of a value, "YYYY-MM-DD": the key for URL state, joins and maps. Null when unparsable. */
|
|
44
|
+
export function toDayKey(value) {
|
|
45
|
+
if (typeof value === "string" && DAY_RE.test(value.trim())) return value.trim();
|
|
46
|
+
const d = toInstant(value);
|
|
47
|
+
if (!d) return null;
|
|
48
|
+
const p = n => String(n).padStart(2, "0");
|
|
49
|
+
return `${d.getUTCFullYear()}-${p(d.getUTCMonth() + 1)}-${p(d.getUTCDate())}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const sameDay = (a, b, timeZone) => {
|
|
53
|
+
const f = new Intl.DateTimeFormat("en-CA", { timeZone, year: "numeric", month: "2-digit", day: "2-digit" });
|
|
54
|
+
return f.format(a) === f.format(b);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* An instant in the viewer's zone, with the zone's short name: "14:05 CET",
|
|
59
|
+
* "18 Nov, 14:05 CET" when it is not today there, "18 Nov 2025, 14:05 CET"
|
|
60
|
+
* in another year.
|
|
61
|
+
*
|
|
62
|
+
* Options: `date` ("auto" default, true, false), `seconds` (false),
|
|
63
|
+
* `zoneName` (true: "CET"), `locale` (the browser's), `timeZone` (the
|
|
64
|
+
* viewer's; tests and "as seen from Zurich" views only), `now` (for "auto").
|
|
65
|
+
* Null or unparsable input gives "—".
|
|
66
|
+
*/
|
|
67
|
+
export function formatTime(value, opts = {}) {
|
|
68
|
+
const d = toInstant(value);
|
|
69
|
+
if (!d) return "—";
|
|
70
|
+
const { date = "auto", seconds = false, zoneName = true, locale, timeZone, now = new Date() } = opts;
|
|
71
|
+
const showDate = date === true || (date === "auto" && !sameDay(d, now, timeZone));
|
|
72
|
+
const otherYear =
|
|
73
|
+
showDate &&
|
|
74
|
+
new Intl.DateTimeFormat("en", { timeZone, year: "numeric" }).format(d) !==
|
|
75
|
+
new Intl.DateTimeFormat("en", { timeZone, year: "numeric" }).format(now);
|
|
76
|
+
return new Intl.DateTimeFormat(locale, {
|
|
77
|
+
timeZone,
|
|
78
|
+
hour: "2-digit",
|
|
79
|
+
minute: "2-digit",
|
|
80
|
+
...(seconds ? { second: "2-digit" } : {}),
|
|
81
|
+
hourCycle: "h23",
|
|
82
|
+
...(showDate ? { day: "numeric", month: "short" } : {}),
|
|
83
|
+
...(otherYear ? { year: "numeric" } : {}),
|
|
84
|
+
...(zoneName ? { timeZoneName: "short" } : {}),
|
|
85
|
+
}).format(d);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A calendar day with no zone shift: "18 Nov 2026". Takes a "YYYY-MM-DD"
|
|
90
|
+
* string, a day's UTC midnight (a parquet `date`), or anything toInstant
|
|
91
|
+
* reads (its UTC day). Options: `year` ("auto" default: shown unless it is
|
|
92
|
+
* the current UTC year; true; false), `weekday` (false; "short" or "long"),
|
|
93
|
+
* `month` ("short" default; "long", "numeric"), `locale`, `now`.
|
|
94
|
+
* Null or unparsable input gives "—".
|
|
95
|
+
*/
|
|
96
|
+
export function formatDate(value, opts = {}) {
|
|
97
|
+
const key = toDayKey(value);
|
|
98
|
+
if (!key) return "—";
|
|
99
|
+
const { year = "auto", weekday = false, month = "short", locale, now = new Date() } = opts;
|
|
100
|
+
const [y, m, d] = key.split("-").map(Number);
|
|
101
|
+
const showYear = year === true || (year === "auto" && y !== now.getUTCFullYear());
|
|
102
|
+
// The day's UTC noon, formatted in UTC: the printed day is the stored day, whatever the viewer's zone.
|
|
103
|
+
return new Intl.DateTimeFormat(locale, {
|
|
104
|
+
timeZone: "UTC",
|
|
105
|
+
day: "numeric",
|
|
106
|
+
month,
|
|
107
|
+
...(showYear ? { year: "numeric" } : {}),
|
|
108
|
+
...(weekday ? { weekday } : {}),
|
|
109
|
+
}).format(new Date(Date.UTC(y, m - 1, d, 12)));
|
|
110
|
+
}
|
package/ui.d.ts
CHANGED
|
@@ -482,3 +482,6 @@ export function csvDate(date: Date): string;
|
|
|
482
482
|
export function csvToTsv(text: string): string;
|
|
483
483
|
/** Download CSV text as a file. Works inside Mako's app frame. */
|
|
484
484
|
export function saveCsv(filename: string, text: string): "downloaded" | "failed";
|
|
485
|
+
|
|
486
|
+
export { formatDate, formatTime, toDayKey, toInstant } from "./index";
|
|
487
|
+
export type { FormatDateOptions, FormatTimeOptions, TimeValue } from "./index";
|