import { ConstEnv } from "@/constants/ConstEnv"; import { isImageFile } from "@/constants/FileExtensions"; import Styles from "@/constants/Styles"; import { apiGetDivisionOneFeature } from "@/lib/api"; import { useAuthSession } from "@/providers/AuthProvider"; import { MaterialCommunityIcons } from "@expo/vector-icons"; import * as FileSystem from 'expo-file-system'; import { startActivityAsync } from 'expo-intent-launcher'; import { useLocalSearchParams } from "expo-router"; import * as Sharing from 'expo-sharing'; import { useEffect, useState } from "react"; import { Platform, Pressable, ScrollView, View } from "react-native"; import ImageViewing from "react-native-image-viewing"; import * as mime from 'react-native-mime-types'; import Toast from "react-native-toast-message"; import Skeleton from "../skeleton"; import { useTheme } from "@/providers/ThemeProvider"; import Text from "../Text"; type Props = { id: string name: string extension: string path: string share: boolean idStorage: string } function getFileIconColor(ext: string): string { const e = ext.toLowerCase() if (['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'].includes(e)) return '#339AF0' if (e === 'pdf') return '#F03E3E' if (['doc', 'docx'].includes(e)) return '#1C7ED6' if (['xls', 'xlsx', 'csv'].includes(e)) return '#2F9E44' if (['ppt', 'pptx'].includes(e)) return '#F97316' if (['zip', 'rar', '7z'].includes(e)) return '#E8590C' return '#868E96' } function getFileIcon(ext: string): string { const e = ext.toLowerCase() if (isImageFile(e)) return 'image-outline' if (e === 'pdf') return 'file-pdf-box' if (['doc', 'docx'].includes(e)) return 'file-word-outline' if (['xls', 'xlsx', 'csv'].includes(e)) return 'file-excel-outline' if (['ppt', 'pptx'].includes(e)) return 'file-powerpoint-outline' if (['zip', 'rar', '7z'].includes(e)) return 'folder-zip-outline' return 'file-document-outline' } export default function FileDivisionDetail({ refreshing }: { refreshing: boolean }) { const { colors } = useTheme(); const [data, setData] = useState([]) const { token, decryptToken } = useAuthSession() const { id } = useLocalSearchParams<{ id: string }>() const [loading, setLoading] = useState(true) const [loadingOpen, setLoadingOpen] = useState(false) const [previewFile, setPreviewFile] = useState(null) async function handleLoad(loading: boolean) { try { setLoading(loading) const hasil = await decryptToken(String(token?.current)) const response = await apiGetDivisionOneFeature({ user: hasil, id, cat: 'new-file' }) setData(response.data) } catch (error) { console.error(error) } finally { setLoading(false) } } useEffect(() => { if (refreshing) handleLoad(false) }, [refreshing]) useEffect(() => { handleLoad(true) }, []) async function openExternal(item: Props) { try { setLoadingOpen(true) const remoteUrl = `${ConstEnv.url_storage}/files/${item.idStorage}` const fileName = `${item.name}.${item.extension}` const localPath = `${FileSystem.documentDirectory}/${fileName}` const dl = await FileSystem.downloadAsync(remoteUrl, localPath) if (dl.status !== 200) throw new Error('Download failed') const contentURL = await FileSystem.getContentUriAsync(dl.uri) const mimeType = mime.lookup(fileName) as string if (Platform.OS === 'android') { await startActivityAsync('android.intent.action.VIEW', { data: contentURL, flags: 1, type: mimeType }) } else { await Sharing.shareAsync(localPath) } } catch { Toast.show({ type: 'error', text1: 'Gagal membuka file' }) } finally { setLoadingOpen(false) } } function handleFilePress(item: Props) { if (isImageFile(item.extension.toLowerCase())) { setPreviewFile(item) } else { openExternal(item) } } return ( Dokumen Terkini {loading ? ( ) : data.length > 0 ? ( {data.map((item, index) => { const iconColor = getFileIconColor(item.extension) const iconName = getFileIcon(item.extension) return ( handleFilePress(item)} style={({ pressed }) => ({ flexDirection: 'row', alignItems: 'center', gap: 10, paddingHorizontal: 12, paddingVertical: 10, borderRadius: 8, borderWidth: 1, backgroundColor: pressed ? colors.icon + '08' : colors.card, borderColor: colors.icon + '20', width: 160, })} > {item.name} {item.extension.toUpperCase()} ) })} ) : ( Belum ada dokumen )} setPreviewFile(null)} doubleTapToZoomEnabled HeaderComponent={() => ( setPreviewFile(null)}> previewFile && openExternal(previewFile)} disabled={loadingOpen}> )} FooterComponent={() => ( {previewFile?.name}.{previewFile?.extension} )} /> ) }