@blocklet/ui-react
Version:
Some useful front-end web components that can be used in Blocklets.
195 lines (194 loc) • 5.14 kB
JavaScript
import T from "is-url";
import { withHttps as u } from "ufo";
import i from "dayjs";
import A from "dayjs/plugin/timezone";
import I from "dayjs/plugin/utc";
import { DurationEnum as s } from "../../../@types/index.js";
i.extend(I);
i.extend(A);
const d = 3600, p = 1800, f = 600, k = 300, b = 60, g = 1, E = i.tz.guess(), y = [
"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"
], C = () => {
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 y;
} catch {
console.warn("Intl.DateTimeFormat timezone support limited");
}
return y;
}, _ = () => C().map((o) => {
try {
const r = i.tz(i(), o).utcOffset() / 60, n = Math.floor(r), a = r % 1 * 60;
return { label: `GMT${n >= 0 ? "+" : ""}${n}:${a === 30 ? "30" : "00"}`, value: o };
} catch {
return console.warn(`Timezone ${o} not supported, skipping`), null;
}
}).filter((o) => o !== null).sort((o, r) => {
const [n, a] = o.label.replace("GMT", "").split(":").map(Number), [c, l] = r.label.replace("GMT", "").split(":").map(Number), m = n * 60 + a;
return c * 60 + l - m;
}).map((o) => ({
label: `(${o.label}) ${o.value}`,
value: o.value
})), h = (t) => T(u(t)), R = (t, e) => {
if (!t || !e || !h(t) || !h(e))
return !1;
const o = u(t.trim()), r = u(e.trim());
return o.toLowerCase() === r.toLowerCase();
}, z = (t) => {
let e = t?.dateRange?.map((r) => i(r)) ?? [];
const o = i();
switch (t?.duration) {
case s.ThirtyMinutes:
e = [o, o.add(30, "minutes")];
break;
case s.OneHour:
e = [o, o.add(1, "hour")];
break;
case s.FourHours:
e = [o, o.add(4, "hours")];
break;
case s.Today:
e = [o, o.endOf("day")];
break;
case s.ThisWeek:
e = [o, o.endOf("week")];
break;
case s.NoClear:
e = [o, o];
break;
}
return e.map((r) => r.toDate());
}, B = (t) => {
const e = i();
return e.isAfter(i(t[0])) && e.isBefore(i(t[1]));
}, L = (t) => {
const { duration: e, dateRange: o } = t ?? {};
return !e || !o ? !1 : e === s.NoClear || i(o?.[0]).isSame(i(o?.[1]));
}, x = (t) => {
const e = i(), r = i(t).diff(e, "seconds"), n = (a) => a * 1e3;
return r >= d ? n(d) : r >= p ? n(p) : r >= f ? n(f) : r >= k ? n(k) : r >= b ? n(b) : r >= g ? n(g) : 0;
}, $ = {
color: "text.primary",
borderColor: "grey.100",
backgroundColor: "background.default",
"&:hover": {
borderColor: "grey.100",
backgroundColor: "action.hover"
},
py: 0.5,
borderRadius: 1
}, F = {
color: "primary.contrastText",
borderColor: "primary.main",
backgroundColor: "primary.main",
"&:hover": {
borderColor: "primary.main",
backgroundColor: "primary.main"
},
py: 0.5,
borderRadius: 1
}, O = {
x: {
domains: ["twitter.com", "x.com"]
},
facebook: {
domains: ["facebook.com", "fb.com"],
options: { skipDarkInvert: !0 }
},
"linkedin-icon": {
domains: ["linkedin.com"],
options: { skipDarkInvert: !0 }
},
"github-icon": {
domains: ["github.com"]
},
"instagram-icon": {
domains: ["instagram.com"]
},
"youtube-icon": {
domains: ["youtube.com", "youtu.be"],
options: { skipDarkInvert: !0 }
},
"tiktok-icon": {
domains: ["tiktok.com"]
},
"reddit-icon": {
domains: ["reddit.com"],
options: { skipDarkInvert: !0 }
},
"medium-icon": {
domains: ["medium.com"]
},
"discord-icon": {
domains: ["discord.com", "discord.gg"],
options: { skipDarkInvert: !0 }
},
telegram: {
domains: ["telegram.org", "t.me"],
options: { skipDarkInvert: !0 }
},
"whatsapp-monochrome-icon": {
domains: ["whatsapp.com"]
},
producthunt: {
domains: ["producthunt.com"],
options: { skipDarkInvert: !0 }
},
ycombinator: {
domains: ["ycombinator.com", "news.ycombinator.com"],
options: { skipDarkInvert: !0 }
}
}, H = (t) => {
try {
const e = u(t), n = new URL(e).hostname.toLowerCase().replace(/^www\./, "");
for (const [a, { domains: c, options: l }] of Object.entries(O))
if (c.some((m) => n === m || n.endsWith(`.${m}`)))
return {
icon: `logos:${a}`,
options: l
};
return;
} catch {
return;
}
};
export {
E as currentTimezone,
$ as defaultButtonStyle,
H as getLogoByUrl,
z as getStatusDuration,
x as getTimeRemaining,
_ as getTimezones,
R as isDuplicateUrl,
L as isNotClear,
h as isValidUrl,
B as isWithinTimeRange,
F as primaryButtonStyle
};