diff --git a/apps/web/src/browser/openFileInPreview.ts b/apps/web/src/browser/openFileInPreview.ts index b89b87c928..b4bbefb132 100644 --- a/apps/web/src/browser/openFileInPreview.ts +++ b/apps/web/src/browser/openFileInPreview.ts @@ -10,6 +10,11 @@ import { type AtomCommandResult, mapAtomCommandResult, } from "@t3tools/client-runtime/state/runtime"; +import { + isWorkspaceBrowserPreviewPath, + isWorkspaceImagePreviewPath, + isWorkspacePreviewEntryPath, +} from "@t3tools/shared/filePreview"; import * as Cause from "effect/Cause"; import * as Data from "effect/Data"; import { AsyncResult } from "effect/unstable/reactivity"; @@ -22,8 +27,9 @@ import { } from "~/previewStateStore"; import { useRightPanelStore } from "~/rightPanelStore"; -export const isBrowserPreviewFile = (path: string): boolean => - /\.(?:html?|pdf)$/i.test(path.split(/[?#]/, 1)[0] ?? ""); +export const isBrowserPreviewFile = isWorkspaceBrowserPreviewPath; +export const isImagePreviewFile = isWorkspaceImagePreviewPath; +export const isWorkspacePreviewFile = isWorkspacePreviewEntryPath; export class BrowserPreviewUnavailableError extends Data.TaggedError( "BrowserPreviewUnavailableError", diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 711a545d90..e315e4b83d 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -76,7 +76,7 @@ import { useAtomCommand } from "../state/use-atom-command"; import { useAtomQueryRunner } from "../state/use-atom-query-runner"; import { isPreviewSupportedInRuntime } from "../previewStateStore"; import { - isBrowserPreviewFile, + isWorkspacePreviewFile, openFileInPreview, openUrlInPreview, BrowserPreviewUnavailableError, @@ -1470,7 +1470,7 @@ function ChatMarkdown({ onOpenInBrowser={ threadRef && isPreviewSupportedInRuntime() && - isBrowserPreviewFile(fileLinkMeta.filePath) + isWorkspacePreviewFile(fileLinkMeta.filePath) ? () => openMarkdownFileInPreview(fileLinkMeta.filePath) : undefined } diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index cf5bb9de5e..cac7b81a5d 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -110,6 +110,8 @@ import { type RightPanelSurface, useRightPanelStore, } from "../rightPanelStore"; +import { isImagePreviewFile, openFileInPreview } from "../browser/openFileInPreview"; +import { resolvePathLinkTarget } from "../terminal-links"; import { isPreviewSupportedInRuntime, setActivePreviewTab, @@ -179,7 +181,12 @@ import { import { terminalEnvironment } from "../state/terminal"; import { threadEnvironment } from "../state/threads"; import { vcsEnvironment } from "../state/vcs"; -import { useEnvironments, usePrimaryEnvironment } from "../state/environments"; +import { + useEnvironmentHttpBaseUrl, + useEnvironments, + usePrimaryEnvironment, +} from "../state/environments"; +import { assetEnvironment } from "../state/assets"; import { useProject, useProjects, @@ -228,6 +235,7 @@ import { sanitizeThreadErrorMessage } from "~/rpc/transportError"; import { RightPanelSheet } from "./RightPanelSheet"; import { previewEnvironment } from "../state/preview"; import { useAtomCommand } from "../state/use-atom-command"; +import { useAtomQueryRunner } from "../state/use-atom-query-runner"; import { Button } from "./ui/button"; import { buildVersionMismatchDismissalKey, @@ -1012,6 +1020,9 @@ function ChatViewContent(props: ChatViewProps) { reportFailure: false, }); const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false }); + const createAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, { + reportFailure: false, + }); const closePreview = useAtomCommand(previewEnvironment.close, "preview close"); const { environments } = useEnvironments(); const primaryEnvironment = usePrimaryEnvironment(); @@ -1339,6 +1350,9 @@ function ChatViewContent(props: ChatViewProps) { ? scopeProjectRef(activeThread.environmentId, activeThread.projectId) : null; const activeProject = useProject(activeProjectRef); + const activeEnvironmentHttpBaseUrl = useEnvironmentHttpBaseUrl( + activeProject?.environmentId ?? null, + ); const activeEnvironmentShell = useEnvironmentQuery( activeThread ? environmentShell.stateAtom(activeThread.environmentId) : null, ); @@ -2740,9 +2754,50 @@ function ChatViewContent(props: ChatViewProps) { const openFileSurface = useCallback( (relativePath: string) => { if (!activeThreadRef || !activeProject) return; - useRightPanelStore.getState().openFile(activeThreadRef, relativePath); + const openFallback = () => { + useRightPanelStore.getState().openFile(activeThreadRef, relativePath); + }; + if ( + !isImagePreviewFile(relativePath) || + !isPreviewSupportedInRuntime() || + !activeWorkspaceRoot || + !activeEnvironmentHttpBaseUrl + ) { + openFallback(); + return; + } + + const absolutePath = resolvePathLinkTarget(relativePath, activeWorkspaceRoot); + void (async () => { + const result = await openFileInPreview({ + threadRef: activeThreadRef, + filePath: absolutePath, + httpBaseUrl: activeEnvironmentHttpBaseUrl, + createAssetUrl, + openPreview, + }); + if (result._tag === "Success" || isAtomCommandInterrupted(result)) { + return; + } + const error = squashAtomCommandFailure(result); + toastManager.add( + stackedThreadToast({ + type: "warning", + title: "Unable to open image preview", + description: error instanceof Error ? error.message : "Opening the file instead.", + }), + ); + openFallback(); + })(); }, - [activeProject, activeThreadRef], + [ + activeEnvironmentHttpBaseUrl, + activeProject, + activeThreadRef, + activeWorkspaceRoot, + createAssetUrl, + openPreview, + ], ); const togglePreviewPanel = useCallback(() => { if (!activeThreadRef || !isPreviewSupportedInRuntime()) return; diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index f39af581d5..341bb553a7 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -21,6 +21,9 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useOpenInPreferredEditor } from "../editorPreferences"; import { type DraftId } from "../composerDraftStore"; import { openDiffFilePrimaryAction } from "../diffFileActions"; +import { isImagePreviewFile, openFileInPreview } from "../browser/openFileInPreview"; +import { resolvePathLinkTarget } from "../terminal-links"; +import { isPreviewSupportedInRuntime } from "../previewStateStore"; import { useCheckpointDiff } from "~/lib/checkpointDiffState"; import { cn } from "~/lib/utils"; import { selectThreadDiffPanelSelection, useDiffPanelStore } from "../diffPanelStore"; @@ -61,10 +64,16 @@ import { } from "./ui/menu"; import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; import { useEnvironmentQuery } from "../state/query"; +import { assetEnvironment } from "../state/assets"; +import { previewEnvironment } from "../state/preview"; import { serverEnvironment } from "../state/server"; import { reviewEnvironment } from "../state/review"; import { vcsEnvironment } from "../state/vcs"; +import { useEnvironmentHttpBaseUrl } from "../state/environments"; +import { useAtomCommand } from "../state/use-atom-command"; +import { useAtomQueryRunner } from "../state/use-atom-query-runner"; import { buildBaseRefChoices, filterBaseRefChoices } from "../lib/baseRefChoices"; +import { stackedThreadToast, toastManager } from "./ui/toast"; type DiffRenderMode = "stacked" | "split"; type DiffThemeType = "light" | "dark"; @@ -213,6 +222,11 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff : null, ); const activeCwd = activeThread?.worktreePath ?? activeProject?.workspaceRoot; + const environmentHttpBaseUrl = useEnvironmentHttpBaseUrl(activeThread?.environmentId ?? null); + const createAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, { + reportFailure: false, + }); + const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false }); const serverConfig = useAtomValue( serverEnvironment.configValueAtom(activeThread?.environmentId ?? null), ); @@ -445,30 +459,72 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff const openDiffFile = useCallback( (filePath: string) => { - openDiffFilePrimaryAction({ - threadRef: routeThreadRef, - filePath, - activeCwd, - openInEditor: (targetPath) => { - void (async () => { - const result = await openInPreferredEditor(targetPath); - if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { - console.warn("Failed to open diff file in editor.", { - operation: "open-diff-file", - ...(routeThreadRef - ? { - environmentId: routeThreadRef.environmentId, - threadId: routeThreadRef.threadId, - } - : {}), - ...safeErrorLogAttributes(squashAtomCommandFailure(result)), - }); - } - })(); - }, - }); + const openFallback = () => { + openDiffFilePrimaryAction({ + threadRef: routeThreadRef, + filePath, + activeCwd, + openInEditor: (targetPath) => { + void (async () => { + const result = await openInPreferredEditor(targetPath); + if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { + console.warn("Failed to open diff file in editor.", { + operation: "open-diff-file", + ...(routeThreadRef + ? { + environmentId: routeThreadRef.environmentId, + threadId: routeThreadRef.threadId, + } + : {}), + ...safeErrorLogAttributes(squashAtomCommandFailure(result)), + }); + } + })(); + }, + }); + }; + + if ( + !routeThreadRef || + !activeCwd || + !environmentHttpBaseUrl || + !isPreviewSupportedInRuntime() || + !isImagePreviewFile(filePath) + ) { + openFallback(); + return; + } + + void (async () => { + const result = await openFileInPreview({ + threadRef: routeThreadRef, + filePath: resolvePathLinkTarget(filePath, activeCwd), + httpBaseUrl: environmentHttpBaseUrl, + createAssetUrl, + openPreview, + }); + if (result._tag === "Success" || isAtomCommandInterrupted(result)) { + return; + } + const error = squashAtomCommandFailure(result); + toastManager.add( + stackedThreadToast({ + type: "warning", + title: "Unable to open image preview", + description: error instanceof Error ? error.message : "Opening the file instead.", + }), + ); + openFallback(); + })(); }, - [activeCwd, openInPreferredEditor, routeThreadRef], + [ + activeCwd, + createAssetUrl, + environmentHttpBaseUrl, + openInPreferredEditor, + openPreview, + routeThreadRef, + ], ); const toggleDiffFileCollapsed = useCallback( (fileKey: string) => { diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsx index 89176cd452..80b81ee376 100644 --- a/apps/web/src/components/files/FilePreviewPanel.tsx +++ b/apps/web/src/components/files/FilePreviewPanel.tsx @@ -15,7 +15,7 @@ import { ChevronRight, Code2, Eye, FolderTree, Globe2, LoaderCircle } from "luci import * as Schema from "effect/Schema"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; -import { isBrowserPreviewFile, openFileInPreview } from "~/browser/openFileInPreview"; +import { isWorkspacePreviewFile, openFileInPreview } from "~/browser/openFileInPreview"; import ChatMarkdown from "~/components/ChatMarkdown"; import { OpenInPicker } from "~/components/chat/OpenInPicker"; import { useTheme } from "~/hooks/useTheme"; @@ -633,8 +633,8 @@ export default function FilePreviewPanel({ isMarkdown && markdownView.path === relativePath && (revealLine === null || markdownView.revealRequestId === revealRequestId); - const canOpenInBrowser = - relativePath !== null && isPreviewSupportedInRuntime() && isBrowserPreviewFile(relativePath); + const canOpenInPreview = + relativePath !== null && isPreviewSupportedInRuntime() && isWorkspacePreviewFile(relativePath); const absolutePath = relativePath ? resolvePathLinkTarget(relativePath, cwd) : null; const breadcrumbs = useMemo( () => (relativePath ? fileBreadcrumbs(projectName, relativePath) : []), @@ -661,7 +661,7 @@ export default function FilePreviewPanel({ }); }; - const handleOpenInBrowser = useCallback(() => { + const handleOpenInPreview = useCallback(() => { if (!absolutePath || !environmentHttpBaseUrl) return; void (async () => { const result = await openFileInPreview({ @@ -678,7 +678,7 @@ export default function FilePreviewPanel({ toastManager.add( stackedThreadToast({ type: "error", - title: "Unable to open file in browser", + title: "Unable to open file in preview", description: error instanceof Error ? error.message : "An error occurred.", }), ); @@ -757,15 +757,15 @@ export default function FilePreviewPanel({ ) : null} - {canOpenInBrowser ? ( + {canOpenInPreview ? ( @@ -773,7 +773,7 @@ export default function FilePreviewPanel({ } /> - Open file in preview browser + Open file in preview ) : null}