HR-ATS-Portal/frontend/src/lib/timeRanges.js

28 lines
1.1 KiB
JavaScript

/* ============================================================
timeRanges.js — the Week / Month / Quarter / Year filter vocabulary shared
by Dashboard and Analytics. One definition so the two screens can never
disagree about what "Quarter" means.
rangeWindow returns FRESH ISO strings on every call — callers must memoise
on the range key (never on the result) or their query keys churn each render
and refetch everything.
============================================================ */
export const RANGES = [
{ key: 'week', label: 'Week', days: 7 },
{ key: 'month', label: 'Month', days: 30 },
{ key: 'quarter', label: 'Quarter', days: 90 },
{ key: 'year', label: 'Year', days: 365 },
]
export function rangeWindow(key) {
const range = RANGES.find((r) => r.key === key) ?? RANGES[1]
const to = new Date()
const from = new Date(to.getTime() - range.days * 86400000)
return { fromDate: from.toISOString(), toDate: to.toISOString() }
}
export function rangeLabel(key) {
return (RANGES.find((r) => r.key === key) ?? RANGES[1]).label
}