136 lines
3.6 KiB
JavaScript
136 lines
3.6 KiB
JavaScript
var WS_LIST = [
|
|
{ id: 'ws_default', slug: 'default', name: 'Default workspace', role: 'Owner', letter: 'D', color: '#0d9488' }
|
|
];
|
|
|
|
var workspaceLoadPromise = null;
|
|
var workspaceLoadFailed = false;
|
|
var lastWorkspaceId = null;
|
|
|
|
function tKey(key) {
|
|
return typeof t === 'function' ? t(key) : key;
|
|
}
|
|
|
|
function workspaceColor(index) {
|
|
return ['#0d9488', '#7c3aed', '#0891b2', '#f59e0b', '#2563eb'][index % 5];
|
|
}
|
|
|
|
function mapWorkspace(record, index) {
|
|
var workspace = record && record.workspace ? record.workspace : record;
|
|
var displayName = workspace.display_name || workspace.slug || workspace.id;
|
|
var settings = workspace.settings || {};
|
|
var role = record && record.role
|
|
? String(record.role).replace(/^\w/, function(char) { return char.toUpperCase(); })
|
|
: tKey('workspace_setup.role.owner');
|
|
return {
|
|
id: workspace.id,
|
|
slug: workspace.slug,
|
|
name: displayName,
|
|
role: role,
|
|
letter: displayName.charAt(0).toUpperCase(),
|
|
color: settings.color || workspaceColor(index),
|
|
status: workspace.status,
|
|
settings: settings,
|
|
};
|
|
}
|
|
|
|
function cacheCurrentWorkspace(workspace) {
|
|
if (!workspace) return;
|
|
try {
|
|
localStorage.setItem('crank_workspace_id', workspace.id);
|
|
localStorage.setItem('crank_workspace_slug', workspace.slug);
|
|
} catch (_error) {}
|
|
}
|
|
|
|
function resolveCurrentWorkspace() {
|
|
return WS_LIST[0] || null;
|
|
}
|
|
|
|
function updateWorkspaceHeader(workspace) {
|
|
var nameEl = document.getElementById('ws-current-name');
|
|
var dotEl = document.getElementById('ws-dot');
|
|
if (nameEl) nameEl.textContent = workspace ? workspace.name : 'No workspace';
|
|
if (dotEl && workspace) {
|
|
dotEl.textContent = workspace.letter;
|
|
dotEl.style.background = workspace.color;
|
|
}
|
|
}
|
|
|
|
function emitWorkspaceChange(workspace) {
|
|
if (!workspace || workspace.id === lastWorkspaceId) {
|
|
return;
|
|
}
|
|
lastWorkspaceId = workspace.id;
|
|
window.dispatchEvent(new CustomEvent('crank:workspacechange', { detail: workspace }));
|
|
}
|
|
|
|
function getCurrentWs() {
|
|
var workspace = resolveCurrentWorkspace();
|
|
if (workspace) {
|
|
cacheCurrentWorkspace(workspace);
|
|
}
|
|
return workspace;
|
|
}
|
|
|
|
function renderWorkspaceHeader() {
|
|
updateWorkspaceHeader(getCurrentWs());
|
|
}
|
|
|
|
async function loadWorkspaces() {
|
|
if (workspaceLoadPromise) return workspaceLoadPromise;
|
|
|
|
workspaceLoadPromise = (async function() {
|
|
if (!window.CrankApi) {
|
|
renderWorkspaceHeader();
|
|
return WS_LIST;
|
|
}
|
|
|
|
try {
|
|
var response = await window.CrankApi.listWorkspaces();
|
|
var items = (response && response.items ? response.items : []).map(mapWorkspace);
|
|
if (items.length > 0) {
|
|
WS_LIST = [items[0]];
|
|
}
|
|
workspaceLoadFailed = false;
|
|
} catch (_error) {
|
|
workspaceLoadFailed = true;
|
|
}
|
|
|
|
renderWorkspaceHeader();
|
|
emitWorkspaceChange(getCurrentWs());
|
|
return WS_LIST;
|
|
}());
|
|
|
|
return workspaceLoadPromise;
|
|
}
|
|
|
|
async function refreshWorkspaces() {
|
|
workspaceLoadPromise = null;
|
|
return loadWorkspaces();
|
|
}
|
|
|
|
function setCurrentWorkspace(workspace) {
|
|
if (workspace) {
|
|
WS_LIST = [workspace];
|
|
cacheCurrentWorkspace(workspace);
|
|
renderWorkspaceHeader();
|
|
emitWorkspaceChange(workspace);
|
|
}
|
|
return Promise.resolve(workspace || null);
|
|
}
|
|
|
|
window.getCurrentWorkspace = getCurrentWs;
|
|
window.whenWorkspacesReady = loadWorkspaces;
|
|
window.refreshWorkspaces = refreshWorkspaces;
|
|
window.setCurrentWorkspace = setCurrentWorkspace;
|
|
window.hasWorkspaceApiFailure = function() { return workspaceLoadFailed; };
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
renderWorkspaceHeader();
|
|
loadWorkspaces();
|
|
});
|
|
|
|
window.addEventListener('crank:sessionchange', function() {
|
|
renderWorkspaceHeader();
|
|
emitWorkspaceChange(getCurrentWs());
|
|
});
|