1359 lines
48 KiB
TypeScript
1359 lines
48 KiB
TypeScript
import { Spinner } from "~/components/ui/spinner";
|
|
import type {
|
|
ChatFileAttachment,
|
|
EditorId,
|
|
EnvironmentId,
|
|
ResolvedKeybindingsConfig,
|
|
ScopedThreadRef,
|
|
} from "@t3tools/contracts";
|
|
import {
|
|
isWorkspaceImagePreviewPath,
|
|
isWorkspaceVideoPreviewPath,
|
|
} from "@t3tools/shared/filePreview";
|
|
import { VirtualizedFile, type SelectedLineRange } from "@pierre/diffs";
|
|
import { Editor } from "@pierre/diffs/editor";
|
|
import { EditProvider, File, type FileOptions, Virtualizer } from "@pierre/diffs/react";
|
|
import { DiffWorkerPoolProvider } from "../DiffWorkerPoolProvider";
|
|
import {
|
|
isAtomCommandInterrupted,
|
|
squashAtomCommandFailure,
|
|
} from "@t3tools/client-runtime/state/runtime";
|
|
import { mediaFileReference } from "@t3tools/client-runtime/media-reference";
|
|
import { Code2, Eye, FolderTree, Globe2 } from "lucide-react";
|
|
import * as Schema from "effect/Schema";
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
import { isBrowserPreviewFile, openFileInPreview } from "~/browser/openFileInPreview";
|
|
import { useAssetUrlRefresh, useAssetUrlState } from "~/assets/assetUrls";
|
|
import { OpenInPicker } from "~/components/chat/OpenInPicker";
|
|
import { PierreEntryIcon } from "~/components/chat/PierreEntryIcon";
|
|
import { MediaVideoPlayer } from "~/components/media/MediaVideoPlayer";
|
|
import { MediaActions, type MediaActionSource } from "~/components/media/MediaActions";
|
|
import { useRemoteOpenState } from "~/remoteOpen";
|
|
import { useClientSettings } from "~/hooks/useSettings";
|
|
import { useTheme } from "~/hooks/useTheme";
|
|
import { getLocalStorageItem, setLocalStorageItem, useLocalStorage } from "~/hooks/useLocalStorage";
|
|
import { useWorkspaceMutationRefresh } from "~/hooks/useWorkspaceMutationRefresh";
|
|
import { DIFF_SURFACE_THEME_UNSAFE_CSS, resolveDiffThemeName } from "~/lib/diffRendering";
|
|
import { PREFERRED_HIGHLIGHTER } from "~/lib/syntaxHighlighting";
|
|
import { cn } from "~/lib/utils";
|
|
import { isPreviewSupportedInRuntime } from "~/previewStateStore";
|
|
import { isAbsolutePath, resolvePathLinkTarget } from "~/terminal-links";
|
|
import { ScrollArea } from "~/components/ui/scroll-area";
|
|
import { Toggle } from "~/components/ui/toggle";
|
|
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
|
|
import { stackedThreadToast, toastManager } from "~/components/ui/toast";
|
|
import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";
|
|
import { buildFileReviewComment } from "~/reviewCommentContext";
|
|
import { assetEnvironment } from "~/state/assets";
|
|
import { useEnvironmentHttpBaseUrl, usePrimaryEnvironmentId } from "~/state/environments";
|
|
import { previewEnvironment } from "~/state/preview";
|
|
import { useAtomCommand } from "~/state/use-atom-command";
|
|
import { useAtomQueryRunner } from "~/state/use-atom-query-runner";
|
|
|
|
import FileBrowserPanel from "./FileBrowserPanel";
|
|
import { FileBreadcrumbs } from "./FileBreadcrumbs";
|
|
import { FileMarkdownPreview } from "./FileMarkdownPreview";
|
|
import {
|
|
type FileCommentAnnotationEntry,
|
|
type FileCommentAnnotationGroup,
|
|
type FileCommentLineAnnotation,
|
|
formatFileCommentRange,
|
|
nextFileCommentId,
|
|
normalizeFileCommentRange,
|
|
remapFileCommentAnnotations,
|
|
} from "./fileCommentAnnotations";
|
|
import { installFileEditorDismissal } from "./fileEditorDismissal";
|
|
import { resolveCenteredFileLineScrollTop } from "./fileLineReveal";
|
|
import { DiffCommentAnnotation } from "../diffs/DiffCommentAnnotation";
|
|
import { projectFileCacheKey, projectFileEditorCacheKey } from "./fileContentRevision";
|
|
import {
|
|
isMarkdownPreviewFile,
|
|
setMarkdownTaskChecked,
|
|
shouldShowFileExplorer,
|
|
} from "./filePreviewMode";
|
|
import { useFileSaveCoordinator } from "./useFileSaveCoordinator";
|
|
import {
|
|
getOptimisticProjectFileQueryData,
|
|
setProjectFileQueryData,
|
|
useProjectFileQuery,
|
|
} from "./projectFilesQueryState";
|
|
|
|
interface FilePreviewPanelProps {
|
|
environmentId: EnvironmentId;
|
|
cwd: string;
|
|
projectName: string;
|
|
relativePath: string | null;
|
|
attachment?: ChatFileAttachment;
|
|
threadRef: ScopedThreadRef;
|
|
composerDraftTarget: ScopedThreadRef | DraftId;
|
|
keybindings: ResolvedKeybindingsConfig;
|
|
availableEditors: ReadonlyArray<EditorId>;
|
|
revealLine: number | null;
|
|
revealRequestId: number;
|
|
onOpenFile: (relativePath: string) => void;
|
|
onPendingChange: (relativePath: string, pending: boolean) => void;
|
|
selectedFilePending: boolean;
|
|
workspaceMutationId: string | null;
|
|
}
|
|
|
|
const FILE_EXPLORER_STORAGE_KEY = "t3code.fileExplorerOpen";
|
|
const RENDER_MARKDOWN_STORAGE_KEY = "t3code.renderMarkdown";
|
|
const RENDER_BROWSER_FILE_STORAGE_KEY = "t3code.renderBrowserFile";
|
|
const FILE_LINK_REVEAL_ATTRIBUTE = "data-file-link-reveal";
|
|
const FILE_LINK_REVEAL_UNSAFE_CSS = `
|
|
${DIFF_SURFACE_THEME_UNSAFE_CSS}
|
|
|
|
diffs-container {
|
|
--diffs-bg: var(--code-background, var(--background)) !important;
|
|
--diffs-light-bg: var(--code-background, var(--background)) !important;
|
|
--diffs-dark-bg: var(--code-background, var(--background)) !important;
|
|
background-color: var(--code-background, var(--background)) !important;
|
|
color: var(--code-foreground, var(--foreground)) !important;
|
|
}
|
|
|
|
[${FILE_LINK_REVEAL_ATTRIBUTE}][data-line] {
|
|
background-color: light-dark(
|
|
color-mix(
|
|
in lab,
|
|
var(--diffs-computed-diff-line-bg) 82%,
|
|
var(--diffs-bg-selection-override, var(--diffs-selection-base))
|
|
),
|
|
color-mix(
|
|
in lab,
|
|
var(--diffs-computed-diff-line-bg) 75%,
|
|
var(--diffs-bg-selection-override, var(--diffs-selection-base))
|
|
)
|
|
) !important;
|
|
}
|
|
|
|
[${FILE_LINK_REVEAL_ATTRIBUTE}][data-column-number] {
|
|
background-color: light-dark(
|
|
color-mix(
|
|
in lab,
|
|
var(--diffs-computed-diff-line-bg) 75%,
|
|
var(--diffs-bg-selection-number-override, var(--diffs-selection-base))
|
|
),
|
|
color-mix(
|
|
in lab,
|
|
var(--diffs-computed-diff-line-bg) 60%,
|
|
var(--diffs-bg-selection-number-override, var(--diffs-selection-base))
|
|
)
|
|
) !important;
|
|
color: var(--diffs-selection-number-fg) !important;
|
|
}
|
|
`;
|
|
type FilePostRender = NonNullable<FileOptions<unknown>["onPostRender"]>;
|
|
|
|
function WorkspaceImagePreview(props: {
|
|
readonly environmentId: EnvironmentId;
|
|
readonly threadRef: ScopedThreadRef;
|
|
readonly absolutePath: string;
|
|
readonly workspaceRoot: string;
|
|
readonly alt: string;
|
|
readonly workspaceMutationId: string | null;
|
|
}) {
|
|
const resource = useMemo(
|
|
() => ({
|
|
_tag: "workspace-file" as const,
|
|
threadId: props.threadRef.threadId,
|
|
path: props.absolutePath,
|
|
}),
|
|
[props.threadRef.threadId, props.absolutePath],
|
|
);
|
|
const assetUrl = useAssetUrlState(props.environmentId, resource);
|
|
const [failedUrl, setFailedUrl] = useState<string | null>(null);
|
|
const revisionSuffix =
|
|
props.workspaceMutationId === null
|
|
? ""
|
|
: `${assetUrl._tag === "Success" && assetUrl.url.includes("?") ? "&" : "?"}workspace-revision=${encodeURIComponent(props.workspaceMutationId)}`;
|
|
const imageUrl = assetUrl._tag === "Success" ? `${assetUrl.url}${revisionSuffix}` : null;
|
|
const actionsSource: MediaActionSource = {
|
|
kind: "image",
|
|
name: props.alt,
|
|
src: imageUrl,
|
|
reference: mediaFileReference(props.absolutePath, props.workspaceRoot),
|
|
asset: { environmentId: props.environmentId, resource },
|
|
};
|
|
|
|
if (assetUrl._tag === "Failure" || (imageUrl !== null && failedUrl === imageUrl)) {
|
|
return (
|
|
<MediaActions source={actionsSource}>
|
|
<div className="flex min-h-0 flex-1 items-center justify-center px-6 text-center text-xs leading-relaxed text-destructive">
|
|
Unable to load workspace image.
|
|
</div>
|
|
</MediaActions>
|
|
);
|
|
}
|
|
|
|
return assetUrl._tag === "Success" && imageUrl !== null ? (
|
|
<div className="flex min-h-0 flex-1 items-center justify-center overflow-auto p-4">
|
|
<MediaActions source={actionsSource}>
|
|
<img
|
|
className="max-h-full max-w-full object-contain"
|
|
src={imageUrl}
|
|
alt={props.alt}
|
|
onError={() => setFailedUrl(imageUrl)}
|
|
/>
|
|
</MediaActions>
|
|
</div>
|
|
) : (
|
|
<div className="flex min-h-0 flex-1 items-center justify-center text-muted-foreground">
|
|
<Spinner className="size-5" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const isPdfPreviewFile = (path: string): boolean => /\.pdf$/i.test(path.split(/[?#]/, 1)[0] ?? "");
|
|
|
|
function BrowserDocumentFrame(props: {
|
|
readonly src: string;
|
|
readonly title: string;
|
|
readonly pdf: boolean;
|
|
}) {
|
|
const className = "min-h-0 flex-1 border-0 bg-white";
|
|
// The built-in PDF viewer needs an unsandboxed frame; a PDF runs no scripts.
|
|
return props.pdf ? (
|
|
// oxlint-disable-next-line react/iframe-missing-sandbox
|
|
<iframe key={props.src} src={props.src} title={props.title} className={className} />
|
|
) : (
|
|
<iframe
|
|
key={props.src}
|
|
src={props.src}
|
|
title={props.title}
|
|
className={className}
|
|
sandbox="allow-scripts allow-forms allow-popups allow-modals"
|
|
/>
|
|
);
|
|
}
|
|
|
|
function AttachmentBrowserPreview(props: {
|
|
readonly environmentId: EnvironmentId;
|
|
readonly attachment: ChatFileAttachment;
|
|
}) {
|
|
const resource = useMemo(
|
|
() => ({
|
|
_tag: "attachment" as const,
|
|
attachmentId: props.attachment.id,
|
|
fileName: props.attachment.name,
|
|
mimeType: props.attachment.mimeType,
|
|
disposition: "inline" as const,
|
|
}),
|
|
[props.attachment.id, props.attachment.mimeType, props.attachment.name],
|
|
);
|
|
const assetUrl = useAssetUrlState(props.environmentId, resource);
|
|
|
|
if (assetUrl._tag === "Failure") {
|
|
return (
|
|
<div className="flex min-h-0 flex-1 items-center justify-center px-6 text-center text-xs leading-relaxed text-destructive">
|
|
Unable to load attachment preview.
|
|
</div>
|
|
);
|
|
}
|
|
if (assetUrl._tag !== "Success") {
|
|
return (
|
|
<div className="flex min-h-0 flex-1 items-center justify-center text-muted-foreground">
|
|
<Spinner className="size-5" />
|
|
</div>
|
|
);
|
|
}
|
|
return (
|
|
<BrowserDocumentFrame
|
|
src={assetUrl.url}
|
|
title={props.attachment.name}
|
|
pdf={
|
|
isPdfPreviewFile(props.attachment.name) ||
|
|
props.attachment.mimeType.split(";", 1)[0]?.trim().toLowerCase() === "application/pdf"
|
|
}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Renders an HTML or PDF file in place from its signed asset URL. HTML runs in
|
|
* a sandboxed frame with an opaque origin, so a page cannot reach the app's
|
|
* session or storage. A file inside the workspace may load sibling assets; a
|
|
* host file outside it is served on its own.
|
|
*/
|
|
function WorkspaceBrowserPreview(props: {
|
|
readonly environmentId: EnvironmentId;
|
|
readonly threadRef: ScopedThreadRef;
|
|
readonly absolutePath: string;
|
|
readonly workspaceRoot: string;
|
|
readonly title: string;
|
|
readonly workspaceMutationId: string | null;
|
|
}) {
|
|
const insideWorkspace =
|
|
mediaFileReference(props.absolutePath, props.workspaceRoot).relativePath !== undefined;
|
|
const resource = useMemo(
|
|
() => ({
|
|
_tag: insideWorkspace ? ("workspace-file" as const) : ("media-file" as const),
|
|
threadId: props.threadRef.threadId,
|
|
path: props.absolutePath,
|
|
}),
|
|
[insideWorkspace, props.threadRef.threadId, props.absolutePath],
|
|
);
|
|
const assetUrl = useAssetUrlState(props.environmentId, resource);
|
|
const revisionSuffix =
|
|
props.workspaceMutationId === null
|
|
? ""
|
|
: `${assetUrl._tag === "Success" && assetUrl.url.includes("?") ? "&" : "?"}workspace-revision=${encodeURIComponent(props.workspaceMutationId)}`;
|
|
|
|
if (assetUrl._tag === "Failure") {
|
|
return (
|
|
<div className="flex min-h-0 flex-1 items-center justify-center px-6 text-center text-xs leading-relaxed text-destructive">
|
|
Unable to load file preview.
|
|
</div>
|
|
);
|
|
}
|
|
if (assetUrl._tag !== "Success") {
|
|
return (
|
|
<div className="flex min-h-0 flex-1 items-center justify-center text-muted-foreground">
|
|
<Spinner className="size-5" />
|
|
</div>
|
|
);
|
|
}
|
|
return (
|
|
<BrowserDocumentFrame
|
|
src={`${assetUrl.url}${revisionSuffix}`}
|
|
title={props.title}
|
|
pdf={isPdfPreviewFile(props.absolutePath)}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function WorkspaceVideoPreview(props: {
|
|
readonly environmentId: EnvironmentId;
|
|
readonly threadRef: ScopedThreadRef;
|
|
readonly absolutePath: string;
|
|
readonly workspaceRoot: string;
|
|
readonly name: string;
|
|
readonly workspaceMutationId: string | null;
|
|
}) {
|
|
const resource = useMemo(
|
|
() => ({
|
|
_tag: "media-file" as const,
|
|
threadId: props.threadRef.threadId,
|
|
path: props.absolutePath,
|
|
}),
|
|
[props.threadRef.threadId, props.absolutePath],
|
|
);
|
|
const assetUrl = useAssetUrlState(props.environmentId, resource);
|
|
const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, resource);
|
|
useWorkspaceMutationRefresh({
|
|
mutationId: props.workspaceMutationId,
|
|
resourceKey: JSON.stringify([props.environmentId, resource]),
|
|
refresh: () => {
|
|
// Failed refreshes flow through assetUrl and can be retried from the player.
|
|
void refreshAssetUrl().catch(() => undefined);
|
|
},
|
|
});
|
|
const revisionSuffix =
|
|
props.workspaceMutationId === null
|
|
? ""
|
|
: `${assetUrl._tag === "Success" && assetUrl.url.includes("?") ? "&" : "?"}workspace-revision=${encodeURIComponent(props.workspaceMutationId)}`;
|
|
const latestUrl = assetUrl._tag === "Success" ? `${assetUrl.url}${revisionSuffix}` : null;
|
|
|
|
return (
|
|
<div className="flex min-h-0 flex-1 items-center justify-center overflow-hidden p-4">
|
|
<MediaVideoPlayer
|
|
src={latestUrl}
|
|
sourceFailed={assetUrl._tag === "Failure"}
|
|
label={props.name}
|
|
revision={props.workspaceMutationId}
|
|
preload="metadata"
|
|
className="flex h-full min-h-0 w-full max-w-5xl items-center justify-center"
|
|
onRetry={refreshAssetUrl}
|
|
actionsSource={{
|
|
kind: "video",
|
|
name: props.name,
|
|
src: latestUrl,
|
|
reference: mediaFileReference(props.absolutePath, props.workspaceRoot),
|
|
asset: { environmentId: props.environmentId, resource },
|
|
}}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function clampFileLine(contents: string, requestedLine: number): number {
|
|
let lineCount = 1;
|
|
for (let index = 0; index < contents.length; index += 1) {
|
|
const character = contents.charCodeAt(index);
|
|
if (character === 10) {
|
|
lineCount += 1;
|
|
} else if (character === 13) {
|
|
lineCount += 1;
|
|
if (contents.charCodeAt(index + 1) === 10) index += 1;
|
|
}
|
|
}
|
|
return Math.min(Math.max(1, requestedLine), lineCount);
|
|
}
|
|
|
|
function updateFileLinkReveal(fileContainer: HTMLElement, line: number | null): void {
|
|
const root = fileContainer.shadowRoot ?? fileContainer;
|
|
for (const element of root.querySelectorAll<HTMLElement>(`[${FILE_LINK_REVEAL_ATTRIBUTE}]`)) {
|
|
element.removeAttribute(FILE_LINK_REVEAL_ATTRIBUTE);
|
|
}
|
|
if (line === null) return;
|
|
|
|
root
|
|
.querySelector<HTMLElement>(`[data-line="${line}"]`)
|
|
?.setAttribute(FILE_LINK_REVEAL_ATTRIBUTE, "");
|
|
root
|
|
.querySelector<HTMLElement>(`[data-column-number="${line}"]`)
|
|
?.setAttribute(FILE_LINK_REVEAL_ATTRIBUTE, "");
|
|
}
|
|
|
|
/**
|
|
* Frames to keep retrying while the file contents or line metrics are not
|
|
* available yet (fresh mounts hydrate asynchronously).
|
|
*/
|
|
const REVEAL_MAX_ATTEMPTS = 30;
|
|
/**
|
|
* After scrolling to the target, hold it for a short window so late
|
|
* programmatic scroll resets (editable-editor focus and state restoration)
|
|
* cannot silently snap the file back to the top. Real user input cancels the
|
|
* guard immediately.
|
|
*/
|
|
const REVEAL_GUARD_FRAMES = 20;
|
|
const REVEAL_GUARD_TOLERANCE_PX = 2;
|
|
|
|
interface FileRevealState {
|
|
frameId: number | null;
|
|
cancelGuard: (() => void) | null;
|
|
handledRequestId: number | null;
|
|
latestRequestId: number | null;
|
|
}
|
|
|
|
function useFileLineReveal(
|
|
relativePath: string | null,
|
|
revealLine: number | null,
|
|
revealRequestId: number,
|
|
): FilePostRender {
|
|
const [revealStatesByPath] = useState(() => new Map<string, FileRevealState>());
|
|
|
|
return useCallback<FilePostRender>(
|
|
(fileContainer, instance, phase) => {
|
|
if (relativePath === null) return;
|
|
|
|
const existingState = revealStatesByPath.get(relativePath);
|
|
const state: FileRevealState = existingState ?? {
|
|
frameId: null,
|
|
cancelGuard: null,
|
|
handledRequestId: null,
|
|
latestRequestId: null,
|
|
};
|
|
if (!existingState) revealStatesByPath.set(relativePath, state);
|
|
|
|
const cancelPendingReveal = () => {
|
|
if (state.frameId !== null) {
|
|
cancelAnimationFrame(state.frameId);
|
|
state.frameId = null;
|
|
}
|
|
state.cancelGuard?.();
|
|
};
|
|
|
|
if (phase === "unmount") {
|
|
cancelPendingReveal();
|
|
return;
|
|
}
|
|
|
|
const contents = instance.file?.contents;
|
|
const targetLine =
|
|
revealLine === null || contents === undefined ? null : clampFileLine(contents, revealLine);
|
|
updateFileLinkReveal(fileContainer, targetLine);
|
|
|
|
if (!(instance instanceof VirtualizedFile)) return;
|
|
|
|
if (state.latestRequestId !== revealRequestId) {
|
|
cancelPendingReveal();
|
|
state.latestRequestId = revealRequestId;
|
|
state.handledRequestId = null;
|
|
}
|
|
|
|
if (revealLine === null) {
|
|
fileContainer.style.minHeight = "";
|
|
return;
|
|
}
|
|
|
|
const scrollContainer = fileContainer.closest<HTMLElement>(".file-preview-virtualizer");
|
|
if (!scrollContainer) return;
|
|
fileContainer.style.minHeight = `${Math.ceil(
|
|
Math.max(instance.height, scrollContainer.clientHeight),
|
|
)}px`;
|
|
|
|
if (state.handledRequestId === revealRequestId || state.frameId !== null) {
|
|
return;
|
|
}
|
|
|
|
const resolveScrollTarget = (line: number): number | null => {
|
|
const linePosition = instance.getLinePosition(line);
|
|
if (!linePosition) return null;
|
|
|
|
const scrollContainerRect = scrollContainer.getBoundingClientRect();
|
|
const fileTop =
|
|
scrollContainer.scrollTop +
|
|
fileContainer.getBoundingClientRect().top -
|
|
scrollContainerRect.top;
|
|
const root = fileContainer.shadowRoot ?? fileContainer;
|
|
const renderedLineElement = root.querySelector<HTMLElement>(`[data-line="${line}"]`);
|
|
const renderedLineRect = renderedLineElement?.getBoundingClientRect();
|
|
|
|
return resolveCenteredFileLineScrollTop({
|
|
scrollTop: scrollContainer.scrollTop,
|
|
scrollHeight: scrollContainer.scrollHeight,
|
|
viewportTop: scrollContainerRect.top,
|
|
viewportHeight: scrollContainer.clientHeight,
|
|
fileTop,
|
|
estimatedLine: linePosition,
|
|
...(renderedLineRect && renderedLineRect.height > 0
|
|
? {
|
|
renderedLine: {
|
|
top: renderedLineRect.top,
|
|
height: renderedLineRect.height,
|
|
},
|
|
}
|
|
: {}),
|
|
});
|
|
};
|
|
|
|
const guardScrollTarget = (line: number) => {
|
|
let framesLeft = REVEAL_GUARD_FRAMES;
|
|
let guardFrameId: number | null = null;
|
|
const cancelGuard = () => {
|
|
if (guardFrameId !== null) {
|
|
cancelAnimationFrame(guardFrameId);
|
|
guardFrameId = null;
|
|
}
|
|
scrollContainer.removeEventListener("wheel", cancelGuard);
|
|
scrollContainer.removeEventListener("touchstart", cancelGuard);
|
|
scrollContainer.removeEventListener("pointerdown", cancelGuard, true);
|
|
window.removeEventListener("keydown", cancelGuard, true);
|
|
if (state.cancelGuard === cancelGuard) state.cancelGuard = null;
|
|
};
|
|
scrollContainer.addEventListener("wheel", cancelGuard, { passive: true });
|
|
scrollContainer.addEventListener("touchstart", cancelGuard, { passive: true });
|
|
// Pierre stops gutter pointer events from bubbling. Listen in capture
|
|
// so starting a comment cancels the reveal guard before the row expands.
|
|
scrollContainer.addEventListener("pointerdown", cancelGuard, {
|
|
passive: true,
|
|
capture: true,
|
|
});
|
|
window.addEventListener("keydown", cancelGuard, true);
|
|
const holdTarget = () => {
|
|
guardFrameId = null;
|
|
framesLeft -= 1;
|
|
if (framesLeft <= 0 || !scrollContainer.isConnected) {
|
|
cancelGuard();
|
|
return;
|
|
}
|
|
const targetTop = resolveScrollTarget(line);
|
|
if (
|
|
targetTop !== null &&
|
|
Math.abs(scrollContainer.scrollTop - targetTop) > REVEAL_GUARD_TOLERANCE_PX
|
|
) {
|
|
scrollContainer.scrollTop = targetTop;
|
|
}
|
|
guardFrameId = requestAnimationFrame(holdTarget);
|
|
};
|
|
guardFrameId = requestAnimationFrame(holdTarget);
|
|
state.cancelGuard = cancelGuard;
|
|
};
|
|
|
|
const scheduleReveal = (attempt: number) => {
|
|
state.frameId = requestAnimationFrame(() => {
|
|
state.frameId = null;
|
|
if (state.latestRequestId !== revealRequestId || !fileContainer.isConnected) {
|
|
return;
|
|
}
|
|
|
|
// Contents and line metrics can lag the first post-render on fresh
|
|
// mounts; clamping against missing contents would scroll to line 1
|
|
// and wrongly mark the request handled.
|
|
const currentContents = instance.file?.contents;
|
|
const line =
|
|
currentContents === undefined ? null : clampFileLine(currentContents, revealLine);
|
|
const targetTop = line === null ? null : resolveScrollTarget(line);
|
|
if (line === null || targetTop === null) {
|
|
if (attempt < REVEAL_MAX_ATTEMPTS) scheduleReveal(attempt + 1);
|
|
return;
|
|
}
|
|
updateFileLinkReveal(fileContainer, line);
|
|
|
|
scrollContainer.scrollTop = targetTop;
|
|
state.handledRequestId = revealRequestId;
|
|
guardScrollTarget(line);
|
|
});
|
|
};
|
|
|
|
scheduleReveal(0);
|
|
},
|
|
[revealStatesByPath, relativePath, revealLine, revealRequestId],
|
|
);
|
|
}
|
|
|
|
interface EditableFileSurfaceProps {
|
|
environmentId: EnvironmentId;
|
|
cwd: string;
|
|
relativePath: string;
|
|
composerDraftTarget: ScopedThreadRef | DraftId;
|
|
contents: string;
|
|
resolvedTheme: "light" | "dark";
|
|
revealRequestId: number;
|
|
wordWrap: boolean;
|
|
onPostRender: FilePostRender;
|
|
onPendingChange: (relativePath: string, pending: boolean) => void;
|
|
}
|
|
|
|
interface FileSelectionOverride {
|
|
revealRequestId: number;
|
|
range: SelectedLineRange | null;
|
|
}
|
|
|
|
function EditableFileSurface({
|
|
environmentId,
|
|
cwd,
|
|
relativePath,
|
|
composerDraftTarget,
|
|
contents,
|
|
resolvedTheme,
|
|
revealRequestId,
|
|
wordWrap,
|
|
onPostRender,
|
|
onPendingChange,
|
|
}: EditableFileSurfaceProps) {
|
|
const addReviewComment = useComposerDraftStore((store) => store.addReviewComment);
|
|
const removeReviewComment = useComposerDraftStore((store) => store.removeReviewComment);
|
|
const [lineAnnotations, setLineAnnotations] = useState<FileCommentLineAnnotation[]>([]);
|
|
const [selectionOverride, setSelectionOverride] = useState<FileSelectionOverride | null>(null);
|
|
const selectedRange =
|
|
selectionOverride?.revealRequestId === revealRequestId ? selectionOverride.range : null;
|
|
const setSelectedRange = useCallback(
|
|
(range: SelectedLineRange | null) => {
|
|
setSelectionOverride({ revealRequestId, range });
|
|
},
|
|
[revealRequestId],
|
|
);
|
|
const surfaceRef = useRef<HTMLDivElement>(null);
|
|
const selectionFrameRef = useRef<number | null>(null);
|
|
const saveCoordinator = useFileSaveCoordinator({
|
|
environmentId,
|
|
cwd,
|
|
relativePath,
|
|
onPendingChange,
|
|
});
|
|
const editor = useMemo(
|
|
() =>
|
|
new Editor<FileCommentAnnotationGroup>({
|
|
persistState: true,
|
|
persistStateStorage: "inMemory",
|
|
onChange: (file, nextLineAnnotations) => {
|
|
setProjectFileQueryData(environmentId, cwd, relativePath, file.contents);
|
|
saveCoordinator.change(file.contents);
|
|
if (nextLineAnnotations) {
|
|
const remapped = remapFileCommentAnnotations(
|
|
nextLineAnnotations as FileCommentLineAnnotation[],
|
|
);
|
|
setLineAnnotations(remapped);
|
|
for (const annotation of remapped) {
|
|
for (const entry of annotation.metadata.entries) {
|
|
if (entry.kind !== "comment") continue;
|
|
addReviewComment(
|
|
composerDraftTarget,
|
|
buildFileReviewComment({
|
|
id: entry.id,
|
|
filePath: relativePath,
|
|
startLine: entry.startLine,
|
|
endLine: entry.endLine,
|
|
text: entry.text,
|
|
contents: file.contents,
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
}),
|
|
[addReviewComment, composerDraftTarget, cwd, environmentId, relativePath, saveCoordinator],
|
|
);
|
|
|
|
useEffect(
|
|
() => () => {
|
|
editor.cleanUp();
|
|
},
|
|
[editor],
|
|
);
|
|
|
|
const removeAnnotationEntry = useCallback(
|
|
(entryId: string) => {
|
|
setSelectedRange(null);
|
|
removeReviewComment(composerDraftTarget, entryId);
|
|
setLineAnnotations((current) => {
|
|
return current.flatMap((annotation) => {
|
|
const entries = annotation.metadata.entries.filter((entry) => entry.id !== entryId);
|
|
return entries.length > 0 ? [{ ...annotation, metadata: { entries } }] : [];
|
|
});
|
|
});
|
|
},
|
|
[composerDraftTarget, removeReviewComment, setSelectedRange],
|
|
);
|
|
|
|
const submitAnnotationEntry = useCallback(
|
|
(entryId: string, text: string) => {
|
|
setSelectedRange(null);
|
|
const entry = lineAnnotations
|
|
.flatMap((annotation) => annotation.metadata.entries)
|
|
.find((candidate) => candidate.id === entryId);
|
|
if (entry) {
|
|
addReviewComment(
|
|
composerDraftTarget,
|
|
buildFileReviewComment({
|
|
id: entry.id,
|
|
filePath: relativePath,
|
|
startLine: entry.startLine,
|
|
endLine: entry.endLine,
|
|
text,
|
|
contents,
|
|
}),
|
|
);
|
|
}
|
|
setLineAnnotations((current) =>
|
|
current.map((annotation) => ({
|
|
...annotation,
|
|
metadata: {
|
|
entries: annotation.metadata.entries.map((annotationEntry) =>
|
|
annotationEntry.id === entryId
|
|
? { ...annotationEntry, kind: "comment", text }
|
|
: annotationEntry,
|
|
),
|
|
},
|
|
})),
|
|
);
|
|
},
|
|
[
|
|
addReviewComment,
|
|
composerDraftTarget,
|
|
contents,
|
|
lineAnnotations,
|
|
relativePath,
|
|
setSelectedRange,
|
|
],
|
|
);
|
|
|
|
const beginComment = useCallback(
|
|
(range: SelectedLineRange) => {
|
|
editor.setSelections([]);
|
|
editor.blur();
|
|
const { startLine, endLine } = normalizeFileCommentRange(range);
|
|
const draftEntry: FileCommentAnnotationEntry = {
|
|
id: nextFileCommentId(),
|
|
kind: "draft",
|
|
startLine,
|
|
endLine,
|
|
text: "",
|
|
};
|
|
setLineAnnotations((current) => {
|
|
const withoutDraft = current.flatMap((annotation) => {
|
|
const entries = annotation.metadata.entries.filter((entry) => entry.kind !== "draft");
|
|
return entries.length > 0 ? [{ ...annotation, metadata: { entries } }] : [];
|
|
});
|
|
const existingIndex = withoutDraft.findIndex(
|
|
(annotation) => annotation.lineNumber === endLine,
|
|
);
|
|
if (existingIndex < 0) {
|
|
return [
|
|
...withoutDraft,
|
|
{
|
|
lineNumber: endLine,
|
|
metadata: { entries: [draftEntry] },
|
|
},
|
|
];
|
|
}
|
|
return withoutDraft.map((annotation, index) =>
|
|
index === existingIndex
|
|
? {
|
|
...annotation,
|
|
metadata: { entries: [...annotation.metadata.entries, draftEntry] },
|
|
}
|
|
: annotation,
|
|
);
|
|
});
|
|
},
|
|
[editor],
|
|
);
|
|
const hasOpenCommentForm = lineAnnotations.some((annotation) =>
|
|
annotation.metadata.entries.some((entry) => entry.kind === "draft"),
|
|
);
|
|
useEffect(() => {
|
|
const root = surfaceRef.current;
|
|
if (!root) return;
|
|
return installFileEditorDismissal({
|
|
root,
|
|
editor,
|
|
isBlocked: () => hasOpenCommentForm,
|
|
onDismiss: () => setSelectedRange(null),
|
|
});
|
|
}, [editor, hasOpenCommentForm, setSelectedRange]);
|
|
const handleLineSelectionEnd = useCallback(
|
|
(range: SelectedLineRange | null) => {
|
|
setSelectedRange(range);
|
|
if (range) {
|
|
beginComment(range);
|
|
}
|
|
},
|
|
[beginComment, setSelectedRange],
|
|
);
|
|
|
|
const handlePostRender = useCallback<FilePostRender>(
|
|
(fileContainer, instance, phase) => {
|
|
onPostRender(fileContainer, instance, phase);
|
|
|
|
if (selectionFrameRef.current !== null) {
|
|
cancelAnimationFrame(selectionFrameRef.current);
|
|
selectionFrameRef.current = null;
|
|
}
|
|
if (phase === "unmount") return;
|
|
|
|
selectionFrameRef.current = requestAnimationFrame(() => {
|
|
selectionFrameRef.current = null;
|
|
if (!fileContainer.isConnected) return;
|
|
instance.setSelectedLines(selectedRange, { notify: false });
|
|
});
|
|
},
|
|
[onPostRender, selectedRange],
|
|
);
|
|
|
|
return (
|
|
<EditProvider editor={editor}>
|
|
<div ref={surfaceRef} className="flex min-h-0 flex-1">
|
|
<Virtualizer
|
|
className="file-preview-virtualizer min-h-0 flex-1 overflow-auto"
|
|
config={{
|
|
overscrollSize: 600,
|
|
intersectionObserverMargin: 1200,
|
|
}}
|
|
>
|
|
<File<FileCommentAnnotationGroup>
|
|
file={{
|
|
name: relativePath,
|
|
contents,
|
|
cacheKey: projectFileEditorCacheKey(
|
|
environmentId,
|
|
cwd,
|
|
relativePath,
|
|
contents,
|
|
editor.getFile(),
|
|
),
|
|
}}
|
|
options={{
|
|
disableFileHeader: true,
|
|
enableGutterUtility: !hasOpenCommentForm,
|
|
enableLineSelection: !hasOpenCommentForm,
|
|
onGutterUtilityClick: setSelectedRange,
|
|
onLineSelectionChange: setSelectedRange,
|
|
onLineSelectionEnd: handleLineSelectionEnd,
|
|
overflow: wordWrap ? "wrap" : "scroll",
|
|
theme: resolveDiffThemeName(resolvedTheme),
|
|
preferredHighlighter: PREFERRED_HIGHLIGHTER,
|
|
themeType: resolvedTheme,
|
|
unsafeCSS: FILE_LINK_REVEAL_UNSAFE_CSS,
|
|
onPostRender: handlePostRender,
|
|
}}
|
|
selectedLines={selectedRange}
|
|
lineAnnotations={lineAnnotations}
|
|
renderAnnotation={(annotation) => (
|
|
<div className="py-1">
|
|
{annotation.metadata.entries.map((entry) => (
|
|
<DiffCommentAnnotation
|
|
key={entry.id}
|
|
kind={entry.kind}
|
|
rangeLabel={formatFileCommentRange(entry.startLine, entry.endLine)}
|
|
text={entry.text}
|
|
onCancel={() => removeAnnotationEntry(entry.id)}
|
|
onComment={(text) => submitAnnotationEntry(entry.id, text)}
|
|
onDelete={() => removeAnnotationEntry(entry.id)}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
className="min-h-full"
|
|
contentEditable
|
|
/>
|
|
</Virtualizer>
|
|
</div>
|
|
</EditProvider>
|
|
);
|
|
}
|
|
|
|
function RenderedMarkdownSurface({
|
|
environmentId,
|
|
cwd,
|
|
relativePath,
|
|
contents,
|
|
threadRef,
|
|
readOnly,
|
|
onPendingChange,
|
|
}: Omit<
|
|
EditableFileSurfaceProps,
|
|
| "resolvedTheme"
|
|
| "composerDraftTarget"
|
|
| "revealLine"
|
|
| "revealRequestId"
|
|
| "wordWrap"
|
|
| "onPostRender"
|
|
> & {
|
|
threadRef: ScopedThreadRef;
|
|
readOnly: boolean;
|
|
}) {
|
|
const saveCoordinator = useFileSaveCoordinator({
|
|
environmentId,
|
|
cwd,
|
|
relativePath,
|
|
onPendingChange,
|
|
});
|
|
|
|
return (
|
|
<ScrollArea className="min-h-0 flex-1">
|
|
<FileMarkdownPreview
|
|
text={contents}
|
|
cwd={cwd}
|
|
relativePath={relativePath}
|
|
threadRef={threadRef}
|
|
onTaskListChange={
|
|
readOnly
|
|
? undefined
|
|
: ({ markerOffset, checked }) => {
|
|
const currentContents =
|
|
getOptimisticProjectFileQueryData(environmentId, cwd, relativePath)?.contents ??
|
|
contents;
|
|
const nextContents = setMarkdownTaskChecked(currentContents, markerOffset, checked);
|
|
if (nextContents === currentContents) return;
|
|
setProjectFileQueryData(environmentId, cwd, relativePath, nextContents);
|
|
saveCoordinator.change(nextContents);
|
|
}
|
|
}
|
|
/>
|
|
</ScrollArea>
|
|
);
|
|
}
|
|
|
|
function renderedToggleLabel(isMarkdown: boolean, rendered: boolean): string {
|
|
if (isMarkdown) return rendered ? "Show markdown source" : "Show rendered markdown";
|
|
return rendered ? "Show HTML source" : "Show rendered page";
|
|
}
|
|
|
|
function initialExplorerOpen(): boolean {
|
|
try {
|
|
return getLocalStorageItem(FILE_EXPLORER_STORAGE_KEY, Schema.Boolean) ?? true;
|
|
} catch (error) {
|
|
console.error(error);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
export default function FilePreviewPanel({
|
|
environmentId,
|
|
cwd,
|
|
projectName,
|
|
relativePath,
|
|
attachment,
|
|
threadRef,
|
|
composerDraftTarget,
|
|
keybindings,
|
|
availableEditors,
|
|
revealLine,
|
|
revealRequestId,
|
|
onOpenFile,
|
|
onPendingChange,
|
|
selectedFilePending,
|
|
workspaceMutationId,
|
|
}: FilePreviewPanelProps) {
|
|
const { resolvedTheme } = useTheme();
|
|
const wordWrap = useClientSettings((settings) => settings.wordWrap);
|
|
const primaryEnvironmentId = usePrimaryEnvironmentId();
|
|
const remoteOpenState = useRemoteOpenState(environmentId);
|
|
const environmentHttpBaseUrl = useEnvironmentHttpBaseUrl(environmentId);
|
|
const createAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, {
|
|
reportFailure: false,
|
|
});
|
|
const openPreview = useAtomCommand(previewEnvironment.open, {
|
|
reportFailure: false,
|
|
});
|
|
const isVideo = relativePath !== null && isWorkspaceVideoPreviewPath(relativePath);
|
|
const isImage = relativePath !== null && !isVideo && isWorkspaceImagePreviewPath(relativePath);
|
|
const isMedia = isImage || isVideo;
|
|
// PDFs have no text to show; HTML has, and can toggle between page and source.
|
|
const isPdf = relativePath !== null && isPdfPreviewFile(relativePath);
|
|
const isHtml = relativePath !== null && !isPdf && isBrowserPreviewFile(relativePath);
|
|
// A file outside the workspace (an absolute path) is shown, never edited.
|
|
const isHostFile =
|
|
attachment !== undefined || (relativePath !== null && isAbsolutePath(relativePath));
|
|
const file = useProjectFileQuery(
|
|
environmentId,
|
|
cwd,
|
|
relativePath,
|
|
attachment === undefined && !isMedia && !isPdf,
|
|
);
|
|
const [explorerOpen, setExplorerOpen] = useState(initialExplorerOpen);
|
|
const showExplorer = shouldShowFileExplorer({
|
|
relativePath,
|
|
explorerOpen,
|
|
attachmentOpen: attachment !== undefined,
|
|
});
|
|
// Reading markdown rendered is a preference, not a property of one file. Keeping
|
|
// it on the panel meant a thread switch dropped it and forced source back.
|
|
const [renderMarkdownPreferred, setRenderMarkdownPreferred] = useLocalStorage(
|
|
RENDER_MARKDOWN_STORAGE_KEY,
|
|
false,
|
|
Schema.Boolean,
|
|
);
|
|
const [renderBrowserFilePreferred, setRenderBrowserFilePreferred] = useLocalStorage(
|
|
RENDER_BROWSER_FILE_STORAGE_KEY,
|
|
true,
|
|
Schema.Boolean,
|
|
);
|
|
// Paired with the path on purpose: each file surface counts its reveals from
|
|
// one, so a bare id would let a dismissed reveal on one file swallow the first
|
|
// reveal on the next.
|
|
const [handledReveal, setHandledReveal] = useState<{ path: string; requestId: number } | null>(
|
|
null,
|
|
);
|
|
const breadcrumbRef = useRef<HTMLDivElement>(null);
|
|
const isMarkdown = relativePath ? isMarkdownPreviewFile(relativePath) : false;
|
|
// A reveal still wins over the preference: the line only exists in the source.
|
|
const revealHandled =
|
|
revealLine === null ||
|
|
(handledReveal?.path === relativePath && handledReveal.requestId === revealRequestId);
|
|
const renderMarkdown = isMarkdown && renderMarkdownPreferred && revealHandled;
|
|
const renderBrowserFile = isPdf || (isHtml && renderBrowserFilePreferred && revealHandled);
|
|
const canToggleRendered = attachment === undefined && (isMarkdown || isHtml);
|
|
const rendered = isMarkdown ? renderMarkdown : renderBrowserFile;
|
|
const setRenderedPreferred = isMarkdown
|
|
? setRenderMarkdownPreferred
|
|
: setRenderBrowserFilePreferred;
|
|
const canOpenInBrowser =
|
|
relativePath !== null &&
|
|
attachment === undefined &&
|
|
!isVideo &&
|
|
isPreviewSupportedInRuntime() &&
|
|
isBrowserPreviewFile(relativePath);
|
|
const absolutePath =
|
|
relativePath && attachment === undefined ? resolvePathLinkTarget(relativePath, cwd) : null;
|
|
const onFilePostRender = useFileLineReveal(relativePath, revealLine, revealRequestId);
|
|
useWorkspaceMutationRefresh({
|
|
enabled:
|
|
attachment === undefined &&
|
|
relativePath !== null &&
|
|
!isMedia &&
|
|
!isPdf &&
|
|
!selectedFilePending,
|
|
mutationId: workspaceMutationId,
|
|
refresh: file.refresh,
|
|
resourceKey: `file:${environmentId}:${cwd}:${relativePath ?? ""}`,
|
|
});
|
|
|
|
useEffect(() => {
|
|
const currentCrumb = breadcrumbRef.current?.querySelector<HTMLElement>(
|
|
"[data-current-file-crumb='true']",
|
|
);
|
|
currentCrumb?.scrollIntoView({ block: "nearest", inline: "end" });
|
|
}, [relativePath]);
|
|
|
|
const toggleExplorer = () => {
|
|
setExplorerOpen((current) => {
|
|
const next = !current;
|
|
try {
|
|
setLocalStorageItem(FILE_EXPLORER_STORAGE_KEY, next, Schema.Boolean);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
return next;
|
|
});
|
|
};
|
|
|
|
const handleOpenInBrowser = useCallback(() => {
|
|
if (!absolutePath || !environmentHttpBaseUrl) return;
|
|
void (async () => {
|
|
const result = await openFileInPreview({
|
|
threadRef,
|
|
filePath: absolutePath,
|
|
workspaceRoot: cwd,
|
|
httpBaseUrl: environmentHttpBaseUrl,
|
|
createAssetUrl,
|
|
openPreview,
|
|
});
|
|
if (result._tag === "Success" || isAtomCommandInterrupted(result)) {
|
|
return;
|
|
}
|
|
const error = squashAtomCommandFailure(result);
|
|
toastManager.add(
|
|
stackedThreadToast({
|
|
type: "error",
|
|
title: "Unable to open file in browser",
|
|
description: error instanceof Error ? error.message : "An error occurred.",
|
|
}),
|
|
);
|
|
})();
|
|
}, [absolutePath, createAssetUrl, cwd, environmentHttpBaseUrl, openPreview, threadRef]);
|
|
|
|
return (
|
|
<div className="flex min-h-0 flex-1 flex-col overflow-hidden bg-background">
|
|
{relativePath ? (
|
|
<div
|
|
className="flex h-10 min-h-10 shrink-0 items-center gap-2 border-b border-border/60 bg-background px-3 in-data-[preview-panel-mode=inline]:mb-3 in-data-[preview-panel-mode=inline]:h-7 in-data-[preview-panel-mode=inline]:min-h-7 in-data-[preview-panel-mode=inline]:border-b-transparent"
|
|
data-surface-subheader
|
|
>
|
|
{attachment ? (
|
|
<div className="flex min-w-0 flex-1 items-center gap-1.5 text-xs">
|
|
<PierreEntryIcon
|
|
pathValue={attachment.name}
|
|
kind="file"
|
|
theme={resolvedTheme}
|
|
className="size-3.5"
|
|
/>
|
|
<span className="truncate font-medium">{attachment.name}</span>
|
|
</div>
|
|
) : (
|
|
<ScrollArea
|
|
ref={breadcrumbRef}
|
|
hideScrollbars
|
|
scrollFade
|
|
className="min-w-0 flex-1 rounded-none"
|
|
data-file-breadcrumbs
|
|
>
|
|
<div className="flex h-full w-max min-w-full items-center text-xs">
|
|
<FileBreadcrumbs
|
|
cwd={cwd}
|
|
environmentId={environmentId}
|
|
onOpenFile={onOpenFile}
|
|
projectName={projectName}
|
|
relativePath={relativePath}
|
|
workspaceMutationId={workspaceMutationId}
|
|
/>
|
|
</div>
|
|
</ScrollArea>
|
|
)}
|
|
{absolutePath &&
|
|
(environmentId === primaryEnvironmentId || remoteOpenState.mode !== "local-exec") ? (
|
|
<OpenInPicker
|
|
environmentId={environmentId}
|
|
keybindings={keybindings}
|
|
availableEditors={availableEditors}
|
|
openInCwd={absolutePath}
|
|
compact
|
|
enableShortcut={false}
|
|
/>
|
|
) : null}
|
|
{canToggleRendered ? (
|
|
<Tooltip>
|
|
<TooltipTrigger
|
|
render={
|
|
<Toggle
|
|
className="shrink-0"
|
|
pressed={rendered}
|
|
onPressedChange={(pressed) => {
|
|
setRenderedPreferred(pressed);
|
|
setHandledReveal(
|
|
pressed && relativePath !== null
|
|
? { path: relativePath, requestId: revealRequestId }
|
|
: null,
|
|
);
|
|
}}
|
|
aria-label={renderedToggleLabel(isMarkdown, rendered)}
|
|
variant="ghost"
|
|
size="sm"
|
|
>
|
|
{rendered ? <Code2 className="size-3.5" /> : <Eye className="size-3.5" />}
|
|
</Toggle>
|
|
}
|
|
/>
|
|
<TooltipPopup>{renderedToggleLabel(isMarkdown, rendered)}</TooltipPopup>
|
|
</Tooltip>
|
|
) : null}
|
|
{canOpenInBrowser ? (
|
|
<Tooltip>
|
|
<TooltipTrigger
|
|
render={
|
|
<Toggle
|
|
className="shrink-0"
|
|
pressed={false}
|
|
onPressedChange={handleOpenInBrowser}
|
|
aria-label="Open file in preview browser"
|
|
variant="ghost"
|
|
size="sm"
|
|
>
|
|
<Globe2 className="size-3.5" />
|
|
</Toggle>
|
|
}
|
|
/>
|
|
<TooltipPopup>Open file in preview browser</TooltipPopup>
|
|
</Tooltip>
|
|
) : null}
|
|
{!isHostFile ? (
|
|
<Tooltip>
|
|
<TooltipTrigger
|
|
render={
|
|
<Toggle
|
|
className="shrink-0"
|
|
pressed={explorerOpen}
|
|
onPressedChange={toggleExplorer}
|
|
aria-label={explorerOpen ? "Hide file explorer" : "Show file explorer"}
|
|
variant="ghost"
|
|
size="sm"
|
|
>
|
|
<FolderTree className="size-3.5" />
|
|
</Toggle>
|
|
}
|
|
/>
|
|
<TooltipPopup>
|
|
{explorerOpen ? "Hide file explorer" : "Show file explorer"}
|
|
</TooltipPopup>
|
|
</Tooltip>
|
|
) : null}
|
|
</div>
|
|
) : null}
|
|
{relativePath && !isMedia && !renderBrowserFile && file.data?.truncated ? (
|
|
<div className="shrink-0 border-b border-warning/20 bg-warning-surface px-3 py-1.5 text-[11px] text-warning-foreground">
|
|
Preview limited to the first 1 MB of a {file.data.byteLength.toLocaleString()} byte file.
|
|
</div>
|
|
) : null}
|
|
<div className="flex min-h-0 flex-1 overflow-hidden">
|
|
<div
|
|
className={cn(
|
|
"min-w-0 flex-1 flex-col overflow-hidden",
|
|
relativePath ? "flex" : "hidden",
|
|
)}
|
|
>
|
|
{relativePath && attachment ? (
|
|
<AttachmentBrowserPreview environmentId={environmentId} attachment={attachment} />
|
|
) : relativePath && isVideo && absolutePath ? (
|
|
<WorkspaceVideoPreview
|
|
key={`${environmentId}:${threadRef.threadId}:${absolutePath}`}
|
|
environmentId={environmentId}
|
|
threadRef={threadRef}
|
|
absolutePath={absolutePath}
|
|
workspaceRoot={cwd}
|
|
name={relativePath}
|
|
workspaceMutationId={workspaceMutationId}
|
|
/>
|
|
) : relativePath && isImage && absolutePath ? (
|
|
<WorkspaceImagePreview
|
|
key={absolutePath}
|
|
environmentId={environmentId}
|
|
threadRef={threadRef}
|
|
absolutePath={absolutePath}
|
|
workspaceRoot={cwd}
|
|
alt={relativePath}
|
|
workspaceMutationId={workspaceMutationId}
|
|
/>
|
|
) : relativePath && renderBrowserFile && absolutePath ? (
|
|
<WorkspaceBrowserPreview
|
|
key={absolutePath}
|
|
environmentId={environmentId}
|
|
threadRef={threadRef}
|
|
absolutePath={absolutePath}
|
|
workspaceRoot={cwd}
|
|
title={relativePath}
|
|
workspaceMutationId={workspaceMutationId}
|
|
/>
|
|
) : relativePath && file.error && file.data === null ? (
|
|
<div className="flex min-h-0 flex-1 items-center justify-center px-6 text-center text-xs leading-relaxed text-destructive">
|
|
{file.error}
|
|
</div>
|
|
) : relativePath && file.data === null ? (
|
|
<div className="flex min-h-0 flex-1 items-center justify-center text-muted-foreground">
|
|
<Spinner className="size-5" />
|
|
</div>
|
|
) : relativePath && file.data ? (
|
|
isMarkdown && renderMarkdown ? (
|
|
// Markdown reconciles in place across text updates, so a file
|
|
// switch needs a new key or the previous file's disclosure and
|
|
// wrap state carries into the next document.
|
|
<RenderedMarkdownSurface
|
|
key={relativePath}
|
|
environmentId={environmentId}
|
|
cwd={cwd}
|
|
relativePath={relativePath}
|
|
threadRef={threadRef}
|
|
contents={file.data.contents}
|
|
readOnly={isHostFile}
|
|
onPendingChange={onPendingChange}
|
|
/>
|
|
) : file.data.truncated || isHostFile ? (
|
|
<DiffWorkerPoolProvider>
|
|
<Virtualizer
|
|
key={`${relativePath}:${resolvedTheme}:${file.data.byteLength}`}
|
|
className="file-preview-virtualizer min-h-0 flex-1 overflow-auto"
|
|
config={{
|
|
overscrollSize: 600,
|
|
intersectionObserverMargin: 1200,
|
|
}}
|
|
>
|
|
<File
|
|
file={{
|
|
name: relativePath,
|
|
contents: file.data.contents,
|
|
cacheKey: projectFileCacheKey(cwd, relativePath, file.data.contents),
|
|
}}
|
|
options={{
|
|
disableFileHeader: true,
|
|
overflow: wordWrap ? "wrap" : "scroll",
|
|
theme: resolveDiffThemeName(resolvedTheme),
|
|
preferredHighlighter: PREFERRED_HIGHLIGHTER,
|
|
themeType: resolvedTheme,
|
|
unsafeCSS: FILE_LINK_REVEAL_UNSAFE_CSS,
|
|
onPostRender: onFilePostRender,
|
|
}}
|
|
className="min-h-full"
|
|
/>
|
|
</Virtualizer>
|
|
</DiffWorkerPoolProvider>
|
|
) : (
|
|
<DiffWorkerPoolProvider>
|
|
<EditableFileSurface
|
|
key={`${relativePath}:${resolvedTheme}`}
|
|
environmentId={environmentId}
|
|
cwd={cwd}
|
|
relativePath={relativePath}
|
|
composerDraftTarget={composerDraftTarget}
|
|
contents={file.data.contents}
|
|
resolvedTheme={resolvedTheme}
|
|
revealRequestId={revealRequestId}
|
|
wordWrap={wordWrap}
|
|
onPostRender={onFilePostRender}
|
|
onPendingChange={onPendingChange}
|
|
/>
|
|
</DiffWorkerPoolProvider>
|
|
)
|
|
) : null}
|
|
</div>
|
|
{showExplorer ? (
|
|
<aside
|
|
className={cn(
|
|
"flex min-h-0 shrink-0 bg-background",
|
|
relativePath
|
|
? "w-[min(22rem,46%)] min-w-64 border-l border-border/60"
|
|
: "min-w-0 flex-1",
|
|
)}
|
|
>
|
|
<FileBrowserPanel
|
|
key={`${environmentId}:${cwd}`}
|
|
environmentId={environmentId}
|
|
cwd={cwd}
|
|
projectName={projectName}
|
|
selectedPath={relativePath}
|
|
selectedPathRevealId={revealRequestId}
|
|
onOpenFile={onOpenFile}
|
|
workspaceMutationId={workspaceMutationId}
|
|
{...(relativePath && !isMedia && !isPdf
|
|
? { onRefreshSelectedFile: file.refresh }
|
|
: {})}
|
|
/>
|
|
</aside>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|