From de428016e1338872d69e7f6046bbb8344c0912b7 Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Fri, 19 May 2023 15:58:43 +0100 Subject: [PATCH] feat(wip): ai command panel --- src/lib/commandCenter/commandCenter.svelte | 44 +++++ .../commands.ts} | 112 ++++++------ src/lib/commandCenter/index.ts | 4 + src/lib/commandCenter/panels/ai.svelte | 5 + src/lib/commandCenter/panels/index.ts | 4 + src/lib/commandCenter/panels/root.svelte | 54 ++++++ src/lib/commandCenter/panels/template.svelte | 130 ++++++++++++++ src/lib/commandCenter/subPanels.ts | 32 ++++ src/lib/components/dialog.svelte | 54 +----- src/routes/console/+layout.svelte | 18 +- src/routes/console/commandCenter.svelte | 166 ------------------ .../organization-[organization]/+page.svelte | 3 +- .../console/project-[project]/+layout.svelte | 3 +- .../project-[project]/auth/+page.svelte | 2 +- .../project-[project]/databases/+page.svelte | 2 +- .../project-[project]/functions/+page.svelte | 2 +- .../overview/platforms/+page.svelte | 2 +- .../project-[project]/storage/+page.svelte | 2 +- 18 files changed, 356 insertions(+), 283 deletions(-) create mode 100644 src/lib/commandCenter/commandCenter.svelte rename src/lib/{helpers/commandCenter.ts => commandCenter/commands.ts} (69%) create mode 100644 src/lib/commandCenter/index.ts create mode 100644 src/lib/commandCenter/panels/ai.svelte create mode 100644 src/lib/commandCenter/panels/index.ts create mode 100644 src/lib/commandCenter/panels/root.svelte create mode 100644 src/lib/commandCenter/panels/template.svelte create mode 100644 src/lib/commandCenter/subPanels.ts delete mode 100644 src/routes/console/commandCenter.svelte diff --git a/src/lib/commandCenter/commandCenter.svelte b/src/lib/commandCenter/commandCenter.svelte new file mode 100644 index 000000000..8ef4e01db --- /dev/null +++ b/src/lib/commandCenter/commandCenter.svelte @@ -0,0 +1,44 @@ + + + + +{#if openSubPanel} + +{/if} diff --git a/src/lib/helpers/commandCenter.ts b/src/lib/commandCenter/commands.ts similarity index 69% rename from src/lib/helpers/commandCenter.ts rename to src/lib/commandCenter/commands.ts index 1cd1f7e3c..d57b28fb7 100644 --- a/src/lib/helpers/commandCenter.ts +++ b/src/lib/commandCenter/commands.ts @@ -1,7 +1,8 @@ +import { debounce } from '$lib/helpers/debounce'; +import { isMac } from '$lib/helpers/platform'; import { derived, writable } from 'svelte/store'; -import { debounce } from './debounce'; -import { isMac } from './platform'; +// Store export type Command = { keys: string[]; /* Ctrl on Windows/Linux, Meta on Mac */ @@ -15,73 +16,27 @@ export type Command = { forceEnable?: boolean; }; -type CommandCenterState = { - commandMap: Map; - disabledMap: Map; -}; +export const commandMap = writable>(new Map()); +export const disabledMap = writable>(new Map()); -export const commandCenter = writable({ - commandMap: new Map(), - disabledMap: new Map() +// Derived stores +export const commands = derived(commandMap, ($commandMap) => { + return Array.from($commandMap.values()).flat(); }); -export const commands = derived(commandCenter, ($commandCenter) => { - return Array.from($commandCenter.commandMap.values()).flat(); -}); - -const commandsEnabled = derived(commandCenter, ($commandCenter) => { +const commandsEnabled = derived(disabledMap, ($disabledMap) => { // If there's an item on the disabledMap that's true, then disable the command center - return Array.from($commandCenter.disabledMap.values()).every((disabled) => !disabled); + return Array.from($disabledMap.values()).every((disabled) => !disabled); }); -export const registerCommands = { - subscribe(runner: (cb: (newCommands: Command[]) => void) => void) { - const uuid = crypto.randomUUID(); - - runner((newCommands: Command[]) => { - commandCenter.update((curr) => { - curr.commandMap.set(uuid, newCommands); - return curr; - }); - }); - - return () => { - commandCenter.update((curr) => { - curr.commandMap.delete(uuid); - return curr; - }); - }; - } -}; - -export const disableCommands = { - subscribe(runner: (cb: (disabled: boolean) => void) => void) { - const uuid = crypto.randomUUID(); - - runner((disabled: boolean) => { - commandCenter.update((curr) => { - curr.disabledMap.set(uuid, disabled); - return curr; - }); - }); - - return () => { - commandCenter.update((curr) => { - curr.disabledMap.delete(uuid); - return curr; - }); - }; - } -}; - function isInputEvent(event: KeyboardEvent) { return ['INPUT', 'TEXTAREA', 'SELECT'].includes((event.target as HTMLElement).tagName); } export const commandCenterKeyDownHandler = derived( - [commandCenter, commandsEnabled], - ([{ commandMap }, enabled]) => { - const commandsArr = Array.from(commandMap.values()).flat(); + [commandMap, commandsEnabled], + ([$commandMap, enabled]) => { + const commandsArr = Array.from($commandMap.values()).flat(); let recentKeyCodes: number[] = []; return (event: KeyboardEvent) => { @@ -113,3 +68,44 @@ export const commandCenterKeyDownHandler = derived( }; } ); + +// Methods +export const registerCommands = { + subscribe(runner: (cb: (newCommands: Command[]) => void) => void) { + const uuid = crypto.randomUUID(); + + runner((newCommands: Command[]) => { + commandMap.update((curr) => { + curr.set(uuid, newCommands); + return curr; + }); + }); + + return () => { + commandMap.update((curr) => { + curr.delete(uuid); + return curr; + }); + }; + } +}; + +export const disableCommands = { + subscribe(runner: (cb: (disabled: boolean) => void) => void) { + const uuid = crypto.randomUUID(); + + runner((disabled: boolean) => { + disabledMap.update((curr) => { + curr.set(uuid, disabled); + return curr; + }); + }); + + return () => { + disabledMap.update((curr) => { + curr.delete(uuid); + return curr; + }); + }; + } +}; diff --git a/src/lib/commandCenter/index.ts b/src/lib/commandCenter/index.ts new file mode 100644 index 000000000..b1d8ce511 --- /dev/null +++ b/src/lib/commandCenter/index.ts @@ -0,0 +1,4 @@ +export * from './commands'; +export * from './subPanels'; +import CommandCenter from './commandCenter.svelte'; +export { CommandCenter }; diff --git a/src/lib/commandCenter/panels/ai.svelte b/src/lib/commandCenter/panels/ai.svelte new file mode 100644 index 000000000..e8b40a4fd --- /dev/null +++ b/src/lib/commandCenter/panels/ai.svelte @@ -0,0 +1,5 @@ + + + diff --git a/src/lib/commandCenter/panels/index.ts b/src/lib/commandCenter/panels/index.ts new file mode 100644 index 000000000..d0b6262f4 --- /dev/null +++ b/src/lib/commandCenter/panels/index.ts @@ -0,0 +1,4 @@ +import AI from './ai.svelte'; +import Root from './root.svelte'; +import Template from './template.svelte'; +export { AI, Root, Template }; diff --git a/src/lib/commandCenter/panels/root.svelte b/src/lib/commandCenter/panels/root.svelte new file mode 100644 index 000000000..47d51eba8 --- /dev/null +++ b/src/lib/commandCenter/panels/root.svelte @@ -0,0 +1,54 @@ + + + + + + diff --git a/src/lib/commandCenter/panels/template.svelte b/src/lib/commandCenter/panels/template.svelte new file mode 100644 index 000000000..2f45281b3 --- /dev/null +++ b/src/lib/commandCenter/panels/template.svelte @@ -0,0 +1,130 @@ + + + + + + + +
+ {#if $subPanels.length} + aaa + {/if} + + + {#if options} +
    + {#each options as option, i} +
  • + {#if selected === i} +
    + {/if} +
    + +
    +
  • + {:else} +
  • + + No options found + +
  • + {/each} +
+ {:else} + + {/if} +
+
+ + diff --git a/src/lib/commandCenter/subPanels.ts b/src/lib/commandCenter/subPanels.ts new file mode 100644 index 000000000..72a489c12 --- /dev/null +++ b/src/lib/commandCenter/subPanels.ts @@ -0,0 +1,32 @@ +import type { SvelteComponentDev } from 'svelte/internal'; +import { writable } from 'svelte/store'; + +export type SubPanel = { + name: string; + component: typeof SvelteComponentDev; +}; + +type CastSubPanel = Omit & { + component: unknown; +}; + +export const subPanels = writable>([]); + +// +export function addSubPanel(subPanel: CastSubPanel) { + subPanels.update((curr) => { + curr.push(subPanel as SubPanel); + return curr; + }); +} + +export function popSubPanel() { + subPanels.update((curr) => { + curr = curr.slice(0, -1); + return curr; + }); +} + +export function clearSubPanels() { + subPanels.set([]); +} diff --git a/src/lib/components/dialog.svelte b/src/lib/components/dialog.svelte index b3949b215..05e339029 100644 --- a/src/lib/components/dialog.svelte +++ b/src/lib/components/dialog.svelte @@ -1,28 +1,12 @@ -{#if show} -
-
- -
+
+
+
-{/if} +
diff --git a/src/routes/console/organization-[organization]/+page.svelte b/src/routes/console/organization-[organization]/+page.svelte index 3496146ca..1df72acc7 100644 --- a/src/routes/console/organization-[organization]/+page.svelte +++ b/src/routes/console/organization-[organization]/+page.svelte @@ -2,10 +2,11 @@ import { goto } from '$app/navigation'; import { base } from '$app/paths'; import { page } from '$app/stores'; + import { registerCommands } from '$lib/commandCenter'; import { CardContainer, Empty, GridItem1, Heading, PaginationWithLimit } from '$lib/components'; import { Pill } from '$lib/elements'; import { Button } from '$lib/elements/forms'; - import { registerCommands } from '$lib/helpers/commandCenter'; + import { Container } from '$lib/layout'; import CreateOrganization from '../createOrganization.svelte'; import type { PageData } from './$types'; diff --git a/src/routes/console/project-[project]/+layout.svelte b/src/routes/console/project-[project]/+layout.svelte index 49e506ac1..69cade632 100644 --- a/src/routes/console/project-[project]/+layout.svelte +++ b/src/routes/console/project-[project]/+layout.svelte @@ -3,8 +3,9 @@ import { sdk } from '$lib/stores/sdk'; import { onMount } from 'svelte'; import { project, stats } from './store'; - import { registerCommands } from '$lib/helpers/commandCenter'; + import { goto } from '$app/navigation'; + import { registerCommands } from '$lib/commandCenter'; onMount(async () => { return sdk.forConsole.client.subscribe(['project', 'console'], (response) => { diff --git a/src/routes/console/project-[project]/auth/+page.svelte b/src/routes/console/project-[project]/auth/+page.svelte index c05426f02..3aa6c74be 100644 --- a/src/routes/console/project-[project]/auth/+page.svelte +++ b/src/routes/console/project-[project]/auth/+page.svelte @@ -27,7 +27,7 @@ import { project } from '../store'; import type { PageData } from './$types'; import Create from './createUser.svelte'; - import { registerCommands } from '$lib/helpers/commandCenter'; + import { registerCommands } from '$lib/commandCenter'; export let data: PageData; diff --git a/src/routes/console/project-[project]/databases/+page.svelte b/src/routes/console/project-[project]/databases/+page.svelte index 334e27f6f..6175f1a05 100644 --- a/src/routes/console/project-[project]/databases/+page.svelte +++ b/src/routes/console/project-[project]/databases/+page.svelte @@ -12,7 +12,7 @@ import Grid from './grid.svelte'; import { columns } from './store'; import Table from './table.svelte'; - import { registerCommands } from '$lib/helpers/commandCenter'; + import { registerCommands } from '$lib/commandCenter'; export let data: PageData; diff --git a/src/routes/console/project-[project]/functions/+page.svelte b/src/routes/console/project-[project]/functions/+page.svelte index 575c88392..341eeec1b 100644 --- a/src/routes/console/project-[project]/functions/+page.svelte +++ b/src/routes/console/project-[project]/functions/+page.svelte @@ -19,7 +19,7 @@ import type { PageData } from './$types'; import Create from './createFunction.svelte'; - import { registerCommands } from '$lib/helpers/commandCenter'; + import { registerCommands } from '$lib/commandCenter'; export let data: PageData; diff --git a/src/routes/console/project-[project]/overview/platforms/+page.svelte b/src/routes/console/project-[project]/overview/platforms/+page.svelte index fddfa5c49..959aafe08 100644 --- a/src/routes/console/project-[project]/overview/platforms/+page.svelte +++ b/src/routes/console/project-[project]/overview/platforms/+page.svelte @@ -36,7 +36,7 @@ import { toLocaleDateTime } from '$lib/helpers/date'; import { app } from '$lib/stores/app'; import type { PageData } from './$types'; - import { registerCommands } from '$lib/helpers/commandCenter'; + import { registerCommands } from '$lib/commandCenter'; export let data: PageData; diff --git a/src/routes/console/project-[project]/storage/+page.svelte b/src/routes/console/project-[project]/storage/+page.svelte index fae25715c..29eea87f5 100644 --- a/src/routes/console/project-[project]/storage/+page.svelte +++ b/src/routes/console/project-[project]/storage/+page.svelte @@ -15,7 +15,7 @@ import { Button } from '$lib/elements/forms'; import { Container } from '$lib/layout'; import type { Models } from '@appwrite.io/console'; - import { registerCommands } from '$lib/helpers/commandCenter'; + import { registerCommands } from '$lib/commandCenter'; import type { PageData } from './$types'; import Create from './create.svelte';