Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Introduce commands for operations that are not queries or mutations

Hakan Shehu4968214fc8de
diff --git a/apps/desktop/src/main/commands/file-dialog-open.ts b/apps/desktop/src/main/commands/file-dialog-open.ts
new file mode 100644
index 00000000..e6561244
--- /dev/null
+++ b/apps/desktop/src/main/commands/file-dialog-open.ts
@@ -0,0 +1,18 @@
+import { CommandHandler } from '@/main/types';
+import { FileDialogOpenCommandInput } from '@/shared/commands/file-dialog-open';
+import { BrowserWindow, dialog } from 'electron';
+
+export class FileDialogOpenCommandHandler
+ implements CommandHandler<FileDialogOpenCommandInput>
+{
+ public async handleCommand(
+ input: FileDialogOpenCommandInput
+ ): Promise<Electron.OpenDialogReturnValue> {
+ const window = BrowserWindow.getFocusedWindow();
+ if (!window) {
+ throw new Error('No focused window');
+ }
+
+ return dialog.showOpenDialog(window, input.options);
+ }
+}
diff --git a/apps/desktop/src/main/commands/file-open.ts b/apps/desktop/src/main/commands/file-open.ts
new file mode 100644
index 00000000..e7f1de5f
--- /dev/null
+++ b/apps/desktop/src/main/commands/file-open.ts
@@ -0,0 +1,11 @@
+import { CommandHandler } from '@/main/types';
+import { FileOpenCommandInput } from '@/shared/commands/file-open';
+import { fileService } from '@/main/services/file-service';
+
+export class FileOpenCommandHandler
+ implements CommandHandler<FileOpenCommandInput>
+{
+ public async handleCommand(input: FileOpenCommandInput): Promise<void> {
+ fileService.openFile(input.userId, input.fileId, input.extension);
+ }
+}
diff --git a/apps/desktop/src/main/commands/index.ts b/apps/desktop/src/main/commands/index.ts
new file mode 100644
index 00000000..77cd69c7
--- /dev/null
+++ b/apps/desktop/src/main/commands/index.ts
@@ -0,0 +1,13 @@
+import { CommandMap } from '@/shared/commands';
+import { CommandHandler } from '@/main/types';
+import { FileDialogOpenCommandHandler } from '@/main/commands/file-dialog-open';
+import { FileOpenCommandHandler } from '@/main/commands/file-open';
+
+type CommandHandlerMap = {
+ [K in keyof CommandMap]: CommandHandler<CommandMap[K]['input']>;
+};
+
+export const commandHandlerMap: CommandHandlerMap = {
+ file_dialog_open: new FileDialogOpenCommandHandler(),
+ file_open: new FileOpenCommandHandler(),
+};
diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts
index a19887ef..812fdd26 100644
--- a/apps/desktop/src/main/index.ts
+++ b/apps/desktop/src/main/index.ts
@@ -1,4 +1,4 @@
-import { app, shell, BrowserWindow, ipcMain, protocol, dialog } from 'electron';
+import { app, shell, BrowserWindow, ipcMain, protocol } from 'electron';
import { join } from 'path';
import { electronApp, optimizer, is } from '@electron-toolkit/utils';
import { eventBus } from '@/shared/lib/event-bus';
@@ -7,13 +7,15 @@ import { socketService } from '@/main/services/socket-service';
import { syncService } from '@/main/services/sync-service';
import { avatarService } from '@/main/services/avatar-service';
import { fileService } from '@/main/services/file-service';
-import { FileMetadata } from '@/shared/types/files';
import { MutationInput, MutationMap } from '@/shared/mutations';
import { QueryInput, QueryMap } from '@/shared/queries';
import { assetService } from '@/main/services/asset-service';
import { radarService } from '@/main/services/radar-service';
import { mutationService } from '@/main/services/mutation-service';
import { queryService } from '@/main/services/query-service';
+import { CommandInput } from '@/shared/commands';
+import { commandService } from '@/main/services/command-service';
+import { CommandMap } from '@/shared/commands';
let subscriptionId: string | null = null;
const icon = join(__dirname, '../assets/icon.png');
@@ -95,7 +97,7 @@ const createWindow = async (): Promise<void> => {
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
// Set app user model id for windows
- electronApp.setAppUserModelId('com.electron');
+ electronApp.setAppUserModelId('com.colanode.desktop');
// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.
@@ -168,35 +170,11 @@ ipcMain.handle('unsubscribe-query', (_: unknown, id: string): void => {
});
ipcMain.handle(
- 'open-file-dialog',
- async (
+ 'execute-command',
+ async <T extends CommandInput>(
_: unknown,
- options: Electron.OpenDialogOptions
- ): Promise<Electron.OpenDialogReturnValue> => {
- const window = BrowserWindow.getFocusedWindow();
- if (!window) {
- throw new Error('No focused window');
- }
-
- return dialog.showOpenDialog(window, options);
- }
-);
-
-ipcMain.handle(
- 'get-file-metadata',
- (_: unknown, path: string): FileMetadata | null => {
- return fileService.getFileMetadata(path);
- }
-);
-
-ipcMain.handle(
- 'open-file',
- async (
- _: unknown,
- userId: string,
- id: string,
- extension: string
- ): Promise<void> => {
- return fileService.openFile(userId, id, extension);
+ input: T
+ ): Promise<CommandMap[T['type']]['output']> => {
+ return commandService.executeCommand(input);
}
);
diff --git a/apps/desktop/src/main/queries/file-metadata-get.ts b/apps/desktop/src/main/queries/file-metadata-get.ts
new file mode 100644
index 00000000..0ee5d5f3
--- /dev/null
+++ b/apps/desktop/src/main/queries/file-metadata-get.ts
@@ -0,0 +1,26 @@
+import { ChangeCheckResult, QueryHandler } from '@/main/types';
+import { FileMetadataGetQueryInput } from '@/shared/queries/file-metadata-get';
+import { Event } from '@/shared/types/events';
+import { FileMetadata } from '@/shared/types/files';
+import { fileService } from '@/main/services/file-service';
+
+export class FileMetadataGetQueryHandler
+ implements QueryHandler<FileMetadataGetQueryInput>
+{
+ public async handleQuery(
+ input: FileMetadataGetQueryInput
+ ): Promise<FileMetadata | null> {
+ const data = fileService.getFileMetadata(input.path);
+ return data;
+ }
+
+ public async checkForChanges(
+ _: Event,
+ __: FileMetadataGetQueryInput,
+ ___: FileMetadata | null
+ ): Promise<ChangeCheckResult<FileMetadataGetQueryInput>> {
+ return {
+ hasChanges: false,
+ };
+ }
+}
diff --git a/apps/desktop/src/main/queries/index.ts b/apps/desktop/src/main/queries/index.ts
index 037f1e16..09030908 100644
--- a/apps/desktop/src/main/queries/index.ts
+++ b/apps/desktop/src/main/queries/index.ts
@@ -14,6 +14,7 @@ import { IconsGetQueryHandler } from '@/main/queries/icons-get';
import { NodeTreeGetQueryHandler } from '@/main/queries/node-tree-get';
import { NodeChildrenGetQueryHandler } from '@/main/queries/node-children-get';
import { ReadStatesGetQueryHandler } from '@/main/queries/read-states-get';
+import { FileMetadataGetQueryHandler } from '@/main/queries/file-metadata-get';
type QueryHandlerMap = {
[K in keyof QueryMap]: QueryHandler<QueryMap[K]['input']>;
@@ -34,4 +35,5 @@ export const queryHandlerMap: QueryHandlerMap = {
node_tree_get: new NodeTreeGetQueryHandler(),
node_children_get: new NodeChildrenGetQueryHandler(),
read_states_get: new ReadStatesGetQueryHandler(),
+ file_metadata_get: new FileMetadataGetQueryHandler(),
};
diff --git a/apps/desktop/src/main/queries/node-children-get.tsx b/apps/desktop/src/main/queries/node-children-get.ts
similarity index 100%
rename from apps/desktop/src/main/queries/node-children-get.tsx
rename to apps/desktop/src/main/queries/node-children-get.ts
diff --git a/apps/desktop/src/main/services/command-service.ts b/apps/desktop/src/main/services/command-service.ts
new file mode 100644
index 00000000..a20ac82c
--- /dev/null
+++ b/apps/desktop/src/main/services/command-service.ts
@@ -0,0 +1,23 @@
+import { CommandInput } from '@/shared/commands';
+
+import { CommandMap } from '@/shared/commands';
+import { commandHandlerMap } from '@/main/commands';
+import { CommandHandler } from '@/main/types';
+
+class CommandService {
+ public async executeCommand<T extends CommandInput>(
+ input: T
+ ): Promise<CommandMap[T['type']]['output']> {
+ const handler = commandHandlerMap[
+ input.type
+ ] as unknown as CommandHandler<T>;
+
+ if (!handler) {
+ throw new Error(`No handler found for command type: ${input.type}`);
+ }
+
+ return handler.handleCommand(input);
+ }
+}
+
+export const commandService = new CommandService();
diff --git a/apps/desktop/src/main/services/mutation-service.ts b/apps/desktop/src/main/services/mutation-service.ts
index 4d61c0b2..1ac04b2d 100644
--- a/apps/desktop/src/main/services/mutation-service.ts
+++ b/apps/desktop/src/main/services/mutation-service.ts
@@ -11,6 +11,7 @@ class MutationService {
const handler = mutationHandlerMap[
input.type
] as unknown as MutationHandler<T>;
+
if (!handler) {
throw new Error(`No handler found for mutation type: ${input.type}`);
}
diff --git a/apps/desktop/src/main/types.ts b/apps/desktop/src/main/types.ts
index f5ff3886..3da45516 100644
--- a/apps/desktop/src/main/types.ts
+++ b/apps/desktop/src/main/types.ts
@@ -1,3 +1,5 @@
+import { CommandMap } from '@/shared/commands';
+import { CommandInput } from '@/shared/commands';
import { MutationInput, MutationMap } from '@/shared/mutations';
import { QueryInput, QueryMap } from '@/shared/queries';
import { Event } from '@/shared/types/events';
@@ -6,6 +8,10 @@ export interface MutationHandler<T extends MutationInput> {
handleMutation: (input: T) => Promise<MutationMap[T['type']]['output']>;
}
+export interface CommandHandler<T extends CommandInput> {
+ handleCommand: (input: T) => Promise<CommandMap[T['type']]['output']>;
+}
+
export interface QueryHandler<T extends QueryInput> {
handleQuery: (input: T) => Promise<QueryMap[T['type']]['output']>;
checkForChanges: (
diff --git a/apps/desktop/src/preload/index.d.ts b/apps/desktop/src/preload/index.d.ts
index c60e1d7a..7270ecda 100644
--- a/apps/desktop/src/preload/index.d.ts
+++ b/apps/desktop/src/preload/index.d.ts
@@ -2,9 +2,9 @@ import { ElectronAPI } from '@electron-toolkit/preload';
import { EventBus } from '@/lib/event-bus';
import { MutationMap, MutationInput } from '@/shared/mutations';
import { QueryMap, QueryInput } from '@/shared/queries';
-import { FileMetadata } from '@/types/files';
+import { CommandMap, CommandInput } from '@/shared/commands';
-interface ColanodeApi {
+interface ColanodeAPI {
init: () => Promise<void>;
logout: (accountId: string) => Promise<void>;
@@ -23,18 +23,15 @@ interface ColanodeApi {
unsubscribeQuery: (id: string) => Promise<void>;
- openFileDialog: (
- options: Electron.OpenDialogOptions
- ) => Promise<Electron.OpenDialogReturnValue>;
-
- openFile: (userId: string, id: string, extension: string) => Promise<void>;
- getFileMetadata: (path: string) => Promise<FileMetadata | null>;
+ executeCommand: <T extends CommandInput>(
+ input: T
+ ) => Promise<CommandMap[T['type']]['output']>;
}
declare global {
interface Window {
electron: ElectronAPI;
- colanode: ColanodeApi;
+ colanode: ColanodeAPI;
eventBus: EventBus;
}
}
diff --git a/apps/desktop/src/preload/index.ts b/apps/desktop/src/preload/index.ts
index ebd45792..95808c36 100644
--- a/apps/desktop/src/preload/index.ts
+++ b/apps/desktop/src/preload/index.ts
@@ -4,6 +4,8 @@ import { eventBus } from '@/shared/lib/event-bus';
import { MutationInput, MutationMap } from '@/shared/mutations';
import { QueryInput, QueryMap } from '@/shared/queries';
import { Event } from '@/shared/types/events';
+import { CommandMap } from '@/shared/commands';
+import { CommandInput } from '@/shared/commands';
// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
@@ -38,16 +40,10 @@ if (process.contextIsolated) {
return ipcRenderer.invoke('unsubscribe-query', id);
},
- openFileDialog: (options: Electron.OpenDialogOptions) => {
- return ipcRenderer.invoke('open-file-dialog', options);
- },
-
- openFile: (userId: string, id: string, extension: string) => {
- return ipcRenderer.invoke('open-file', userId, id, extension);
- },
-
- getFileMetadata: (path: string) => {
- return ipcRenderer.invoke('get-file-metadata', path);
+ executeCommand: <T extends CommandInput>(
+ input: T
+ ): Promise<CommandMap[T['type']]['output']> => {
+ return ipcRenderer.invoke('execute-command', input);
},
});
diff --git a/apps/desktop/src/renderer/components/accounts/account-update.tsx b/apps/desktop/src/renderer/components/accounts/account-update.tsx
index ce9de7da..06538e99 100644
--- a/apps/desktop/src/renderer/components/accounts/account-update.tsx
+++ b/apps/desktop/src/renderer/components/accounts/account-update.tsx
@@ -93,11 +93,14 @@ export const AccountUpdate = ({ account }: { account: Account }) => {
}
setIsFileDialogOpen(true);
- const result = await window.colanode.openFileDialog({
- properties: ['openFile'],
- filters: [
- { name: 'Images', extensions: ['jpg', 'png', 'jpeg'] },
- ],
+ const result = await window.colanode.executeCommand({
+ type: 'file_dialog_open',
+ options: {
+ properties: ['openFile'],
+ filters: [
+ { name: 'Images', extensions: ['jpg', 'png', 'jpeg'] },
+ ],
+ },
});
if (result.canceled || !result.filePaths.length) {
diff --git a/apps/desktop/src/renderer/components/avatars/avatar-upload.tsx b/apps/desktop/src/renderer/components/avatars/avatar-upload.tsx
index 8ea86f02..8599a33a 100644
--- a/apps/desktop/src/renderer/components/avatars/avatar-upload.tsx
+++ b/apps/desktop/src/renderer/components/avatars/avatar-upload.tsx
@@ -60,9 +60,12 @@ export const AvatarUpload = ({ onUpload }: AvatarUploadProps) => {
}
setIsFileDialogOpen(true);
- const result = await window.colanode.openFileDialog({
- properties: ['openFile'],
- filters: [{ name: 'Images', extensions: ['jpg', 'png', 'jpeg'] }],
+ const result = await window.colanode.executeCommand({
+ type: 'file_dialog_open',
+ options: {
+ properties: ['openFile'],
+ filters: [{ name: 'Images', extensions: ['jpg', 'png', 'jpeg'] }],
+ },
});
if (result.canceled || !result.filePaths.length) {
diff --git a/apps/desktop/src/renderer/components/files/file-body.tsx b/apps/desktop/src/renderer/components/files/file-body.tsx
index eda9fa54..5e3aa3e2 100644
--- a/apps/desktop/src/renderer/components/files/file-body.tsx
+++ b/apps/desktop/src/renderer/components/files/file-body.tsx
@@ -24,11 +24,12 @@ export const FileBody = ({ file }: FileBodyProps) => {
variant="outline"
size="sm"
onClick={() =>
- window.colanode.openFile(
- workspace.userId,
- file.id,
- file.attributes.extension
- )
+ window.colanode.executeCommand({
+ type: 'file_open',
+ userId: workspace.userId,
+ fileId: file.id,
+ extension: file.attributes.extension,
+ })
}
>
<SquareArrowOutUpRight className="mr-1 size-4" /> Open
diff --git a/apps/desktop/src/renderer/components/folders/folder-body.tsx b/apps/desktop/src/renderer/components/folders/folder-body.tsx
index c3b32e74..8ed522ee 100644
--- a/apps/desktop/src/renderer/components/folders/folder-body.tsx
+++ b/apps/desktop/src/renderer/components/folders/folder-body.tsx
@@ -72,10 +72,13 @@ export const FolderBody = ({ folder }: FolderBodyProps) => {
}
isDialogOpenedRef.current = true;
- const result = await window.colanode.openFileDialog({
- properties: ['openFile'],
- buttonLabel: 'Upload',
- title: 'Upload files to folder',
+ const result = await window.colanode.executeCommand({
+ type: 'file_dialog_open',
+ options: {
+ properties: ['openFile'],
+ buttonLabel: 'Upload',
+ title: 'Upload files to folder',
+ },
});
if (result.canceled) {
diff --git a/apps/desktop/src/renderer/components/messages/message-create.tsx b/apps/desktop/src/renderer/components/messages/message-create.tsx
index 65ff085f..85b06ecd 100644
--- a/apps/desktop/src/renderer/components/messages/message-create.tsx
+++ b/apps/desktop/src/renderer/components/messages/message-create.tsx
@@ -102,10 +102,13 @@ export const MessageCreate = React.forwardRef<MessageCreateRefProps>(
return;
}
- const result = await window.colanode.openFileDialog({
- properties: ['openFile'],
- buttonLabel: 'Upload',
- title: 'Upload files to message',
+ const result = await window.colanode.executeCommand({
+ type: 'file_dialog_open',
+ options: {
+ properties: ['openFile'],
+ buttonLabel: 'Upload',
+ title: 'Upload files to message',
+ },
});
if (result.canceled) {
@@ -113,7 +116,10 @@ export const MessageCreate = React.forwardRef<MessageCreateRefProps>(
}
const filePath = result.filePaths[0];
- const fileMetadata = await window.colanode.getFileMetadata(filePath);
+ const fileMetadata = await window.colanode.executeQuery({
+ type: 'file_metadata_get',
+ path: filePath,
+ });
if (fileMetadata === null) {
toast({
diff --git a/apps/desktop/src/renderer/components/workspaces/workspace-form.tsx b/apps/desktop/src/renderer/components/workspaces/workspace-form.tsx
index fae90d03..99736495 100644
--- a/apps/desktop/src/renderer/components/workspaces/workspace-form.tsx
+++ b/apps/desktop/src/renderer/components/workspaces/workspace-form.tsx
@@ -78,11 +78,14 @@ export const WorkspaceForm = ({
}
setIsFileDialogOpen(true);
- const result = await window.colanode.openFileDialog({
- properties: ['openFile'],
- filters: [
- { name: 'Images', extensions: ['jpg', 'png', 'jpeg'] },
- ],
+ const result = await window.colanode.executeCommand({
+ type: 'file_dialog_open',
+ options: {
+ properties: ['openFile'],
+ filters: [
+ { name: 'Images', extensions: ['jpg', 'png', 'jpeg'] },
+ ],
+ },
});
if (result.canceled || !result.filePaths.length) {
diff --git a/apps/desktop/src/renderer/editor/commands/file.tsx b/apps/desktop/src/renderer/editor/commands/file.tsx
index f525a2cd..11407070 100644
--- a/apps/desktop/src/renderer/editor/commands/file.tsx
+++ b/apps/desktop/src/renderer/editor/commands/file.tsx
@@ -14,10 +14,13 @@ export const FileCommand: EditorCommand = {
return;
}
- const result = await window.colanode.openFileDialog({
- properties: ['openFile'],
- buttonLabel: 'Upload',
- title: 'Upload files to page',
+ const result = await window.colanode.executeCommand({
+ type: 'file_dialog_open',
+ options: {
+ properties: ['openFile'],
+ buttonLabel: 'Upload',
+ title: 'Upload files to page',
+ },
});
if (result.canceled) {
diff --git a/apps/desktop/src/shared/commands/file-dialog-open.ts b/apps/desktop/src/shared/commands/file-dialog-open.ts
new file mode 100644
index 00000000..73207a1f
--- /dev/null
+++ b/apps/desktop/src/shared/commands/file-dialog-open.ts
@@ -0,0 +1,13 @@
+export type FileDialogOpenCommandInput = {
+ type: 'file_dialog_open';
+ options: Electron.OpenDialogOptions;
+};
+
+declare module '@/shared/commands' {
+ interface CommandMap {
+ file_dialog_open: {
+ input: FileDialogOpenCommandInput;
+ output: Electron.OpenDialogReturnValue;
+ };
+ }
+}
diff --git a/apps/desktop/src/shared/commands/file-open.ts b/apps/desktop/src/shared/commands/file-open.ts
new file mode 100644
index 00000000..0dc6598b
--- /dev/null
+++ b/apps/desktop/src/shared/commands/file-open.ts
@@ -0,0 +1,15 @@
+export type FileOpenCommandInput = {
+ type: 'file_open';
+ userId: string;
+ fileId: string;
+ extension: string;
+};
+
+declare module '@/shared/commands' {
+ interface CommandMap {
+ file_open: {
+ input: FileOpenCommandInput;
+ output: void;
+ };
+ }
+}
diff --git a/apps/desktop/src/shared/commands/index.ts b/apps/desktop/src/shared/commands/index.ts
new file mode 100644
index 00000000..f0296b9b
--- /dev/null
+++ b/apps/desktop/src/shared/commands/index.ts
@@ -0,0 +1,3 @@
+export interface CommandMap {}
+
+export type CommandInput = CommandMap[keyof CommandMap]['input'];
diff --git a/apps/desktop/src/shared/queries/file-metadata-get.ts b/apps/desktop/src/shared/queries/file-metadata-get.ts
new file mode 100644
index 00000000..58e1e01a
--- /dev/null
+++ b/apps/desktop/src/shared/queries/file-metadata-get.ts
@@ -0,0 +1,15 @@
+import { FileMetadata } from '@/shared/types/files';
+
+export type FileMetadataGetQueryInput = {
+ type: 'file_metadata_get';
+ path: string;
+};
+
+declare module '@/shared/queries' {
+ interface QueryMap {
+ file_metadata_get: {
+ input: FileMetadataGetQueryInput;
+ output: FileMetadata | null;
+ };
+ }
+}
diff --git a/apps/desktop/src/shared/queries/node-children-get.tsx b/apps/desktop/src/shared/queries/node-children-get.ts
similarity index 100%
rename from apps/desktop/src/shared/queries/node-children-get.tsx
rename to apps/desktop/src/shared/queries/node-children-get.ts