stackchain-dashboard/frontend/today-rollover.js
timmy 53c4340d76
All checks were successful
CI / lint (pull_request) Successful in 3m19s
CI / build-release (pull_request) Successful in 8s
CI / browser-journey (pull_request) Successful in 4m30s
CI / release-candidate (pull_request) Has been skipped
fix: tolerate invalid device timezones in Week Ahead (Closes #1177)
2026-08-20 12:26:35 +00:00

44 lines
1.7 KiB
JavaScript

function createTodayRollover(options = {}) {
const now = options.now || (() => new Date());
const resolvedTimeZone = options.resolvedTimeZone || (() =>
Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC');
function timeZone() {
const candidate = options.timeZone?.() || resolvedTimeZone();
try {
new Intl.DateTimeFormat('en', { timeZone: candidate }).format(now());
return candidate;
} catch (_error) {
return 'UTC';
}
}
function localDate() {
if (options.localDate) return options.localDate();
const parts = new Intl.DateTimeFormat('en-CA', {
timeZone: timeZone(), year: 'numeric', month: '2-digit', day: '2-digit',
}).formatToParts(now());
const value = Object.fromEntries(parts.map(part => [part.type, part.value]));
return `${value.year}-${value.month}-${value.day}`;
}
function reviewState(plan) {
if (!Array.isArray(plan?.ids) || !plan.ids.length) return 'empty';
if (!plan.plan_date) return 'legacy';
return plan.plan_date === localDate() ? 'current' : 'stale';
}
function operation({ operation_id, base_revision, selected_ids, capacity_minutes, estimates = {} }) {
const ids = [...new Set((selected_ids || []).filter(id => typeof id === 'string' && id))];
const selected = new Set(ids);
return {
operation_id, action: 'rollover', item_id: 'plan', base_revision,
plan_date: localDate(), timezone: timeZone(), ids, capacity_minutes,
estimates: Object.fromEntries(Object.entries(estimates).filter(([id]) => selected.has(id))),
};
}
return { localDate, timeZone, reviewState, operation };
}
if (typeof module !== 'undefined' && module.exports) module.exports = createTodayRollover;