2021-11-09 09:44:48 +05:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2022-02-28 13:48:59 +05:00
|
|
|
import { View } from 'react-native';
|
2021-11-09 09:44:48 +05:00
|
|
|
import { FlatList } from 'react-native-gesture-handler';
|
2021-09-29 12:56:07 +05:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
|
2022-02-28 23:25:18 +05:00
|
|
|
import { eSubscribeEvent, eUnSubscribeEvent } from '../../services/event-manager';
|
2022-03-07 15:19:07 +05:00
|
|
|
import { useThemeStore } from '../../stores/theme';
|
2021-11-09 09:44:48 +05:00
|
|
|
import { db } from '../../utils/database';
|
2022-02-28 13:48:59 +05:00
|
|
|
import { eCloseAttachmentDialog, eOpenAttachmentsDialog } from '../../utils/events';
|
2022-03-07 15:19:07 +05:00
|
|
|
import filesystem from '../../utils/filesystem';
|
2022-02-28 13:48:59 +05:00
|
|
|
import { SIZE } from '../../utils/size';
|
|
|
|
|
import DialogHeader from '../dialog/dialog-header';
|
2022-03-07 15:19:07 +05:00
|
|
|
import { Toast } from '../toast';
|
|
|
|
|
import Input from '../ui/input';
|
|
|
|
|
import Seperator from '../ui/seperator';
|
2022-02-28 13:48:59 +05:00
|
|
|
import SheetWrapper from '../ui/sheet';
|
|
|
|
|
import Paragraph from '../ui/typography/paragraph';
|
|
|
|
|
import { AttachmentItem } from './attachment-item';
|
2021-09-29 12:56:07 +05:00
|
|
|
export const AttachmentDialog = () => {
|
2022-02-28 23:25:18 +05:00
|
|
|
const colors = useThemeStore(state => state.colors);
|
2021-09-29 12:56:07 +05:00
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
|
const [note, setNote] = useState(null);
|
|
|
|
|
const actionSheetRef = useRef();
|
2021-10-01 12:03:25 +05:00
|
|
|
const [attachments, setAttachments] = useState([]);
|
2022-03-07 15:19:07 +05:00
|
|
|
const attachmentSearchValue = useRef();
|
|
|
|
|
const searchTimer = useRef();
|
2021-09-29 12:56:07 +05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-10-01 12:03:25 +05:00
|
|
|
eSubscribeEvent(eOpenAttachmentsDialog, open);
|
|
|
|
|
eSubscribeEvent(eCloseAttachmentDialog, close);
|
2021-09-29 12:56:07 +05:00
|
|
|
return () => {
|
2021-10-01 12:03:25 +05:00
|
|
|
eUnSubscribeEvent(eOpenAttachmentsDialog, open);
|
|
|
|
|
eUnSubscribeEvent(eCloseAttachmentDialog, close);
|
2021-09-29 12:56:07 +05:00
|
|
|
};
|
2021-10-01 12:03:25 +05:00
|
|
|
}, [visible]);
|
2021-09-29 12:56:07 +05:00
|
|
|
|
2022-03-07 15:19:07 +05:00
|
|
|
const open = data => {
|
|
|
|
|
if (data?.id) {
|
|
|
|
|
setNote(data);
|
|
|
|
|
let _attachments = db.attachments.ofNote(data.id, 'all');
|
|
|
|
|
setAttachments(_attachments);
|
|
|
|
|
} else {
|
|
|
|
|
setAttachments([...db.attachments.all]);
|
|
|
|
|
}
|
2021-09-29 12:56:07 +05:00
|
|
|
setVisible(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (visible) {
|
|
|
|
|
actionSheetRef.current?.show();
|
|
|
|
|
}
|
|
|
|
|
}, [visible]);
|
|
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
|
actionSheetRef.current?.hide();
|
2021-10-01 12:03:25 +05:00
|
|
|
setVisible(false);
|
2021-09-29 12:56:07 +05:00
|
|
|
};
|
|
|
|
|
|
2022-03-07 15:19:07 +05:00
|
|
|
const onChangeText = text => {
|
|
|
|
|
attachmentSearchValue.current = text;
|
|
|
|
|
console.log(attachmentSearchValue.current?.length);
|
|
|
|
|
if (!attachmentSearchValue.current || attachmentSearchValue.current === '') {
|
|
|
|
|
console.log('resetting all');
|
|
|
|
|
setAttachments([...db.attachments.all]);
|
|
|
|
|
}
|
|
|
|
|
console.log(attachments.length);
|
|
|
|
|
clearTimeout(searchTimer.current);
|
|
|
|
|
searchTimer.current = setTimeout(() => {
|
|
|
|
|
let results = db.lookup.attachments(db.attachments.all, attachmentSearchValue.current);
|
|
|
|
|
console.log('results', results.length, attachments.length);
|
|
|
|
|
if (results.length === 0) return;
|
|
|
|
|
setAttachments(results);
|
|
|
|
|
}, 300);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const renderItem = ({ item, index }) => (
|
|
|
|
|
<AttachmentItem setAttachments={setAttachments} attachment={item} />
|
|
|
|
|
);
|
|
|
|
|
|
2021-10-01 12:03:25 +05:00
|
|
|
return !visible ? null : (
|
2021-12-25 11:16:33 +05:00
|
|
|
<SheetWrapper
|
2021-09-29 12:56:07 +05:00
|
|
|
centered={false}
|
|
|
|
|
fwdRef={actionSheetRef}
|
|
|
|
|
onClose={async () => {
|
|
|
|
|
setVisible(false);
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2022-03-07 15:19:07 +05:00
|
|
|
<Toast context="local" />
|
2021-09-29 12:56:07 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
alignSelf: 'center',
|
2021-10-01 12:03:25 +05:00
|
|
|
paddingHorizontal: 12
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2022-03-07 15:19:07 +05:00
|
|
|
<DialogHeader
|
|
|
|
|
title={note ? 'Attachments' : 'Manage attachments'}
|
|
|
|
|
paragraph="Tap on an attachment to view properties"
|
|
|
|
|
button={{
|
|
|
|
|
title: 'Check all',
|
|
|
|
|
type: 'accent',
|
|
|
|
|
onPress: async () => {
|
|
|
|
|
for (let attachment of attachments) {
|
|
|
|
|
let result = await filesystem.checkAttachment(attachment.metadata.hash);
|
|
|
|
|
if (result.failed) {
|
|
|
|
|
db.attachments.markAsFailed(attachment.metadata.hash, result.failed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
setAttachments([...db.attachments.all]);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<Seperator />
|
|
|
|
|
{!note ? (
|
|
|
|
|
<Input
|
|
|
|
|
placeholder="Filter attachments by filename, type or hash"
|
|
|
|
|
onChangeText={onChangeText}
|
|
|
|
|
onSubmit={() => {
|
|
|
|
|
onChangeText(attachmentSearchValue.current);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
|
2021-10-01 12:03:25 +05:00
|
|
|
<FlatList
|
2021-09-29 12:56:07 +05:00
|
|
|
nestedScrollEnabled
|
|
|
|
|
overScrollMode="never"
|
|
|
|
|
scrollToOverflowEnabled={false}
|
|
|
|
|
keyboardDismissMode="none"
|
|
|
|
|
keyboardShouldPersistTaps="always"
|
|
|
|
|
onMomentumScrollEnd={() => {
|
|
|
|
|
actionSheetRef.current?.handleChildScrollEnd();
|
2021-10-01 12:03:25 +05:00
|
|
|
}}
|
|
|
|
|
ListEmptyComponent={
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
height: 150,
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'center'
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-10-01 12:03:25 +05:00
|
|
|
<Icon name="attachment" size={60} color={colors.icon} />
|
2022-03-07 15:19:07 +05:00
|
|
|
<Paragraph>{note ? `No attachments on this note` : `No attachments`}</Paragraph>
|
2021-10-01 12:03:25 +05:00
|
|
|
</View>
|
|
|
|
|
}
|
2022-03-07 15:19:07 +05:00
|
|
|
ListFooterComponent={
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
height: 350
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
}
|
2021-10-01 12:03:25 +05:00
|
|
|
data={attachments}
|
2022-03-07 15:19:07 +05:00
|
|
|
keyExtractor={item => item.id}
|
|
|
|
|
renderItem={renderItem}
|
2021-10-01 12:03:25 +05:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Paragraph
|
|
|
|
|
color={colors.icon}
|
|
|
|
|
size={SIZE.xs}
|
|
|
|
|
style={{
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
marginTop: 10
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-10-01 12:03:25 +05:00
|
|
|
<Icon name="shield-key-outline" size={SIZE.xs} color={colors.icon} />
|
|
|
|
|
{' '}All attachments are end-to-end encrypted.
|
|
|
|
|
</Paragraph>
|
2021-09-29 12:56:07 +05:00
|
|
|
</View>
|
2021-12-25 11:16:33 +05:00
|
|
|
</SheetWrapper>
|
2021-09-29 12:56:07 +05:00
|
|
|
);
|
|
|
|
|
};
|