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

WorkspaceFileWebPreview.tsx

109 lines4.3 KB

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