Files
notesnook/apps/mobile/app/components/attachments/index.js

203 lines
5.7 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
2023-03-16 21:22:21 +05:00
import React, { useRef, useState } from "react";
import { View } from "react-native";
2023-04-11 23:01:32 +05:00
import { FlatList } from "react-native-actions-sheet";
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
2022-08-29 16:19:17 +05:00
import { db } from "../../common/database";
import filesystem from "../../common/filesystem";
2023-03-16 21:22:21 +05:00
import { presentSheet } from "../../services/event-manager";
import { useThemeStore } from "../../stores/use-theme-store";
import { SIZE } from "../../utils/size";
2023-04-11 23:01:32 +05:00
import SheetProvider from "../sheet-provider";
import { IconButton } from "../ui/icon-button";
import Input from "../ui/input";
import Seperator from "../ui/seperator";
2023-04-11 23:01:32 +05:00
import Heading from "../ui/typography/heading";
import Paragraph from "../ui/typography/paragraph";
import { AttachmentItem } from "./attachment-item";
2023-04-11 23:01:32 +05:00
import DownloadAttachments from "./download-attachments";
2023-03-16 21:22:21 +05:00
2023-04-11 23:01:32 +05:00
export const AttachmentDialog = ({ note }) => {
const colors = useThemeStore((state) => state.colors);
2023-03-16 21:22:21 +05:00
const [attachments, setAttachments] = useState(
2023-04-11 23:01:32 +05:00
note
? db.attachments.ofNote(note.id, "all")
2023-03-16 21:22:21 +05:00
: [...(db.attachments.all || [])]
);
2022-03-07 15:19:07 +05:00
const attachmentSearchValue = useRef();
const searchTimer = useRef();
2022-03-09 16:35:35 +05:00
const [loading, setLoading] = useState(false);
2021-09-29 12:56:07 +05:00
const onChangeText = (text) => {
2022-03-07 15:19:07 +05:00
attachmentSearchValue.current = text;
if (
!attachmentSearchValue.current ||
attachmentSearchValue.current === ""
) {
2022-03-07 15:19:07 +05:00
setAttachments([...db.attachments.all]);
}
clearTimeout(searchTimer.current);
searchTimer.current = setTimeout(() => {
let results = db.lookup.attachments(
db.attachments.all,
attachmentSearchValue.current
);
2022-03-07 15:19:07 +05:00
if (results.length === 0) return;
setAttachments(results);
}, 300);
};
const renderItem = ({ item }) => (
2022-03-07 15:19:07 +05:00
<AttachmentItem setAttachments={setAttachments} attachment={item} />
);
2023-04-11 23:01:32 +05:00
const onCheck = async () => {
setLoading(true);
for (let attachment of attachments) {
let result = await filesystem.checkAttachment(attachment.metadata.hash);
if (result.failed) {
db.attachments.markAsFailed(attachment.metadata.hash, result.failed);
} else {
db.attachments.markAsFailed(attachment.id, null);
}
setAttachments([...db.attachments.all]);
}
setLoading(false);
};
2023-03-16 21:22:21 +05:00
return (
<View
style={{
width: "100%",
alignSelf: "center",
paddingHorizontal: 12
2022-01-22 12:57:05 +05:00
}}
>
2023-04-11 23:01:32 +05:00
<SheetProvider context="attachments-list" />
<View
style={{
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center"
2023-03-16 21:22:21 +05:00
}}
2023-04-11 23:01:32 +05:00
>
<Heading>Attachments</Heading>
<View
style={{
flexDirection: "row"
}}
>
<IconButton
name="check-all"
customStyle={{
height: 40,
width: 40,
marginRight: 10
}}
2023-04-11 23:57:12 +05:00
color={colors.pri}
2023-04-11 23:01:32 +05:00
size={SIZE.lg}
onPress={onCheck}
/>
<IconButton
name="download"
customStyle={{
height: 40,
width: 40
}}
2023-04-11 23:57:12 +05:00
color={colors.pri}
2023-04-11 23:01:32 +05:00
onPress={() => {
DownloadAttachments.present(
"attachments-list",
attachments,
!!note
);
}}
size={SIZE.lg}
/>
</View>
</View>
2023-03-16 21:22:21 +05:00
<Seperator />
{!note ? (
<Input
placeholder="Filter attachments by filename, type or hash"
onChangeText={onChangeText}
onSubmit={() => {
onChangeText(attachmentSearchValue.current);
2022-03-07 15:19:07 +05:00
}}
/>
2023-03-16 21:22:21 +05:00
) : null}
<FlatList
keyboardDismissMode="none"
keyboardShouldPersistTaps="always"
2023-03-20 17:07:52 +05:00
maxToRenderPerBatch={10}
initialNumToRender={10}
windowSize={5}
2023-03-16 21:22:21 +05:00
ListEmptyComponent={
<View
style={{
height: 150,
justifyContent: "center",
alignItems: "center"
}}
>
<Icon name="attachment" size={60} color={colors.icon} />
<Paragraph>
{note ? "No attachments on this note" : "No attachments"}
</Paragraph>
</View>
}
ListFooterComponent={
<View
style={{
height: 350
2022-03-07 15:19:07 +05:00
}}
/>
2023-03-16 21:22:21 +05:00
}
data={attachments}
keyExtractor={(item) => item.id}
renderItem={renderItem}
/>
2022-03-07 15:19:07 +05:00
2023-03-16 21:22:21 +05:00
<Paragraph
color={colors.icon}
size={SIZE.xs}
style={{
textAlign: "center",
marginTop: 10
}}
>
<Icon name="shield-key-outline" size={SIZE.xs} color={colors.icon} />
{" "}All attachments are end-to-end encrypted.
</Paragraph>
</View>
2021-09-29 12:56:07 +05:00
);
};
2023-03-16 21:22:21 +05:00
AttachmentDialog.present = (note) => {
presentSheet({
2023-04-11 23:01:32 +05:00
component: () => <AttachmentDialog note={note} />
2023-03-16 21:22:21 +05:00
});
};