Skip to content

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

feat: add attachments management dialog

thecodrrc0e6a31d1173
diff --git a/apps/web/src/app-effects.js b/apps/web/src/app-effects.js
index 62a60a4ab..1cd983b66 100644
--- a/apps/web/src/app-effects.js
+++ b/apps/web/src/app-effects.js
@@ -3,6 +3,7 @@ import { useStore } from "./stores/app-store";
import { useStore as useUserStore } from "./stores/user-store";
import { useStore as useNotesStore } from "./stores/note-store";
import { useStore as useThemeStore } from "./stores/theme-store";
+import { useStore as useAttachmentStore } from "./stores/attachment-store";
import { resetReminders } from "./common/reminders";
import { introduceFeatures, showUpgradeReminderDialogs } from "./common";
import { AppEventManager, AppEvents } from "./common/app-events";
@@ -15,14 +16,12 @@ import useAnnouncements from "./utils/use-announcements";
import {
showAnnouncementDialog,
showBuyDialog,
- showEmailVerificationDialog,
- showFeatureDialog,
showInvalidSystemTimeDialog,
showOnboardingDialog,
} from "./common/dialog-controller";
import useSystemTheme from "./utils/use-system-theme";
import { isTesting } from "./utils/platform";
-import { updateStatus, removeStatus } from "./hooks/use-status";
+import { updateStatus, removeStatus, getStatus } from "./hooks/use-status";
import { showToast } from "./utils/toast";
import { interruptedOnboarding } from "./components/dialogs/onboarding-dialog";
@@ -39,6 +38,7 @@ export default function AppEffects({ setShow }) {
const addReminder = useStore((store) => store.addReminder);
const initUser = useUserStore((store) => store.init);
const initNotes = useNotesStore((store) => store.init);
+ const initAttachments = useAttachmentStore((store) => store.init);
const setIsVaultCreated = useStore((store) => store.setIsVaultCreated);
const setTheme = useThemeStore((store) => store.setTheme);
const followSystemTheme = useThemeStore((store) => store.followSystemTheme);
@@ -64,6 +64,7 @@ export default function AppEffects({ setShow }) {
}
);
+ initAttachments();
refreshNavItems();
initNotes();
(async function () {
@@ -82,6 +83,7 @@ export default function AppEffects({ setShow }) {
};
},
[
+ initAttachments,
sync,
updateLastSynced,
refreshNavItems,
diff --git a/apps/web/src/assets/attachment.svg b/apps/web/src/assets/attachment.svg
new file mode 100644
index 000000000..85d1a4662
--- /dev/null
+++ b/apps/web/src/assets/attachment.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" data-name="Layer 1" viewBox="0 0 782 701.9"><path fill="#f2f2f2" d="m400.5 1.5-25.4 6.6L61.6 88.9 36 95.5a48.2 48.2 0 0 0-34.6 58.6L112 582a48.2 48.2 0 0 0 58.6 34.6l364.3-94a48.2 48.2 0 0 0 34.7-58.6L459 36a48.2 48.2 0 0 0-58.6-34.6Z"/><path fill="#fff" d="m404 15-30.2 7.7-304 78.4-30.2 7.8a34.3 34.3 0 0 0-24.7 41.8l110.4 427.8a34.3 34.3 0 0 0 41.7 24.7h.1l364.3-94a34.3 34.3 0 0 0 24.7-41.7l-110.4-428A34.3 34.3 0 0 0 404 15Z"/><path fill="#f2f2f2" d="M381.2 153.5 197 201a8 8 0 0 1-4-15.5L377.2 138a8 8 0 0 1 4 15.5Zm38.8 17.9-216.3 55.8a8 8 0 1 1-4-15.5L416 155.9a8 8 0 0 1 4 15.5Zm-8.5 99.5-184.3 47.5a8 8 0 1 1-4-15.5l184.3-47.5a8 8 0 1 1 4 15.5Zm38.7 17.9L234 344.6a8 8 0 1 1-4-15.5l216.2-55.8a8 8 0 0 1 4 15.5Zm-8.5 99.5-184.2 47.5a8 8 0 0 1-4-15.5l184.2-47.6a8 8 0 0 1 4 15.6Zm38.8 17.9L264.2 462a8 8 0 1 1-4-15.6l216.3-55.7a8 8 0 0 1 4 15.5Z"/><path fill="#e6e6e6" d="m165.5 249.7-65.2 16.9a3.8 3.8 0 0 1-4.7-2.8l-15-58a3.8 3.8 0 0 1 2.8-4.7l65.2-16.8a3.8 3.8 0 0 1 4.7 2.7l15 58a3.8 3.8 0 0 1-2.8 4.7Zm30.2 117.4L130.5 384a3.8 3.8 0 0 1-4.6-2.7l-15-58a3.8 3.8 0 0 1 2.7-4.8l65.3-16.8a3.8 3.8 0 0 1 4.6 2.8l15 58a3.8 3.8 0 0 1-2.8 4.7ZM226 484.5l-65.2 16.8a3.8 3.8 0 0 1-4.7-2.7l-15-58.1a3.8 3.8 0 0 1 2.8-4.7l65.2-16.8a3.8 3.8 0 0 1 4.7 2.8l15 58a3.8 3.8 0 0 1-2.8 4.7ZM654.7 110H278.3a48.2 48.2 0 0 0-48 48.1V600a48.2 48.2 0 0 0 48 48.1h376.4a48.2 48.2 0 0 0 48-48.1V158a48.2 48.2 0 0 0-48-48.1Z"/><path fill="#fff" d="M654.7 123.8H278.3a34.3 34.3 0 0 0-34.2 34.3V600a34.3 34.3 0 0 0 34.2 34.3h376.4a34.3 34.3 0 0 0 34.2-34.3V158a34.3 34.3 0 0 0-34.2-34.3Z"/><circle cx="694.2" cy="614" r="87.9" fill="#323232"/><path fill="#fff" d="M736.2 602.6h-30.5V572a11.5 11.5 0 0 0-23 0v30.6h-30.5a11.5 11.5 0 0 0 0 22.9h30.5V656a11.5 11.5 0 1 0 23 0v-30.5h30.5a11.5 11.5 0 0 0 0-23Z"/><path fill="#e6e6e6" d="M598 366.7H407.7a8 8 0 1 1 0-16H598a8 8 0 0 1 0 16Zm33 27H407.8a8 8 0 1 1 0-16h223.4a8 8 0 1 1 0 16Zm-33 94.2H407.7a8 8 0 1 1 0-16H598a8 8 0 0 1 0 16Zm33 27.1H407.8a8 8 0 1 1 0-16.1h223.4a8 8 0 1 1 0 16ZM365 406h-67.3a3.8 3.8 0 0 1-3.8-3.9v-60a3.8 3.8 0 0 1 3.8-3.8h67.4a3.8 3.8 0 0 1 3.8 3.9v60a3.8 3.8 0 0 1-3.8 3.8Zm0 121.2h-67.3a3.8 3.8 0 0 1-3.8-3.8v-60a3.8 3.8 0 0 1 3.8-3.8h67.4a3.8 3.8 0 0 1 3.8 3.8v60a3.8 3.8 0 0 1-3.8 3.8Z"/><path fill="#ccc" d="M598.2 231.7H458a8 8 0 0 1 0-16h140.3a8 8 0 0 1 0 16Zm33.1 27.1H457.9a8 8 0 1 1 0-16h173.4a8 8 0 0 1 0 16Z"/><path fill="#323232" d="M426.9 291.5H297.5a3.8 3.8 0 0 1-3.8-3.8v-101a3.8 3.8 0 0 1 3.8-3.8H427a3.8 3.8 0 0 1 3.8 3.9v100.9a3.8 3.8 0 0 1-3.8 3.8Z"/></svg>
\ No newline at end of file
diff --git a/apps/web/src/common/dialogcontroller.js b/apps/web/src/common/dialogcontroller.js
index 5f348529c..cd3bb2b5f 100644
--- a/apps/web/src/common/dialogcontroller.js
+++ b/apps/web/src/common/dialogcontroller.js
@@ -602,6 +602,12 @@ export function showImportDialog() {
));
}
+export function showAttachmentsDialog() {
+ return showDialog((Dialogs, perform) => (
+ <Dialogs.AttachmentsDialog onClose={(res) => perform(res)} />
+ ));
+}
+
export function showOnboardingDialog(type) {
if (!type) return;
diff --git a/apps/web/src/common/list-profiles.js b/apps/web/src/common/list-profiles.js
index d0580d6da..9397b7944 100644
--- a/apps/web/src/common/list-profiles.js
+++ b/apps/web/src/common/list-profiles.js
@@ -12,6 +12,7 @@ import {
} from "./height-calculator";
import { db } from "./db";
import { getTotalNotes } from ".";
+import Attachment from "../components/attachment";
function createProfile(item, itemHeight, estimatedItemHeight) {
return { item, itemHeight, estimatedItemHeight };
@@ -25,7 +26,6 @@ const NotesProfile = createProfile(
item={item}
tags={getTags(item)}
notebook={getNotebook(item.notebooks, context?.type)}
- attachments={getAttachmentsCount(item.id)}
date={getDate(item, type)}
context={context}
/>
@@ -76,6 +76,12 @@ const TrashProfile = createProfile(
Math.max(MAX_HEIGHTS.note, MAX_HEIGHTS.notebook)
);
+const AttachmentProfile = createProfile(
+ (index, item, type) => <Attachment index={index} item={item} />,
+ (item) => getNoteHeight(item),
+ Math.max(MAX_HEIGHTS.note, MAX_HEIGHTS.notebook)
+);
+
const Profiles = {
home: NotesProfile,
notebooks: NotebooksProfile,
@@ -83,7 +89,9 @@ const Profiles = {
tags: TagsProfile,
topics: TopicsProfile,
trash: TrashProfile,
+ attachments: AttachmentProfile,
};
+
export default Profiles;
function getTags(item) {
@@ -117,10 +125,6 @@ function getNotebook(notebooks, contextType) {
};
}
-function getAttachmentsCount(noteId) {
- return db.attachments.ofNote(noteId, "all").length;
-}
-
function getDate(item, groupType) {
const sortBy = db.settings.getGroupOptions(groupType).sortBy;
switch (sortBy) {
diff --git a/apps/web/src/common/multi-select.ts b/apps/web/src/common/multi-select.ts
index b19be826c..2447d604c 100644
--- a/apps/web/src/common/multi-select.ts
+++ b/apps/web/src/common/multi-select.ts
@@ -1,6 +1,7 @@
import { showMultiDeleteConfirmation } from "./dialog-controller";
import { store as noteStore } from "../stores/note-store";
import { store as notebookStore } from "../stores/notebook-store";
+import { store as attachmentStore } from "../stores/attachment-store";
import { db } from "./db";
import { showToast } from "../utils/toast";
import Vault from "./vault";
@@ -88,8 +89,36 @@ async function deleteTopics(notebookId: string, topics: any[]) {
showToast("success", `${topics.length} topics deleted`);
}
+async function deleteAttachments(attachments: any[]) {
+ if (
+ !window.confirm(
+ "Are you sure you want to permanently delete these attachments? This action is IRREVERSIBLE."
+ )
+ )
+ return;
+
+ await TaskManager.startTask({
+ type: "status",
+ id: "deleteAttachments",
+ action: async (report) => {
+ for (let i = 0; i < attachments.length; ++i) {
+ const attachment = attachments[i];
+ report({
+ text: `Deleting ${attachments.length} attachments...`,
+ current: i,
+ total: attachments.length,
+ });
+ console.log(attachment);
+ await attachmentStore.permanentDelete(attachment.metadata.hash);
+ }
+ },
+ });
+ showToast("success", `${attachments.length} attachments deleted`);
+}
+
export const Multiselect = {
moveNotebooksToTrash,
moveNotesToTrash,
deleteTopics,
+ deleteAttachments,
};
diff --git a/apps/web/src/components/attachment/index.js b/apps/web/src/components/attachment/index.js
new file mode 100644
index 000000000..77eff2fa6
--- /dev/null
+++ b/apps/web/src/components/attachment/index.js
@@ -0,0 +1,193 @@
+import { Box, Flex, Text } from "rebass";
+import { formatBytes } from "../../utils/filename";
+import ListItem from "../list-item";
+import * as Icon from "../icons";
+import { downloadAttachment } from "../../common/attachments";
+import { reuploadAttachment } from "../editor/plugins/picker";
+import { store } from "../../stores/attachment-store";
+import { db } from "../../common/db";
+import { Multiselect } from "../../common/multi-select";
+import { showAttachmentsDialog } from "../../common/dialog-controller";
+
+const workStatusMap = {
+ recheck: "Rechecking...",
+ delete: "Deleting...",
+};
+function Attachment({ item, isCompact, index }) {
+ const attachment = item;
+ const status = attachment.status;
+
+ return (
+ <ListItem
+ selectable
+ isCompact={isCompact}
+ item={{ ...attachment, title: attachment.metadata.filename }}
+ title={attachment.metadata.filename}
+ body={
+ attachment.failed && <Text variant={"error"}>{attachment.failed}</Text>
+ }
+ menu={{
+ items: menuItems,
+ extraData: { attachment },
+ }}
+ onClick={() => {
+ if (isCompact) showAttachmentsDialog();
+ }}
+ footer={
+ isCompact ? (
+ <Flex
+ alignItems="center"
+ sx={{
+ fontSize: "subBody",
+ color: "fontTertiary",
+ }}
+ >
+ {status ? (
+ <Text variant="subBody" title={`${status.type}ing`}>
+ {formatBytes(status.loaded, 1)}/{formatBytes(status.total, 1)}
+ </Text>
+ ) : (
+ <Text mr={1}>{formatBytes(attachment.length)}</Text>
+ )}
+ {attachment.failed && (
+ <Icon.AttachmentError
+ sx={{ flexShrink: 0 }}
+ color={"error"}
+ size={13}
+ title={attachment.failed}
+ />
+ )}
+
+ {attachment.isDeleting ? (
+ <Icon.Loading
+ sx={{ flexShrink: 0 }}
+ size={13}
+ title={"Deleting.."}
+ />
+ ) : attachment.dateUploaded ? (
+ <Icon.DoubleCheckmark
+ sx={{ flexShrink: 0 }}
+ color={"primary"}
+ size={13}
+ title={"Uploaded"}
+ />
+ ) : (
+ <Icon.Checkmark
+ sx={{ flexShrink: 0 }}
+ color={"icon"}
+ size={13}
+ title={"Waiting for upload"}
+ />
+ )}
+ </Flex>
+ ) : attachment.working ? (
+ <Flex>
+ <Icon.Loading size={13} />
+ <Text variant={"subBody"} ml={1}>
+ {workStatusMap[attachment.working]}
+ </Text>
+ </Flex>
+ ) : (
+ <Flex flexDirection={"column"}>
+ {status && (
+ <Flex flexDirection={"column"}>
+ <Text variant="subBody">
+ {formatBytes(status.loaded, 1)}/{formatBytes(status.total, 1)}{" "}
+ ({status.type}ing)
+ </Text>
+ <Box
+ sx={{
+ my: 1,
+ bg: "primary",
+ height: "2px",
+ width: `${status.progress}%`,
+ }}
+ />
+ </Flex>
+ )}
+ <Flex
+ alignItems="center"
+ sx={{
+ fontSize: "subBody",
+ color: "fontTertiary",
+ }}
+ >
+ <Text mr={1}>{formatBytes(attachment.length)}</Text>
+ <Text mr={1}>{attachment.metadata.type}</Text>
+ {attachment.noteIds && (
+ <Text mr={1}>{attachment.noteIds.length} notes</Text>
+ )}
+ {attachment.metadata.hash && (
+ <Text mr={0}>{attachment.metadata.hash}</Text>
+ )}
+ </Flex>
+ </Flex>
+ )
+ }
+ index={index}
+ />
+ );
+}
+export default Attachment;
+
+const menuItems = [
+ {
+ key: "recheck",
+ title: () => "Recheck",
+ icon: Icon.DoubleCheckmark,
+ disabled: ({ attachment }) => !attachment.dateUploaded,
+ disableReason: "This attachment is not uploaded yet.",
+ onClick: async ({ items }) => {
+ await store.recheck(items.map((i) => i.metadata.hash));
+ },
+ multiSelect: true,
+ },
+ {
+ key: "rename",
+ title: () => "Rename",
+ icon: Icon.Rename,
+ onClick: async ({ attachment }) => {
+ const newName = prompt("Rename file", attachment.metadata.filename);
+ await store.rename(attachment.metadata.hash, newName);
+ },
+ },
+ {
+ key: "download",
+ title: ({ attachment }) =>
+ attachment.status?.type === "download" ? "Cancel download" : "Download",
+ icon: Icon.Download,
+ disabled: ({ attachment }) => !attachment.dateUploaded,
+ disableReason: "This attachment is not uploaded yet.",
+ onClick: async ({ attachment }) => {
+ const isDownloading = attachment.status?.type === "download";
+ if (isDownloading) {
+ await db.fs.cancel(attachment.metadata.hash, "download");
+ } else await downloadAttachment(attachment.metadata.hash);
+ },
+ },
+ {
+ key: "reupload",
+ title: ({ attachment }) =>
+ attachment.status?.type === "upload" ? "Cancel upload" : "Reupload",
+ icon: Icon.Reupload,
+ onClick: async ({ attachment }) => {
+ const isDownloading = attachment.status?.type === "upload";
+ if (isDownloading) {
+ await db.fs.cancel(attachment.metadata.hash, "upload");
+ } else
+ await reuploadAttachment(
+ attachment.metadata.type,
+ attachment.metadata.hash
+ );
+ },
+ },
+ {
+ key: "permanent-delete",
+ color: "error",
+ iconColor: "error",
+ title: () => "Delete permanently",
+ icon: Icon.DeleteForver,
+ onClick: ({ items }) => Multiselect.deleteAttachments(items),
+ multiSelect: true,
+ },
+];
diff --git a/apps/web/src/components/dialogs/attachments-dialog.js b/apps/web/src/components/dialogs/attachments-dialog.js
new file mode 100644
index 000000000..ad0683347
--- /dev/null
+++ b/apps/web/src/components/dialogs/attachments-dialog.js
@@ -0,0 +1,49 @@
+import { useEffect } from "react";
+import { Flex } from "rebass";
+import { getTotalSize } from "../../common/attachments";
+import { useStore } from "../../stores/attachment-store";
+import { formatBytes } from "../../utils/filename";
+import Field from "../field";
+import ListContainer from "../list-container";
+import AttachmentsPlaceholder from "../placeholders/attachments-placeholder";
+import Dialog from "./dialog";
+
+function AttachmentsDialog({ onClose }) {
+ const attachments = useStore((store) => store.attachments);
+ const refresh = useStore((store) => store.refresh);
+ const filter = useStore((store) => store.filter);
+ useEffect(() => {
+ refresh();
+ }, [refresh]);
+
+ return (
+ <Dialog
+ isOpen={true}
+ width={500}
+ title="Attachments"
+ description={`${attachments.length} attachments | ${formatBytes(
+ getTotalSize(attachments)
+ )} occupied`}
+ onClose={onClose}
+ noScroll
+ negativeButton={{ text: "Close", onClick: onClose }}
+ >
+ <Flex flexDirection={"column"} height={500} px={2}>
+ <Field
+ placeholder="Filter attachments by filename, type or hash"
+ sx={{ mb: 1, px: 1 }}
+ onChange={(e) => filter(e.target.value)}
+ />
+ <ListContainer
+ header={<div />}
+ type="attachments"
+ groupType="attachments"
+ placeholder={AttachmentsPlaceholder}
+ items={attachments}
+ />
+ </Flex>
+ </Dialog>
+ );
+}
+
+export default AttachmentsDialog;
diff --git a/apps/web/src/components/dialogs/dialog.js b/apps/web/src/components/dialogs/dialog.js
index dd86f142d..4f420cad8 100644
--- a/apps/web/src/components/dialogs/dialog.js
+++ b/apps/web/src/components/dialogs/dialog.js
@@ -102,9 +102,13 @@ function Dialog(props) {
)}
</Flex>
<Flex variant="columnFill" sx={{ overflowY: "hidden" }} my={1}>
- <FlexScrollContainer style={{ paddingRight: 20, paddingLeft: 20 }}>
- {props.children}
- </FlexScrollContainer>
+ {props.noScroll ? (
+ props.children
+ ) : (
+ <FlexScrollContainer style={{ paddingRight: 20, paddingLeft: 20 }}>
+ {props.children}
+ </FlexScrollContainer>
+ )}
</Flex>
{(props.positiveButton || props.negativeButton) && (
<Flex
diff --git a/apps/web/src/components/dialogs/index.js b/apps/web/src/components/dialogs/index.js
index 1a8f577ea..23cadea91 100644
--- a/apps/web/src/components/dialogs/index.js
+++ b/apps/web/src/components/dialogs/index.js
@@ -15,6 +15,7 @@ import ReminderDialog from "./reminderdialog";
import AnnouncementDialog from "./announcementdialog";
import IssueDialog from "./issuedialog";
import OnboardingDialog from "./onboarding-dialog";
+import AttachmentsDialog from "./attachmentsdialog";
const Dialogs = {
AddNotebookDialog,
@@ -34,5 +35,6 @@ const Dialogs = {
IssueDialog,
ImportDialog,
OnboardingDialog,
+ AttachmentsDialog,
};
export default Dialogs;
diff --git a/apps/web/src/components/icons/index.js b/apps/web/src/components/icons/index.js
index 2a0d79197..64db9cb6b 100644
--- a/apps/web/src/components/icons/index.js
+++ b/apps/web/src/components/icons/index.js
@@ -137,6 +137,12 @@ import {
mdiCurrencyUsd,
mdiAlertOctagonOutline,
mdiGithub,
+ mdiAlertCircleOutline,
+ mdiProgressUpload,
+ mdiRenameBox,
+ mdiFormTextbox,
+ mdiCheckAll,
+ mdiCloudOffOutline,
} from "@mdi/js";
import { useTheme } from "emotion-theming";
import { AnimatedFlex } from "../animated";
@@ -248,12 +254,14 @@ export const DarkMode = createIcon(mdiWeatherNight);
export const LightMode = createIcon(mdiWeatherSunny);
export const Theme = createIcon(mdiThemeLightDark);
export const Checkmark = createIcon(mdiCheck);
+export const DoubleCheckmark = createIcon(mdiCheckAll);
export const CheckCircle = createIcon(mdiCheckCircle);
export const CheckCircleOutline = createIcon(mdiCheckCircleOutline);
export const Properties = createIcon(mdiDotsVertical);
export const Markdown = createIcon(mdiLanguageMarkdownOutline);
export const PDF = createIcon(mdiFilePdfBox);
export const Attachment = createIcon(mdiAttachment);
+export const AttachmentError = createIcon(mdiAlertCircleOutline);
export const Write = createIcon(mdiPencilOutline);
export const HTML = createIcon(mdiLanguageHtml5);
export const Text = createIcon(mdiFormatTitle);
@@ -345,3 +353,8 @@ export const OrderOldestNewest = createIcon(mdiOrderNumericDescending);
export const OrderNewestOldest = createIcon(mdiOrderNumericAscending);
export const Saved = createIcon(mdiContentSaveCheckOutline);
export const NotSaved = createIcon(mdiContentSaveAlertOutline);
+
+export const Reupload = createIcon(mdiProgressUpload);
+export const Rename = createIcon(mdiFormTextbox);
+export const Upload = createIcon(mdiCloudOffOutline);
+export const Uploaded = createIcon(mdiCloudCheckOutline);
diff --git a/apps/web/src/components/list-container/index.js b/apps/web/src/components/list-container/index.js
index ebcfd21a2..354b4f165 100644
--- a/apps/web/src/components/list-container/index.js
+++ b/apps/web/src/components/list-container/index.js
@@ -33,8 +33,7 @@ function ListContainer(props) {
<Flex variant="columnFill">
{!props.items.length && props.placeholder ? (
<>
- {header}
- <ReminderBar />
+ {header || <ReminderBar />}
<Flex variant="columnCenterFill">
{props.isLoading ? <Icon.Loading rotate /> : <props.placeholder />}
</Flex>
diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.js
index a2f38e321..aaaffb108 100644
--- a/apps/web/src/components/note/index.js
+++ b/apps/web/src/components/note/index.js
@@ -5,6 +5,7 @@ import TimeAgo from "../time-ago";
import ListItem from "../list-item";
import { showMoveNoteDialog } from "../../common/dialog-controller";
import { store, useStore } from "../../stores/note-store";
+import { useStore as useAttachmentStore } from "../../stores/attachment-store";
import { db } from "../../common/db";
import { showUnpinnedToast } from "../../common/toasts";
import { showToast } from "../../utils/toast";
@@ -16,10 +17,17 @@ import { exportNotes } from "../../common/export";
import { Multiselect } from "../../common/multi-select";
function Note(props) {
- const { tags, notebook, item, index, context, attachments, date } = props;
+ const { tags, notebook, item, index, context, date } = props;
const note = item;
const isOpened = useStore((store) => store.selectedNote === note.id);
const isCompact = useStore((store) => store.viewMode === "compact");
+ const attachments = useAttachmentStore((store) =>
+ store.attachments.filter((a) => a.noteIds.includes(note.id))
+ );
+ const failed = useMemo(
+ () => attachments.filter((a) => a.failed),
+ [attachments]
+ );
const primary = useMemo(() => {
if (!note.color) return "primary";
@@ -112,10 +120,17 @@ function Note(props) {
mr={1}
/>
- {attachments > 0 && (
+ {attachments.length > 0 && (
<Flex mr={1}>
<Icon.Attachment size={13} color="fontTertiary" />
- <Text ml={"2px"}>{attachments}</Text>
+ <Text ml={"2px"}>{attachments.length}</Text>
+ </Flex>
+ )}
+
+ {failed.length > 0 && (
+ <Flex mr={1} title={`Errors in ${failed.length} attachments.`}>
+ <Icon.AttachmentError size={13} color="error" />
+ <Text ml={"2px"}>{failed.length}</Text>
</Flex>
)}
@@ -180,7 +195,6 @@ export default React.memo(Note, function (prevProps, nextProps) {
prevItem.locked === nextItem.locked &&
prevItem.conflicted === nextItem.conflicted &&
prevItem.color === nextItem.color &&
- prevProps.attachments === nextProps.attachments &&
prevProps.notebook?.dateEdited === nextProps.notebook?.dateEdited &&
JSON.stringify(prevProps.tags) === JSON.stringify(nextProps.tags)
);
diff --git a/apps/web/src/components/placeholders/attachments-placeholder.js b/apps/web/src/components/placeholders/attachments-placeholder.js
new file mode 100644
index 000000000..f011ad94c
--- /dev/null
+++ b/apps/web/src/components/placeholders/attachments-placeholder.js
@@ -0,0 +1,12 @@
+import Placeholder from "./index";
+
+function AttachmentsPlaceholder() {
+ return (
+ <Placeholder
+ id="attachments"
+ title="Your attachments"
+ text="You haven't attached any files yet."
+ />
+ );
+}
+export default AttachmentsPlaceholder;
diff --git a/apps/web/src/components/placeholders/loader.js b/apps/web/src/components/placeholders/loader.js
index 79f3c5901..44c237125 100644
--- a/apps/web/src/components/placeholders/loader.js
+++ b/apps/web/src/components/placeholders/loader.js
@@ -6,6 +6,7 @@ import { ReactComponent as Search } from "../../assets/search.svg";
import { ReactComponent as Tag } from "../../assets/tag.svg";
import { ReactComponent as Trash } from "../../assets/trash.svg";
import { ReactComponent as Fav } from "../../assets/fav.svg";
+import { ReactComponent as Attachment } from "../../assets/attachment.svg";
const Placeholders = {
note: Note,
@@ -16,6 +17,7 @@ const Placeholders = {
tag: Tag,
trash: Trash,
favorites: Fav,
+ attachments: Attachment,
};
export default function PlaceholderLoader({ name, onLoad, ...restProps }) {
diff --git a/apps/web/src/components/properties/index.js b/apps/web/src/components/properties/index.js
index ccbbf8c16..301dd19f9 100644
--- a/apps/web/src/components/properties/index.js
+++ b/apps/web/src/components/properties/index.js
@@ -1,21 +1,22 @@
import React, { useCallback, useEffect, useState } from "react";
import * as Icon from "../icons";
-import { Flex, Text, Button, Box } from "rebass";
+import { Flex, Text } from "rebass";
import { useStore } from "../../stores/editor-store";
import { COLORS } from "../../common";
-import { AppEventManager, AppEvents } from "../../common/app-events";
import { db } from "../../common/db";
import { useStore as useAppStore } from "../../stores/app-store";
+import { useStore as useAttachmentStore } from "../../stores/attachment-store";
import { AnimatedFlex } from "../animated";
import Toggle from "./toggle";
import { navigate } from "../../navigation";
import IconTag from "../icon-tag";
-import { formatBytes, truncateFilename } from "../../utils/filename";
import ScrollContainer from "../scroll-container";
-import { downloadAttachment } from "../../common/attachments";
import { formatDate } from "notes-core/utils/date";
import Vault from "../../common/vault";
import TimeAgo from "../time-ago";
+import Attachment from "../attachment";
+import { formatBytes } from "../../utils/filename";
+import { getTotalSize } from "../../common/attachments";
const tools = [
{ key: "pinned", icon: Icon.Pin, label: "Pin" },
@@ -42,7 +43,6 @@ const metadataItems = [
];
function Properties() {
- const [attachmentsStatus, setAttachmentsStatus] = useState({});
const [versionHistory, setVersionHistory] = useState([]);
const toggleLocked = useStore((store) => store.toggleLocked);
@@ -52,16 +52,11 @@ function Properties() {
const setColor = useStore((store) => store.setColor);
const toggleProperties = useStore((store) => store.toggleProperties);
const isFocusMode = useAppStore((store) => store.isFocusMode);
-
const session = useStore((store) => store.session);
- const {
- id: sessionId,
- color,
- notebooks,
- attachments,
- sessionType,
- dateCreated,
- } = session;
+ const attachments = useAttachmentStore((store) =>
+ store.attachments.filter((a) => a.noteIds.includes(session.id))
+ );
+ const { id: sessionId, color, notebooks, sessionType, dateCreated } = session;
const isPreviewMode = sessionType === "preview";
const changeState = useCallback(
@@ -82,31 +77,6 @@ function Properties() {
[setSession, toggleLocked]
);
- useEffect(() => {
- const event = AppEventManager.subscribe(
- AppEvents.UPDATE_ATTACHMENT_PROGRESS,
- ({ hash, type, total, loaded }) => {
- if (!attachments.find((a) => a.metadata.hash === hash)) return;
- const percent = Math.round((loaded / total) * 100);
- setAttachmentsStatus((status) => {
- const copy = { ...status };
- copy[hash] =
- percent >= 100
- ? null
- : {
- type,
- loaded,
- progress: percent,
- };
- return copy;
- });
- }
- );
- return () => {
- event.unsubscribe();
- };
- }, [attachments]);
-
useEffect(() => {
if (!sessionId) return;
@@ -290,100 +260,13 @@ function Properties() {
{attachments?.length > 0 && (
<Card
title="Attachments"
- subtitle={`${attachments.length} attachments`}
+ subtitle={`${attachments.length} attachments | ${formatBytes(
+ getTotalSize(attachments)
+ )} occupied`}
>
- {attachments.map((attachment) => {
- const attachmentStatus =
- attachmentsStatus[attachment.metadata.hash];
- return (
- <Flex
- key={attachment.id}
- py={1}
- px={2}
- sx={{
- borderBottom: "1px solid var(--border)",
- ":last-of-type": { borderBottom: "none" },
- ":hover .attachment-download-btn": {
- opacity: 1,
- },
- }}
- title={attachment.metadata.filename}
- alignItems="center"
- justifyContent="space-between"
- >
- <Flex flexDirection="column" flex={1}>
- <Text
- variant="body"
- fontSize="subtitle"
- sx={{
- whiteSpace: "nowrap",
- textOverflow: "ellipsis",
- overflow: "hidden",
- }}
- >
- {truncateFilename(attachment.metadata.filename)}
- </Text>
- {attachmentStatus && (
- <Box
- sx={{
- my: 1,
- bg: "primary",
- height: "2px",
- width: `${attachmentStatus.progress}%`,
- }}
- />
- )}
- <Flex mt={1}>
- {attachmentStatus ? (
- <Text variant="subBody">
- {formatBytes(attachmentStatus.loaded, 1)} of{" "}
- {formatBytes(attachment.length, 1)} (
- {attachmentStatus.type}ing)
- </Text>
- ) : (
- <Text variant="subBody">
- {formatBytes(attachment.length, 1)}
- </Text>
- )}
- </Flex>
- </Flex>
-
- <Box>
- {attachmentStatus ? (
- <Button
- title="Cancel download"
- variant="tool"
- p={1}
- m={1}
- sx={{ ":hover": { bg: "hover" } }}
- onClick={async () => {
- await db.fs.cancel(
- attachment.metadata.hash,
- "download"
- );
- }}
- >
- <Icon.Close size={16} />
- </Button>
- ) : (
- <Button
- className="attachment-download-btn"
- title="Download attachment"
- variant="tool"
- p={1}
- m={1}
- sx={{ ":hover": { bg: "hover" }, opacity: 0 }}
- onClick={async () => {
- await downloadAttachment(attachment.metadata.hash);
- }}
- >
- <Icon.Download size={16} />
- </Button>
- )}
- </Box>
- </Flex>
- );
- })}
+ {attachments.map((attachment, i) => (
+ <Attachment item={attachment} index={i} isCompact />
+ ))}
</Card>
)}
<Card
diff --git a/apps/web/src/views/settings.js b/apps/web/src/views/settings.js
index 9845c5d83..fccba64bd 100644
--- a/apps/web/src/views/settings.js
+++ b/apps/web/src/views/settings.js
@@ -19,6 +19,7 @@ import {
showLoadingDialog,
showBuyDialog,
showPasswordDialog,
+ showAttachmentsDialog,
} from "../common/dialog-controller";
import { createBackup, SUBSCRIPTION_STATUS, verifyAccount } from "../common";
import { db } from "../common/db";
@@ -245,6 +246,12 @@ function Settings(props) {
tip="Set a new password for your account"
/>
</Button>
+ <Button variant="list" onClick={() => showAttachmentsDialog()}>
+ <Tip
+ text="Manage attachments"
+ tip="Re-upload, delete & manage your attachments."
+ />
+ </Button>
<Button variant="list" onClick={() => sync(true, true)}>
<Tip
text="Having problems with syncing?"