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

WorkspaceFileWebPreview.tsx

106 lines4.0 KB

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