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