rspace-online/shared/components/rstack-app-switcher.ts

254 lines
7.4 KiB
TypeScript

/**
* <rstack-app-switcher> — Dropdown to switch between rSpace modules.
*
* Attributes:
* current — the active module ID (highlighted)
*
* Methods:
* setModules(list) — provide the list of available modules
*/
export interface AppSwitcherModule {
id: string;
name: string;
icon: string;
description: string;
standaloneDomain?: string;
}
// Category definitions for the rApp dropdown (display-only grouping)
const MODULE_CATEGORIES: Record<string, string> = {
canvas: "Creating",
notes: "Creating",
pubs: "Creating",
swag: "Creating",
splat: "Creating",
cal: "Planning",
trips: "Planning",
work: "Planning",
forum: "Discussing & Deciding",
inbox: "Discussing & Deciding",
choices: "Discussing & Deciding",
vote: "Discussing & Deciding",
funds: "Funding & Commerce",
wallet: "Funding & Commerce",
cart: "Funding & Commerce",
providers: "Funding & Commerce",
books: "Sharing & Media",
files: "Sharing & Media",
tube: "Sharing & Media",
data: "Sharing & Media",
maps: "Sharing & Media",
network: "Sharing & Media",
};
const CATEGORY_ORDER = [
"Creating",
"Planning",
"Discussing & Deciding",
"Funding & Commerce",
"Sharing & Media",
];
export class RStackAppSwitcher extends HTMLElement {
#shadow: ShadowRoot;
#modules: AppSwitcherModule[] = [];
constructor() {
super();
this.#shadow = this.attachShadow({ mode: "open" });
}
static get observedAttributes() {
return ["current"];
}
get current(): string {
return this.getAttribute("current") || "";
}
connectedCallback() {
this.#render();
}
attributeChangedCallback() {
this.#render();
}
setModules(modules: AppSwitcherModule[]) {
this.#modules = modules;
this.#render();
}
#renderGroupedModules(current: string): string {
// Group modules by category
const groups = new Map<string, AppSwitcherModule[]>();
const uncategorized: AppSwitcherModule[] = [];
for (const m of this.#modules) {
const cat = MODULE_CATEGORIES[m.id];
if (cat) {
if (!groups.has(cat)) groups.set(cat, []);
groups.get(cat)!.push(m);
} else {
uncategorized.push(m);
}
}
let html = "";
for (const cat of CATEGORY_ORDER) {
const items = groups.get(cat);
if (!items || items.length === 0) continue;
html += `<div class="category-header">${cat}</div>`;
html += items.map((m) => this.#renderItem(m, current)).join("");
}
if (uncategorized.length > 0) {
html += `<div class="category-header">Other</div>`;
html += uncategorized.map((m) => this.#renderItem(m, current)).join("");
}
return html;
}
#renderItem(m: AppSwitcherModule, current: string): string {
return `
<div class="item-row ${m.id === current ? "active" : ""}">
<a class="item"
href="/${this.#getSpaceSlug()}/${m.id}"
data-id="${m.id}">
<span class="item-icon">${m.icon}</span>
<div class="item-text">
<span class="item-name">${m.name}</span>
<span class="item-desc">${m.description}</span>
</div>
</a>
${m.standaloneDomain ? `<a class="item-ext" href="https://${m.standaloneDomain}" target="_blank" rel="noopener" title="${m.standaloneDomain}">↗</a>` : ""}
</div>
`;
}
#render() {
const current = this.current;
const currentMod = this.#modules.find((m) => m.id === current);
const label = currentMod ? `${currentMod.icon} ${currentMod.name}` : "🌌 rSpace";
this.#shadow.innerHTML = `
<style>${STYLES}</style>
<div class="switcher">
<button class="trigger" id="trigger">${label} <span class="caret">▾</span></button>
<div class="menu" id="menu">
${this.#renderGroupedModules(current)}
</div>
</div>
`;
const trigger = this.#shadow.getElementById("trigger")!;
const menu = this.#shadow.getElementById("menu")!;
trigger.addEventListener("click", (e) => {
e.stopPropagation();
menu.classList.toggle("open");
});
// Prevent external links from closing the menu prematurely
this.#shadow.querySelectorAll(".item-ext").forEach((el) => {
el.addEventListener("click", (e) => e.stopPropagation());
});
document.addEventListener("click", () => menu.classList.remove("open"));
}
#getSpaceSlug(): string {
// Read from the space switcher or URL
const spaceSwitcher = document.querySelector("rstack-space-switcher");
if (spaceSwitcher) return spaceSwitcher.getAttribute("current") || "personal";
// Fallback: parse from URL (/:space/:module)
const parts = window.location.pathname.split("/").filter(Boolean);
return parts[0] || "personal";
}
static define(tag = "rstack-app-switcher") {
if (!customElements.get(tag)) customElements.define(tag, RStackAppSwitcher);
}
}
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;
background: rgba(255,255,255,0.08); color: inherit;
}
:host-context([data-theme="light"]) .trigger {
background: rgba(0,0,0,0.05); color: #0f172a;
}
:host-context([data-theme="dark"]) .trigger {
background: rgba(255,255,255,0.08); color: #e2e8f0;
}
.trigger:hover { background: rgba(255,255,255,0.12); }
:host-context([data-theme="light"]) .trigger:hover { background: rgba(0,0,0,0.08); }
.caret { font-size: 0.7em; opacity: 0.6; }
.menu {
position: absolute; top: 100%; left: 0; margin-top: 6px;
min-width: 260px; border-radius: 12px; overflow: hidden;
overflow-y: auto; max-height: 70vh;
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);
}
.item-row {
display: flex; align-items: center;
transition: background 0.12s;
}
:host-context([data-theme="light"]) .item-row { color: #374151; }
:host-context([data-theme="light"]) .item-row:hover { background: #f1f5f9; }
:host-context([data-theme="light"]) .item-row.active { background: #e0f2fe; }
:host-context([data-theme="dark"]) .item-row { color: #e2e8f0; }
:host-context([data-theme="dark"]) .item-row:hover { background: rgba(255,255,255,0.05); }
:host-context([data-theme="dark"]) .item-row.active { background: rgba(6,182,212,0.1); }
.item {
display: flex; align-items: center; gap: 12px;
padding: 10px 14px; text-decoration: none;
cursor: pointer; flex: 1; min-width: 0; color: inherit;
}
.item-ext {
display: flex; align-items: center; justify-content: center;
width: 32px; height: 100%; flex-shrink: 0;
font-size: 0.8rem; text-decoration: none; opacity: 0;
transition: opacity 0.15s;
}
.item-row:hover .item-ext { opacity: 0.5; }
.item-ext:hover { opacity: 1 !important; }
:host-context([data-theme="light"]) .item-ext { color: #06b6d4; }
:host-context([data-theme="dark"]) .item-ext { color: #22d3ee; }
.item-icon { font-size: 1.3rem; width: 28px; text-align: center; flex-shrink: 0; }
.item-text { display: flex; flex-direction: column; min-width: 0; }
.item-name { font-size: 0.875rem; font-weight: 600; }
.item-desc { font-size: 0.75rem; opacity: 0.6; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.category-header {
padding: 8px 14px 4px; font-size: 0.7rem; font-weight: 700;
text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.5;
user-select: none;
}
.category-header:not(:first-child) {
border-top: 1px solid rgba(128,128,128,0.15);
margin-top: 4px; padding-top: 10px;
}
`;