@blocklet/ui-react
Version:
Some useful front-end web components that can be used in Blocklets.
122 lines (121 loc) • 3.57 kB
JavaScript
import n from "dayjs";
import T from "dayjs/plugin/timezone";
import g from "dayjs/plugin/utc";
import { DurationEnum as s } from "../../../@types/index.js";
n.extend(g);
n.extend(T);
const u = 3600, l = 1800, m = 600, d = 300, f = 60, p = 1, M = n.tz.guess(), A = [
"America/New_York",
"America/Chicago",
"America/Denver",
"America/Los_Angeles",
"Europe/London",
"Europe/Paris",
"Europe/Berlin",
"Europe/Rome",
"Asia/Tokyo",
"Asia/Shanghai",
"Asia/Hong_Kong",
"Asia/Singapore",
"Asia/Seoul",
"Asia/Kolkata",
"Australia/Sydney",
"Australia/Melbourne",
"Pacific/Auckland",
"America/Sao_Paulo",
"America/Mexico_City",
"Africa/Cairo",
"UTC"
], h = () => {
if (typeof Intl < "u" && Intl.supportedValuesOf)
try {
return Intl.supportedValuesOf("timeZone");
} catch {
console.warn("Intl.supportedValuesOf not supported, falling back to common timezones");
}
if (typeof Intl < "u" && Intl.DateTimeFormat)
try {
if (new Intl.DateTimeFormat("en", { timeZone: "UTC" }).resolvedOptions().timeZone)
return A;
} catch {
console.warn("Intl.DateTimeFormat timezone support limited");
}
return A;
}, I = () => h().map((e) => {
try {
const t = n.tz(n(), e).utcOffset() / 60, a = Math.floor(t), i = t % 1 * 60;
return { label: `GMT${a >= 0 ? "+" : ""}${a}:${i === 30 ? "30" : "00"}`, value: e };
} catch {
return console.warn(`Timezone ${e} not supported, skipping`), null;
}
}).filter((e) => e !== null).sort((e, t) => {
const [a, i] = e.label.replace("GMT", "").split(":").map(Number), [c, b] = t.label.replace("GMT", "").split(":").map(Number), y = a * 60 + i;
return c * 60 + b - y;
}).map((e) => ({
label: `(${e.label}) ${e.value}`,
value: e.value
})), N = (o) => /^(https?:\/\/)?((([a-zA-Z\d]([a-zA-Z\d-]*[a-zA-Z\d])*)\.)+[a-zA-Z]{2,}|((\d{1,3}\.){3}\d{1,3}))(:\d+)?(\/[-a-zA-Z\d%_.~+]*)*(\?[;&a-zA-Z\d%_.~+=-]*)?(#[a-zA-Z\d_]*)?$/.test(o), _ = (o) => {
let r = o?.dateRange?.map((t) => n(t)) ?? [];
const e = n();
switch (o?.duration) {
case s.ThirtyMinutes:
r = [e, e.add(30, "minutes")];
break;
case s.OneHour:
r = [e, e.add(1, "hour")];
break;
case s.FourHours:
r = [e, e.add(4, "hours")];
break;
case s.Today:
r = [e, e.endOf("day")];
break;
case s.ThisWeek:
r = [e, e.endOf("week")];
break;
case s.NoClear:
r = [e, e];
break;
}
return r.map((t) => t.toDate());
}, E = (o) => {
const r = n();
return r.isAfter(n(o[0])) && r.isBefore(n(o[1]));
}, Z = (o) => {
const { duration: r, dateRange: e } = o ?? {};
return !r || !e ? !1 : r === s.NoClear || n(e?.[0]).isSame(n(e?.[1]));
}, v = (o) => {
const r = n(), t = n(o).diff(r, "seconds"), a = (i) => i * 1e3;
return t >= u ? a(u) : t >= l ? a(l) : t >= m ? a(m) : t >= d ? a(d) : t >= f ? a(f) : t >= p ? a(p) : 0;
}, w = {
color: "text.primary",
borderColor: "grey.100",
backgroundColor: "background.default",
"&:hover": {
borderColor: "grey.100",
backgroundColor: "action.hover"
},
py: 0.5,
borderRadius: 1
}, B = {
color: "primary.contrastText",
borderColor: "primary.main",
backgroundColor: "primary.main",
"&:hover": {
borderColor: "primary.main",
backgroundColor: "primary.main"
},
py: 0.5,
borderRadius: 1
};
export {
M as currentTimezone,
w as defaultButtonStyle,
_ as getStatusDuration,
v as getTimeRemaining,
I as getTimezones,
Z as isNotClear,
N as isValidUrl,
E as isWithinTimeRange,
B as primaryButtonStyle
};