fix(web): preview workspace images in the file panel (#3996)
Co-authored-by: Rhiz3K <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
3 files changed
+80
-6
No files matched your search
@@ -9,7 +9,15 @@ import { usePreparedConnection } from "~/state/session";
|
||||
|
||||
export { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
|
||||
|
||||
export function useAssetUrl(environmentId: EnvironmentId, resource: AssetResource): string | null {
|
||||
export type AssetUrlState =
|
||||
| { readonly _tag: "Loading" }
|
||||
| { readonly _tag: "Failure" }
|
||||
| { readonly _tag: "Success"; readonly url: string };
|
||||
|
||||
export function useAssetUrlState(
|
||||
environmentId: EnvironmentId,
|
||||
resource: AssetResource,
|
||||
): AssetUrlState {
|
||||
const preparedConnection = usePreparedConnection(environmentId);
|
||||
const result = useAtomValue(
|
||||
assetEnvironment.createUrl({
|
||||
@@ -17,10 +25,22 @@ export function useAssetUrl(environmentId: EnvironmentId, resource: AssetResourc
|
||||
input: { resource },
|
||||
}),
|
||||
);
|
||||
if (result._tag === "Failure") {
|
||||
return { _tag: "Failure" };
|
||||
}
|
||||
if (preparedConnection._tag === "None" || result._tag !== "Success") {
|
||||
return { _tag: "Loading" };
|
||||
}
|
||||
const url = resolveAssetUrl(preparedConnection.value.httpBaseUrl, result.value.relativeUrl);
|
||||
return url === null ? { _tag: "Failure" } : { _tag: "Success", url };
|
||||
}
|
||||
|
||||
export function useAssetUrl(environmentId: EnvironmentId, resource: AssetResource): string | null {
|
||||
const result = useAssetUrlState(environmentId, resource);
|
||||
if (result._tag !== "Success") {
|
||||
return null;
|
||||
}
|
||||
return resolveAssetUrl(preparedConnection.value.httpBaseUrl, result.value.relativeUrl);
|
||||
return result.url;
|
||||
}
|
||||
|
||||
export function useAssetUrls(
|
||||
|
||||
@@ -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";
|
||||
@@ -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 { useAssetUrlState } from "~/assets/assetUrls";
|
||||
import ChatMarkdown from "~/components/ChatMarkdown";
|
||||
import { OpenInPicker } from "~/components/chat/OpenInPicker";
|
||||
import { useClientSettings } from "~/hooks/useSettings";
|
||||
@@ -113,6 +115,43 @@ 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 assetUrl = useAssetUrlState(props.environmentId, {
|
||||
_tag: "workspace-file",
|
||||
threadId: props.threadRef.threadId,
|
||||
path: props.absolutePath,
|
||||
});
|
||||
const [failedUrl, setFailedUrl] = useState<string | null>(null);
|
||||
|
||||
if (assetUrl._tag === "Failure" || (assetUrl._tag === "Success" && failedUrl === assetUrl.url)) {
|
||||
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 workspace image.
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return assetUrl._tag === "Success" ? (
|
||||
<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={assetUrl.url}
|
||||
alt={props.alt}
|
||||
onError={() => setFailedUrl(assetUrl.url)}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex min-h-0 flex-1 items-center justify-center text-muted-foreground">
|
||||
<LoaderCircle className="size-5 animate-spin" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function clampFileLine(contents: string, requestedLine: number): number {
|
||||
let lineCount = 1;
|
||||
for (let index = 0; index < contents.length; index += 1) {
|
||||
@@ -630,7 +669,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;
|
||||
@@ -818,7 +858,15 @@ export default function FilePreviewPanel({
|
||||
relativePath ? "flex" : "hidden",
|
||||
)}
|
||||
>
|
||||
{relativePath && file.error && file.data === null ? (
|
||||
{relativePath && isImage && absolutePath ? (
|
||||
<WorkspaceImagePreview
|
||||
key={absolutePath}
|
||||
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>
|
||||
|
||||
@@ -6,7 +6,7 @@ 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";
|
||||
@@ -14,6 +14,9 @@ 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 });
|
||||
}
|
||||
@@ -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]);
|
||||
|
||||
Reference in new issue
Block a user