From 52eb324514cbc49e5ea3fbc4f6f3acb4f199a7e9 Mon Sep 17 00:00:00 2001 From: fede Date: Thu, 19 Nov 2026 09:07:21 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20a=C3=B1adido=20panel=20para=20acceder?= =?UTF-8?q?=20a=20las=20paginas=20mas=20rapido?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fede --- front/src/lib/CommandPalette.svelte.ts | 186 ++++++++++++++++++ .../src/lib/components/CommandPalette.svelte | 148 ++++++++++++++ 2 files changed, 334 insertions(+) create mode 100644 front/src/lib/CommandPalette.svelte.ts create mode 100644 front/src/lib/components/CommandPalette.svelte diff --git a/front/src/lib/CommandPalette.svelte.ts b/front/src/lib/CommandPalette.svelte.ts new file mode 100644 index 0000000..9c8cbd0 --- /dev/null +++ b/front/src/lib/CommandPalette.svelte.ts @@ -0,0 +1,186 @@ +import { goto } from '$app/navigation'; +import { tick } from 'svelte'; +import type { Grupo, GruposYPermisosResponse } from '../types'; + +export interface OpcionCommandPalette { + nombre: string; + alias: string; + imagen?: string; + grupo: string; +} + +interface DependenciasCommandPalette { + grupos: () => Promise | undefined; + habilitado: () => boolean; +} + +export class CommandPaletteController { + abierto = $state(false); + consulta = $state(''); + activo = $state(0); + opciones = $state([]); + cargando = $state(false); + + listaEl = $state(null); + panelEl = $state(null); + + filtrados = $derived.by(() => { + const q = this.normalizar(this.consulta.trim()); + if (!q) return this.opciones; + + return this.opciones.filter((opcion) => + this.normalizar(`${opcion.nombre} ${opcion.alias} ${opcion.grupo}`).includes(q) + ); + }); + + private deps: DependenciasCommandPalette; + + constructor(deps: DependenciasCommandPalette) { + this.deps = deps; + + // Los efectos se registran al construir la clase, por lo que debe + // instanciarse durante la inicialización de un componente. + $effect(() => this.registrarAtajo()); + $effect(() => this.cargarOpciones()); + $effect(() => this.desplazarActivo()); + } + + async abrir() { + if (this.abierto || !this.deps.habilitado()) return; + + this.consulta = ''; + this.activo = 0; + this.abierto = true; + + await tick(); + this.panelEl?.querySelector('.bx--search-input')?.focus(); + } + + cerrar() { + this.abierto = false; + this.consulta = ''; + this.activo = 0; + } + + reiniciarActivo() { + this.activo = 0; + } + + enfocar(indice: number) { + this.activo = indice; + } + + seleccionar(opcion?: OpcionCommandPalette) { + if (!opcion) return; + + this.cerrar(); + void goto(this.destino(opcion.alias)); + } + + manejarTeclas(event: KeyboardEvent) { + if (event.key === 'ArrowDown') { + event.preventDefault(); + this.mover(1); + } else if (event.key === 'ArrowUp') { + event.preventDefault(); + this.mover(-1); + } else if (event.key === 'Enter') { + event.preventDefault(); + this.seleccionar(this.filtrados[this.activo]); + } + } + + private mover(delta: number) { + const total = this.filtrados.length; + if (total === 0) return; + + this.activo = (this.activo + delta + total) % total; + } + + private registrarAtajo() { + const onKey = (event: KeyboardEvent) => { + if ((event.ctrlKey || event.metaKey) && !event.altKey && event.key.toLowerCase() === 'k') { + event.preventDefault(); + + if (this.abierto) { + this.cerrar(); + } else { + void this.abrir(); + } + } + }; + + window.addEventListener('keydown', onKey); + return () => window.removeEventListener('keydown', onKey); + } + + private cargarOpciones() { + const promesa = this.deps.grupos(); + if (!promesa) return; + + let cancelado = false; + this.cargando = true; + + promesa + .then((respuesta) => { + if (cancelado) return; + this.opciones = this.aplanar(respuesta?.groups ?? []); + }) + .catch(() => { + if (!cancelado) this.opciones = []; + }) + .finally(() => { + if (!cancelado) this.cargando = false; + }); + + return () => { + cancelado = true; + }; + } + + private desplazarActivo() { + const indice = this.activo; + if (!this.abierto) return; + + void tick().then(() => { + const el = this.listaEl?.querySelector(`[data-indice="${indice}"]`); + el?.scrollIntoView({ block: 'nearest' }); + }); + } + + private destino(alias: string): string { + return alias.startsWith('/') ? alias : `/${alias}`; + } + + private normalizar(texto: string): string { + return texto + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, '') + .toLowerCase(); + } + + private aplanar(grupos: Grupo[]): OpcionCommandPalette[] { + const mapa = new Map(); + + const visitar = (lista: Grupo[]) => { + for (const grupo of lista) { + for (const permiso of grupo.permisos ?? []) { + if (!permiso.alias || mapa.has(permiso.alias)) continue; + + mapa.set(permiso.alias, { + nombre: permiso.nombre, + alias: permiso.alias, + imagen: permiso.imagen, + grupo: grupo.nombre + }); + } + + if (grupo.grupos_hijos?.length) visitar(grupo.grupos_hijos); + } + }; + + visitar(grupos); + + return [...mapa.values()].sort((a, b) => a.nombre.localeCompare(b.nombre, 'es')); + } +} diff --git a/front/src/lib/components/CommandPalette.svelte b/front/src/lib/components/CommandPalette.svelte new file mode 100644 index 0000000..01f01c8 --- /dev/null +++ b/front/src/lib/components/CommandPalette.svelte @@ -0,0 +1,148 @@ + + +{#if habilitado} + + +
+ paleta.abrir()} + /> +
+{/if} + + paleta.cerrar()} +> + + + + 0 + ? `command-palette-op-${paleta.activo}` + : undefined} + on:input={() => paleta.reiniciarActivo()} + on:keydown={(event) => paleta.manejarTeclas(event)} + /> + +
+ {#if paleta.cargando} +

+ Cargando acciones... +

+ {:else if paleta.opciones.length === 0} +

+ No tenés permisos para acceder a ninguna acción. +

+ {:else if paleta.filtrados.length === 0} +

+ No se encontraron acciones para "{paleta.consulta}". +

+ {:else} +
+ {#each paleta.filtrados as opcion, i (opcion.alias)} + paleta.seleccionar(opcion)} + on:mouseenter={() => paleta.enfocar(i)} + > + + + {opcion.nombre} + {opcion.grupo} + /{opcion.alias.replace(/^\//, '')} + + + {/each} +
+ {/if} +
+ +
+ ↑↓ navegar + ↵ abrir + Esc cerrar +
+
+