import { useGT } from "gt-react"; import { useAtomValue } from "@effect/atom-react"; import { type KeybindingCommand, type KeybindingShortcut, MODEL_PICKER_JUMP_KEYBINDING_COMMANDS, type ScopedThreadRef, THREAD_JUMP_KEYBINDING_COMMANDS, } from "@t3tools/contracts"; import { type FilePreviewScroll, filePreviewRestoreScrollMessage, isFilePreviewScrollRestored, readFilePreviewScroll, type HtmlRenderShortcut, htmlRenderShortcutsMessage, htmlRenderThemeFragment, htmlRenderThemeMessage, htmlRenderResult, readHtmlRenderContentHeight, readHtmlRenderImageRequest, readHtmlRenderKeyDown, readHtmlRenderLinkRequest, } from "@t3tools/shared/htmlRender"; import { isMacPlatform, matchesKeybindingShortcut, normalizeEventKey, } from "@t3tools/shared/keybindings"; import { lazy, Suspense, useEffect, useEffectEvent, useLayoutEffect, useRef, useState, type ReactNode, } from "react"; import { isElectron } from "~/env"; import { MoonIcon, SunIcon } from "lucide-react"; import { FileSurfaceAction, FileSurfaceLoading } from "./fileSurfaceChrome"; import type { PdfViewState } from "./PdfViewer"; import { useClientSettings } from "~/hooks/useSettings"; import { useRenderedAppearance } from "~/hooks/useTheme"; import "./pdfAppearance.css"; import { useOpenLink } from "~/browser/useOpenLink"; import { ExpandedImageDialog } from "~/components/chat/ExpandedImageDialog"; import type { ExpandedImagePreview } from "~/components/chat/ExpandedImagePreview"; import { useHtmlRenderTheme } from "~/hooks/useHtmlRenderTheme"; import { shortcutConflictKey } from "~/keybindings"; import { useOpenChangeRequestLink } from "~/lib/openPullRequestLink"; import { readLocalApi } from "~/localApi"; import { cn } from "~/lib/utils"; import { primaryServerKeybindingsAtom, serverEnvironment } from "~/state/server"; /** * The page reports the keys, and a page script can report any key it likes, * so only commands that change what the reader sees are here, never one that * changes a thread. */ const HTML_RENDER_SHORTCUT_COMMANDS: ReadonlySet = new Set([ "commandPalette.toggle", "filePicker.toggle", "projectSearch.toggle", "chat.find", "sidebar.toggle", "rightPanel.toggle", "rightPanel.toggleMaximized", "threadPanel.toggle", "terminal.toggle", "preview.toggle", "navigation.back", "navigation.forward", "thread.previous", "thread.next", ...THREAD_JUMP_KEYBINDING_COMMANDS, ...MODEL_PICKER_JUMP_KEYBINDING_COMMANDS, ]); export const isPdfPreviewFile = (path: string): boolean => /\.pdf$/i.test(path.split(/[?#]/, 1)[0] ?? ""); const PdfViewer = lazy(() => import("./PdfViewer")); // The document stays mounted while each revision gets a fresh PDF.js document. function PdfDocument(props: { src: string; title: string; documentKey: string }) { const gt = useGT(); const [view, setView] = useState({ page: 0, offset: 0, zoom: null, left: 0 }); const appearance = useClientSettings((settings) => settings.pdfAppearance); const themeAppearance = useRenderedAppearance(); const [override, setOverride] = useState(null); const dark = override ?? (appearance === "dark" || (appearance === "match-app" && themeAppearance === "dark")); return (
setOverride(!dark)} > {dark ? : } } />
); } // Keep signed URL renewals from reloading the same revision. function PdfDocumentRevision(props: { src: string; title: string; positionKey: string; initialView: PdfViewState; onViewChange: (view: PdfViewState) => void; appearanceAction: ReactNode; }) { const [src] = useState(props.src); // The Electron shell does not enable PDF plugins. WebKit embeds on iOS // expose only the first page, even when pdfViewerEnabled reports support. const native = !isElectron && navigator.pdfViewerEnabled && !/iPad|iPhone|iPod/.test(navigator.userAgent) && !(navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1); return native ? ( <>
{props.appearanceAction}
{/* oxlint-disable-next-line react/iframe-missing-sandbox -- Chromium's PDF plugin requires an unsandboxed frame; src is an exact-file asset grant. */}