From 09df2013e38cb7611ceb58e267c58fc736fd43a1 Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Tue, 20 Jun 2023 18:47:44 +0100 Subject: [PATCH] feat: grouped options --- src/lib/commandCenter/commands.ts | 27 ++++ src/lib/commandCenter/panels/template.svelte | 136 +++++++++++++----- src/routes/console/+layout.svelte | 6 +- .../organization-[organization]/+page.svelte | 10 +- .../console/project-[project]/+layout.svelte | 18 ++- .../project-[project]/auth/+page.svelte | 6 +- 6 files changed, 152 insertions(+), 51 deletions(-) diff --git a/src/lib/commandCenter/commands.ts b/src/lib/commandCenter/commands.ts index d57b28fb7..1e7907f4f 100644 --- a/src/lib/commandCenter/commands.ts +++ b/src/lib/commandCenter/commands.ts @@ -3,6 +3,18 @@ import { isMac } from '$lib/helpers/platform'; import { derived, writable } from 'svelte/store'; // Store +export type CommandGroup = + | 'ungrouped' + | 'navigation' + | 'project' + | 'auth' + | 'help' + | 'account' + | 'platforms' + | 'databases' + | 'functions' + | 'storage'; + export type Command = { keys: string[]; /* Ctrl on Windows/Linux, Meta on Mac */ @@ -14,6 +26,7 @@ export type Command = { label?: string; disabled?: boolean; forceEnable?: boolean; + group?: CommandGroup; }; export const commandMap = writable>(new Map()); @@ -24,6 +37,20 @@ export const commands = derived(commandMap, ($commandMap) => { return Array.from($commandMap.values()).flat(); }); +export const groupedCommands = derived(commands, ($commands) => { + const res = new Map(); + + for (const command of $commands) { + if (!command.group) { + res.set('ungrouped', [...(res.get('ungrouped') || []), command]); + } else { + res.set(command.group, [...(res.get(command.group) || []), command]); + } + } + + return res; +}); + const commandsEnabled = derived(disabledMap, ($disabledMap) => { // If there's an item on the disabledMap that's true, then disable the command center return Array.from($disabledMap.values()).every((disabled) => !disabled); diff --git a/src/lib/commandCenter/panels/template.svelte b/src/lib/commandCenter/panels/template.svelte index 27f2a05e1..e42cddb82 100644 --- a/src/lib/commandCenter/panels/template.svelte +++ b/src/lib/commandCenter/panels/template.svelte @@ -1,4 +1,6 @@ @@ -97,36 +149,41 @@ class:press={!$commandCenterCtx.isInitialPanel} class:scale-up={$commandCenterCtx.isInitialPanel && $commandCenterCtx.open}>
-
- + +
- -
+
+ - {#if options} + {#if groupsAndOptions}
    - {#each options as option, i} -
  • - {#if selected === i} -
    - {/if} - -
  • + {#each groupsAndOptions as item} + {@const isSelected = !isGroup(item) && item.index === selected} + {#if isGroup(item)} +
  • + {item.name} +
  • + {:else} +
  • + {#if isSelected} +
    + {/if} + +
  • + {/if} {:else}
  • @@ -209,6 +266,13 @@ background-color: transparent; } + .group { + color: hsl(var(--color-neutral-70)); + margin-inline-start: 0.25rem; + position: relative; + z-index: 10; + } + .result { transition: 150ms; position: relative; diff --git a/src/routes/console/+layout.svelte b/src/routes/console/+layout.svelte index 2245e8bf0..ebc8a9eba 100644 --- a/src/routes/console/+layout.svelte +++ b/src/routes/console/+layout.svelte @@ -39,7 +39,8 @@ }, keys: ['a'], ctrl: true, - shift: true + shift: true, + group: 'navigation' }, { label: 'Go to Home', @@ -48,7 +49,8 @@ }, keys: ['g', 'h'], // Check if pathname not like /console/organization-6374c26ba90067a6315d - disabled: $page.url.pathname.includes('/console/organization-') + disabled: $page.url.pathname.includes('/console/organization-'), + group: 'navigation' } ]); diff --git a/src/routes/console/organization-[organization]/+page.svelte b/src/routes/console/organization-[organization]/+page.svelte index 1df72acc7..e7e31843e 100644 --- a/src/routes/console/organization-[organization]/+page.svelte +++ b/src/routes/console/organization-[organization]/+page.svelte @@ -2,7 +2,7 @@ import { goto } from '$app/navigation'; import { base } from '$app/paths'; import { page } from '$app/stores'; - import { registerCommands } from '$lib/commandCenter'; + import { Command, registerCommands } from '$lib/commandCenter'; import { CardContainer, Empty, GridItem1, Heading, PaginationWithLimit } from '$lib/components'; import { Pill } from '$lib/elements'; import { Button } from '$lib/elements/forms'; @@ -48,12 +48,13 @@ } $: $registerCommands([ - ...data.projects.projects.map((project, i) => ({ + ...data.projects.projects.map((project, i) => ({ label: `Project ${project.name}`, callback: () => { goto(`/console/project-${project.$id}`); }, - keys: ['p', `${i + 1}`] + keys: ['p', `${i + 1}`], + group: 'project' })), { label: 'Create project', @@ -61,7 +62,8 @@ showCreate = true; }, keys: ['c'], - disabled: showCreate + disabled: showCreate, + group: 'project' } ]); diff --git a/src/routes/console/project-[project]/+layout.svelte b/src/routes/console/project-[project]/+layout.svelte index 69cade632..1b78929e4 100644 --- a/src/routes/console/project-[project]/+layout.svelte +++ b/src/routes/console/project-[project]/+layout.svelte @@ -20,11 +20,11 @@ $: $registerCommands([ { label: 'Go to Overview', - keys: ['g', 'o'], - callback: () => { goto(`/console/project-${$project.$id}`); - } + }, + keys: ['g', 'o'], + group: 'navigation' }, { @@ -32,28 +32,32 @@ callback: () => { goto(`/console/project-${$project.$id}/auth`); }, - keys: ['g', 'a'] + keys: ['g', 'a'], + group: 'navigation' }, { label: 'Go to Databases', callback: () => { goto(`/console/project-${$project.$id}/databases`); }, - keys: ['g', 'd'] + keys: ['g', 'd'], + group: 'navigation' }, { label: 'Go to Functions', callback: () => { goto(`/console/project-${$project.$id}/functions`); }, - keys: ['g', 'f'] + keys: ['g', 'f'], + group: 'navigation' }, { label: 'Go to Storage', callback: () => { goto(`/console/project-${$project.$id}/storage`); }, - keys: ['g', 's'] + keys: ['g', 's'], + group: 'navigation' } ]); diff --git a/src/routes/console/project-[project]/auth/+page.svelte b/src/routes/console/project-[project]/auth/+page.svelte index 3aa6c74be..60ff10d36 100644 --- a/src/routes/console/project-[project]/auth/+page.svelte +++ b/src/routes/console/project-[project]/auth/+page.svelte @@ -44,14 +44,16 @@ showCreate = true; }, keys: ['c'], - disabled: showCreate + disabled: showCreate, + group: 'auth' }, { label: 'Go to Overview', callback: () => { goto(`${base}/console/project-${$project.$id}/overview`); }, - keys: ['o'] + keys: ['o'], + group: 'navigation' } ]);