Skip to content

Commit

Permalink
Implemented command pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszmigas committed Mar 6, 2024
1 parent 1f9ab48 commit fb9f0eb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
4 changes: 2 additions & 2 deletions apps/web/src/commands/addWorkspaceLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,5 +12,5 @@ export const command = {
) => {
console.log("Adding a new layer to the workspace", context, payload);
},
};
} as const;

32 changes: 30 additions & 2 deletions apps/web/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -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> = U extends Command
? Parameters<U["execute"]>[1] extends undefined
? [name: U["name"]]
: [name: U["name"], params: Parameters<U["execute"]>[1]]
: never;

const createContext = (): CommandContext => ({});

export const executeCommand = async (
...[name, params]: MapToExecuteCommand<Command>
) => {
const context = createContext();
const command = commandByName.get(name);

if (!command) {
throw new Error(`Command not found: ${name}`);
}

return command.execute(context, params);
};

16 changes: 16 additions & 0 deletions apps/web/src/commands/saveAsCurrentWorkspace.ts
Original file line number Diff line number Diff line change
@@ -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;

14 changes: 11 additions & 3 deletions apps/web/src/components/menu-bar/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { executeCommand } from "@/commands";
import { MenuItem } from "@/utils/menuItem";

export const menuBarDefinition: MenuItem[] = [
Expand All @@ -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",
Expand Down

0 comments on commit fb9f0eb

Please sign in to comment.