99 lines
3.1 KiB
JavaScript
99 lines
3.1 KiB
JavaScript
// Generated, do not edit. sha256:0b8a580ca944f426e2e3aa4800988bad2046937a8c0409e4dceebf09b6b3e9f7
|
|
import { CONFIG } from '../shared/config.js';
|
|
import { t } from './i18n/index.js';
|
|
import { serverFoutTekst } from './data/_rpc.js';
|
|
import { locked } from './license.js';
|
|
|
|
const SLEUTEL = CONFIG.PORTFOLIO.ACTIVE_KEY;
|
|
const WISSEL_SLEUTEL = CONFIG.PORTFOLIO.SWITCH_KEY;
|
|
|
|
export function cacheSleutels(naam = cachedPortfolioName()) {
|
|
const achtervoegsel = naam ? `:${naam}` : '';
|
|
return {
|
|
theme: `ui_theme${achtervoegsel}`, accent: `ui_accent${achtervoegsel}`, depth: `ui_depth${achtervoegsel}`,
|
|
look: `ui_look${achtervoegsel}`,
|
|
};
|
|
}
|
|
|
|
export function cachedPortfolioName() {
|
|
try { return localStorage.getItem(SLEUTEL) || ''; }
|
|
catch { return ''; }
|
|
}
|
|
|
|
function toonNaam(naam) {
|
|
const html = document.documentElement;
|
|
if (naam) {
|
|
html.dataset.portfolio = naam;
|
|
html.style.setProperty('--portfolio-name', JSON.stringify(naam));
|
|
} else {
|
|
delete html.dataset.portfolio;
|
|
html.style.removeProperty('--portfolio-name');
|
|
}
|
|
try { if (naam) localStorage.setItem(SLEUTEL, naam); else localStorage.removeItem(SLEUTEL); }
|
|
catch { }
|
|
}
|
|
|
|
let opgebouwdMet = null;
|
|
|
|
export async function refreshActivePortfolio() {
|
|
try {
|
|
const r = await fetch('/api/portfolios');
|
|
if (!r.ok) return cachedPortfolioName();
|
|
const uit = await r.json();
|
|
toonNaam(locked('portfolios') ? '' : uit.actief || '');
|
|
if (opgebouwdMet === null) opgebouwdMet = uit.actief || '';
|
|
return uit.actief || '';
|
|
} catch {
|
|
return cachedPortfolioName();
|
|
}
|
|
}
|
|
|
|
export function watchPortfolioChanges({ minIntervalMs = 2000 } = {}) {
|
|
let laatst = 0;
|
|
const kijk = async () => {
|
|
if (document.visibilityState === 'hidden') return;
|
|
if (Date.now() - laatst < minIntervalMs) return;
|
|
laatst = Date.now();
|
|
try {
|
|
const r = await fetch('/api/portfolios', { cache: 'no-store' });
|
|
if (!r.ok) return;
|
|
const { actief } = await r.json();
|
|
if (opgebouwdMet && actief && actief !== opgebouwdMet) location.reload();
|
|
} catch { }
|
|
};
|
|
window.addEventListener('focus', kijk);
|
|
document.addEventListener('visibilitychange', kijk);
|
|
}
|
|
|
|
export async function switchPortfolio(naam) {
|
|
try { localStorage.setItem(WISSEL_SLEUTEL, '1'); } catch { }
|
|
let uit;
|
|
try {
|
|
const r = await fetch('/api/portfolios/activate', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ naam }),
|
|
});
|
|
uit = await r.json();
|
|
if (!r.ok) throw new Error(serverFoutTekst(uit) || t('portfolios.switchFailed'));
|
|
} catch (e) {
|
|
try { localStorage.removeItem(WISSEL_SLEUTEL); } catch { }
|
|
throw e;
|
|
}
|
|
if (uit.gewisseld) toonNaam(uit.naam);
|
|
else { try { localStorage.removeItem(WISSEL_SLEUTEL); } catch { } }
|
|
return uit;
|
|
}
|
|
|
|
export async function reloadWhenBack({ intervalMs = 250, timeoutMs = 30_000 } = {}) {
|
|
const eind = Date.now() + timeoutMs;
|
|
while (Date.now() < eind) {
|
|
await new Promise(klaar => setTimeout(klaar, intervalMs));
|
|
try {
|
|
const r = await fetch('/api/health', { cache: 'no-store' });
|
|
if (r.ok) { location.reload(); return true; }
|
|
} catch { }
|
|
}
|
|
return false;
|
|
}
|