2022-03-07 15:19:07 +05:00
|
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
2022-03-14 16:28:31 +05:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-03-07 15:19:07 +05:00
|
|
|
import { View } from 'react-native';
|
2022-03-14 16:28:31 +05:00
|
|
|
import { ScrollView } from 'react-native-gesture-handler';
|
2022-05-20 14:49:38 +05:00
|
|
|
import picker from '../../screens/editor/tiptap/picker';
|
2022-03-07 15:19:07 +05:00
|
|
|
import { eSendEvent, presentSheet, ToastEvent } from '../../services/event-manager';
|
2022-03-09 19:04:01 +05:00
|
|
|
import PremiumService from '../../services/premium';
|
2022-04-24 05:59:14 +05:00
|
|
|
import { useAttachmentStore } from '../../stores/use-attachment-store';
|
|
|
|
|
import { useThemeStore } from '../../stores/use-theme-store';
|
2022-03-07 15:19:07 +05:00
|
|
|
import { formatBytes } from '../../utils';
|
|
|
|
|
import { db } from '../../utils/database';
|
2022-03-14 16:28:31 +05:00
|
|
|
import { eCloseAttachmentDialog, eCloseProgressDialog } from '../../utils/events';
|
2022-03-07 15:19:07 +05:00
|
|
|
import filesystem from '../../utils/filesystem';
|
|
|
|
|
import { useAttachmentProgress } from '../../utils/hooks/use-attachment-progress';
|
|
|
|
|
import { SIZE } from '../../utils/size';
|
2022-03-14 16:28:31 +05:00
|
|
|
import { sleep } from '../../utils/time';
|
2022-03-07 15:19:07 +05:00
|
|
|
import { Dialog } from '../dialog';
|
|
|
|
|
import { presentDialog } from '../dialog/functions';
|
2022-03-14 16:28:31 +05:00
|
|
|
import { openNote } from '../list-items/note/wrapper';
|
2022-03-07 15:19:07 +05:00
|
|
|
import { DateMeta } from '../properties/date-meta';
|
|
|
|
|
import { Button } from '../ui/button';
|
|
|
|
|
import { Notice } from '../ui/notice';
|
2022-03-14 16:28:31 +05:00
|
|
|
import { PressableButton } from '../ui/pressable';
|
2022-03-07 15:19:07 +05:00
|
|
|
import Heading from '../ui/typography/heading';
|
|
|
|
|
import Paragraph from '../ui/typography/paragraph';
|
|
|
|
|
|
2022-03-14 16:28:31 +05:00
|
|
|
const Actions = ({ attachment, setAttachments, fwdRef }) => {
|
2022-03-07 15:19:07 +05:00
|
|
|
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);
|
2022-03-14 16:28:31 +05:00
|
|
|
const [notes, setNotes] = useState([]);
|
|
|
|
|
const [loading, setLoading] = useState({
|
|
|
|
|
name: null
|
|
|
|
|
});
|
2022-03-07 15:19:07 +05:00
|
|
|
|
|
|
|
|
const actions = [
|
|
|
|
|
{
|
|
|
|
|
name: 'Download',
|
|
|
|
|
onPress: async () => {
|
|
|
|
|
if (currentProgress) {
|
|
|
|
|
await db.fs.cancel(attachment.metadata.hash, 'download');
|
|
|
|
|
useAttachmentStore.getState().remove(attachment.metadata.hash);
|
|
|
|
|
}
|
|
|
|
|
filesystem.downloadAttachment(attachment.metadata.hash, false);
|
|
|
|
|
eSendEvent(eCloseProgressDialog, contextId);
|
|
|
|
|
},
|
|
|
|
|
icon: 'download'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Reupload',
|
|
|
|
|
onPress: async () => {
|
2022-03-11 14:52:05 +05:00
|
|
|
if (!PremiumService.get()) {
|
2022-03-09 19:04:01 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Upgrade to pro',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local'
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-07 15:19:07 +05:00
|
|
|
await picker.pick({
|
|
|
|
|
reupload: true,
|
|
|
|
|
hash: attachment.metadata.hash,
|
|
|
|
|
context: contextId,
|
|
|
|
|
type: attachment.metadata.type
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
icon: 'upload'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Run file check',
|
|
|
|
|
onPress: async () => {
|
2022-03-14 16:28:31 +05:00
|
|
|
setLoading({
|
|
|
|
|
name: 'Run file check'
|
|
|
|
|
});
|
2022-03-07 15:19:07 +05:00
|
|
|
let res = await filesystem.checkAttachment(attachment.metadata.hash);
|
|
|
|
|
if (res.failed) {
|
|
|
|
|
db.attachments.markAsFailed(attachment.id, res.failed);
|
|
|
|
|
setFailed(res.failed);
|
2022-03-14 16:28:31 +05:00
|
|
|
} else {
|
|
|
|
|
setFailed(null);
|
|
|
|
|
db.attachments.markAsFailed(attachment.id, null);
|
2022-03-07 15:19:07 +05:00
|
|
|
}
|
2022-03-14 16:28:31 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'File check passed',
|
|
|
|
|
type: 'success',
|
|
|
|
|
context: 'local'
|
|
|
|
|
});
|
|
|
|
|
setAttachments([...db.attachments.all]);
|
|
|
|
|
setLoading({
|
|
|
|
|
name: null
|
|
|
|
|
});
|
2022-03-07 15:19:07 +05:00
|
|
|
},
|
|
|
|
|
icon: 'file-check'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Rename',
|
|
|
|
|
onPress: () => {
|
|
|
|
|
presentDialog({
|
|
|
|
|
context: contextId,
|
|
|
|
|
input: true,
|
|
|
|
|
title: 'Rename file',
|
|
|
|
|
paragraph: 'Enter a new name for the file',
|
|
|
|
|
defaultValue: attachment.metadata.filename,
|
|
|
|
|
positivePress: async value => {
|
|
|
|
|
if (value && value.length > 0) {
|
|
|
|
|
await db.attachments.add({
|
|
|
|
|
hash: attachment.metadata.hash,
|
|
|
|
|
filename: value
|
|
|
|
|
});
|
|
|
|
|
setFilename(value);
|
|
|
|
|
setAttachments([...db.attachments.all]);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
positiveText: 'Rename'
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
icon: 'form-textbox'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Delete',
|
|
|
|
|
onPress: async () => {
|
|
|
|
|
await db.attachments.remove(attachment.metadata.hash, false);
|
|
|
|
|
setAttachments([...db.attachments.all]);
|
|
|
|
|
eSendEvent(eCloseProgressDialog, contextId);
|
|
|
|
|
},
|
|
|
|
|
icon: 'delete-outline'
|
|
|
|
|
}
|
|
|
|
|
];
|
2022-03-14 16:28:31 +05:00
|
|
|
|
|
|
|
|
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]);
|
2022-03-07 15:19:07 +05:00
|
|
|
|
|
|
|
|
return (
|
2022-03-14 16:28:31 +05:00
|
|
|
<ScrollView
|
|
|
|
|
onMomentumScrollEnd={() => {
|
|
|
|
|
fwdRef?.current?.handleChildScrollEnd();
|
|
|
|
|
}}
|
|
|
|
|
nestedScrollEnabled={true}
|
|
|
|
|
style={{
|
|
|
|
|
maxHeight: '100%'
|
|
|
|
|
}}
|
|
|
|
|
>
|
2022-03-07 15:19:07 +05:00
|
|
|
<Dialog context={contextId} />
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
borderBottomWidth: 1,
|
2022-03-14 16:28:31 +05:00
|
|
|
borderBottomColor: colors.nav,
|
|
|
|
|
marginBottom: notes && notes.length > 0 ? 0 : 12
|
2022-03-07 15:19:07 +05:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Heading
|
|
|
|
|
style={{
|
|
|
|
|
paddingHorizontal: 12
|
|
|
|
|
}}
|
|
|
|
|
size={SIZE.lg}
|
|
|
|
|
>
|
|
|
|
|
{filename}
|
|
|
|
|
</Heading>
|
|
|
|
|
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
marginBottom: 10,
|
|
|
|
|
paddingHorizontal: 12
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Paragraph
|
|
|
|
|
size={SIZE.xs + 1}
|
|
|
|
|
style={{
|
|
|
|
|
marginRight: 10
|
|
|
|
|
}}
|
|
|
|
|
color={colors.icon}
|
|
|
|
|
>
|
|
|
|
|
{attachment.metadata.type}
|
|
|
|
|
</Paragraph>
|
|
|
|
|
<Paragraph
|
|
|
|
|
style={{
|
|
|
|
|
marginRight: 10
|
|
|
|
|
}}
|
|
|
|
|
size={SIZE.xs + 1}
|
|
|
|
|
color={colors.icon}
|
|
|
|
|
>
|
|
|
|
|
{formatBytes(attachment.length)}
|
|
|
|
|
</Paragraph>
|
|
|
|
|
|
|
|
|
|
<Paragraph
|
|
|
|
|
style={{
|
|
|
|
|
marginRight: 10
|
|
|
|
|
}}
|
|
|
|
|
size={SIZE.xs + 1}
|
|
|
|
|
color={colors.icon}
|
|
|
|
|
>
|
|
|
|
|
{attachment.noteIds.length} note{attachment.noteIds.length > 1 ? 's' : ''}
|
|
|
|
|
</Paragraph>
|
|
|
|
|
<Paragraph
|
|
|
|
|
onPress={() => {
|
|
|
|
|
Clipboard.setString(attachment.metadata.hash);
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
type: 'success',
|
|
|
|
|
heading: 'Attachment hash copied',
|
|
|
|
|
context: 'local'
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
size={SIZE.xs + 1}
|
|
|
|
|
color={colors.icon}
|
|
|
|
|
>
|
|
|
|
|
{attachment.metadata.hash}
|
|
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
|
|
|
|
|
|
|
|
|
<DateMeta item={attachment} />
|
|
|
|
|
</View>
|
|
|
|
|
|
2022-03-14 16:28:31 +05:00
|
|
|
{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}
|
|
|
|
|
|
2022-03-07 15:19:07 +05:00
|
|
|
{actions.map(item => (
|
|
|
|
|
<Button
|
|
|
|
|
key={item.name}
|
|
|
|
|
buttonType={{
|
|
|
|
|
text: item.on
|
|
|
|
|
? colors.accent
|
|
|
|
|
: item.name === 'Delete' || item.name === 'PermDelete'
|
|
|
|
|
? colors.errorText
|
|
|
|
|
: colors.pri
|
|
|
|
|
}}
|
|
|
|
|
onPress={item.onPress}
|
|
|
|
|
title={item.name}
|
|
|
|
|
icon={item.icon}
|
2022-03-14 16:28:31 +05:00
|
|
|
loading={loading?.name === item.name}
|
2022-03-07 15:19:07 +05:00
|
|
|
type={item.on ? 'shade' : 'gray'}
|
|
|
|
|
fontSize={SIZE.sm}
|
|
|
|
|
style={{
|
|
|
|
|
borderRadius: 0,
|
|
|
|
|
justifyContent: 'flex-start',
|
|
|
|
|
alignSelf: 'flex-start',
|
|
|
|
|
width: '100%'
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
paddingHorizontal: 12
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{failed ? (
|
|
|
|
|
<Notice
|
|
|
|
|
type="alert"
|
|
|
|
|
text={`File check failed with error: ${attachment.failed} Try reuploading the file to fix the issue.`}
|
|
|
|
|
size="small"
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</View>
|
2022-03-14 16:28:31 +05:00
|
|
|
</ScrollView>
|
2022-03-07 15:19:07 +05:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Actions.present = (attachment, set, context) => {
|
|
|
|
|
presentSheet({
|
|
|
|
|
context: context,
|
2022-03-14 16:28:31 +05:00
|
|
|
component: ref => <Actions fwdRef={ref} setAttachments={set} attachment={attachment} />
|
2022-03-07 15:19:07 +05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Actions;
|