Files
notesnook/apps/mobile/src/components/attachments/attachmentitem.js

147 lines
4.3 KiB
JavaScript
Raw Normal View History

2022-03-07 15:19:07 +05:00
import React from 'react';
2022-02-28 13:48:59 +05:00
import { TouchableOpacity, View } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
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';
2022-02-28 13:48:59 +05:00
import { db } from '../../utils/database';
2022-03-07 15:19:07 +05:00
import { useAttachmentProgress } from '../../utils/hooks/use-attachment-progress';
2022-02-28 13:48:59 +05:00
import { SIZE } from '../../utils/size';
import SheetProvider from '../sheet-provider';
import { IconButton } from '../ui/icon-button';
2022-07-06 12:17:08 +05:00
import { ProgressCircleComponent } from '../ui/svg/lazy';
2022-02-28 13:48:59 +05:00
import Paragraph from '../ui/typography/paragraph';
2022-03-07 15:19:07 +05:00
import Actions from './actions';
2022-02-28 13:48:59 +05:00
function getFileExtension(filename) {
var ext = /^.+\.([^.]+)$/.exec(filename);
return ext == null ? '' : ext[1];
}
2022-03-07 15:19:07 +05:00
export const AttachmentItem = ({ attachment, encryption, setAttachments }) => {
2022-02-28 23:25:18 +05:00
const colors = useThemeStore(state => state.colors);
2022-03-07 15:19:07 +05:00
const [currentProgress, setCurrentProgress] = useAttachmentProgress(attachment, encryption);
2022-02-28 13:48:59 +05:00
const encryptionProgress = encryption
? useAttachmentStore(state => state.encryptionProgress)
: null;
2022-03-07 15:19:07 +05:00
const onPress = () => {
Actions.present(attachment, setAttachments, attachment.metadata.hash);
2022-02-28 13:48:59 +05:00
};
return (
2022-03-07 15:19:07 +05:00
<TouchableOpacity
activeOpacity={0.9}
onPress={onPress}
2022-02-28 13:48:59 +05:00
style={{
flexDirection: 'row',
marginVertical: 5,
justifyContent: 'space-between',
padding: 12,
paddingVertical: 6,
borderRadius: 5,
backgroundColor: colors.nav
}}
type="grayBg"
>
<SheetProvider context={attachment.metadata.hash} />
<View
style={{
flexShrink: 1,
flexDirection: 'row',
alignItems: 'center'
}}
>
<View
style={{
justifyContent: 'center',
alignItems: 'center',
marginLeft: -5
}}
>
<Icon name="file" size={SIZE.xxxl} color={colors.icon} />
<Paragraph
adjustsFontSizeToFit
size={6}
color={colors.light}
style={{
position: 'absolute'
}}
>
{getFileExtension(attachment.metadata.filename).toUpperCase()}
</Paragraph>
</View>
<View
style={{
flexShrink: 1,
marginLeft: 10
}}
>
<Paragraph
size={SIZE.sm - 1}
style={{
flexWrap: 'wrap',
marginBottom: 2.5
}}
numberOfLines={1}
lineBreakMode="middle"
color={colors.pri}
>
{attachment.metadata.filename}
</Paragraph>
<Paragraph color={colors.icon} size={SIZE.xs}>
{formatBytes(attachment.length)}{' '}
{currentProgress?.type ? '(' + currentProgress.type + 'ing - tap to cancel)' : ''}
</Paragraph>
</View>
</View>
{currentProgress || encryptionProgress || encryption ? (
<TouchableOpacity
activeOpacity={0.9}
onPress={() => {
if (encryption) return;
db.fs.cancel(attachment.metadata.hash);
setCurrentProgress(null);
}}
style={{
justifyContent: 'center',
marginLeft: 5,
marginTop: 5,
marginRight: -5
}}
>
2022-07-06 12:17:08 +05:00
<React.Suspense fallback={<View />}>
<ProgressCircleComponent
size={SIZE.xxl}
progress={
encryptionProgress
? encryptionProgress
: currentProgress?.value
? currentProgress?.value / 100
: 0
}
showsText
textStyle={{
fontSize: 10
}}
color={colors.accent}
formatText={progress => (progress * 100).toFixed(0)}
borderWidth={0}
thickness={2}
/>
</React.Suspense>
2022-02-28 13:48:59 +05:00
</TouchableOpacity>
) : (
2022-03-07 15:19:07 +05:00
<>
{attachment.failed ? (
<IconButton onPress={onPress} name="alert-circle-outline" color={colors.errorText} />
) : null}
</>
2022-02-28 13:48:59 +05:00
)}
2022-03-07 15:19:07 +05:00
</TouchableOpacity>
2022-02-28 13:48:59 +05:00
);
};