Skip to content

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

fix attachment manager bugs

ammarahm-ed60a46f8b27bc
diff --git a/apps/mobile/src/components/attachments/actions.js b/apps/mobile/src/components/attachments/actions.js
index 6b81b7af5..0f7188b99 100644
--- a/apps/mobile/src/components/attachments/actions.js
+++ b/apps/mobile/src/components/attachments/actions.js
@@ -1,6 +1,7 @@
import Clipboard from '@react-native-clipboard/clipboard';
-import React, { useState } from 'react';
+import React, { useEffect, useState } from 'react';
import { View } from 'react-native';
+import { ScrollView } from 'react-native-gesture-handler';
import picker from '../../screens/editor/tiny/toolbar/picker';
import { eSendEvent, presentSheet, ToastEvent } from '../../services/event-manager';
import PremiumService from '../../services/premium';
@@ -8,24 +9,31 @@ import { useAttachmentStore } from '../../stores/stores';
import { useThemeStore } from '../../stores/theme';
import { formatBytes } from '../../utils';
import { db } from '../../utils/database';
-import { eCloseProgressDialog } from '../../utils/events';
+import { eCloseAttachmentDialog, eCloseProgressDialog } from '../../utils/events';
import filesystem from '../../utils/filesystem';
import { useAttachmentProgress } from '../../utils/hooks/use-attachment-progress';
import { SIZE } from '../../utils/size';
+import { sleep } from '../../utils/time';
import { Dialog } from '../dialog';
import { presentDialog } from '../dialog/functions';
+import { openNote } from '../list-items/note/wrapper';
import { DateMeta } from '../properties/date-meta';
import { Button } from '../ui/button';
import { Notice } from '../ui/notice';
+import { PressableButton } from '../ui/pressable';
import Heading from '../ui/typography/heading';
import Paragraph from '../ui/typography/paragraph';
-const Actions = ({ attachment, setAttachments }) => {
+const Actions = ({ attachment, setAttachments, fwdRef }) => {
const colors = useThemeStore(state => state.colors);
const contextId = attachment.metadata.hash;
const [filename, setFilename] = useState(attachment.metadata.filename);
const [currentProgress, setCurrentProgress] = useAttachmentProgress(attachment);
const [failed, setFailed] = useState(attachment.failed);
+ const [notes, setNotes] = useState([]);
+ const [loading, setLoading] = useState({
+ name: null
+ });
const actions = [
{
@@ -63,11 +71,26 @@ const Actions = ({ attachment, setAttachments }) => {
{
name: 'Run file check',
onPress: async () => {
+ setLoading({
+ name: 'Run file check'
+ });
let res = await filesystem.checkAttachment(attachment.metadata.hash);
if (res.failed) {
db.attachments.markAsFailed(attachment.id, res.failed);
setFailed(res.failed);
+ } else {
+ setFailed(null);
+ db.attachments.markAsFailed(attachment.id, null);
}
+ ToastEvent.show({
+ heading: 'File check passed',
+ type: 'success',
+ context: 'local'
+ });
+ setAttachments([...db.attachments.all]);
+ setLoading({
+ name: null
+ });
},
icon: 'file-check'
},
@@ -105,16 +128,44 @@ const Actions = ({ attachment, setAttachments }) => {
icon: 'delete-outline'
}
];
- console.log(attachment);
+
+ const getNotes = () => {
+ let allNotes = db.notes.all;
+ let attachmentNotes = attachment.noteIds?.map(id => {
+ let index = allNotes?.findIndex(note => id === note.id);
+ if (index !== -1) {
+ return allNotes[index];
+ } else {
+ return {
+ type: 'notfound',
+ title: `Note with id ${id} does not exist.`,
+ id: id
+ };
+ }
+ });
+ return attachmentNotes;
+ };
+
+ useEffect(() => {
+ setNotes(getNotes());
+ }, [attachment]);
return (
- <View>
+ <ScrollView
+ onMomentumScrollEnd={() => {
+ fwdRef?.current?.handleChildScrollEnd();
+ }}
+ nestedScrollEnabled={true}
+ style={{
+ maxHeight: '100%'
+ }}
+ >
<Dialog context={contextId} />
<View
style={{
- marginBottom: 12,
borderBottomWidth: 1,
- borderBottomColor: colors.nav
+ borderBottomColor: colors.nav,
+ marginBottom: notes && notes.length > 0 ? 0 : 12
}}
>
<Heading
@@ -180,6 +231,50 @@ const Actions = ({ attachment, setAttachments }) => {
<DateMeta item={attachment} />
</View>
+ {notes && notes.length > 0 ? (
+ <View
+ style={{
+ borderBottomWidth: 1,
+ borderBottomColor: colors.nav,
+ marginBottom: 12,
+ paddingVertical: 12
+ }}
+ >
+ <>
+ <Heading
+ style={{
+ paddingHorizontal: 12
+ }}
+ size={SIZE.sm}
+ >
+ List of notes:
+ </Heading>
+
+ {notes.map(item => (
+ <PressableButton
+ onPress={async () => {
+ if (item.type === 'notfound') return;
+ eSendEvent(eCloseProgressDialog, contextId);
+ await sleep(150);
+ eSendEvent(eCloseAttachmentDialog);
+ await sleep(300);
+ openNote(item, item.type === 'trash');
+ }}
+ customStyle={{
+ paddingVertical: 12,
+ alignItems: 'flex-start',
+
+ paddingHorizontal: 12
+ }}
+ key={item.id}
+ >
+ <Paragraph size={SIZE.xs + 1}>{item.title}</Paragraph>
+ </PressableButton>
+ ))}
+ </>
+ </View>
+ ) : null}
+
{actions.map(item => (
<Button
key={item.name}
@@ -193,6 +288,7 @@ const Actions = ({ attachment, setAttachments }) => {
onPress={item.onPress}
title={item.name}
icon={item.icon}
+ loading={loading?.name === item.name}
type={item.on ? 'shade' : 'gray'}
fontSize={SIZE.sm}
style={{
@@ -217,14 +313,14 @@ const Actions = ({ attachment, setAttachments }) => {
/>
) : null}
</View>
- </View>
+ </ScrollView>
);
};
Actions.present = (attachment, set, context) => {
presentSheet({
context: context,
- component: <Actions setAttachments={set} attachment={attachment} />
+ component: ref => <Actions fwdRef={ref} setAttachments={set} attachment={attachment} />
});
};
diff --git a/apps/mobile/src/components/attachments/index.js b/apps/mobile/src/components/attachments/index.js
index 1f9561c65..0bf17b7c9 100644
--- a/apps/mobile/src/components/attachments/index.js
+++ b/apps/mobile/src/components/attachments/index.js
@@ -106,8 +106,10 @@ export const AttachmentDialog = () => {
let result = await filesystem.checkAttachment(attachment.metadata.hash);
if (result.failed) {
db.attachments.markAsFailed(attachment.metadata.hash, result.failed);
- setAttachments([...db.attachments.all]);
+ } else {
+ db.attachments.markAsFailed(attachment.id, null);
}
+ setAttachments([...db.attachments.all]);
}
setLoading(false);
}
diff --git a/apps/mobile/src/components/listitems/note/wrapper.js b/apps/mobile/src/components/listitems/note/wrapper.js
index c65dc2856..87b48e708 100644
--- a/apps/mobile/src/components/listitems/note/wrapper.js
+++ b/apps/mobile/src/components/listitems/note/wrapper.js
@@ -12,87 +12,88 @@ import { tabBarRef } from '../../../utils/global-refs';
import { presentDialog } from '../../dialog/functions';
import SelectionWrapper from '../selection-wrapper';
-export const NoteWrapper = React.memo(
- ({ item, index, tags, dateBy }) => {
- const isTrash = item.type === 'trash';
- const setSelectedItem = useSelectionStore(state => state.setSelectedItem);
- const onPress = async () => {
- let _note = item;
- if (!isTrash) {
- _note = db.notes.note(item.id).data;
- }
+export const openNote = async (item, isTrash, setSelectedItem) => {
+ let _note = item;
+ if (!isTrash) {
+ _note = db.notes.note(item.id).data;
+ }
- if (history.selectedItemsList.length > 0 && history.selectionMode) {
- setSelectedItem(_note);
- return;
- } else {
- history.selectedItemsList = [];
- }
+ if (history.selectedItemsList.length > 0 && history.selectionMode) {
+ setSelectedItem && setSelectedItem(_note);
+ return;
+ } else {
+ history.selectedItemsList = [];
+ }
- if (_note.conflicted) {
- eSendEvent(eShowMergeDialog, _note);
- return;
- }
+ if (_note.conflicted) {
+ eSendEvent(eShowMergeDialog, _note);
+ return;
+ }
- if (_note.locked) {
- openVault({
- item: _note,
- novault: true,
- locked: true,
- goToEditor: true,
- title: 'Open note',
- description: 'Unlock note to open it in editor.'
+ if (_note.locked) {
+ openVault({
+ item: _note,
+ novault: true,
+ locked: true,
+ goToEditor: true,
+ title: 'Open note',
+ description: 'Unlock note to open it in editor.'
+ });
+ return;
+ }
+ if (isTrash) {
+ presentDialog({
+ title: `Restore ${item.itemType}`,
+ paragraph: `Restore or delete ${item.itemType} forever`,
+ positiveText: 'Restore',
+ negativeText: 'Delete',
+ positivePress: async () => {
+ await db.trash.restore(item.id);
+ Navigation.setRoutesToUpdate([
+ Navigation.routeNames.Tags,
+ Navigation.routeNames.Notes,
+ Navigation.routeNames.Notebooks,
+ Navigation.routeNames.NotesPage,
+ Navigation.routeNames.Favorites,
+ Navigation.routeNames.Trash
+ ]);
+ useSelectionStore.getState().setSelectionMode(false);
+ ToastEvent.show({
+ heading: 'Restore successful',
+ type: 'success'
});
- return;
- }
- if (isTrash) {
- presentDialog({
- title: `Restore ${item.itemType}`,
- paragraph: `Restore or delete ${item.itemType} forever`,
- positiveText: 'Restore',
- negativeText: 'Delete',
- positivePress: async () => {
- await db.trash.restore(item.id);
- Navigation.setRoutesToUpdate([
- Navigation.routeNames.Tags,
- Navigation.routeNames.Notes,
- Navigation.routeNames.Notebooks,
- Navigation.routeNames.NotesPage,
- Navigation.routeNames.Favorites,
- Navigation.routeNames.Trash
- ]);
- useSelectionStore.getState().setSelectionMode(false);
- ToastEvent.show({
- heading: 'Restore successful',
- type: 'success'
- });
- },
- onClose: async () => {
- await db.trash.delete(item.id);
- useTrashStore.getState().setTrash();
- useSelectionStore.getState().setSelectionMode(false);
- ToastEvent.show({
- heading: 'Permanantly deleted items',
- type: 'success',
- context: 'local'
- });
- }
+ },
+ onClose: async () => {
+ await db.trash.delete(item.id);
+ useTrashStore.getState().setTrash();
+ useSelectionStore.getState().setSelectionMode(false);
+ ToastEvent.show({
+ heading: 'Permanantly deleted items',
+ type: 'success',
+ context: 'local'
});
- } else {
- useEditorStore.getState().setReadonly(_note?.readonly);
- eSendEvent(eOnLoadNote, _note);
- if (!DDS.isTab) {
- tabBarRef.current?.goToPage(1);
- }
}
- };
+ });
+ } else {
+ useEditorStore.getState().setReadonly(_note?.readonly);
+ eSendEvent(eOnLoadNote, _note);
+ if (!DDS.isTab) {
+ tabBarRef.current?.goToPage(1);
+ }
+ }
+};
+
+export const NoteWrapper = React.memo(
+ ({ item, index, tags, dateBy }) => {
+ const isTrash = item.type === 'trash';
+ const setSelectedItem = useSelectionStore(state => state.setSelectedItem);
return (
<SelectionWrapper
index={index}
height={100}
testID={notesnook.ids.note.get(index)}
- onPress={onPress}
+ onPress={() => openNote(item, isTrash, setSelectedItem)}
item={item}
>
<NoteItem item={item} dateBy={dateBy} tags={tags} isTrash={isTrash} />
diff --git a/apps/mobile/src/utils/filesystem/download.js b/apps/mobile/src/utils/filesystem/download.js
index cbf5d6cab..f0946c27f 100644
--- a/apps/mobile/src/utils/filesystem/download.js
+++ b/apps/mobile/src/utils/filesystem/download.js
@@ -156,7 +156,9 @@ export async function getUploadedFileSize(hash) {
headers: { Authorization: `Bearer ${token}` }
});
- const contentLength = parseInt(attachmentInfo.headers['content-length']);
+ const contentLength = parseInt(attachmentInfo.headers?.get('content-length'));
+ console.log('contentLength:', contentLength, attachmentInfo.headers);
+
return isNaN(contentLength) ? 0 : contentLength;
}