Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: resolve edge case in merge conflict

thecodrr927d2a363e37
diff --git a/apps/web/src/components/spliteditor/contenttoggle.js b/apps/web/src/components/spliteditor/contenttoggle.js
index 9e991f50d..65f5186f0 100644
--- a/apps/web/src/components/spliteditor/contenttoggle.js
+++ b/apps/web/src/components/spliteditor/contenttoggle.js
@@ -1,9 +1,6 @@
-import React, { useCallback } from "react";
+import React from "react";
import { Flex, Text, Button } from "rebass";
import { timeConverter } from "../../utils/time";
-import { store as notestore } from "../../stores/note-store";
-import { db } from "../../common/db";
-import { hashNavigate } from "../../navigation";
import diff from "./differ";
function ContentToggle(props) {
@@ -15,43 +12,9 @@ function ContentToggle(props) {
label,
dateEdited,
editors,
- note,
+ resolveConflict,
} = props;
- const resolveConflict = useCallback(
- async (selectedContent, otherContent) => {
- selectedContent = {
- data: selectedContent,
- type: "tiny",
- resolved: true,
- };
-
- await db.notes.add({
- id: note.id,
- content: selectedContent,
- conflicted: false,
- });
- if (otherContent) {
- otherContent = {
- data: otherContent,
- type: "tiny",
- };
- await db.notes.add({
- ...note,
- content: otherContent,
- id: undefined,
- dateCreated: undefined,
- dateEdited: undefined,
- conflicted: false,
- title: note.title + " (DUPLICATE)",
- });
- }
- notestore.refresh();
- hashNavigate(`/notes/${note.id}/edit`, true);
- },
- [note]
- );
-
return (
<Flex flexDirection="column" sx={sx}>
<Flex>
diff --git a/apps/web/src/components/spliteditor/index.js b/apps/web/src/components/spliteditor/index.js
index 7881947f7..dc23ba96c 100644
--- a/apps/web/src/components/spliteditor/index.js
+++ b/apps/web/src/components/spliteditor/index.js
@@ -1,22 +1,73 @@
-import React, { useState, useRef, useEffect } from "react";
+import React, { useState, useRef, useEffect, useCallback } from "react";
import { Flex, Box, Text } from "rebass";
import SimpleEditor from "./simpleeditor";
import ContentToggle from "./content-toggle";
import { store as notesStore } from "../../stores/note-store";
import { db } from "../../common/db";
import { useStore as useAppStore } from "../../stores/app-store";
+import { useStore as useUserStore } from "../../stores/user-store";
import { hashNavigate } from "../../navigation";
import diff from "./differ";
+import { showToast } from "../../utils/toast";
function SplitEditor(props) {
const { noteId } = props;
const setIsEditorOpen = useAppStore((store) => store.setIsEditorOpen);
+ const sync = useUserStore((store) => store.sync);
const [conflictedNote, setConflictedNote] = useState();
const [remoteContent, setRemoteContent] = useState();
const [localContent, setLocalContent] = useState();
const [htmlDiff, setHtmlDiff] = useState({});
+ const resolveConflict = useCallback(
+ async (selectedContent, otherContent) => {
+ const note = conflictedNote;
+ if (!note) return;
+
+ selectedContent = {
+ data: selectedContent,
+ type: "tiny",
+ resolved: true,
+ };
+
+ await db.notes.add({
+ id: note.id,
+ content: selectedContent,
+ conflicted: false,
+ });
+ if (otherContent) {
+ otherContent = {
+ data: otherContent,
+ type: "tiny",
+ };
+ await db.notes.add({
+ ...note,
+ content: otherContent,
+ id: undefined,
+ dateCreated: undefined,
+ dateEdited: undefined,
+ conflicted: false,
+ title: note.title + " (DUPLICATE)",
+ });
+ }
+ notesStore.refresh();
+ hashNavigate(`/notes/${note.id}/edit`, true);
+
+ const conflictsCount = db.notes.conflicted?.length;
+ if (conflictsCount) {
+ showToast(
+ "success",
+ `Conflict resolved. ${conflictsCount} conflicts left.`
+ );
+ } else {
+ showToast("success", "All conflicts resolved. Starting sync.");
+ await sync();
+ }
+ },
+ [conflictedNote, sync]
+ );
+
useEffect(() => {
(async function () {
await db.notes.init();
@@ -27,13 +78,16 @@ function SplitEditor(props) {
}
notesStore.setSelectedNote(noteId);
note = note.data;
+
const content = await db.content.raw(note.contentId);
+ if (!content.conflicted) resolveConflict(content.data);
+
setConflictedNote(note);
setLocalContent({ ...content, conflicted: false });
setRemoteContent(content.conflicted);
setHtmlDiff(diff.diff_dual_pane(content.data, content.conflicted.data));
})();
- }, [noteId]);
+ }, [noteId, resolveConflict]);
useEffect(() => {
setIsEditorOpen(true);
@@ -72,11 +126,11 @@ function SplitEditor(props) {
>
<ContentToggle
label="Current note"
+ resolveConflict={resolveConflict}
dateEdited={localContent.dateEdited}
isSelected={selectedDelta === 0}
isOtherSelected={selectedDelta === 1}
onToggle={() => setSelectedDelta((s) => (s === 0 ? -1 : 0))}
- note={conflictedNote}
editors={() => ({
selectedEditor: remoteEditor.current.editor,
otherEditor: localEditor.current.editor,
@@ -123,8 +177,8 @@ function SplitEditor(props) {
pb: 1,
pt: [1, 1, 0],
}}
+ resolveConflict={resolveConflict}
label="Incoming note"
- note={conflictedNote}
isSelected={selectedDelta === 1}
isOtherSelected={selectedDelta === 0}
dateEdited={remoteContent.dateEdited}