Files
pikasTech-HWLAB/web/hwlab-cloud-web/src/config/runtime.ts
T

60 lines
2.1 KiB
TypeScript

import type { WorkbenchTraceTimelinePolicy } from "@/components/workbench/trace-lifecycle";
export interface DisplayTimeConfig {
timeZone: string;
locale: string;
label: string;
}
export function displayTimeConfig(): DisplayTimeConfig {
const config = window.HWLAB_CLOUD_WEB_CONFIG?.displayTime;
const timeZone = requiredDisplayTimeField(config?.timeZone, "timeZone");
const locale = requiredDisplayTimeField(config?.locale, "locale");
const label = requiredDisplayTimeField(config?.label, "label");
validateDisplayTimeZone(timeZone);
validateDisplayLocale(locale);
return { timeZone, locale, label };
}
export function displayTimeLabel(): string {
return displayTimeConfig().label;
}
export function formatDisplayDateTime(value: string | Date, options: Intl.DateTimeFormatOptions = {}): string {
const date = value instanceof Date ? value : new Date(value);
if (!Number.isFinite(date.getTime())) return String(value);
const config = displayTimeConfig();
return new Intl.DateTimeFormat(config.locale, { hour12: false, ...options, timeZone: config.timeZone }).format(date);
}
function requiredDisplayTimeField(value: unknown, field: string): string {
if (typeof value !== "string" || value.trim().length === 0) {
throw new Error(`HWLAB Cloud Web displayTime.${field} is required`);
}
return value.trim();
}
function validateDisplayTimeZone(timeZone: string): void {
try {
new Intl.DateTimeFormat("en-US", { timeZone }).format(new Date(0));
} catch {
throw new Error(`HWLAB Cloud Web displayTime.timeZone is invalid: ${timeZone}`);
}
}
function validateDisplayLocale(locale: string): void {
try {
new Intl.DateTimeFormat(locale).format(new Date(0));
} catch {
throw new Error(`HWLAB Cloud Web displayTime.locale is invalid: ${locale}`);
}
}
export function workbenchTraceTimelinePolicy(): WorkbenchTraceTimelinePolicy {
const config = window.HWLAB_CLOUD_WEB_CONFIG?.workbench?.traceTimeline;
return {
autoExpandRunning: config?.autoExpandRunning === true,
autoCollapseTerminal: config?.autoCollapseTerminal === true
};
}