refactor: one-liner registers

This commit is contained in:
tglide committed 2023-05-15 23:13:50 +01:00
1 parent a7c53ae522
commit afcbb5dca6
12 files changed
+122 -126

No files matched your search

+28 -28
View File
@@ -1,4 +1,3 @@
import { onDestroy } from 'svelte';
import { derived, writable } from 'svelte/store';
// type Key = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k'
@@ -7,7 +6,7 @@ import { derived, writable } from 'svelte/store';
// | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X'
// | 'Y' | 'Z' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
type Command = {
export type Command = {
keys: string[];
/* Ctrl on Windows/Linux, Meta on Mac */
meta?: boolean;
@@ -20,7 +19,7 @@ type Command = {
};
type CommandCenterState = {
commandMap: Map<string, Command[]>;
commandMap: Map<RegisterProxy, Command[]>;
enabled: boolean;
};
@@ -39,37 +38,38 @@ export const commands = derived(commandCenter, ($commandCenter) => {
return Array.from($commandCenter.commandMap.values()).flat();
});
export function CommandRegistrant() {
const uuid = crypto.randomUUID();
function createRegisterProxy() {
return new Proxy(
{},
{
get(_obj, field, receiver) {
if (field !== 'register') return;
return (newCommands: Command[]) => {
commandCenter.update((curr) => {
curr.commandMap.set(receiver, newCommands);
return curr;
});
};
}
}
) as { register: (newCommands: Command[]) => void };
}
onDestroy(() => {
commandCenter.update((curr) => {
curr.commandMap.delete(uuid);
return curr;
});
});
export type RegisterProxy = ReturnType<typeof createRegisterProxy>;
return {
register(newCommands: Command[]) {
export const cmdRegistrant = {
subscribe(runner: (proxy: RegisterProxy) => void) {
const proxy = createRegisterProxy();
runner(proxy);
return () => {
commandCenter.update((curr) => {
curr.commandMap.set(uuid, newCommands);
curr.commandMap.delete(proxy);
return curr;
});
}
};
}
export function ExtendCommandRegistrant(baseCommands: Command[]) {
return () => {
const registrant = CommandRegistrant();
return {
register: (newCommands?: Command[]) => {
registrant.register([...baseCommands, ...(newCommands ?? [])]);
}
};
};
}
}
};
const debounce = (fn: () => void, ms: number) => {
let timeout: NodeJS.Timeout;
+1
View File
@@ -1,3 +1,4 @@
export function isMac(): boolean {
console.log('isMac()', window.navigator.platform.toUpperCase().indexOf('MAC') >= 0);
return window.navigator.platform.toUpperCase().indexOf('MAC') >= 0;
}
+2 -3
View File
@@ -16,11 +16,10 @@
import Create from './createOrganization.svelte';
import { goto } from '$app/navigation';
import { commandCenterKeyDownHandler, CommandRegistrant } from '$lib/helpers/commandCenter';
import { cmdRegistrant, commandCenterKeyDownHandler } from '$lib/helpers/commandCenter';
import CommandCenter from './commandCenter.svelte';
const { register } = CommandRegistrant();
$: register([
$: $cmdRegistrant.register([
{
label: 'Go to Account',
callback: () => {
@@ -13,8 +13,6 @@
import CreateOrganization from '../../createOrganization.svelte';
import { sdk } from '$lib/stores/sdk';
import type { PageData } from './$types';
import { CommandRegistrant } from '$lib/helpers/commandCenter';
import { goto } from '$app/navigation';
export let data: PageData;
+9 -6
View File
@@ -1,14 +1,13 @@
<script lang="ts">
import Dialog from '$lib/components/dialog.svelte';
import { CommandRegistrant, commands, commandCenter } from '$lib/helpers/commandCenter';
import { commands, commandCenter, cmdRegistrant } from '$lib/helpers/commandCenter';
import { isMac } from '$lib/helpers/platform';
let open = false;
let search = '';
let selected = 0;
const { register } = CommandRegistrant();
$: register([
$: $cmdRegistrant.register([
{
callback: () => {
open = !open;
@@ -68,13 +67,13 @@
</span>
<div class="u-flex u-gap-4">
{#if command.meta}
<kbd class="kbd"> {isMac ? '⌘' : 'ctrl'} </kbd>
<kbd class="kbd"> {isMac() ? '⌘' : 'ctrl'} </kbd>
{/if}
{#if command.shift}
<kbd class="kbd"> ⇧ </kbd>
<kbd class="kbd"> {isMac() ? '⇧' : 'shift'} </kbd>
{/if}
{#if command.alt}
<kbd class="kbd"> {isMac ? '⌥' : 'alt'} </kbd>
<kbd class="kbd"> {isMac() ? '⌥' : 'alt'} </kbd>
{/if}
{#each command.keys as key}
<kbd class="kbd">
@@ -106,4 +105,8 @@
.result[data-selected] {
background-color: hsl(var(--color-neutral-200));
}
.kbd {
padding-inline: 0.25rem;
}
</style>
@@ -1,15 +1,15 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { page } from '$app/stores';
import { CardContainer, Empty, GridItem1, Heading, PaginationWithLimit } from '$lib/components';
import { Pill } from '$lib/elements';
import { GridItem1, Heading, Empty, CardContainer, PaginationWithLimit } from '$lib/components';
import { Button } from '$lib/elements/forms';
import { cmdRegistrant } from '$lib/helpers/commandCenter';
import { Container } from '$lib/layout';
import CreateProject from './createProject.svelte';
import CreateOrganization from '../createOrganization.svelte';
import type { PageData } from './$types';
import { page } from '$app/stores';
import { CommandRegistrant } from '$lib/helpers/commandCenter';
import { goto } from '$app/navigation';
import CreateProject from './createProject.svelte';
export let data: PageData;
@@ -46,8 +46,7 @@
);
}
const { register } = CommandRegistrant();
$: register([
$: $cmdRegistrant.register([
...data.projects.projects.map((project, i) => ({
label: `Project ${project.name}`,
callback: () => {
@@ -36,8 +36,7 @@
await goto(`${base}/console/project-${projectId}/auth/user-${event.detail.$id}`);
}
const { register } = $projectRegistrant();
$: register([
$: $projectRegistrant.register([
{
label: 'Create user',
callback: () => {
@@ -23,8 +23,7 @@
await goto(`${base}/console/project-${project}/databases/database-${event.detail.$id}`);
}
const { register } = $projectRegistrant();
$: register([
$: $projectRegistrant.register([
{
label: 'Create database',
callback: () => {
@@ -1,24 +1,24 @@
<script lang="ts">
import { beforeNavigate } from '$app/navigation';
import { base } from '$app/paths';
import { page } from '$app/stores';
import { Button } from '$lib/elements/forms';
import { tooltip } from '$lib/actions/tooltip';
import {
Empty,
CardContainer,
Empty,
GridItem1,
Heading,
PaginationWithLimit,
Id
Id,
PaginationWithLimit
} from '$lib/components';
import { Button } from '$lib/elements/forms';
import { toLocaleDateTime } from '$lib/helpers/date';
import { Container } from '$lib/layout';
import { base } from '$app/paths';
import { tooltip } from '$lib/actions/tooltip';
import { app } from '$lib/stores/app';
import { wizard } from '$lib/stores/wizard';
import { beforeNavigate, goto } from '$app/navigation';
import { toLocaleDateTime } from '$lib/helpers/date';
import Create from './createFunction.svelte';
import type { PageData } from './$types';
import { CommandRegistrant } from '$lib/helpers/commandCenter';
import Create from './createFunction.svelte';
import { projectRegistrant } from '../store';
export let data: PageData;
@@ -35,8 +35,7 @@
wizard.hide();
});
const { register } = $projectRegistrant();
$: register([
$: $projectRegistrant.register([
{
label: 'Create function',
callback: openWizard,
@@ -1,12 +1,12 @@
<script lang="ts" context="module">
import { wizard } from '$lib/stores/wizard';
import { versions } from './wizard/store';
import SearchLight from '$lib/images/search-light.svg';
import SearchDark from '$lib/images/search-dark.svg';
import SearchLight from '$lib/images/search-light.svg';
import { wizard } from '$lib/stores/wizard';
import CreateAndroid from './createAndroid.svelte';
import CreateApple from './createApple.svelte';
import CreateFlutter from './createFlutter.svelte';
import CreateWeb from './createWeb.svelte';
import { versions } from './wizard/store';
export enum Platform {
Web,
@@ -29,16 +29,14 @@
</script>
<script lang="ts">
import { base } from '$app/paths';
import { page } from '$app/stores';
import { DropList, DropListItem, Heading } from '$lib/components';
import { Button } from '$lib/elements/forms';
import { toLocaleDateTime } from '$lib/helpers/date';
import { base } from '$app/paths';
import { app } from '$lib/stores/app';
import type { PageData } from './$types';
import { CommandRegistrant } from '$lib/helpers/commandCenter';
import { goto } from '$app/navigation';
import { project, projectRegistrant } from '../../store';
import { projectRegistrant } from '../../store';
export let data: PageData;
@@ -74,8 +72,7 @@
}
};
const { register } = $projectRegistrant();
$: register();
$: $projectRegistrant.register();
</script>
<div class="common-section u-flex u-gap-12">
@@ -1,23 +1,23 @@
<script lang="ts">
import { page } from '$app/stores';
import { goto } from '$app/navigation';
import { Button } from '$lib/elements/forms';
import { base } from '$app/paths';
import { page } from '$app/stores';
import { tooltip } from '$lib/actions/tooltip';
import {
CardContainer,
Empty,
GridItem1,
CardContainer,
Heading,
PaginationWithLimit,
Id
Id,
PaginationWithLimit
} from '$lib/components';
import { Pill } from '$lib/elements';
import Create from './create.svelte';
import { Button } from '$lib/elements/forms';
import { Container } from '$lib/layout';
import { base } from '$app/paths';
import { tooltip } from '$lib/actions/tooltip';
import type { Models } from '@appwrite.io/console';
import type { PageData } from './$types';
import { projectRegistrant } from '../store';
import type { PageData } from './$types';
import Create from './create.svelte';
export let data: PageData;
@@ -30,8 +30,7 @@
await goto(`${base}/console/project-${project}/storage/bucket-${event.detail.$id}`);
}
const { register } = $projectRegistrant();
$: register([
$: $projectRegistrant.register([
{
label: 'Create bucket',
callback: () => {
+47 -44
View File
@@ -1,54 +1,57 @@
import { derived, writable } from 'svelte/store';
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { cmdRegistrant, type Command } from '$lib/helpers/commandCenter';
import type { Models } from '@appwrite.io/console';
import type { BarSeriesOption } from 'echarts/charts';
import { ExtendCommandRegistrant } from '$lib/helpers/commandCenter';
import { goto } from '$app/navigation';
import { derived, writable } from 'svelte/store';
export const project = derived(page, ($page) => $page.data.project as Models.Project);
export const projectRegistrant = derived<
typeof project,
ReturnType<typeof ExtendCommandRegistrant>
>(project, ($project) => {
return ExtendCommandRegistrant([
{
label: 'Go to Overview',
keys: ['o'],
callback: () => {
goto(`/console/project-${$project.$id}`);
}
},
export const projectRegistrant = derived([project, cmdRegistrant], ([$project, $registrant]) => {
return {
register: (c: Command[] = []) =>
$registrant.register([
...[
{
label: 'Go to Overview',
keys: ['o'],
callback: () => {
goto(`/console/project-${$project.$id}`);
}
},
{
label: 'Go to Auth',
callback: () => {
goto(`/console/project-${$project.$id}/auth`);
},
keys: ['a']
},
{
label: 'Go to Databases',
callback: () => {
goto(`/console/project-${$project.$id}/databases`);
},
keys: ['d']
},
{
label: 'Go to Functions',
callback: () => {
goto(`/console/project-${$project.$id}/functions`);
},
keys: ['f']
},
{
label: 'Go to Storage',
callback: () => {
goto(`/console/project-${$project.$id}/storage`);
},
keys: ['s']
}
]);
{
label: 'Go to Auth',
callback: () => {
goto(`/console/project-${$project.$id}/auth`);
},
keys: ['a']
},
{
label: 'Go to Databases',
callback: () => {
goto(`/console/project-${$project.$id}/databases`);
},
keys: ['d']
},
{
label: 'Go to Functions',
callback: () => {
goto(`/console/project-${$project.$id}/functions`);
},
keys: ['f']
},
{
label: 'Go to Storage',
callback: () => {
goto(`/console/project-${$project.$id}/storage`);
},
keys: ['s']
}
],
...(c ?? [])
])
};
});
export const onboarding = derived(