From fb9f0eb940d045f543e014c988dbff062ce33776 Mon Sep 17 00:00:00 2001 From: Mateusz Migas <54471371+mateuszmigas@users.noreply.github.com> Date: Wed, 6 Mar 2024 22:06:34 +0100 Subject: [PATCH] Implemented command pattern --- apps/web/src/commands/addWorkspaceLayer.ts | 4 +-- apps/web/src/commands/index.ts | 32 +++++++++++++++++-- .../src/commands/saveAsCurrentWorkspace.ts | 16 ++++++++++ .../src/components/menu-bar/definitions.ts | 14 ++++++-- 4 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 apps/web/src/commands/saveAsCurrentWorkspace.ts diff --git a/apps/web/src/commands/addWorkspaceLayer.ts b/apps/web/src/commands/addWorkspaceLayer.ts index ad3e52fa..d88c25a6 100644 --- a/apps/web/src/commands/addWorkspaceLayer.ts +++ b/apps/web/src/commands/addWorkspaceLayer.ts @@ -3,7 +3,7 @@ import { CommandContext } from "./context"; export const command = { name: "addWorkspaceLayer", description: "Add a new layer to the workspace", - run: async ( + execute: async ( context: CommandContext, payload: { workspaceId: string; @@ -12,5 +12,5 @@ export const command = { ) => { console.log("Adding a new layer to the workspace", context, payload); }, -}; +} as const; diff --git a/apps/web/src/commands/index.ts b/apps/web/src/commands/index.ts index cdfcd040..1eaf185a 100644 --- a/apps/web/src/commands/index.ts +++ b/apps/web/src/commands/index.ts @@ -1,4 +1,32 @@ -export const executeCommand = (command: string) => { - console.log(`Executing command: ${command}`); +import { command as saveAsCurrentWorkspace } from "./saveAsCurrentWorkspace"; +import { command as addWorkspaceLayer } from "./addWorkspaceLayer"; +import { CommandContext } from "./context"; +const commands = [saveAsCurrentWorkspace, addWorkspaceLayer] as const; +const commandByName = new Map( + commands.map((command) => [command.name, command]) +); + +export type Command = (typeof commands)[number]; +export type CommandId = Command["name"]; + +type MapToExecuteCommand = U extends Command + ? Parameters[1] extends undefined + ? [name: U["name"]] + : [name: U["name"], params: Parameters[1]] + : never; + +const createContext = (): CommandContext => ({}); + +export const executeCommand = async ( + ...[name, params]: MapToExecuteCommand +) => { + const context = createContext(); + const command = commandByName.get(name); + + if (!command) { + throw new Error(`Command not found: ${name}`); + } + + return command.execute(context, params); }; diff --git a/apps/web/src/commands/saveAsCurrentWorkspace.ts b/apps/web/src/commands/saveAsCurrentWorkspace.ts new file mode 100644 index 00000000..f88ce524 --- /dev/null +++ b/apps/web/src/commands/saveAsCurrentWorkspace.ts @@ -0,0 +1,16 @@ +import { CommandContext } from "./context"; + +export const command = { + name: "saveAsCurrentWorkspace", + description: "Save the current workspace as a new file", + execute: async ( + context: CommandContext, + payload: { + workspaceId: string; + layerName: string; + } + ) => { + console.log("Saving sheet", context, payload); + }, +} as const; + diff --git a/apps/web/src/components/menu-bar/definitions.ts b/apps/web/src/components/menu-bar/definitions.ts index 0fb70e4e..7226abd3 100644 --- a/apps/web/src/components/menu-bar/definitions.ts +++ b/apps/web/src/components/menu-bar/definitions.ts @@ -1,3 +1,4 @@ +import { executeCommand } from "@/commands"; import { MenuItem } from "@/utils/menuItem"; export const menuBarDefinition: MenuItem[] = [ @@ -7,15 +8,22 @@ export const menuBarDefinition: MenuItem[] = [ items: [ { type: "leaf", - label: "New Tab", - action: { onClick: () => console.log("New Tab") }, + label: "Save As", + action: { + onClick: () => { + executeCommand("saveAsCurrentWorkspace", { + workspaceId: "123", + layerName: "test", + }); + }, + }, }, { type: "separator", }, { type: "parent", - label: "Recent", + label: "Temp don't use", items: [ { type: "leaf",