git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/confirm.ts
import {local} from '@/lib/reactive'
/** How long a first tap waits for the second before the button goes back to itself. */
export const ARMED_MS = 4000
/**
* A second tap to confirm, for changes worth a moment's thought but not a
* dialog: the first tap arms the button (it says what will happen), the
* second, within a few seconds, does it. One armed button at a time.
*/
export const armed = local({key: '', until: 0})
/** A tap on `key`'s button: `fire` when it was armed and still is, else it arms. */
export function tap(state: {key: string; until: number}, key: string, now: number): 'arm' | 'fire' {
if (state.key === key && now < state.until) {
state.key = ''
state.until = 0
return 'fire'
}
state.key = key
state.until = now + ARMED_MS
return 'arm'
}
export function isArmed(state: {key: string; until: number}, key: string, now: number): boolean {
return state.key === key && now < state.until
}