Skip to content

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

feat: show proper progress dialog when encrypting

thecodrr8b50a578cef6
diff --git a/apps/web/src/common/dialogcontroller.js b/apps/web/src/common/dialogcontroller.js
index 03cc51b7d..402d8933e 100644
--- a/apps/web/src/common/dialogcontroller.js
+++ b/apps/web/src/common/dialogcontroller.js
@@ -342,6 +342,21 @@ export function showLoadingDialog(dialogData) {
));
}
+export function showProgressDialog(dialogData) {
+ const { title, message, subtitle, total, setProgress, action } = dialogData;
+ return showDialog((Dialogs, perform) => (
+ <Dialogs.ProgressDialog
+ title={title}
+ subtitle={subtitle}
+ message={message}
+ total={total}
+ setProgress={setProgress}
+ action={action}
+ onDone={(e) => perform(e)}
+ />
+ ));
+}
+
export function showMoveNoteDialog(noteIds) {
return showDialog((Dialogs, perform) => (
<Dialogs.MoveDialog
diff --git a/apps/web/src/components/dialogs/index.js b/apps/web/src/components/dialogs/index.js
index 72ba2ce91..f01179bee 100644
--- a/apps/web/src/components/dialogs/index.js
+++ b/apps/web/src/components/dialogs/index.js
@@ -4,6 +4,7 @@ import Confirm from "./confirm";
import EmailVerificationDialog from "./emailverificationdialog";
import ExportDialog from "./exportdialog";
import LoadingDialog from "./loadingdialog";
+import ProgressDialog from "./progressdialog";
import MoveDialog from "./movenotedialog";
import PasswordDialog from "./passworddialog";
import RecoveryKeyDialog from "./recoverykeydialog";
@@ -26,5 +27,6 @@ const Dialogs = {
ItemDialog,
SessionExpiredDialog,
FeatureDialog,
+ ProgressDialog,
};
export default Dialogs;
diff --git a/apps/web/src/components/dialogs/progress-dialog.js b/apps/web/src/components/dialogs/progress-dialog.js
new file mode 100644
index 000000000..624b85e38
--- /dev/null
+++ b/apps/web/src/components/dialogs/progress-dialog.js
@@ -0,0 +1,54 @@
+import { useEffect, useState } from "react";
+import { Box, Flex, Text } from "rebass";
+import Dialog from "./dialog";
+
+function ProgressDialog(props) {
+ const [{ loaded, progress }, setProgress] = useState({
+ loaded: 0,
+ progress: 0,
+ });
+
+ useEffect(() => {
+ if (!props.setProgress) return;
+ const undo = props.setProgress(setProgress);
+ return () => {
+ undo && undo();
+ };
+ }, [props, setProgress]);
+
+ useEffect(() => {
+ (async function () {
+ try {
+ props.onDone(await props.action());
+ } catch (e) {
+ props.onDone(e);
+ }
+ })();
+ }, [props]);
+
+ return (
+ <Dialog
+ isOpen={true}
+ title={props.title}
+ description={props.subtitle}
+ onClose={() => {}}
+ >
+ <Flex flexDirection="column">
+ <Text variant="body">{props.message}</Text>
+ <Text variant="subBody">
+ {loaded} of {props.total}
+ </Text>
+ <Box
+ sx={{
+ alignSelf: "start",
+ my: 1,
+ bg: "primary",
+ height: "2px",
+ width: `${progress}%`,
+ }}
+ />
+ </Flex>
+ </Dialog>
+ );
+}
+export default ProgressDialog;
diff --git a/apps/web/src/components/editor/plugins/picker.js b/apps/web/src/components/editor/plugins/picker.js
index 4fe8eae2c..4eca503ad 100644
--- a/apps/web/src/components/editor/plugins/picker.js
+++ b/apps/web/src/components/editor/plugins/picker.js
@@ -1,7 +1,9 @@
import Compressor from "compressorjs";
+import { AppEventManager, AppEvents } from "../../../common";
import { db } from "../../../common/db";
-import { showLoadingDialog } from "../../../common/dialog-controller";
+import { showProgressDialog } from "../../../common/dialog-controller";
import fs from "../../../interfaces/fs";
+import { formatBytes } from "../../../utils/filename";
function register(editor) {
editor.ui.registry.addButton("attachment", {
@@ -39,9 +41,25 @@ async function pickFile() {
const selectedFile = await showFilePicker({ acceptedFileTypes: "*/*" });
if (!selectedFile) return;
- const result = await showLoadingDialog({
- title: `${selectedFile.name}`,
- subtitle: "Please wait while we encrypt & attach your file.",
+ const result = await showProgressDialog({
+ title: `Encrypting attachment`,
+ subtitle: "Please wait while we encrypt this attachment for upload.",
+ message: selectedFile.name,
+ total: formatBytes(selectedFile.size, 0),
+ setProgress: (set) => {
+ const event = AppEventManager.subscribe(
+ AppEvents.UPDATE_ATTACHMENT_PROGRESS,
+ ({ type, total, loaded }) => {
+ if (type !== "encrypt") return;
+
+ const percent = Math.round((loaded / total) * 100);
+ set({ loaded: formatBytes(loaded, 0), progress: percent });
+ }
+ );
+ return () => {
+ event.unsubscribe();
+ };
+ },
action: async () => {
const reader = selectedFile.stream().getReader();
const { hash, type: hashType } = await fs.hashStream(reader);
@@ -109,6 +127,7 @@ async function pickImage() {
}
async function getEncryptionKey() {
+ return { password: "helloworld" };
const key = await db.user.getEncryptionKey();
if (!key) throw new Error("No encryption key found. Are you logged in?");
return key; // { password: "helloworld" };
diff --git a/apps/web/src/components/properties/index.js b/apps/web/src/components/properties/index.js
index 550510e22..fb408c6c2 100644
--- a/apps/web/src/components/properties/index.js
+++ b/apps/web/src/components/properties/index.js
@@ -10,6 +10,7 @@ import Toggle from "./toggle";
import { navigate } from "../../navigation";
import IconTag from "../icon-tag";
import FS from "../../interfaces/fs";
+import { formatBytes, truncateFilename } from "../../utils/filename";
const tools = [
{ key: "pinned", icon: Icon.Pin, label: "Pin" },
@@ -236,7 +237,7 @@ function Properties({ noteId }) {
overflow: "hidden",
}}
>
- {formatFilename(attachment.metadata.filename)}
+ {truncateFilename(attachment.metadata.filename)}
</Text>
{attachmentStatus && (
<Box
@@ -343,27 +344,3 @@ function Card({ title, children }) {
</Flex>
);
}
-
-function formatBytes(bytes, decimals = 2) {
- if (bytes === 0) return "0B";
-
- const k = 1024;
- const dm = decimals < 0 ? 0 : decimals;
- const sizes = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"];
-
- const i = Math.floor(Math.log(bytes) / Math.log(k));
-
- return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + sizes[i];
-}
-
-function formatFilename(filename) {
- const MAX_LENGTH = 38;
- if (filename.length > MAX_LENGTH) {
- return (
- filename.substr(0, MAX_LENGTH / 2) +
- "..." +
- filename.substr(filename.lastIndexOf(".") - 4)
- );
- }
- return filename;
-}
diff --git a/apps/web/src/utils/filename.js b/apps/web/src/utils/filename.js
index be3123642..0f75deee5 100644
--- a/apps/web/src/utils/filename.js
+++ b/apps/web/src/utils/filename.js
@@ -52,3 +52,27 @@ export function sanitizeFilename(input) {
var replacement = "-";
return sanitize(input, replacement);
}
+
+export function formatBytes(bytes, decimals = 2) {
+ if (bytes === 0) return "0 B";
+
+ const k = 1024;
+ const dm = decimals < 0 ? 0 : decimals;
+ const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
+
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
+
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
+}
+
+export function truncateFilename(filename) {
+ const MAX_LENGTH = 38;
+ if (filename.length > MAX_LENGTH) {
+ return (
+ filename.substr(0, MAX_LENGTH / 2) +
+ "..." +
+ filename.substr(filename.lastIndexOf(".") - 4)
+ );
+ }
+ return filename;
+}