Compare commits

..
Author SHA1 Message Date
Cursor AgentandJulius Marminge d9380cdecb Optimize expanded image dialog rerenders
Co-authored-by: Julius Marminge <[email protected]>
2026-05-15 16:09:12 +00:00
8 changed files with 93 additions and 69 deletions

No files matched your search

+2 -21
View File
@@ -41,7 +41,8 @@ describe("VcsProjectConfig", () => {
yield* fileSystem.makeDirectory(nested, { recursive: true });
yield* fileSystem.writeFileString(
path.join(configDir, "vcs.json"),
'{"vcs":{"kind":"jj"}}',
// @effect-diagnostics-next-line preferSchemaOverJson:off
JSON.stringify({ vcs: { kind: "jj" } }),
);
const config = yield* VcsProjectConfig.VcsProjectConfig;
@@ -52,26 +53,6 @@ describe("VcsProjectConfig", () => {
);
});
it.layer(TestLayer)("ignores malformed .t3code/vcs.json files", (it) => {
it.effect("falls back to auto", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-vcs-config-test-",
});
const configDir = path.join(root, ".t3code");
yield* fileSystem.makeDirectory(configDir, { recursive: true });
yield* fileSystem.writeFileString(path.join(configDir, "vcs.json"), "{ not-json");
const config = yield* VcsProjectConfig.VcsProjectConfig;
const kind = yield* config.resolveKind({ cwd: root });
assert.equal(kind, "auto");
}),
);
});
it.layer(TestLayer)("falls back to auto when no config exists", (it) => {
it.effect("returns auto", () =>
Effect.gen(function* () {
+25 -16
View File
@@ -2,7 +2,6 @@ import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
@@ -16,10 +15,16 @@ const ProjectVcsConfig = Schema.Struct({
),
vcsKind: Schema.optional(VcsDriverKind),
});
const ProjectVcsConfigJson = Schema.fromJsonString(ProjectVcsConfig);
const decodeProjectVcsConfig = Schema.decodeUnknownOption(ProjectVcsConfigJson);
const isProjectVcsConfig = Schema.is(ProjectVcsConfig);
type ProjectVcsConfigFile = Schema.Schema.Type<typeof ProjectVcsConfig>;
interface ProjectVcsConfigFile {
readonly vcs?:
| {
readonly kind?: VcsDriverKindType | undefined;
}
| undefined;
readonly vcsKind?: VcsDriverKindType | undefined;
}
export interface VcsProjectConfigResolveInput {
readonly cwd: string;
@@ -40,8 +45,13 @@ function configuredKind(config: ProjectVcsConfigFile): VcsDriverKindType | "auto
return config.vcs?.kind ?? config.vcsKind ?? "auto";
}
function parseConfig(raw: string): Option.Option<ProjectVcsConfigFile> {
return decodeProjectVcsConfig(raw);
function parseConfig(raw: string): ProjectVcsConfigFile | null {
try {
const parsed = JSON.parse(raw) as unknown;
return isProjectVcsConfig(parsed) ? parsed : null;
} catch {
return null;
}
}
export const make = Effect.fn("makeVcsProjectConfig")(function* () {
@@ -53,12 +63,12 @@ export const make = Effect.fn("makeVcsProjectConfig")(function* () {
while (true) {
const candidate = path.join(current, ".t3code", "vcs.json");
if (yield* fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false))) {
return Option.some(candidate);
return candidate;
}
const parent = path.dirname(current);
if (parent === current) {
return Option.none<string>();
return null;
}
current = parent;
}
@@ -68,27 +78,26 @@ export const make = Effect.fn("makeVcsProjectConfig")(function* () {
configPath: string,
) {
const raw = yield* fileSystem.readFileString(configPath).pipe(
Effect.map(Option.some),
Effect.catch((error) =>
Effect.logWarning("failed to read VCS project config", {
configPath,
error,
}).pipe(Effect.as(Option.none<string>())),
}).pipe(Effect.as(null)),
),
);
if (Option.isNone(raw)) {
if (raw === null) {
return "auto" as const;
}
const parsed = parseConfig(raw.value);
if (Option.isNone(parsed)) {
const parsed = parseConfig(raw);
if (parsed === null) {
yield* Effect.logWarning("invalid VCS project config", {
configPath,
});
return "auto" as const;
}
return configuredKind(parsed.value);
return configuredKind(parsed);
});
const resolveKind: VcsProjectConfigShape["resolveKind"] = Effect.fn(
@@ -99,11 +108,11 @@ export const make = Effect.fn("makeVcsProjectConfig")(function* () {
}
const configPath = yield* findConfigPath(input.cwd);
if (Option.isNone(configPath)) {
if (configPath === null) {
return "auto";
}
return yield* readConfiguredKind(configPath.value);
return yield* readConfiguredKind(configPath);
});
return VcsProjectConfig.of({
@@ -29,7 +29,7 @@ import {
} from "../Services/WorkspaceEntries.ts";
import { WorkspacePaths } from "../Services/WorkspacePaths.ts";
const WORKSPACE_CACHE_TTL = Duration.seconds(15);
const WORKSPACE_CACHE_TTL_MS = 15_000;
const WORKSPACE_CACHE_MAX_KEYS = 4;
const WORKSPACE_INDEX_MAX_ENTRIES = 25_000;
const WORKSPACE_SCAN_READDIR_CONCURRENCY = 32;
@@ -402,7 +402,8 @@ export const makeWorkspaceEntries = Effect.gen(function* () {
buildWorkspaceIndex,
{
capacity: WORKSPACE_CACHE_MAX_KEYS,
timeToLive: (exit) => (Exit.isSuccess(exit) ? WORKSPACE_CACHE_TTL : Duration.zero),
timeToLive: (exit) =>
Exit.isSuccess(exit) ? Duration.millis(WORKSPACE_CACHE_TTL_MS) : Duration.zero,
},
);
+6 -2
View File
@@ -147,7 +147,7 @@ import { ExpandedImageDialog } from "./chat/ExpandedImageDialog";
import { PullRequestThreadDialog } from "./PullRequestThreadDialog";
import { MessagesTimeline } from "./chat/MessagesTimeline";
import { ChatHeader } from "./chat/ChatHeader";
import { type ExpandedImagePreview } from "./chat/ExpandedImagePreview";
import { getExpandedImagePreviewKey, type ExpandedImagePreview } from "./chat/ExpandedImagePreview";
import { NoActiveThreadState } from "./NoActiveThreadState";
import { resolveEffectiveEnvMode, resolveEnvironmentOptionLabel } from "./BranchToolbar.logic";
import { ProviderStatusBanner } from "./chat/ProviderStatusBanner";
@@ -3773,7 +3773,11 @@ export default function ChatView(props: ChatViewProps) {
) : null}
{expandedImage && (
<ExpandedImageDialog preview={expandedImage} onClose={closeExpandedImage} />
<ExpandedImageDialog
key={getExpandedImagePreviewKey(expandedImage)}
preview={expandedImage}
onClose={closeExpandedImage}
/>
)}
</div>
);
@@ -8,27 +8,20 @@ interface ExpandedImageDialogProps {
onClose: () => void;
}
export const ExpandedImageDialog = memo(function ExpandedImageDialog({
preview: initialPreview,
function clampImageIndex(index: number, imageCount: number): number {
if (imageCount <= 0) return 0;
return Math.min(Math.max(index, 0), imageCount - 1);
}
function useExpandedImageDialogKeyboardNavigation({
imageCount,
navigateImage,
onClose,
}: ExpandedImageDialogProps) {
const [preview, setPreview] = useState(initialPreview);
// Sync when the parent hands us a new preview reference.
useEffect(() => {
setPreview(initialPreview);
}, [initialPreview]);
const navigateImage = useCallback((direction: -1 | 1) => {
setPreview((existing) => {
if (existing.images.length <= 1) return existing;
const nextIndex =
(existing.index + direction + existing.images.length) % existing.images.length;
if (nextIndex === existing.index) return existing;
return { ...existing, index: nextIndex };
});
}, []);
}: {
imageCount: number;
navigateImage: (direction: -1 | 1) => void;
onClose: () => void;
}) {
useEffect(() => {
const onKeyDown = (event: globalThis.KeyboardEvent) => {
if (event.key === "Escape") {
@@ -37,7 +30,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
onClose();
return;
}
if (preview.images.length <= 1) return;
if (imageCount <= 1) return;
if (event.key === "ArrowLeft") {
event.preventDefault();
event.stopPropagation();
@@ -51,9 +44,40 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [navigateImage, onClose, preview.images.length]);
}, [imageCount, navigateImage, onClose]);
}
const item = preview.images[preview.index];
export const ExpandedImageDialog = memo(function ExpandedImageDialog({
preview,
onClose,
}: ExpandedImageDialogProps) {
const imageCount = preview.images.length;
const [imageIndex, setImageIndex] = useState(() => clampImageIndex(preview.index, imageCount));
const activeImageIndex = clampImageIndex(imageIndex, imageCount);
const navigateImage = useCallback(
(direction: -1 | 1) => {
setImageIndex((existing) => {
if (imageCount <= 1) return existing;
return (existing + direction + imageCount) % imageCount;
});
},
[imageCount],
);
const navigateToPreviousImage = useCallback(() => {
navigateImage(-1);
}, [navigateImage]);
const navigateToNextImage = useCallback(() => {
navigateImage(1);
}, [navigateImage]);
useExpandedImageDialogKeyboardNavigation({
imageCount,
navigateImage,
onClose,
});
const item = preview.images[activeImageIndex];
if (!item) return null;
return (
@@ -69,14 +93,14 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
aria-label="Close image preview"
onClick={onClose}
/>
{preview.images.length > 1 && (
{imageCount > 1 && (
<Button
type="button"
size="icon"
variant="ghost"
className="absolute left-2 top-1/2 z-20 -translate-y-1/2 text-white/90 hover:bg-white/10 hover:text-white sm:left-6"
aria-label="Previous image"
onClick={() => navigateImage(-1)}
onClick={navigateToPreviousImage}
>
<ChevronLeftIcon className="size-5" />
</Button>
@@ -100,17 +124,17 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
/>
<p className="mt-2 max-w-[92vw] truncate text-center text-xs text-muted-foreground/80">
{item.name}
{preview.images.length > 1 ? ` (${preview.index + 1}/${preview.images.length})` : ""}
{imageCount > 1 ? ` (${activeImageIndex + 1}/${imageCount})` : ""}
</p>
</div>
{preview.images.length > 1 && (
{imageCount > 1 && (
<Button
type="button"
size="icon"
variant="ghost"
className="absolute right-2 top-1/2 z-20 -translate-y-1/2 text-white/90 hover:bg-white/10 hover:text-white sm:right-6"
aria-label="Next image"
onClick={() => navigateImage(1)}
onClick={navigateToNextImage}
>
<ChevronRightIcon className="size-5" />
</Button>
@@ -8,6 +8,11 @@ export interface ExpandedImagePreview {
index: number;
}
export function getExpandedImagePreviewKey(preview: ExpandedImagePreview): string {
const selectedImage = preview.images[preview.index];
return `${preview.index}:${preview.images.length}:${selectedImage?.src ?? ""}`;
}
export function buildExpandedImagePreview(
images: ReadonlyArray<{ id: string; name: string; previewUrl?: string }>,
selectedImageId: string,