import KeepAwake from '@sayem314/react-native-keep-awake';
import React, { useEffect, useRef, useState } from 'react';
import { Modal, SafeAreaView, Text, View } from 'react-native';
import Animated from 'react-native-reanimated';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import Editor from '../../screens/editor';
import { editorController } from '../../screens/editor/tiptap/utils';
import { DDS } from '../../services/device-detection';
import { eSendEvent, eSubscribeEvent, eUnSubscribeEvent } from '../../services/event-manager';
import Navigation from '../../services/navigation';
import Sync from '../../services/sync';
import { useThemeStore } from '../../stores/use-theme-store';
import { dHeight } from '../../utils';
import { db } from '../../utils/database';
import { eOnLoadNote, eShowMergeDialog } from '../../utils/events';
import { SIZE } from '../../utils/size';
import { timeConverter } from '../../utils/time';
import BaseDialog from '../dialog/base-dialog';
import DialogButtons from '../dialog/dialog-buttons';
import DialogContainer from '../dialog/dialog-container';
import DialogHeader from '../dialog/dialog-header';
import { Button } from '../ui/button';
import { IconButton } from '../ui/icon-button';
import Seperator from '../ui/seperator';
import Paragraph from '../ui/typography/paragraph';
const MergeConflicts = () => {
const colors = useThemeStore(state => state.colors);
const [visible, setVisible] = useState(false);
const [keep, setKeep] = useState(null);
const [copy, setCopy] = useState(null);
const [dialogVisible, setDialogVisible] = useState(false);
const insets = useSafeAreaInsets();
const content = useRef({});
const isKeepingConflicted = !keep?.conflicted;
const isKeeping = !!keep;
const applyChanges = async () => {
let _content = keep;
let note = db.notes.note(_content.noteId).data;
await db.notes.add({
id: note.id,
conflicted: false,
dateEdited: _content.dateEdited
});
await db.content.add({
id: note.contentId,
data: _content.data,
type: _content.type,
dateResolved: content.current.conflicted.dateModified,
sessionId: Date.now(),
conflicted: false
});
if (copy) {
await db.notes.add({
title: note.title + ' (Copy)',
content: {
data: copy.data,
type: copy.type
}
});
}
Navigation.queueRoutesForUpdate(
'Notes',
'Favorites',
'ColoredNotes',
'TaggedNotes',
'TopicNotes'
);
if (editorController.current?.note?.id === note.id) {
//TODO
}
close();
await Sync.run();
};
const show = async item => {
let noteContent = await db.content.raw(item.contentId);
content.current = { ...noteContent };
if (!noteContent.conflicted) {
content.current.conflicted = { ...noteContent };
}
setVisible(true);
};
useEffect(() => {
eSubscribeEvent(eShowMergeDialog, show);
return () => {
eUnSubscribeEvent(eShowMergeDialog, show);
};
}, []);
const close = () => {
setVisible(false);
setCopy(null);
setKeep(null);
setDialogVisible(false);
};
const ConfigBar = ({ isDiscarded, keeping, back, isCurrent, contentToKeep }) => {
return (
{back && }
{isCurrent ? '(This Device)' : '(Incoming)'}
{'\n'}
{timeConverter(contentToKeep.dateEdited)}
{isDiscarded ? (
);
};
return !visible ? null : (
{
close();
}}
supportedOrientations={[
'portrait',
'portrait-upside-down',
'landscape',
'landscape-left',
'landscape-right'
]}
visible={true}
>
{dialogVisible && (
setDialogVisible(false)}
onPressPositive={applyChanges}
/>
)}
{
const note = db.notes.note(content.current?.noteId)?.data;
if (!note) return;
eSendEvent(eOnLoadNote + ':conflictPrimary', { ...note, content: content.current });
}}
/>
{
const note = db.notes.note(content.current?.noteId)?.data;
if (!note) return;
eSendEvent(eOnLoadNote + ':conflictSecondary', {
...note,
content: content.current.conflicted
});
}}
/>
);
};
export default MergeConflicts;