Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

WorkspaceFileWebPreview.tsx

107 lines4.1 KB

1import { useEffect, useState } from "react";
2import { BackHandler, Platform, View } from "react-native";
3import { WebView } from "react-native-webview";
4import { SafeAreaView, useSafeAreaInsets } from "react-native-safe-area-context";
5​
6import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
7import { AppText as Text } from "../../components/ui/AppText";
8import { FilePreviewLoading } from "./FilePreviewFeedback";
9import { LoadingStrip } from "../../components/ui/LoadingStrip";
10import { PdfPreview } from "../../components/PdfPreview";
11import { ControlPill } from "../../components/ui/ControlPill";
12import { isPdfPreviewUri } from "../../lib/pdfPreview";
13import { useNativeColumnLayoutMetrics } from "../../native/native-layout-metrics";
14​
15export 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} />;
21​
22function 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;
26 const [loadProgress, setLoadProgress] = useState(0);
27 const [loadError, setLoadError] = useState<string | null>(null);
28 const [linkedPdf, setLinkedPdf] = useState<string | null>(null);
29​
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 );
45 }
46​
47 return (
48 <View className="relative flex-1 bg-card">
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>
59 </View>
60 ) : null}
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>
67 </View>
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>
105 </View>
106 );