Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Improve document saving mutations
Hakan Shehu1c91f087f7a3
diff --git a/apps/desktop/src/main/services/workspaces/mutation-service.ts b/apps/desktop/src/main/services/workspaces/mutation-service.tsindex 6586421c..5c382b2f 100644--- a/apps/desktop/src/main/services/workspaces/mutation-service.ts+++ b/apps/desktop/src/main/services/workspaces/mutation-service.ts@@ -50,18 +50,18 @@ export class MutationService {return false;}- const unsyncedMutations = await this.workspace.database+ const pendingMutations = await this.workspace.database.selectFrom('mutations').selectAll().orderBy('id', 'asc').limit(READ_SIZE).execute();- if (unsyncedMutations.length === 0) {+ if (pendingMutations.length === 0) {return false;}- const allMutations: Mutation[] = unsyncedMutations.map(mapMutation);+ const allMutations: Mutation[] = pendingMutations.map(mapMutation);const { validMutations, deletedMutationIds } =this.consolidateMutations(allMutations);@@ -73,7 +73,7 @@ export class MutationService {}this.debug(- `Sending ${unsyncedMutations.length} local pending mutations for user ${this.workspace.id}`+ `Sending ${pendingMutations.length} local pending mutations for user ${this.workspace.id}`);const totalBatches = Math.ceil(validMutations.length / BATCH_SIZE);@@ -118,9 +118,11 @@ export class MutationService {this.debug(`Failed to send local pending mutations for user ${this.workspace.id}: ${error}`);++ return false;}- return unsyncedMutations.length > 0;+ return pendingMutations.length > 0;}private async revertInvalidMutations(): Promise<void> {diff --git a/apps/desktop/src/renderer/components/pages/page-body.tsx b/apps/desktop/src/renderer/components/pages/page-body.tsxindex 483a246c..96cbbec2 100644--- a/apps/desktop/src/renderer/components/pages/page-body.tsx+++ b/apps/desktop/src/renderer/components/pages/page-body.tsx@@ -5,7 +5,6 @@ import { JSONContent } from '@tiptap/core';import { Document } from '@/renderer/components/documents/document';import { ScrollArea } from '@/renderer/components/ui/scroll-area';import { useWorkspace } from '@/renderer/contexts/workspace';-import { useMutation } from '@/renderer/hooks/use-mutation';import { toast } from '@/renderer/hooks/use-toast';import { useRadar } from '@/renderer/contexts/radar';@@ -17,30 +16,28 @@ interface PageBodyProps {export const PageBody = ({ page, role }: PageBodyProps) => {const workspace = useWorkspace();const radar = useRadar();- const { mutate } = useMutation();const canEdit = hasEntryRole(role, 'editor');const handleUpdate = useCallback(- (before: JSONContent, after: JSONContent) => {- mutate({- input: {- type: 'page_content_update',- accountId: workspace.accountId,- workspaceId: workspace.id,- pageId: page.id,- before,- after,- },- onError(error) {- toast({- title: 'Failed to update page',- description: error.message,- variant: 'destructive',- });- },+ async (before: JSONContent, after: JSONContent) => {+ const result = await window.colanode.executeMutation({+ type: 'page_content_update',+ accountId: workspace.accountId,+ workspaceId: workspace.id,+ pageId: page.id,+ before,+ after,});++ if (!result.success) {+ toast({+ title: 'Failed to update page',+ description: result.error.message,+ variant: 'destructive',+ });+ }},- [mutate, workspace.userId, page.id]+ [workspace.accountId, workspace.id, page.id]);useEffect(() => {diff --git a/apps/desktop/src/renderer/components/records/record-body.tsx b/apps/desktop/src/renderer/components/records/record-body.tsxindex ba4d09a7..10e6c695 100644--- a/apps/desktop/src/renderer/components/records/record-body.tsx+++ b/apps/desktop/src/renderer/components/records/record-body.tsx@@ -14,7 +14,6 @@ import { RecordProvider } from '@/renderer/components/records/record-provider';import { ScrollArea } from '@/renderer/components/ui/scroll-area';import { Separator } from '@/renderer/components/ui/separator';import { useWorkspace } from '@/renderer/contexts/workspace';-import { useMutation } from '@/renderer/hooks/use-mutation';import { toast } from '@/renderer/hooks/use-toast';import { useRadar } from '@/renderer/contexts/radar';@@ -27,32 +26,30 @@ interface RecordBodyProps {export const RecordBody = ({ record, database, role }: RecordBodyProps) => {const workspace = useWorkspace();const radar = useRadar();- const { mutate } = useMutation();const canEdit =record.createdBy === workspace.userId || hasEntryRole(role, 'editor');const handleUpdate = useCallback(- (before: JSONContent, after: JSONContent) => {- mutate({- input: {- type: 'record_content_update',- accountId: workspace.accountId,- workspaceId: workspace.id,- recordId: record.id,- before,- after,- },- onError(error) {- toast({- title: 'Failed to update record',- description: error.message,- variant: 'destructive',- });- },+ async (before: JSONContent, after: JSONContent) => {+ const result = await window.colanode.executeMutation({+ type: 'record_content_update',+ accountId: workspace.accountId,+ workspaceId: workspace.id,+ recordId: record.id,+ before,+ after,});++ if (!result.success) {+ toast({+ title: 'Failed to update page',+ description: result.error.message,+ variant: 'destructive',+ });+ }},- [mutate]+ [workspace.accountId, workspace.id, record.id]);useEffect(() => {