Files
OwnCord/Client/tauri-client/src/components/QuickSwitcher.ts
T
jevb 39658e919b refactor: extensibility overhaul — handler registry, permission checker, sidebar decomposition, DX improvements
Server:
- Unified permission checker (permissions/checker.go) replaces 3 duplicated implementations
- WS handler registry pattern (ws/registry.go) replaces monolithic switch (747→184 lines)
- Split handlers into domain files: handlers_chat.go, handlers_presence.go, handlers_reaction.go
- Shared message type constants (ws/message_types.go) — no more string literals
- Admin API split into helpers.go, types.go, middleware.go (api.go now 61 lines)
- Dev seed script (scripts/seed.go) with -confirm-dev safety flag
- Air hot reload config (.air.toml)
- Fix: DM attachment permission now uses participant check, not role check
- Fix: Typing broadcast now checks ReadMessages permission for non-DM channels

Client:
- Extract preferences to @lib/preferences.ts (fixes lib→component dependency)
- Extract roles to dedicated roles.store.ts (was mixed into channels store)
- Decompose SidebarArea (921→598 lines) into 4 sub-components
- Shared modal factory (lib/modalFactory.ts) with tests
- Global showToast() helper (lib/toast.ts) — 18 call sites migrated
- Protocol type constants (lib/protocolTypes.ts) synced with server
- Remove 38 unnecessary type casts across 17 files
- Component test harness (tests/helpers/test-harness.ts) with 8 tests
- Fix: DM section "View All" respects collapsed state
- Fix: Modal onClose fires on external signal abort
- Fix: savePref wrapped in try/catch for quota exceeded
- Fix: loadPref null guard added

Triple-reviewed: Claude code-review agent + OpenAI Codex CLI + GitHub Copilot
2026-03-29 12:19:08 +02:00

199 lines
5.7 KiB
TypeScript

// Step 8.60 — Quick switcher modal (Ctrl+K) for fast channel navigation.
// Uses @lib/dom helpers exclusively. Never sets innerHTML with user content.
import { createElement, setText, appendChildren, clearChildren } from "@lib/dom";
import { createIcon } from "@lib/icons";
import { channelsStore } from "@stores/channels.store";
import type { Channel } from "@stores/channels.store";
import type { MountableComponent } from "@lib/safe-render";
export interface QuickSwitcherOptions {
readonly onSelectChannel: (channelId: number) => void;
readonly onClose: () => void;
}
export function createQuickSwitcher(options: QuickSwitcherOptions): MountableComponent {
const ac = new AbortController();
const signal = ac.signal;
let root: HTMLDivElement | null = null;
let resultsDiv: HTMLDivElement;
let input: HTMLInputElement;
let activeIndex = 0;
let filteredChannels: readonly Channel[] = [];
let unsubscribe: (() => void) | null = null;
function getChannelIcon(ch: Channel): SVGSVGElement {
return ch.type === "voice" ? createIcon("volume-2", 14) : createIcon("hash", 14);
}
function getFilteredChannels(query: string): readonly Channel[] {
const state = channelsStore.getState();
const all = Array.from(state.channels.values());
const sorted = [...all].sort((a, b) => a.position - b.position);
if (query.length === 0) return sorted;
const lower = query.toLowerCase();
return sorted.filter((ch) => ch.name.toLowerCase().includes(lower));
}
function renderResults(): void {
clearChildren(resultsDiv);
activeIndex = Math.min(activeIndex, Math.max(0, filteredChannels.length - 1));
for (let i = 0; i < filteredChannels.length; i++) {
const ch = filteredChannels[i]!;
const isActive = i === activeIndex;
const item = createElement("div", {
class: isActive
? "quick-switcher__item quick-switcher__item--active"
: "quick-switcher__item",
"data-channelid": String(ch.id),
});
const icon = createElement("span", { class: "quick-switcher__icon" });
icon.appendChild(getChannelIcon(ch));
const name = createElement("span", { class: "quick-switcher__name" });
setText(name, ch.name);
const parts: (Element | string)[] = [icon, name];
if (ch.category !== null) {
const category = createElement("span", { class: "quick-switcher__category" });
setText(category, ch.category);
parts.push(category);
}
appendChildren(item, ...parts);
item.addEventListener("click", () => {
options.onSelectChannel(ch.id);
options.onClose();
}, { signal });
resultsDiv.appendChild(item);
}
}
function handleInput(): void {
const query = input.value.trim();
filteredChannels = getFilteredChannels(query);
activeIndex = 0;
renderResults();
}
function handleKeydown(e: KeyboardEvent): void {
if (e.key === "Escape") {
e.preventDefault();
options.onClose();
return;
}
if (e.key === "ArrowDown") {
e.preventDefault();
if (filteredChannels.length > 0) {
activeIndex = (activeIndex + 1) % filteredChannels.length;
renderResults();
}
return;
}
if (e.key === "ArrowUp") {
e.preventDefault();
if (filteredChannels.length > 0) {
activeIndex = (activeIndex - 1 + filteredChannels.length) % filteredChannels.length;
renderResults();
}
return;
}
if (e.key === "Enter") {
e.preventDefault();
const selected = filteredChannels[activeIndex];
if (selected !== undefined) {
options.onSelectChannel(selected.id);
options.onClose();
}
}
}
function handleBackdropClick(e: MouseEvent): void {
if (e.target === root) {
options.onClose();
}
}
function handleGlobalKeydown(e: KeyboardEvent): void {
if ((e.ctrlKey || e.metaKey) && e.key === "k") {
e.preventDefault();
if (root !== null && root.parentNode !== null) {
options.onClose();
}
}
}
function refreshFromStore(): void {
const query = input?.value.trim() ?? "";
filteredChannels = getFilteredChannels(query);
renderResults();
}
function mount(container: Element): void {
// Overlay backdrop
root = createElement("div", {
class: "quick-switcher-overlay",
style: "position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 1000; display: flex; justify-content: center; padding-top: 20vh;",
});
// Modal container
const modal = createElement("div", { class: "quick-switcher" });
// Search input
input = createElement("input", {
class: "quick-switcher__input",
type: "text",
placeholder: "Where do you want to go?",
});
// Results list
resultsDiv = createElement("div", { class: "quick-switcher__results" });
appendChildren(modal, input, resultsDiv);
root.appendChild(modal);
container.appendChild(root);
// Initial render
filteredChannels = getFilteredChannels("");
renderResults();
// Event listeners
input.addEventListener("input", handleInput, { signal });
input.addEventListener("keydown", handleKeydown, { signal });
root.addEventListener("click", handleBackdropClick, { signal });
document.addEventListener("keydown", handleGlobalKeydown, { signal });
// Subscribe to store changes
unsubscribe = channelsStore.subscribeSelector(
(s) => s.channels,
refreshFromStore,
);
// Auto-focus
requestAnimationFrame(() => input.focus());
}
function destroy(): void {
ac.abort();
if (unsubscribe !== null) {
unsubscribe();
unsubscribe = null;
}
root?.remove();
root = null;
}
return { mount, destroy };
}