Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions apps/web/src/components/files/FilePreviewPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ResolvedKeybindingsConfig,
ScopedThreadRef,
} from "@t3tools/contracts";
import { isWorkspaceImagePreviewPath } from "@t3tools/shared/filePreview";
import { VirtualizedFile, type SelectedLineRange } from "@pierre/diffs";
import { Editor } from "@pierre/diffs/editor";
import { EditorProvider, File, type FileOptions, Virtualizer } from "@pierre/diffs/react";
Expand All @@ -16,6 +17,7 @@ import * as Schema from "effect/Schema";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";

import { isBrowserPreviewFile, openFileInPreview } from "~/browser/openFileInPreview";
import { useAssetUrl } from "~/assets/assetUrls";
import ChatMarkdown from "~/components/ChatMarkdown";
import { OpenInPicker } from "~/components/chat/OpenInPicker";
import { useClientSettings } from "~/hooks/useSettings";
Expand Down Expand Up @@ -113,6 +115,29 @@ const FILE_LINK_REVEAL_UNSAFE_CSS = `
`;
type FilePostRender = NonNullable<FileOptions<unknown>["onPostRender"]>;

function WorkspaceImagePreview(props: {
readonly environmentId: EnvironmentId;
readonly threadRef: ScopedThreadRef;
readonly absolutePath: string;
readonly alt: string;
}) {
const src = useAssetUrl(props.environmentId, {
_tag: "workspace-file",
threadId: props.threadRef.threadId,
path: props.absolutePath,
});

return src ? (
<div className="flex min-h-0 flex-1 items-center justify-center overflow-auto p-4">
<img className="max-h-full max-w-full object-contain" src={src} alt={props.alt} />
</div>
) : (
<div className="flex min-h-0 flex-1 items-center justify-center text-muted-foreground">
<LoaderCircle className="size-5 animate-spin" />
</div>
Comment thread
cursor[bot] marked this conversation as resolved.
);
}

function clampFileLine(contents: string, requestedLine: number): number {
let lineCount = 1;
for (let index = 0; index < contents.length; index += 1) {
Expand Down Expand Up @@ -630,7 +655,8 @@ export default function FilePreviewPanel({
const openPreview = useAtomCommand(previewEnvironment.open, {
reportFailure: false,
});
const file = useProjectFileQuery(environmentId, cwd, relativePath);
const isImage = relativePath !== null && isWorkspaceImagePreviewPath(relativePath);
const file = useProjectFileQuery(environmentId, cwd, relativePath, !isImage);
const [explorerOpen, setExplorerOpen] = useState(initialExplorerOpen);
const [markdownView, setMarkdownView] = useState<{
path: string | null;
Expand Down Expand Up @@ -818,7 +844,14 @@ export default function FilePreviewPanel({
relativePath ? "flex" : "hidden",
)}
>
{relativePath && file.error && file.data === null ? (
{relativePath && isImage && absolutePath ? (
<WorkspaceImagePreview
environmentId={environmentId}
threadRef={threadRef}
absolutePath={absolutePath}
alt={relativePath}
/>
) : 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>
Expand Down
10 changes: 8 additions & 2 deletions apps/web/src/components/files/projectFilesQueryState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import type {
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Option from "effect/Option";
import { AsyncResult } from "effect/unstable/reactivity";
import { AsyncResult, Atom } from "effect/unstable/reactivity";
import { useCallback } from "react";

import { appAtomRegistry } from "~/rpc/atomRegistry";
import { projectEnvironment } from "~/state/projects";
import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime";

const EMPTY_PROJECT_FILE_PATH = "";
const EMPTY_PROJECT_FILE_QUERY_ATOM = Atom.make(
AsyncResult.initial<ProjectReadFileResult, never>(false),
).pipe(Atom.withLabel("project-file-query:empty"));
function optimisticFileAtom(environmentId: EnvironmentId, cwd: string, relativePath: string) {
return projectEnvironment.optimisticFile({ environmentId, cwd, relativePath });
}
Expand Down Expand Up @@ -137,8 +140,11 @@ export function useProjectFileQuery(
environmentId: EnvironmentId,
cwd: string,
relativePath: string | null,
enabled = true,
): ProjectQueryState<ProjectReadFileResult> {
const atom = getProjectFileQueryAtom(environmentId, cwd, relativePath);
const atom = enabled
? getProjectFileQueryAtom(environmentId, cwd, relativePath)
: EMPTY_PROJECT_FILE_QUERY_ATOM;
const result = useAtomValue(atom);
const refreshAtom = useAtomRefresh(atom);
const refresh = useCallback(() => refreshAtom(), [refreshAtom]);
Expand Down
Loading