Skip to content

Commit

Permalink
refactor: strongly type manually dispatched commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebrynes7 committed Mar 26, 2024
1 parent 053c102 commit 6e72d03
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
9 changes: 3 additions & 6 deletions plugin/src/commands/addTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ import type { MakeCommand } from ".";
import type TodoistPlugin from "..";
import type { TaskCreationOptions } from "../ui/createTaskModal";

const addTask: MakeCommand = (plugin: TodoistPlugin) => {
export const addTask: MakeCommand = (plugin: TodoistPlugin) => {
return {
id: "add-task",
name: "Add task",
callback: makeCallback(plugin),
};
};

const addTaskWithPageInContent: MakeCommand = (plugin: TodoistPlugin) => {
export const addTaskWithPageInContent: MakeCommand = (plugin: TodoistPlugin) => {
return {
id: "add-task-page-content",
name: "Add task with current page in task content",
callback: makeCallback(plugin, { appendLinkToContent: true }),
};
};

const addTaskWithPageInDescription: MakeCommand = (plugin: TodoistPlugin) => {
export const addTaskWithPageInDescription: MakeCommand = (plugin: TodoistPlugin) => {
return {
id: "add-task-page-description",
name: "Add task with current page in task description",
Expand Down Expand Up @@ -59,5 +58,3 @@ const grabSelection = (plugin: TodoistPlugin): string => {
const getFileContext = (plugin: TodoistPlugin): TFile | undefined => {
return plugin.app.workspace.getActiveFile() ?? undefined;
};

export default [addTask, addTaskWithPageInContent, addTaskWithPageInDescription];
32 changes: 15 additions & 17 deletions plugin/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { type Command as ObsidianCommand } from "obsidian";
import type TodoistPlugin from "..";
import debug from "../log";
import addTaskCommands from "./addTask";
import { addTask, addTaskWithPageInContent, addTaskWithPageInDescription } from "./addTask";

export type MakeCommand = (plugin: TodoistPlugin) => ObsidianCommand;
export type MakeCommand = (plugin: TodoistPlugin) => Omit<ObsidianCommand, "id">;

const syncCommand: MakeCommand = (plugin: TodoistPlugin) => {
return {
id: "todoist-sync",
name: "Sync with Todoist",
callback: async () => {
debug("Syncing with Todoist API");
Expand All @@ -16,23 +15,22 @@ const syncCommand: MakeCommand = (plugin: TodoistPlugin) => {
};
};

const commands: MakeCommand[] = [syncCommand, ...addTaskCommands];
const commands = {
"todoist-sync": syncCommand,
"add-task": addTask,
"add-task-page-content": addTaskWithPageInContent,
"add-task-page-description": addTaskWithPageInDescription,
};

type CommandId = keyof typeof commands;

export const registerCommands = (plugin: TodoistPlugin) => {
for (const make of commands) {
plugin.addCommand(make(plugin));
for (const [id, make] of Object.entries(commands)) {
plugin.addCommand({ id, ...make(plugin) });
}
};

// TODO: Strongly type the IDs
export const fireCommand = (plugin: TodoistPlugin, id: string) => {
for (const make of commands) {
const cmd = make(plugin);
if (cmd.id === id) {
cmd.callback?.();
return;
}
}

throw Error(`Failed to find command by ID: ${id}`);
export const fireCommand = <K extends CommandId>(id: K, plugin: TodoistPlugin) => {
const make = commands[id];
make(plugin).callback?.();
};
2 changes: 1 addition & 1 deletion plugin/src/ui/TodoistQuery.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
});
function callTaskModal() {
fireCommand(plugin, "add-task-page-content");
fireCommand("add-task-page-content", plugin);
}
async function forceRefresh() {
Expand Down

0 comments on commit 6e72d03

Please sign in to comment.