Gate Studio frontend by workspace and isolate browser catalog state
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
(function (root) {
|
||||
'use strict';
|
||||
function scopedStorage(storage, clientId) {
|
||||
if (!/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(clientId || '')) throw new Error('Verified client identity required');
|
||||
const prefix = 'aura-studio:' + clientId.toLowerCase() + ':';
|
||||
return Object.freeze({
|
||||
getItem(key) { return storage.getItem(prefix + key); },
|
||||
setItem(key, value) { storage.setItem(prefix + key, value); },
|
||||
removeItem(key) { storage.removeItem(prefix + key); }
|
||||
});
|
||||
}
|
||||
function escapeText(value) {
|
||||
return String(value ?? '').replace(/[&<>"']/g, char => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[char]));
|
||||
}
|
||||
if (typeof module === 'object' && module.exports) { module.exports = { scopedStorage, escapeText }; return; }
|
||||
|
||||
async function start() {
|
||||
const gate = document.getElementById('studioWorkspaceGate');
|
||||
const status = document.getElementById('workspaceStatus');
|
||||
const form = document.getElementById('workspaceForm');
|
||||
const tenantInput = document.getElementById('workspaceTenant');
|
||||
const login = document.getElementById('workspaceLogin');
|
||||
const retry = document.getElementById('workspaceRetry');
|
||||
const params = new URLSearchParams(location.search);
|
||||
const tenant = params.get('tenant') || '';
|
||||
tenantInput.value = tenant;
|
||||
document.querySelectorAll('body > :not(#studioWorkspaceGate):not(script)').forEach(el => el.inert = true);
|
||||
retry.onclick = () => location.reload();
|
||||
form.onsubmit = event => {
|
||||
event.preventDefault();
|
||||
const next = new URL(location.href);
|
||||
next.searchParams.set('tenant', tenantInput.value.trim());
|
||||
location.assign(next.href);
|
||||
};
|
||||
try {
|
||||
const response = await fetch('/api/workspace' + (tenant ? '?tenant=' + encodeURIComponent(tenant) : ''), { credentials: 'same-origin', cache: 'no-store' });
|
||||
const data = await response.json();
|
||||
if (!response.ok || data.success !== true) {
|
||||
status.textContent = data.error || 'Workspace যাচাই করা যায়নি।';
|
||||
if (data.code === 'WORKSPACE_REQUIRED') form.hidden = false;
|
||||
if (response.status === 401) {
|
||||
const configResponse = await fetch('/api/studio-config', { cache: 'no-store' });
|
||||
const config = await configResponse.json();
|
||||
if (configResponse.ok && config.loginUrl) {
|
||||
const target = new URL(config.loginUrl);
|
||||
if (!['https:', 'http:'].includes(target.protocol) || target.username || target.password) throw new Error('Invalid login destination');
|
||||
target.searchParams.set('next', location.href);
|
||||
login.href = target.href; login.hidden = false;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
const workspace = data.workspace;
|
||||
const runtime = {
|
||||
workspace: Object.freeze({ ...workspace }),
|
||||
local: scopedStorage(localStorage, workspace.clientId),
|
||||
session: scopedStorage(sessionStorage, workspace.clientId),
|
||||
escapeText,
|
||||
apiUrl(path) {
|
||||
const url = new URL(path, location.origin);
|
||||
if (url.origin !== location.origin || !url.pathname.startsWith('/api/')) throw new Error('Invalid Studio API URL');
|
||||
url.searchParams.set('tenant', workspace.tenant);
|
||||
return url.pathname + url.search;
|
||||
}
|
||||
};
|
||||
Object.defineProperty(window, 'AuraStudio', { value: Object.freeze(runtime), writable: false });
|
||||
const script = document.createElement('script');
|
||||
script.src = '/app.js?v=workspace-1';
|
||||
script.onerror = () => { status.textContent = 'Studio load হয়নি। Refresh করে আবার চেষ্টা করুন।'; };
|
||||
script.onload = () => {
|
||||
try {
|
||||
document.dispatchEvent(new Event('aura-studio-ready'));
|
||||
document.querySelectorAll('body > :not(#studioWorkspaceGate):not(script)').forEach(el => el.inert = false);
|
||||
gate.hidden = true;
|
||||
const button = document.createElement('button');
|
||||
button.type = 'button'; button.className = 'pill-btn';
|
||||
button.textContent = workspace.name || workspace.tenant;
|
||||
button.title = 'Workspace নির্বাচন';
|
||||
button.onclick = () => { gate.hidden = false; form.hidden = false; status.textContent = 'অন্য workspace নির্বাচন করুন।'; };
|
||||
document.querySelector('.header-actions')?.prepend(button);
|
||||
} catch { status.textContent = 'Studio চালু হয়নি। Refresh করে আবার চেষ্টা করুন।'; }
|
||||
};
|
||||
document.body.appendChild(script);
|
||||
} catch {
|
||||
status.textContent = 'Workspace service-এর উত্তর পাওয়া যায়নি। Refresh করে আবার চেষ্টা করুন।';
|
||||
}
|
||||
}
|
||||
document.addEventListener('DOMContentLoaded', start, { once: true });
|
||||
})(typeof globalThis === 'object' ? globalThis : this);
|
||||
Reference in New Issue
Block a user