- Introduced new utility functions for date formatting and module interval labeling. - Replaced inline badge variant logic with a dedicated function for improved readability. - Updated imports to streamline component usage and enhance maintainability. - Enhanced the overall structure of the module display for better clarity and user experience.
44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
import type { ModuleRow } from '$lib/api/types.js';
|
||
|
||
/** Форматирование ISO-даты для таблиц модулей и расписания. */
|
||
export function formatDateTime(value: string | null | undefined): string {
|
||
if (typeof value !== 'string' || value.trim().length === 0) return '—';
|
||
const parsed = new Date(value);
|
||
if (Number.isNaN(parsed.getTime())) return '—';
|
||
return parsed.toLocaleString('ru-RU');
|
||
}
|
||
|
||
/** Подпись интервала refresh: секунды, cron или комбинация. */
|
||
export function moduleIntervalLabel(moduleRow: ModuleRow): string {
|
||
const cron = typeof moduleRow.cron_expr === 'string' ? moduleRow.cron_expr.trim() : '';
|
||
const raw = moduleRow.refresh_interval_sec as unknown;
|
||
const interval =
|
||
typeof raw === 'number'
|
||
? raw
|
||
: typeof raw === 'string' && raw.trim().length > 0
|
||
? Number(raw)
|
||
: null;
|
||
const intervalLabel = interval !== null && Number.isFinite(interval) ? `${interval}с` : '';
|
||
if (cron && intervalLabel) return `${intervalLabel} (${cron})`;
|
||
if (cron) return cron;
|
||
if (intervalLabel) return intervalLabel;
|
||
return '—';
|
||
}
|
||
|
||
export function moduleTypeBadgeVariant(
|
||
type: string
|
||
): 'default' | 'secondary' | 'outline' | 'destructive' {
|
||
switch (type) {
|
||
case 'AS_PREFIXES':
|
||
return 'default';
|
||
case 'CDN_CIDRS':
|
||
return 'secondary';
|
||
case 'DOMAINS':
|
||
return 'outline';
|
||
case 'IP_RANGES':
|
||
return 'outline';
|
||
default:
|
||
return 'outline';
|
||
}
|
||
}
|