2026-11-17 14:08:13 +00:00
const SPAN_STEPS = 1000 ;
const SHORTEST = 60 ;
2026-11-06 08:28:04 +00:00
function windowQuery () {
const end = ui . asOf === null ? nowSeconds () : ui . asOf ;
const begin = ui . preset === 0 ? 0 : end - ui . preset ;
return `from= ${ Math . floor ( begin ) }${ ui . asOf === null ? "" : `&to= ${ Math . ceil ( end ) } ` } ` ;
}
const localInput = ( ts ) => {
const date = stampOf ( ts );
return new Date ( date . getTime () - date . getTimezoneOffset () * 60000 ). toISOString (). slice ( 0 , 16 );
};
2026-11-17 14:08:13 +00:00
function spanName ( seconds ) {
if ( seconds >= DAY ) {
return ` ${ Math . round ( seconds / DAY ) } d` ;
}
return seconds >= 3600 ? ` ${ Math . round ( seconds / 3600 ) } h` : ` ${ Math . max ( Math . round ( seconds / 60 ), 1 ) } min` ;
}
function windowName () {
const preset = PRESETS . find (([ seconds ]) => seconds === ui . preset );
return ` ${ preset ? preset [ 1 ] : spanName ( ui . preset ) }${ ui . asOf === null ? "" : " · past" } ` ;
}
/**
* @param {number} begin the window's start in epoch seconds.
* @param {number} end its end; at or after now the window is live and keeps its length.
*/
function setWindow ( begin , end ) {
stopReplay ();
const now = nowSeconds ();
const stop = Math . min ( end , now );
ui . preset = Math . max ( Math . round ( stop - begin ), SHORTEST );
ui . asOf = end >= now ? null : stop ;
reload ();
}
2026-11-06 22:20:06 +00:00
function customRange () {
2026-11-17 14:08:13 +00:00
const from = h ( "input" , { type : "datetime-local" , "aria-label" : "From" , value : localInput ( state . since )});
const to = h ( "input" , { type : "datetime-local" , "aria-label" : "To" , value : localInput ( state . until )});
2026-11-08 17:26:51 +00:00
return h ( "span" , { class : "custom" }, from , h ( "span" , { class : "muted" }, "to" ), to ,
2026-11-06 08:28:04 +00:00
h ( "button" , { type : "button" , class : "btn small soft" , on : { click : () => {
const begin = Date . parse ( from . value ) / 1000 ;
const end = Date . parse ( to . value ) / 1000 ;
if ( ! Number . isFinite ( begin ) || ! Number . isFinite ( end ) || begin >= end ) {
2026-11-08 17:26:51 +00:00
toast ( "Time range invalid" , [ "“From” must come before “To”." ], false );
2026-11-06 08:28:04 +00:00
return ;
}
2026-11-17 14:08:13 +00:00
setWindow ( begin , end );
2026-11-08 17:26:51 +00:00
}}}, "Apply" ));
2026-11-06 22:20:06 +00:00
}
2026-11-17 14:08:13 +00:00
function spanSlider () {
const [ from , to ] = [ "from" , "to" ]. map (( end , index ) => h ( "input" , { type : "range" , class : `span- ${ end } ` , min : 0 , max : SPAN_STEPS , step : 1 , value : index * SPAN_STEPS , "aria-label" : `Time range ${ end } ` }));
const fill = h ( "span" , { class : "span-fill" , "aria-hidden" : "true" });
const at = ( value ) => state . first + ( Math . max ( state . now - state . first , 1 ) * Number ( value )) / SPAN_STEPS ;
const moved = ( event ) => {
if ( Number ( from . value ) >= Number ( to . value )) {
event . target . value = String ( event . target === from ? Number ( to . value ) - 1 : Number ( from . value ) + 1 );
}
setWindow ( at ( from . value ), Number ( to . value ) >= SPAN_STEPS ? nowSeconds () : at ( to . value ));
};
from . addEventListener ( "input" , moved );
to . addEventListener ( "input" , moved );
return { box : h ( "div" , { class : "span-slider" }, fill , from , to ), from , to , fill };
}
function timeSection () {
2026-11-08 17:26:51 +00:00
const presets = h ( "div" , { class : "segmented" , role : "group" , "aria-label" : "Time range" });
2026-11-17 14:08:13 +00:00
for ( const [ seconds , name ] of PRESETS ) {
2026-11-06 08:28:04 +00:00
presets . append ( h ( "button" , { type : "button" , "data-preset" : seconds , on : { click : () => choosePreset ( seconds )}}, name ));
}
2026-11-17 14:08:13 +00:00
const slider = spanSlider ();
const play = iconBtn ( "bi-play-fill" , "Replay" , "btn small soft" , () => ( ui . playing ? stopReplay () : startReplay ()));
const liveButton = h ( "button" , { type : "button" , class : "btn small ghost" , on : { click : () => {
2026-11-06 08:28:04 +00:00
stopReplay ();
ui . asOf = null ;
reload ();
2026-11-17 14:08:13 +00:00
}}}, "Live" );
const label = h ( "span" , { class : "window-label" });
timeDom = { presets , from : slider . from , to : slider . to , fill : slider . fill , play , live : liveButton , label };
return [ h ( "h3" , {}, "Time range" ), presets , slider . box , h ( "div" , { class : "replay" }, play , liveButton , label ), customRange ()];
2026-11-06 08:28:04 +00:00
}
function reload () {
load ();
2026-11-17 14:08:13 +00:00
fetchRates ();
2026-11-06 08:28:04 +00:00
if ( ui . screen === "stream" ) {
fetchEvents ();
}
}
function choosePreset ( seconds ) {
stopReplay ();
ui . preset = seconds ;
2026-11-17 14:08:13 +00:00
ui . asOf = null ;
2026-11-06 08:28:04 +00:00
reload ();
}
function drawTime () {
if ( ! timeDom ) {
return ;
}
for ( const button of timeDom . presets . children ) {
2026-11-17 14:08:13 +00:00
button . setAttribute ( "aria-pressed" , String ( Number ( button . dataset . preset ) === ui . preset && ui . asOf === null ));
2026-11-06 08:28:04 +00:00
}
const span = Math . max ( state . now - state . first , 1 );
2026-11-17 14:08:13 +00:00
const end = ui . asOf === null ? state . now : ui . asOf ;
const place = ( t ) => Math . min ( Math . max ( Math . round (( SPAN_STEPS * ( t - state . first )) / span ), 0 ), SPAN_STEPS );
const [ low , high ] = [ place ( ui . preset === 0 ? state . first : end - ui . preset ), ui . asOf === null ? SPAN_STEPS : place ( end )];
timeDom . from . value = String ( Math . min ( low , high - 1 ));
timeDom . to . value = String ( high );
timeDom . from . classList . toggle ( "on-top" , low > SPAN_STEPS / 2 );
timeDom . fill . style . left = ` ${ ( 100 * low ) / SPAN_STEPS } %` ;
timeDom . fill . style . right = ` ${ 100 - ( 100 * high ) / SPAN_STEPS } %` ;
timeDom . live . disabled = live ();
2026-11-06 08:28:04 +00:00
timeDom . label . textContent = ` ${ absolute . format ( stampOf ( state . since )) } – ${ absolute . format ( stampOf ( state . until )) }${ live () ? " · live" : "" } ` ;
}
function startReplay () {
2026-11-17 14:08:13 +00:00
const began = state . first + ( ui . preset === 0 ? 0 : ui . preset );
2026-11-06 08:28:04 +00:00
const span = Math . max ( state . now - began , 1 );
const resume = ui . asOf !== null && ui . asOf < state . now - 60 ;
2026-11-17 14:08:13 +00:00
let progress = resume ? Math . max (( ui . asOf - began ) / span , 0 ) : 0 ;
2026-11-06 08:28:04 +00:00
ui . playing = true ;
2026-11-07 15:03:14 +00:00
relabel ( timeDom . play , "bi-pause-fill" , "Pause" );
2026-11-06 08:28:04 +00:00
const advance = async () => {
if ( ! ui . playing ) {
return ;
}
progress = Math . min ( progress + 1 / 48 , 1 );
ui . asOf = progress >= 1 ? null : began + span * progress ;
await load ();
2026-11-17 14:08:13 +00:00
fetchRates ();
2026-11-06 08:28:04 +00:00
if ( ui . screen === "stream" ) {
fetchEvents ();
}
if ( progress >= 1 ) {
stopReplay ();
} else if ( ui . playing ) {
replay = window . setTimeout ( advance , 500 );
}
};
advance ();
}
function stopReplay () {
ui . playing = false ;
window . clearTimeout ( replay );
if ( timeDom ) {
2026-11-08 17:26:51 +00:00
relabel ( timeDom . play , "bi-play-fill" , "Replay" );
2026-11-06 08:28:04 +00:00
}
}