Deskripsi: - detail open file pada halaman detaul diskusi umum - upload dan hapus file pada halaman edit diskusi umum - refresh halaman detail diskusi ummum No Issues
149 lines
6.4 KiB
TypeScript
149 lines
6.4 KiB
TypeScript
import { ColorsStatus } from "@/constants/ColorsStatus";
|
|
import { ConstEnv } from "@/constants/ConstEnv";
|
|
import Styles from "@/constants/Styles";
|
|
import { Ionicons } from "@expo/vector-icons";
|
|
import * as FileSystem from 'expo-file-system';
|
|
import { startActivityAsync } from 'expo-intent-launcher';
|
|
import * as Sharing from 'expo-sharing';
|
|
import React, { useState } from "react";
|
|
import { Alert, Dimensions, Platform, Pressable, View } from "react-native";
|
|
import { ScrollView } from "react-native-gesture-handler";
|
|
import * as mime from 'react-native-mime-types';
|
|
import Text from "./Text";
|
|
|
|
|
|
type Props = {
|
|
title?: string
|
|
subtitle?: string | React.ReactNode
|
|
icon: React.ReactNode
|
|
desc?: string
|
|
rightTopInfo?: string
|
|
onPress?: () => void
|
|
onLongPress?: () => void
|
|
borderType: 'all' | 'bottom' | 'none'
|
|
leftBottomInfo?: React.ReactNode | string
|
|
rightBottomInfo?: React.ReactNode | string
|
|
titleWeight?: 'normal' | 'bold'
|
|
bgColor?: 'white' | 'transparent'
|
|
width?: number
|
|
descEllipsize?: boolean
|
|
textColor?: string,
|
|
colorPress?: boolean
|
|
titleShowAll?: boolean
|
|
dataFile: { id: string; idStorage: string; name: string; extension: string }[]
|
|
}
|
|
|
|
export default function BorderBottomItem2({ title, subtitle, icon, desc, onPress, onLongPress, rightTopInfo, borderType, leftBottomInfo, rightBottomInfo, titleWeight, bgColor, width, descEllipsize, textColor, colorPress, titleShowAll, dataFile }: Props) {
|
|
const lebarDim = Dimensions.get("window").width;
|
|
const lebar = width ? lebarDim * width / 100 : 'auto';
|
|
const textColorFix = textColor ? textColor : 'black';
|
|
const [isTap, setIsTap] = useState(false);
|
|
const [loadingOpen, setLoadingOpen] = useState(false)
|
|
|
|
|
|
const openFile = (item: { idStorage: string; name: string; extension: string }) => {
|
|
if (Platform.OS == 'android') setLoadingOpen(true)
|
|
let remoteUrl = ConstEnv.url_storage + '/files/' + item.idStorage;
|
|
const fileName = item.name + '.' + item.extension;
|
|
let localPath = `${FileSystem.documentDirectory}/${fileName}`;
|
|
const mimeType = mime.lookup(fileName)
|
|
|
|
FileSystem.downloadAsync(remoteUrl, localPath).then(async ({ uri }) => {
|
|
const contentURL = await FileSystem.getContentUriAsync(uri);
|
|
setLoadingOpen(false)
|
|
try {
|
|
if (Platform.OS == 'android') {
|
|
await startActivityAsync(
|
|
'android.intent.action.VIEW',
|
|
{
|
|
data: contentURL,
|
|
flags: 1,
|
|
type: mimeType as string,
|
|
}
|
|
);
|
|
} else if (Platform.OS == 'ios') {
|
|
Sharing.shareAsync(localPath);
|
|
}
|
|
} catch (error) {
|
|
Alert.alert('INFO', 'Gagal membuka file, tidak ada aplikasi yang dapat membuka file ini');
|
|
} finally {
|
|
if (Platform.OS == 'android') setLoadingOpen(false)
|
|
}
|
|
});
|
|
};
|
|
|
|
|
|
return (
|
|
<Pressable onLongPress={onLongPress} onPress={onPress}
|
|
onPressIn={() => setIsTap(true)}
|
|
onPressOut={() => setIsTap(false)}
|
|
style={({ pressed }) => [
|
|
borderType == 'bottom'
|
|
? Styles.wrapItemBorderBottom
|
|
: borderType == 'all'
|
|
? Styles.wrapItemBorderAll
|
|
: Styles.wrapItemBorderNone,
|
|
bgColor && bgColor == 'white' && ColorsStatus.white,
|
|
// efek warna saat ditekan (sementara)
|
|
isTap && colorPress && ColorsStatus.pressedGray,
|
|
]}
|
|
>
|
|
<View style={[Styles.rowItemsCenter]}>
|
|
{icon}
|
|
<View style={[Styles.rowSpaceBetween, width ? { width: lebar } : { width: '88%' }]}>
|
|
<View style={[Styles.ml10, rightTopInfo ? { width: '70%' } : { width: '90%' }]}>
|
|
<Text style={[titleWeight == 'normal' ? Styles.textDefault : Styles.textDefaultSemiBold, { color: textColorFix }]} numberOfLines={titleShowAll ? 0 : 1} ellipsizeMode='tail'>{title}</Text>
|
|
{
|
|
subtitle &&
|
|
typeof subtitle == "string"
|
|
? <Text style={[Styles.textMediumNormal, { lineHeight: 15, color: textColorFix }]}>{subtitle}</Text>
|
|
: <View style={{ alignItems: 'flex-start' }}>
|
|
{subtitle}
|
|
</View>
|
|
}
|
|
</View>
|
|
{
|
|
rightTopInfo && <Text style={[Styles.textInformation, Styles.mt05, { color: textColorFix }]}>{rightTopInfo}</Text>
|
|
}
|
|
</View>
|
|
|
|
</View>
|
|
{desc && <Text style={[Styles.textDefault, Styles.mt05, { textAlign: 'left', color: textColorFix }]} numberOfLines={descEllipsize == false ? 0 : 2} ellipsizeMode='tail'>{desc}</Text>}
|
|
{
|
|
dataFile.length > 0 && (
|
|
<ScrollView horizontal style={[Styles.mv05]} showsHorizontalScrollIndicator={false}>
|
|
{dataFile.map((item, index) => (
|
|
<Pressable
|
|
key={index}
|
|
style={[Styles.rowItemsCenter, Styles.borderAll, Styles.round10, Styles.ph05, Styles.pv03, Styles.mr05]}
|
|
onPress={() => { openFile({ idStorage: item.idStorage, name: item.name, extension: item.extension }) }}
|
|
>
|
|
<Ionicons name="document-text" size={18} color="grey" style={Styles.mr05} />
|
|
<Text style={[Styles.textInformation, Styles.cGray, Styles.mb05]}>{item.name}.{item.extension}</Text>
|
|
</Pressable>
|
|
))}
|
|
</ScrollView>
|
|
)
|
|
}
|
|
{
|
|
(leftBottomInfo || rightBottomInfo) &&
|
|
(
|
|
<View style={[rightBottomInfo && !leftBottomInfo ? Styles.rowSpaceBetweenReverse : Styles.rowSpaceBetween, Styles.mt05]}>
|
|
{
|
|
typeof leftBottomInfo == 'string' ?
|
|
<Text style={[Styles.textInformation, Styles.cGray]}>{leftBottomInfo}</Text>
|
|
:
|
|
leftBottomInfo
|
|
}
|
|
{
|
|
typeof rightBottomInfo == 'string' ?
|
|
<Text style={[Styles.textInformation, Styles.cGray]}>{rightBottomInfo}</Text>
|
|
:
|
|
rightBottomInfo
|
|
}
|
|
</View>
|
|
)
|
|
}
|
|
</Pressable>
|
|
)
|
|
} |