Skip to content

Commit 5410f24

Browse files
Merge origin/main into fix/v2-stale-chat-image
2 parents 2f7f6e1 + f3e5a16 commit 5410f24

80 files changed

Lines changed: 2873 additions & 756 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/electron/ElectronProtocol.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ describe("ElectronProtocol", () => {
322322
]);
323323
assert.deepEqual(directives["media-src"], ["'self'", "t3code:", "blob:", "http:", "https:"]);
324324
assert.deepEqual(directives["frame-src"], ["'self'", "blob:", "http:", "https:"]);
325+
assert.deepEqual(directives["object-src"], ["'self'", "blob:", "http:", "https:"]);
325326
assert.deepEqual(directives["font-src"], ["'self'", "t3code:", "data:"]);
326327
});
327328
});

‎apps/desktop/src/electron/ElectronProtocol.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export function makeDesktopContentSecurityPolicy(input: DesktopProtocolRegistrat
9595
`font-src 'self' ${input.scheme}: data:`,
9696
"worker-src 'self' blob:",
9797
"frame-src 'self' blob: http: https:",
98+
"object-src 'self' blob: http: https:",
9899
"form-action 'self'",
99100
].join("; ");
100101
}

‎apps/mobile/app.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ const config: ExpoConfig = {
317317
},
318318
plugins: [
319319
"expo-asset",
320+
"react-native-pdf-jsi",
320321
[
321322
"expo-font",
322323
{

‎apps/mobile/package.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@noble/curves": "catalog:",
5959
"@pierre/diffs": "catalog:",
6060
"@react-native-ai/apple": "0.12.0",
61+
"@react-native-async-storage/async-storage": "3.1.1",
6162
"@react-native-menu/menu": "^2.0.0",
6263
"@react-navigation/elements": "2.9.45",
6364
"@react-navigation/native": "7.5.1",
@@ -84,6 +85,7 @@
8485
"diff": "8.0.3",
8586
"effect": "catalog:",
8687
"expo": "58.0.14",
88+
"expo-application": "~58.1.2",
8789
"expo-asset": "~58.0.16",
8890
"expo-audio": "58.0.9",
8991
"expo-auth-session": "~58.0.13",
@@ -122,10 +124,13 @@
122124
"react": "catalog:",
123125
"react-dom": "catalog:",
124126
"react-native": "0.88.0",
127+
"react-native-blob-util": "0.25.1",
125128
"react-native-enriched-markdown": "file:deps/react-native-enriched-markdown-1.2.0-t3.15.tgz",
126129
"react-native-gesture-handler": "~3.3.0",
127130
"react-native-image-viewing": "^0.2.2",
128131
"react-native-keyboard-controller": "1.22.7",
132+
"react-native-nitro-modules": "0.37.1",
133+
"react-native-pdf-jsi": "5.0.0",
129134
"react-native-reanimated": "4.7.1",
130135
"react-native-safe-area-context": "~5.10.1",
131136
"react-native-screens": "file:deps/react-native-screens-5.0.0-t3.27.tgz",

‎apps/mobile/src/components/FilePreview.tsx‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,43 @@ import * as Dialog from "./ui/Dialog";
22
import { useEffect, useEffectEvent } from "react";
33
import { Modal, Pressable, View } from "react-native";
44
import ImageViewing from "react-native-image-viewing";
5+
import { useSafeAreaInsets } from "react-native-safe-area-context";
56

67
import { openAttachmentInViewer } from "../lib/attachmentDownload";
78
import type { ResolvedFilePreviewSource } from "./FilePreviewModal.types";
89
import { MediaImagePreview } from "./MediaImagePreview";
10+
import { PdfPreview } from "./PdfPreview";
11+
import { ControlPill } from "./ui/ControlPill";
912
import { AppText as Text } from "./ui/AppText";
1013

14+
function PdfDocumentPreview(props: {
15+
readonly source: ResolvedFilePreviewSource;
16+
readonly onRequestClose: () => void;
17+
}) {
18+
const insets = useSafeAreaInsets();
19+
return (
20+
<Modal
21+
animationType="slide"
22+
onRequestClose={props.onRequestClose}
23+
statusBarTranslucent
24+
navigationBarTranslucent
25+
>
26+
<View
27+
className="flex-1 bg-card"
28+
style={{ paddingTop: insets.top, paddingBottom: insets.bottom }}
29+
>
30+
<View className="flex-row items-center gap-3 border-b border-border px-4 py-2">
31+
<Text className="flex-1 font-t3-semibold text-foreground" numberOfLines={1}>
32+
{props.source.name ?? "PDF"}
33+
</Text>
34+
<ControlPill label="Close" variant="pill" onPress={props.onRequestClose} />
35+
</View>
36+
<PdfPreview uri={props.source.uri} />
37+
</View>
38+
</Modal>
39+
);
40+
}
41+
1142
function DocumentPreview(props: {
1243
readonly source: ResolvedFilePreviewSource;
1344
readonly onRequestClose: () => void;
@@ -71,6 +102,7 @@ export function FilePreview(props: {
71102
readonly onRequestClose: () => void;
72103
readonly onOpenError?: (error: unknown) => void;
73104
}) {
105+
if (props.source.kind === "pdf") return <PdfDocumentPreview {...props} />;
74106
if (props.source.kind !== "image") return <DocumentPreview {...props} />;
75107
if (props.source.actionsSource) return <MediaImagePreview {...props} />;
76108
return (
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { useEffect, useRef, useState } from "react";
2+
import { View } from "react-native";
3+
import Pdf from "react-native-pdf-jsi";
4+
5+
import { FilePreviewLoading, FilePreviewNotice } from "../features/files/FilePreviewFeedback";
6+
import { pdfInitialPage, pdfLinkUrl, preparePdfPreview } from "../lib/pdfPreview";
7+
import { tryOpenExternalUrl } from "../lib/openExternalUrl";
8+
import { ControlPill } from "./ui/ControlPill";
9+
10+
function NativePdfPreview(props: { readonly uri: string; readonly onRetry: () => void }) {
11+
const [localUri, setLocalUri] = useState<string | null>(null);
12+
const [error, setError] = useState<string | null>(null);
13+
const cached = useRef<Awaited<ReturnType<typeof preparePdfPreview>>>(null);
14+
15+
useEffect(() => {
16+
const controller = new AbortController();
17+
void preparePdfPreview(props.uri, controller.signal)
18+
.then((file) => {
19+
if (controller.signal.aborted) file?.dispose();
20+
else {
21+
cached.current = file;
22+
setLocalUri(file?.uri ?? null);
23+
}
24+
})
25+
.catch((cause: unknown) => {
26+
if (!controller.signal.aborted) {
27+
setError(cause instanceof Error ? cause.message : "The PDF could not be loaded.");
28+
}
29+
});
30+
return () => {
31+
controller.abort();
32+
cached.current?.dispose();
33+
cached.current = null;
34+
};
35+
}, [props.uri]);
36+
37+
if (error) {
38+
return (
39+
<View className="flex-1 gap-4 bg-card">
40+
<FilePreviewNotice title="Could not load PDF">{error}</FilePreviewNotice>
41+
<View className="items-center">
42+
<ControlPill label="Try again" variant="pill" onPress={props.onRetry} />
43+
</View>
44+
</View>
45+
);
46+
}
47+
if (localUri === null) return <FilePreviewLoading message="Loading PDF..." background="card" />;
48+
49+
return (
50+
<Pdf
51+
source={{ uri: localUri }}
52+
page={pdfInitialPage(props.uri)}
53+
fitPolicy={0}
54+
trustAllCerts={false}
55+
enableAnnotationRendering
56+
enableDoubleTapZoom
57+
onPressLink={(href) => {
58+
const url = pdfLinkUrl(href, props.uri);
59+
if (url !== null) void tryOpenExternalUrl(url, "file-preview");
60+
}}
61+
onError={() => {
62+
cached.current?.dispose();
63+
cached.current = null;
64+
setError("The PDF could not be rendered. It may be damaged or password protected.");
65+
}}
66+
style={{ flex: 1, width: "100%" }}
67+
/>
68+
);
69+
}
70+
71+
export function PdfPreview(props: { readonly uri: string }) {
72+
const [attempt, setAttempt] = useState(0);
73+
return (
74+
<NativePdfPreview
75+
key={`${props.uri}:${attempt}`}
76+
uri={props.uri}
77+
onRetry={() => setAttempt((value) => value + 1)}
78+
/>
79+
);
80+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useState } from "react";
2+
import { View } from "react-native";
3+
import { WebView } from "react-native-webview";
4+
5+
import { FilePreviewLoading, FilePreviewNotice } from "../features/files/FilePreviewFeedback";
6+
7+
export function PdfPreview(props: { readonly uri: string }) {
8+
const [error, setError] = useState<string | null>(null);
9+
return (
10+
<View className="flex-1 bg-card">
11+
{error ? <FilePreviewNotice title="Could not load PDF">{error}</FilePreviewNotice> : null}
12+
<WebView
13+
source={{ uri: props.uri }}
14+
originWhitelist={["*"]}
15+
automaticallyAdjustContentInsets={false}
16+
contentInsetAdjustmentBehavior="never"
17+
startInLoadingState
18+
onError={(event) =>
19+
setError(event.nativeEvent.description || "The PDF could not be loaded.")
20+
}
21+
onHttpError={(event) =>
22+
setError(`The server returned HTTP ${event.nativeEvent.statusCode}.`)
23+
}
24+
renderLoading={() => <FilePreviewLoading message="Loading PDF..." background="card" />}
25+
style={{ flex: 1, backgroundColor: "transparent" }}
26+
/>
27+
</View>
28+
);
29+
}

‎apps/mobile/src/components/ui/AppSymbol.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import IconArrowUpRightCircle from "@tabler/icons-react-native/IconArrowUpRightC
2323
import IconArrowsLeftRight from "@tabler/icons-react-native/IconArrowsLeftRight";
2424
import IconArrowsDiagonal2 from "@tabler/icons-react-native/IconArrowsDiagonal2";
2525
import IconArrowsMinimize from "@tabler/icons-react-native/IconArrowsMinimize";
26+
import IconBan from "@tabler/icons-react-native/IconBan";
2627
import IconBellRinging from "@tabler/icons-react-native/IconBellRinging";
2728
import IconBolt from "@tabler/icons-react-native/IconBolt";
2829
import IconBox from "@tabler/icons-react-native/IconBox";
@@ -236,6 +237,7 @@ const ANDROID_ICON_BY_SF_SYMBOL = {
236237
play: IconPlayerPlay,
237238
plus: IconPlus,
238239
minus: IconMinus,
240+
"slash.circle": IconBan,
239241
"qrcode.viewfinder": IconQrcode,
240242
"point.3.connected.trianglepath.dotted": IconNetwork,
241243
"point.topleft.down.curvedto.point.bottomright.up": IconGitMerge,

‎apps/mobile/src/features/browser/BrowserPreviewRouteScreen.tsx‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
33
import * as Dialog from "../../components/ui/Dialog";
44
import { useIsFocused, useNavigation, type StaticScreenProps } from "@react-navigation/native";
55
import { createNativeStackNavigator } from "@react-navigation/native-stack";
6-
import {
7-
EnvironmentId,
8-
FILL_PREVIEW_VIEWPORT,
9-
ThreadId,
10-
type PreviewSessionSnapshot,
11-
} from "@t3tools/contracts";
12-
import { normalizePreviewUrl } from "@t3tools/shared/preview";
6+
import { EnvironmentId, ThreadId, type PreviewSessionSnapshot } from "@t3tools/contracts";
7+
import { normalizePreviewUrl, previewPdfAssetUrl } from "@t3tools/shared/preview";
8+
import * as Option from "effect/Option";
139
import { useCallback, useEffect, useRef, useState } from "react";
1410
import { AppState, Platform, TextInput, View } from "react-native";
1511
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -18,6 +14,7 @@ import { ControlPill } from "../../components/ui/ControlPill";
1814
import { AppBar } from "../../components/ui/AppBar";
1915

2016
import { useThreadServerBrowserTabs } from "../../state/preview";
17+
import { usePreparedConnection } from "../../state/session";
2118
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
2219
import { BrowserTabMenu } from "./BrowserTabMenu";
2320
import { browserTabTitle, browserTabUrl, latestBrowserTab } from "./browserTabs";
@@ -79,6 +76,8 @@ function BrowserPreviewScreen({
7976
}) {
8077
const insets = useSafeAreaInsets();
8178
const { themeVariables } = useAppearancePreferences();
79+
const prepared = usePreparedConnection(environmentId);
80+
const httpBaseUrl = Option.isSome(prepared) ? prepared.value.httpBaseUrl : null;
8281
const focused = useIsFocused();
8382
const [foreground, setForeground] = useState(AppState.currentState !== "background");
8483
const [selectedTabId, setSelectedTabId] = useState(initialTabId);
@@ -88,6 +87,8 @@ function BrowserPreviewScreen({
8887
const streamRef = useRef<PreviewStreamRef>(null);
8988
const { tabs, loaded } = useThreadServerBrowserTabs({ environmentId, threadId, enabled: true });
9089
const tab = tabs.find((entry) => entry.tabId === selectedTabId) ?? latestBrowserTab(tabs);
90+
const pdfUri =
91+
tab?.navStatus._tag === "Success" ? previewPdfAssetUrl(tab.navStatus.url, httpBaseUrl) : null;
9192
const tabId = tab?.tabId ?? null;
9293
if (tabId !== null && tabId !== selectedTabId) {
9394
setPictureInPicture(NO_PICTURE_IN_PICTURE);
@@ -121,7 +122,9 @@ function BrowserPreviewScreen({
121122
options={{ headerBackVisible: false }}
122123
actions={
123124
// Android WebView cannot play video in picture in picture.
124-
Platform.OS === "ios" && pictureInPicture.supported
125+
Platform.OS === "ios" &&
126+
pictureInPicture.supported &&
127+
(pictureInPicture.active || pdfUri === null)
125128
? [
126129
{
127130
accessibilityLabel: pictureInPicture.active
@@ -181,6 +184,7 @@ function BrowserPreviewScreen({
181184
environmentId={environmentId}
182185
threadId={threadId}
183186
tabId={tab.tabId}
187+
pdfUri={pdfUri}
184188
ref={streamRef}
185189
interactive
186190
background={themeVariables["--color-sheet-solid"]}

0 commit comments

Comments
 (0)