|
1 | | -import { useState } from "react"; |
2 | | -import { View } from "react-native"; |
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { BackHandler, Platform, View } from "react-native"; |
3 | 3 | import { WebView } from "react-native-webview"; |
| 4 | +import { SafeAreaView, useSafeAreaInsets } from "react-native-safe-area-context"; |
4 | 5 |
|
5 | 6 | import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; |
6 | 7 | import { AppText as Text } from "../../components/ui/AppText"; |
7 | 8 | import { FilePreviewLoading } from "./FilePreviewFeedback"; |
8 | 9 | import { LoadingStrip } from "../../components/ui/LoadingStrip"; |
| 10 | +import { PdfPreview } from "../../components/PdfPreview"; |
| 11 | +import { ControlPill } from "../../components/ui/ControlPill"; |
| 12 | +import { isPdfPreviewUri } from "../../lib/pdfPreview"; |
| 13 | +import { useNativeColumnLayoutMetrics } from "../../native/native-layout-metrics"; |
9 | 14 |
|
10 | 15 | export function WorkspaceFileWebPreview(props: { readonly uri: string | null }) { |
| 16 | + if (props.uri === null) { |
| 17 | + return <FilePreviewLoading message="Preparing preview..." background="card" />; |
| 18 | + } |
| 19 | + return <DocumentWebPreview key={props.uri} uri={props.uri} />; |
| 20 | +} |
| 21 | + |
| 22 | +function DocumentWebPreview(props: { readonly uri: string }) { |
| 23 | + const insets = useSafeAreaInsets(); |
| 24 | + const columnMetrics = useNativeColumnLayoutMetrics(); |
| 25 | + const pdfTopInset = Platform.OS === "ios" ? (columnMetrics?.safeArea.top ?? insets.top) : 0; |
11 | 26 | const [loadProgress, setLoadProgress] = useState(0); |
12 | 27 | const [loadError, setLoadError] = useState<string | null>(null); |
| 28 | + const [linkedPdf, setLinkedPdf] = useState<string | null>(null); |
13 | 29 |
|
14 | | - if (props.uri === null) { |
15 | | - return <FilePreviewLoading message="Preparing preview..." background="card" />; |
| 30 | + useEffect(() => { |
| 31 | + if (linkedPdf === null) return; |
| 32 | + const subscription = BackHandler.addEventListener("hardwareBackPress", () => { |
| 33 | + setLinkedPdf(null); |
| 34 | + return true; |
| 35 | + }); |
| 36 | + return () => subscription.remove(); |
| 37 | + }, [linkedPdf]); |
| 38 | + |
| 39 | + if (isPdfPreviewUri(props.uri)) { |
| 40 | + return ( |
| 41 | + <View className="flex-1" style={{ paddingTop: pdfTopInset }}> |
| 42 | + <PdfPreview uri={props.uri} /> |
| 43 | + </View> |
| 44 | + ); |
16 | 45 | } |
17 | 46 |
|
18 | 47 | return ( |
19 | 48 | <View className="relative flex-1 bg-card"> |
20 | | - {loadProgress > 0 && loadProgress < 1 ? <LoadingStrip progress={loadProgress} /> : null} |
21 | | - {loadError ? ( |
22 | | - <View className="border-b border-border bg-card px-4 py-2"> |
23 | | - <Text className="text-xs font-t3-bold text-foreground">Preview failed</Text> |
24 | | - <Text className="mt-0.5 text-xs leading-snug text-foreground-muted">{loadError}</Text> |
| 49 | + {linkedPdf ? ( |
| 50 | + <View className="flex-1" style={{ paddingTop: pdfTopInset }}> |
| 51 | + <PdfPreview key={linkedPdf} uri={linkedPdf} /> |
| 52 | + <SafeAreaView edges={["bottom"]} className="border-t border-border px-4 py-2"> |
| 53 | + <ControlPill |
| 54 | + label="Back to document" |
| 55 | + variant="pill" |
| 56 | + onPress={() => setLinkedPdf(null)} |
| 57 | + /> |
| 58 | + </SafeAreaView> |
25 | 59 | </View> |
26 | 60 | ) : null} |
27 | | - <WebView |
28 | | - source={{ uri: props.uri }} |
29 | | - originWhitelist={["*"]} |
30 | | - allowsBackForwardNavigationGestures |
31 | | - allowsFullscreenVideo |
32 | | - setSupportMultipleWindows={false} |
33 | | - startInLoadingState |
34 | | - onLoadProgress={(event) => { |
35 | | - setLoadProgress(event.nativeEvent.progress); |
36 | | - }} |
37 | | - onLoadStart={() => { |
38 | | - setLoadProgress(0.05); |
39 | | - setLoadError(null); |
40 | | - }} |
41 | | - onLoadEnd={() => { |
42 | | - setLoadProgress(0); |
43 | | - }} |
44 | | - onError={(event) => { |
45 | | - setLoadProgress(0); |
46 | | - setLoadError(event.nativeEvent.description || "The file could not be rendered."); |
47 | | - }} |
48 | | - renderLoading={() => ( |
49 | | - <View className="absolute inset-0 items-center justify-center bg-card"> |
50 | | - <AppActivityIndicator /> |
| 61 | + <View className="flex-1" style={linkedPdf ? { display: "none" } : undefined}> |
| 62 | + {loadProgress > 0 && loadProgress < 1 ? <LoadingStrip progress={loadProgress} /> : null} |
| 63 | + {loadError ? ( |
| 64 | + <View className="border-b border-border bg-card px-4 py-2"> |
| 65 | + <Text className="text-xs font-t3-bold text-foreground">Preview failed</Text> |
| 66 | + <Text className="mt-0.5 text-xs leading-snug text-foreground-muted">{loadError}</Text> |
51 | 67 | </View> |
52 | | - )} |
53 | | - style={{ flex: 1, backgroundColor: "transparent" }} |
54 | | - /> |
| 68 | + ) : null} |
| 69 | + <WebView |
| 70 | + source={{ uri: props.uri }} |
| 71 | + originWhitelist={["*"]} |
| 72 | + allowsBackForwardNavigationGestures |
| 73 | + allowsFullscreenVideo |
| 74 | + setSupportMultipleWindows={false} |
| 75 | + onShouldStartLoadWithRequest={(request) => { |
| 76 | + if (request.isTopFrame !== false && isPdfPreviewUri(request.url)) { |
| 77 | + setLinkedPdf(request.url); |
| 78 | + return false; |
| 79 | + } |
| 80 | + return true; |
| 81 | + }} |
| 82 | + startInLoadingState |
| 83 | + onLoadProgress={(event) => { |
| 84 | + setLoadProgress(event.nativeEvent.progress); |
| 85 | + }} |
| 86 | + onLoadStart={() => { |
| 87 | + setLoadProgress(0.05); |
| 88 | + setLoadError(null); |
| 89 | + }} |
| 90 | + onLoadEnd={() => { |
| 91 | + setLoadProgress(0); |
| 92 | + }} |
| 93 | + onError={(event) => { |
| 94 | + setLoadProgress(0); |
| 95 | + setLoadError(event.nativeEvent.description || "The file could not be rendered."); |
| 96 | + }} |
| 97 | + renderLoading={() => ( |
| 98 | + <View className="absolute inset-0 items-center justify-center bg-card"> |
| 99 | + <AppActivityIndicator /> |
| 100 | + </View> |
| 101 | + )} |
| 102 | + style={{ flex: 1, backgroundColor: "transparent" }} |
| 103 | + /> |
| 104 | + </View> |
55 | 105 | </View> |
56 | 106 | ); |
57 | 107 | } |
0 commit comments