Files
Enterprise-Knowledge-Base/frontend/src/utils/cronHumanize.ts
T
wizardchen adabf8e089 feat(datasource): implement data source management functionality
- Added a new API for managing data sources, including CRUD operations and connection validation.
- Introduced UI components for data source settings, sync logs, and editor dialog, enhancing user experience in managing external data sources.
- Updated localization files to support new data source features in multiple languages.
- Implemented cron expression humanization for better scheduling visibility.
- Enhanced knowledge base editor to include data source management options.
2026-03-30 20:46:07 +08:00

47 lines
1.7 KiB
TypeScript

// cronHumanize maps known cron schedule presets to human-readable i18n keys.
// Falls back to the raw cron expression for unknown patterns.
const CRON_PRESET_MAP: Record<string, string> = {
'0 */30 * * * *': 'datasource.scheduleHuman.30min',
'0 0 * * * *': 'datasource.scheduleHuman.1h',
'0 0 */6 * * *': 'datasource.scheduleHuman.6h',
'0 0 */12 * * *': 'datasource.scheduleHuman.12h',
'0 0 2 * * *': 'datasource.scheduleHuman.24h',
}
/**
* Convert a cron expression to a human-readable string.
* Uses i18n translate function for known presets, raw expression otherwise.
*/
export function humanizeCron(cron: string, t: (key: string) => string): string {
const key = CRON_PRESET_MAP[cron]
if (key) return t(key)
return cron || '--'
}
/**
* Format a timestamp as relative time (e.g. "3小时前", "2天前").
* Falls back to locale date string for timestamps older than 30 days.
*/
export function relativeTime(ts: string | null, t: (key: string, params?: Record<string, string | number>) => string): string {
if (!ts) return t('datasource.neverSynced')
const then = new Date(ts).getTime()
if (isNaN(then)) return t('datasource.neverSynced')
const now = Date.now()
const diffMs = now - then
// Future time or just happened (within 60s)
if (diffMs < 60000) return t('datasource.justNow')
const minutes = Math.floor(diffMs / 60000)
if (minutes < 60) return t('datasource.minutesAgo', { n: minutes })
const hours = Math.floor(diffMs / 3600000)
if (hours < 24) return t('datasource.hoursAgo', { n: hours })
const days = Math.floor(diffMs / 86400000)
if (days < 30) return t('datasource.daysAgo', { n: days })
return new Date(ts).toLocaleDateString()
}