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

WorkspaceFileWebPreview.tsx

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