264 lines
8.3 KiB
TypeScript
264 lines
8.3 KiB
TypeScript
/**
|
|
* <rstack-space-switcher> — Dropdown to switch between user's spaces.
|
|
*
|
|
* Attributes:
|
|
* current — the active space slug
|
|
* name — the display name of the active space
|
|
*
|
|
* Fetches the user's spaces from /api/spaces on click.
|
|
* Passes auth token so the API returns private spaces the user can access.
|
|
*/
|
|
|
|
import { isAuthenticated, getAccessToken } from "./rstack-identity";
|
|
|
|
interface SpaceInfo {
|
|
slug: string;
|
|
name: string;
|
|
icon?: string;
|
|
role?: string;
|
|
visibility?: string;
|
|
}
|
|
|
|
export class RStackSpaceSwitcher extends HTMLElement {
|
|
#shadow: ShadowRoot;
|
|
#spaces: SpaceInfo[] = [];
|
|
#loaded = false;
|
|
|
|
constructor() {
|
|
super();
|
|
this.#shadow = this.attachShadow({ mode: "open" });
|
|
}
|
|
|
|
static get observedAttributes() {
|
|
return ["current", "name"];
|
|
}
|
|
|
|
get current(): string {
|
|
return this.getAttribute("current") || "";
|
|
}
|
|
|
|
get spaceName(): string {
|
|
return this.getAttribute("name") || this.current;
|
|
}
|
|
|
|
connectedCallback() {
|
|
this.#render();
|
|
}
|
|
|
|
attributeChangedCallback() {
|
|
this.#render();
|
|
}
|
|
|
|
async #loadSpaces() {
|
|
if (this.#loaded) return;
|
|
try {
|
|
const headers: Record<string, string> = {};
|
|
const token = getAccessToken();
|
|
if (token) headers["Authorization"] = `Bearer ${token}`;
|
|
|
|
const res = await fetch("/api/spaces", { headers });
|
|
if (res.ok) {
|
|
const data = await res.json();
|
|
this.#spaces = data.spaces || [];
|
|
}
|
|
} catch {
|
|
// Offline or API not available — just show current
|
|
}
|
|
this.#loaded = true;
|
|
}
|
|
|
|
/** Force reload spaces on next open (e.g. after auth change) */
|
|
reload() {
|
|
this.#loaded = false;
|
|
this.#spaces = [];
|
|
}
|
|
|
|
#render() {
|
|
const current = this.current;
|
|
const name = this.spaceName;
|
|
|
|
this.#shadow.innerHTML = `
|
|
<style>${STYLES}</style>
|
|
<div class="switcher">
|
|
<button class="trigger" id="trigger">
|
|
<span class="space-name">${name || "Select space"}</span>
|
|
<span class="caret">▾</span>
|
|
</button>
|
|
<div class="menu" id="menu">
|
|
<div class="menu-loading">Loading spaces...</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
const trigger = this.#shadow.getElementById("trigger")!;
|
|
const menu = this.#shadow.getElementById("menu")!;
|
|
|
|
trigger.addEventListener("click", async (e) => {
|
|
e.stopPropagation();
|
|
const isOpen = menu.classList.toggle("open");
|
|
if (isOpen && !this.#loaded) {
|
|
await this.#loadSpaces();
|
|
this.#renderMenu(menu, current);
|
|
}
|
|
});
|
|
|
|
document.addEventListener("click", () => menu.classList.remove("open"));
|
|
}
|
|
|
|
#visibilityInfo(s: SpaceInfo): { cls: string; label: string } {
|
|
const v = s.visibility || "public_read";
|
|
if (v === "members_only") return { cls: "vis-private", label: "\uD83D\uDD12" };
|
|
if (v === "authenticated") return { cls: "vis-permissioned", label: "\uD83D\uDD11" };
|
|
return { cls: "vis-public", label: "\uD83D\uDD13" };
|
|
}
|
|
|
|
#renderMenu(menu: HTMLElement, current: string) {
|
|
if (this.#spaces.length === 0) {
|
|
menu.innerHTML = `
|
|
<div class="menu-empty">
|
|
${isAuthenticated() ? "No spaces yet" : "Sign in to see your spaces"}
|
|
</div>
|
|
<a class="item item--create" href="/new">+ Create new space</a>
|
|
`;
|
|
return;
|
|
}
|
|
|
|
const moduleId = this.#getCurrentModule();
|
|
|
|
// Separate user's own spaces (has a role) from public-only
|
|
const mySpaces = this.#spaces.filter((s) => s.role);
|
|
const publicSpaces = this.#spaces.filter((s) => !s.role);
|
|
|
|
let html = "";
|
|
|
|
if (mySpaces.length > 0) {
|
|
html += `<div class="section-label">Your spaces</div>`;
|
|
html += mySpaces
|
|
.map((s) => {
|
|
const vis = this.#visibilityInfo(s);
|
|
return `
|
|
<a class="item ${vis.cls} ${s.slug === current ? "active" : ""}"
|
|
href="/${s.slug}/${moduleId}">
|
|
<span class="item-icon">${s.icon || "🌐"}</span>
|
|
<span class="item-name">${s.name}</span>
|
|
<span class="item-vis ${vis.cls}">${vis.label}</span>
|
|
</a>
|
|
`;
|
|
})
|
|
.join("");
|
|
}
|
|
|
|
if (publicSpaces.length > 0) {
|
|
if (mySpaces.length > 0) html += `<div class="divider"></div>`;
|
|
html += `<div class="section-label">Public spaces</div>`;
|
|
html += publicSpaces
|
|
.map((s) => {
|
|
const vis = this.#visibilityInfo(s);
|
|
return `
|
|
<a class="item ${vis.cls} ${s.slug === current ? "active" : ""}"
|
|
href="/${s.slug}/${moduleId}">
|
|
<span class="item-icon">${s.icon || "🌐"}</span>
|
|
<span class="item-name">${s.name}</span>
|
|
<span class="item-vis ${vis.cls}">${vis.label}</span>
|
|
</a>
|
|
`;
|
|
})
|
|
.join("");
|
|
}
|
|
|
|
html += `<div class="divider"></div>`;
|
|
html += `<a class="item item--create" href="/new">+ Create new space</a>`;
|
|
|
|
menu.innerHTML = html;
|
|
}
|
|
|
|
#getCurrentModule(): string {
|
|
const parts = window.location.pathname.split("/").filter(Boolean);
|
|
return parts[1] || "canvas";
|
|
}
|
|
|
|
static define(tag = "rstack-space-switcher") {
|
|
if (!customElements.get(tag)) customElements.define(tag, RStackSpaceSwitcher);
|
|
}
|
|
}
|
|
|
|
const STYLES = `
|
|
:host { display: contents; }
|
|
|
|
.switcher { position: relative; }
|
|
|
|
.trigger {
|
|
display: flex; align-items: center; gap: 6px;
|
|
padding: 6px 14px; border-radius: 8px; border: none;
|
|
font-size: 0.9rem; font-weight: 600; cursor: pointer;
|
|
transition: background 0.15s; color: inherit;
|
|
}
|
|
:host-context([data-theme="light"]) .trigger { background: rgba(0,0,0,0.05); color: #0f172a; }
|
|
:host-context([data-theme="light"]) .trigger:hover { background: rgba(0,0,0,0.08); }
|
|
:host-context([data-theme="dark"]) .trigger { background: rgba(255,255,255,0.08); color: #e2e8f0; }
|
|
:host-context([data-theme="dark"]) .trigger:hover { background: rgba(255,255,255,0.12); }
|
|
.space-name { max-width: 160px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
.caret { font-size: 0.7em; opacity: 0.6; }
|
|
|
|
.menu {
|
|
position: absolute; top: 100%; left: 0; margin-top: 6px;
|
|
min-width: 260px; max-height: 400px; overflow-y: auto;
|
|
border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,0.25);
|
|
display: none; z-index: 200;
|
|
}
|
|
.menu.open { display: block; }
|
|
:host-context([data-theme="light"]) .menu { background: white; border: 1px solid rgba(0,0,0,0.1); }
|
|
:host-context([data-theme="dark"]) .menu { background: #1e293b; border: 1px solid rgba(255,255,255,0.1); }
|
|
|
|
.section-label {
|
|
padding: 8px 14px 4px; font-size: 0.7rem; font-weight: 600;
|
|
text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.5;
|
|
}
|
|
|
|
.item {
|
|
display: flex; align-items: center; gap: 10px;
|
|
padding: 10px 14px; text-decoration: none; cursor: pointer;
|
|
transition: background 0.12s; border-left: 3px solid transparent;
|
|
}
|
|
:host-context([data-theme="light"]) .item { color: #374151; }
|
|
:host-context([data-theme="light"]) .item:hover { background: #f1f5f9; }
|
|
:host-context([data-theme="light"]) .item.active { background: #e0f2fe; }
|
|
:host-context([data-theme="dark"]) .item { color: #e2e8f0; }
|
|
:host-context([data-theme="dark"]) .item:hover { background: rgba(255,255,255,0.05); }
|
|
:host-context([data-theme="dark"]) .item.active { background: rgba(6,182,212,0.1); }
|
|
|
|
/* Visibility color accents — left border */
|
|
.item.vis-public { border-left-color: #34d399; }
|
|
.item.vis-private { border-left-color: #f87171; }
|
|
.item.vis-permissioned { border-left-color: #fbbf24; }
|
|
|
|
.item-icon { font-size: 1.1rem; flex-shrink: 0; }
|
|
.item-name { font-size: 0.875rem; font-weight: 500; flex: 1; }
|
|
|
|
/* Visibility badge */
|
|
.item-vis {
|
|
font-size: 0.9rem; padding: 2px 4px; border-radius: 4px; flex-shrink: 0;
|
|
line-height: 1; display: flex; align-items: center; justify-content: center;
|
|
}
|
|
.item-vis.vis-public { background: rgba(52,211,153,0.15); color: #34d399; }
|
|
.item-vis.vis-private { background: rgba(248,113,113,0.15); color: #f87171; }
|
|
.item-vis.vis-permissioned { background: rgba(251,191,36,0.15); color: #fbbf24; }
|
|
:host-context([data-theme="light"]) .item-vis.vis-public { background: #d1fae5; color: #059669; }
|
|
:host-context([data-theme="light"]) .item-vis.vis-private { background: #fee2e2; color: #dc2626; }
|
|
:host-context([data-theme="light"]) .item-vis.vis-permissioned { background: #fef3c7; color: #d97706; }
|
|
|
|
.item--create {
|
|
font-size: 0.85rem; font-weight: 600; color: #06b6d4 !important;
|
|
border-left-color: transparent !important;
|
|
}
|
|
.item--create:hover { background: rgba(6,182,212,0.08) !important; }
|
|
|
|
.divider { height: 1px; margin: 4px 0; }
|
|
:host-context([data-theme="light"]) .divider { background: rgba(0,0,0,0.08); }
|
|
:host-context([data-theme="dark"]) .divider { background: rgba(255,255,255,0.08); }
|
|
|
|
.menu-loading, .menu-empty {
|
|
padding: 16px; text-align: center; font-size: 0.85rem; color: #94a3b8;
|
|
}
|
|
`;
|