1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
import React, { useEffect, useMemo, useState } from "react";
import { ActivityIndicator, StyleSheet, View } from "react-native";
import ReactNativeBlobUtil from "react-native-blob-util";
import Pdf from "react-native-pdf";
import { Text } from "@/components/ui/Text";
import { useQuery } from "@tanstack/react-query";
import { useColorScheme } from "nativewind";
interface PDFViewerProps {
source: string;
headers?: Record<string, string>;
}
export function PDFViewer({ source, headers }: PDFViewerProps) {
const [pdfRenderError, setPdfRenderError] = useState<string | null>(null);
const { colorScheme } = useColorScheme();
const isDark = colorScheme === "dark";
const colors = {
background: isDark ? "#000" : "#fff",
foreground: isDark ? "#fff" : "#000",
mutedForeground: isDark ? "#888" : "#666",
};
const {
data: localPath,
isLoading,
error: downloadError,
} = useQuery({
queryKey: ["pdf", source],
queryFn: async () => {
// Create a temporary filename
const fileName = `temp_${Date.now()}.pdf`;
const { dirs } = ReactNativeBlobUtil.fs;
const path = `${dirs.DocumentDir}/${fileName}`;
const response = await ReactNativeBlobUtil.config({
fileCache: true,
path,
}).fetch("GET", source, headers ?? {});
return response.path();
},
enabled: !!source,
});
// Merge download and render errors
const error = useMemo(() => {
if (downloadError) {
let errorMessage = "Failed to download PDF";
if (downloadError.message.includes("Network request failed")) {
errorMessage = "Network error. Please check your connection.";
} else if (
downloadError.message.includes("401") ||
downloadError.message.includes("403")
) {
errorMessage = "Authentication failed. Please sign in again.";
} else if (downloadError.message.includes("404")) {
errorMessage = "PDF not found.";
}
return errorMessage;
}
if (pdfRenderError) {
return pdfRenderError;
}
return null;
}, [downloadError, pdfRenderError]);
// Cleanup function to remove temporary file on unmount
useEffect(() => {
return () => {
if (localPath) {
ReactNativeBlobUtil.fs.unlink(localPath).catch(() => ({}));
}
};
}, [source, headers]);
if (error) {
return (
<View style={[styles.container, { backgroundColor: colors.background }]}>
<Text style={[styles.errorText, { color: colors.foreground }]}>
{error}
</Text>
</View>
);
}
if (isLoading || !localPath) {
return (
<View style={[styles.container, { backgroundColor: colors.background }]}>
<View style={styles.loadingContainer}>
<ActivityIndicator size="large" color={colors.foreground} />
<Text style={[styles.loadingText, { color: colors.mutedForeground }]}>
Downloading PDF...
</Text>
</View>
</View>
);
}
return (
<View style={[styles.container, { backgroundColor: colors.background }]}>
<Pdf
style={StyleSheet.absoluteFillObject}
source={{ uri: `file://${localPath}`, cache: true }}
spacing={16}
maxScale={3}
onLoadComplete={() => ({})}
onError={() => setPdfRenderError("Failed to render PDF")}
trustAllCerts={false}
renderActivityIndicator={() => (
<ActivityIndicator size="large" color={colors.foreground} />
)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
loadingContainer: {
...StyleSheet.absoluteFillObject,
justifyContent: "center",
alignItems: "center",
zIndex: 1,
},
loadingText: {
marginTop: 12,
fontSize: 16,
},
errorText: {
fontSize: 16,
textAlign: "center",
padding: 20,
},
});
|