adobe-to-docusign-migrator/web/static/js/state.js

51 lines
1.5 KiB
JavaScript

// Global application state with simple pub/sub
const _listeners = {};
export const state = {
project: null, // { id, name, createdAt }
auth: {
adobe: false,
docusign: false,
adobeLabel: 'Adobe Sign',
adobeAccountId: null,
adobeAccountName: null,
docusignLabel: 'Docusign',
docusignAccountId: null,
docusignAccountName: null,
docusignAccountsCount: 0,
docusignAccountSelectionRequired: false,
},
templates: [], // [{ adobe_id, name, status, blockers, warnings, ... }]
templatesError: null, // Visible error state for template loading failures
selectedIds: new Set(),
lastMigrationResults: null, // final batch job results
issueCount: 0, // blocked template count (drives nav badge)
};
// Subscribe to state key changes: fn is called with (newValue, oldValue)
export function subscribe(key, fn) {
if (!_listeners[key]) _listeners[key] = [];
_listeners[key].push(fn);
}
// Publish a state change
export function publish(key, newValue) {
const old = state[key];
state[key] = newValue;
(_listeners[key] || []).forEach(fn => {
try { fn(newValue, old); } catch (e) { console.error('state listener error', e); }
});
}
// Convenience setter that publishes
export function setState(key, value) {
publish(key, value);
}
// Recompute derived values after template list updates
export function updateDerivedState() {
const blocked = state.templates.filter(t => t.blockers && t.blockers.length > 0).length;
setState('issueCount', blocked);
}