stackchain-dashboard/frontend/task-overlay-history.js
timmy 8989e93c2c
All checks were successful
CI / lint (pull_request) Successful in 48s
CI / build-release (pull_request) Successful in 5s
CI / release-candidate (pull_request) Has been skipped
feat: skip blocked Today work (#437)
2026-08-10 00:39:13 +00:00

61 lines
1.8 KiB
JavaScript

(function (root, factory) {
const api = factory();
if (typeof module === 'object' && module.exports) module.exports = api;
else root.createTaskOverlayHistory = api;
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
'use strict';
const allowed = new Set(['new', 'find', 'search', 'search-preview', 'plan-today', 'plan-today-preview', 'today-readiness']);
return function createTaskOverlayHistory({ history, eventTarget, onChange }) {
let active = allowed.has(history.state?.taskOverlay) ? history.state.taskOverlay : null;
let started = false;
function stateKind(state = history.state) {
return allowed.has(state?.taskOverlay) ? state.taskOverlay : null;
}
function apply(state) {
const next = stateKind(state);
if (next === active) return;
const previous = active;
active = next;
onChange(next, previous);
}
return {
start() {
if (started) return;
started = true;
eventTarget.addEventListener('popstate', event => apply(event.state));
},
open(kind) {
if (!allowed.has(kind)) return false;
if (active === kind) return true;
const previous = active;
const state = { ...(history.state || {}), taskOverlay: kind };
history.pushState(state, '');
active = kind;
onChange(kind, previous);
return true;
},
close() {
if (!active) return false;
history.back();
return true;
},
leave() {
if (!active) return false;
const previous = active;
const state = { ...(history.state || {}) };
delete state.taskOverlay;
history.replaceState(state, '');
active = null;
onChange(null, previous);
return true;
},
current() { return active; },
};
};
});