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

WorkspaceFileWebPreview.tsx

60 lines2.1 KB

1import { useState } from "react";
2import { ActivityIndicator, View } from "react-native";
3import { WebView } from "react-native-webview";
4​
5import { AppText as Text } from "../../components/AppText";
6import { LoadingStrip } from "../../components/LoadingStrip";
7​
8export function WorkspaceFileWebPreview(props: { readonly uri: string | null }) {
9 const [loadProgress, setLoadProgress] = useState(0);
10 const [loadError, setLoadError] = useState<string | null>(null);
11​
12 if (props.uri === null) {
13 return (
14 <View className="flex-1 items-center justify-center gap-3 bg-card px-6">
15 <ActivityIndicator />
16 <Text className="text-center text-sm text-foreground-muted">Preparing preview...</Text>
17 </View>
18 );
19 }
20​
21 return (
22 <View className="relative flex-1 bg-card">
23 {loadProgress > 0 && loadProgress < 1 ? <LoadingStrip progress={loadProgress} /> : null}
24 {loadError ? (
25 <View className="border-b border-border bg-card px-4 py-2">
26 <Text className="text-xs font-t3-bold text-foreground">Preview failed</Text>
27 <Text className="mt-0.5 text-xs leading-snug text-foreground-muted">{loadError}</Text>
28 </View>
29 ) : null}
30 <WebView
31 source={{ uri: props.uri }}
32 originWhitelist={["*"]}
33 allowsBackForwardNavigationGestures
34 allowsFullscreenVideo
35 setSupportMultipleWindows={false}
36 startInLoadingState
37 onLoadProgress={(event) => {
38 setLoadProgress(event.nativeEvent.progress);
39 }}
40 onLoadStart={() => {
41 setLoadProgress(0.05);
42 setLoadError(null);
43 }}
44 onLoadEnd={() => {
45 setLoadProgress(0);
46 }}
47 onError={(event) => {
48 setLoadProgress(0);
49 setLoadError(event.nativeEvent.description || "The file could not be rendered.");
50 }}
51 renderLoading={() => (
52 <View className="absolute inset-0 items-center justify-center bg-card">
53 <ActivityIndicator />
54 </View>
55 )}
56 style={{ flex: 1, backgroundColor: "transparent" }}
57 />
58 </View>
59 );