Files
sunscope/assets/js/events/dynamic-message.js
T
fraxle 916efca55e 3.5
UI Overhaul
Profiles now in popup
Current profile always on screen
Hidden columns only when edit neeeded
Add pulldowns in to profile config
Customised the day tabs for vehicle, indoor and pet
2026-07-18 15:45:20 +01:00

33 lines
1.7 KiB
JavaScript

// ------------------------------------------------------------------------
// dynamic-message.js - Rewrites a cosmic event's message based on where
// today sits vs. the peak.
//
// Before peak : "is active and building - peak on <date>. <detail> (~NN% chance...)"
// On peak -1d : "peaks tonight - <detail> (~NN% chance...)"
// After peak : "is past its peak (<date>) but still possibly visible - <detail> (~NN% chance...)"
//
// Single-day events (start === end) keep their static message unchanged.
// `ev.chance` (see ../events/activity-profile.js), when present, is
// appended as an estimated viewing-chance figure.
// ------------------------------------------------------------------------
export function dynamicCosmicMessage(ev, dateStr) {
if (!ev.peak || ev.start === ev.end) return ev.message;
var today = new Date(dateStr + 'T00:00Z');
var peak = new Date(ev.peak + 'T00:00Z');
var diffDays = Math.round((today - peak) / 86400000);
var peakFmt = peak.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', timeZone: 'UTC' });
var base = ev.message
.replace(/^.*?(?:peaks tonight|peak tonight|is active tonight|is active|peaks)\s*—\s*/i, '')
.trim();
var baseCapd = base.charAt(0).toUpperCase() + base.slice(1);
var chanceSuffix = typeof ev.chance === 'number' ? ' (~' + ev.chance + '% chance of a good show tonight)' : '';
if (diffDays < -1) {
return ev.title + ' is active and building — peak on ' + peakFmt + '. ' + baseCapd + chanceSuffix;
} else if (diffDays <= 1) {
return ev.title + ' peaks tonight — ' + base + chanceSuffix;
} else {
return ev.title + ' is past its peak (' + peakFmt + ') but still possibly visible — ' + base + chanceSuffix;
}
}