Skip to content

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

6.9 KB
Raw Blame
/*
This file is part of the Notesnook project (https://notesnook.com/)

Copyright (C) 2023 Streetwriters (Private) Limited

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import { useThemeColors } from "@notesnook/theme";
import React, { useEffect, useState } from "react";
import { View } from "react-native";
import { db } from "../../common/database";
import { ReadonlyEditor } from "../../screens/editor/readonly-editor";
import { useTabStore } from "../../screens/editor/tiptap/use-tab-store";
import { editorController } from "../../screens/editor/tiptap/utils";
import { ToastManager, eSendEvent } from "../../services/event-manager";
import Navigation from "../../services/navigation";
import { useSelectionStore } from "../../stores/use-selection-store";
import { useTrashStore } from "../../stores/use-trash-store";
import { PinnedNotesWidget } from "../../services/pinned-notes-widget";
import { eCloseSheet, eOnLoadNote } from "../../utils/events";
import { Dialog } from "../dialog";
import DialogHeader from "../dialog/dialog-header";
import { presentDialog } from "../dialog/functions";
import { Button } from "../ui/button";
import Paragraph from "../ui/typography/paragraph";
import { diff } from "diffblazer";
import { strings } from "@notesnook/intl";
import { DefaultAppStyles } from "../../utils/styles";
import {
  HistorySession,
  isEncryptedContent,
  Note,
  NoteContent,
  TrashOrItem
} from "@notesnook/core";

/**
 *
 * @param {any} param0
 * @returns
 */
export default function NotePreview({
  session,
  content,
  note,
  update
}: {
  session?: HistorySession & { session: string };
  content:
    | Partial<
        NoteContent<boolean> & {
          title: string;
        }
      >
    | undefined;
  note: TrashOrItem<Note>;
  update?: () => void;
}) {
  const { colors } = useThemeColors();
  const [locked, setLocked] = useState(false);

  async function restore() {
    if (note && note.type === "trash") {
      await db.trash.restore(note.id);
      Navigation.queueRoutesForUpdate();
      PinnedNotesWidget.updateNotes();
      useSelectionStore.getState().setSelectionMode();
      ToastManager.show({
        heading: strings.noteRestored(),
        type: "success"
      });
      eSendEvent(eCloseSheet);
      return;
    }
    if (session) {
      await db.noteHistory.restore(session.id);
      if (useTabStore.getState().hasTabForNote(session?.noteId)) {
        const note = editorController.current.note.current[session?.noteId];
        if (note) {
          eSendEvent(eOnLoadNote, {
            item: note,
            forced: true
          });
        }
      }
      eSendEvent(eCloseSheet, "note_history");
      eSendEvent(eCloseSheet);
      Navigation.queueRoutesForUpdate();

      ToastManager.show({
        heading: strings.noteRestoredFromHistory(),
        type: "success"
      });
    }
  }

  useEffect(() => {
    db.vaults.itemExists(note).then((locked) => setLocked(locked));
  }, [note]);

  const deleteNote = async () => {
    const isSession = !!session;

    presentDialog({
      title: isSession ? strings.deleteVersion() : strings.deleteNote(),
      paragraph: isSession
        ? strings.deleteVersionConfirmation()
        : strings.deleteNoteConfirmation(),
      positiveText: strings.delete(),
      negativeText: strings.cancel(),
      context: "preview",
      positivePress: async () => {
        if (isSession) {
          await db.noteHistory.remove(session.id);
          update?.();
          eSendEvent(eCloseSheet, "note_history");
          Navigation.queueRoutesForUpdate();
        } else if (note.type === "trash") {
          await db.trash.delete(note.id);
          useTrashStore.getState().refresh();
          useSelectionStore.getState().setSelectionMode();
          eSendEvent(eCloseSheet);
        }
        ToastManager.show({
          heading: isSession ? strings.versionDeleted() : strings.noteDeleted(),
          type: "success",
          context: isSession ? "local" : "global"
        });

        return true;
      },
      positiveType: "error"
    });
  };

  return (
    <View
      style={{
        height: locked || session?.locked ? null : 600,
        width: "100%"
      }}
    >
      <Dialog context="preview" />
      <DialogHeader
        padding={12}
        title={content?.title || note.title || session?.session}
      />
      {!session?.locked && !locked && content?.data ? (
        <View
          style={{
            flex: 1,
            backgroundColor: colors.primary.background
          }}
        >
          <ReadonlyEditor
            editorId="historyPreview"
            onLoad={async (loadContent) => {
              try {
                if (content?.data) {
                  const currentContent = note?.contentId
                    ? await db.content.get(note.contentId)
                    : undefined;

                  if (
                    currentContent?.data &&
                    !isEncryptedContent(currentContent)
                  ) {
                    loadContent({
                      data: diff(
                        currentContent?.data || "<p></p>",
                        content.data as string
                      ),
                      id: session?.noteId || note.id
                    });
                  }
                }
              } catch (e) {
                ToastManager.error(
                  e as Error,
                  "Failed to load history preview",
                  "local"
                );
              }
            }}
          />
        </View>
      ) : (
        <View
          style={{
            width: "100%",
            flex: 1,
            justifyContent: "center",
            alignItems: "center"
          }}
        >
          <Paragraph color={colors.secondary.paragraph}>
            {!content?.data
              ? strings.noContent()
              : strings.encryptedNoteHistoryNotice()}
          </Paragraph>
        </View>
      )}

      <View
        style={{
          paddingHorizontal: DefaultAppStyles.GAP
        }}
      >
        <Button
          onPress={restore}
          title={strings.restore()}
          type="accent"
          width="100%"
        />
        <Button
          onPress={deleteNote}
          title={strings.deletePermanently()}
          type="error"
          width="100%"
          style={{
            marginTop: DefaultAppStyles.GAP_VERTICAL
          }}
        />
      </View>
    </View>
  );
}