Compare commits

..
Author SHA1 Message Date
Cursor AgentandJulius Marminge 43501dcb3e Add expanded image React Scan recordings
Co-authored-by: Julius Marminge <[email protected]>
2026-05-17 16:11:07 +00:00
Cursor AgentandJulius Marminge 0e1d87f791 Stabilize expanded image keyboard listener
Co-authored-by: Julius Marminge <[email protected]>
2026-05-17 16:07:44 +00:00
Cursor AgentandJulius Marminge 19adca2085 Avoid derived image dialog rerenders
Co-authored-by: Julius Marminge <[email protected]>
2026-05-17 16:06:45 +00:00
8 changed files with 164 additions and 237 deletions

No files matched your search

@@ -17,7 +17,7 @@ import {
SshLaunchError,
SshPairingError,
SshPasswordPromptError,
type SshReadinessError,
SshReadinessError,
} from "@t3tools/ssh/errors";
import { SshEnvironmentManager, type RemoteT3RunnerOptions } from "@t3tools/ssh/tunnel";
import * as Context from "effect/Context";
@@ -25,7 +25,6 @@ import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import { HttpClient } from "effect/unstable/http";
import { ChildProcessSpawner } from "effect/unstable/process";
@@ -80,13 +79,11 @@ function discoverDesktopSshHostsEffect(input?: { readonly homeDir?: string }) {
return discoverSshHosts(input ?? {});
}
const isSshPasswordPromptError = Schema.is(SshPasswordPromptError);
export function isDesktopSshPasswordPromptCancellation(
error: unknown,
): error is SshPasswordPromptError {
return (
isSshPasswordPromptError(error) &&
error instanceof SshPasswordPromptError &&
DesktopSshPasswordPrompts.isDesktopSshPasswordPromptCancellation(error.cause)
);
}
@@ -2,7 +2,6 @@ import { assert, describe, it } from "@effect/vitest";
import { SshHttpBridgeError } from "@t3tools/ssh/errors";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http";
import * as DesktopSshRemoteApi from "./DesktopSshRemoteApi.ts";
@@ -32,8 +31,6 @@ function makeLayer(
);
}
const isSshHttpBridgeError = Schema.is(SshHttpBridgeError);
describe("DesktopSshRemoteApi", () => {
it.effect("fetches and decodes the remote environment descriptor", () => {
const requestUrls: string[] = [];
@@ -76,7 +73,7 @@ describe("DesktopSshRemoteApi", () => {
assert.instanceOf(error, DesktopSshRemoteApi.DesktopSshRemoteApiError);
assert.equal(error.operation, "fetch-environment-descriptor");
assert.equal(isSshHttpBridgeError(error.cause), false);
assert.equal(error.cause instanceof SshHttpBridgeError, false);
}).pipe(Effect.provide(layer));
});
});
@@ -1,4 +1,4 @@
import { memo, useCallback, useEffect, useState } from "react";
import { memo, useCallback, useEffect, useEffectEvent, useState } from "react";
import { ChevronLeftIcon, ChevronRightIcon, XIcon } from "lucide-react";
import { Button } from "../ui/button";
import type { ExpandedImagePreview } from "./ExpandedImagePreview";
@@ -8,52 +8,82 @@ interface ExpandedImageDialogProps {
onClose: () => void;
}
function useExpandedImagePreviewNavigation(sourcePreview: ExpandedImagePreview) {
const [navigationState, setNavigationState] = useState(() => ({
sourcePreview,
index: sourcePreview.index,
}));
const index =
navigationState.sourcePreview === sourcePreview ? navigationState.index : sourcePreview.index;
const navigateImage = useCallback(
(direction: -1 | 1) => {
setNavigationState((existing) => {
if (sourcePreview.images.length <= 1) return existing;
const currentIndex =
existing.sourcePreview === sourcePreview ? existing.index : sourcePreview.index;
const nextIndex =
(currentIndex + direction + sourcePreview.images.length) % sourcePreview.images.length;
if (nextIndex === currentIndex && existing.sourcePreview === sourcePreview) {
return existing;
}
return { sourcePreview, index: nextIndex };
});
},
[sourcePreview],
);
return {
images: sourcePreview.images,
index,
navigateImage,
};
}
function useExpandedImageKeyboardShortcuts(input: {
imageCount: number;
navigateImage: (direction: -1 | 1) => void;
onClose: () => void;
}) {
const onKeyDown = useEffectEvent((event: globalThis.KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
input.onClose();
return;
}
if (input.imageCount <= 1) return;
if (event.key === "ArrowLeft") {
event.preventDefault();
event.stopPropagation();
input.navigateImage(-1);
return;
}
if (event.key !== "ArrowRight") return;
event.preventDefault();
event.stopPropagation();
input.navigateImage(1);
});
useEffect(() => {
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, []);
}
export const ExpandedImageDialog = memo(function ExpandedImageDialog({
preview: initialPreview,
onClose,
}: ExpandedImageDialogProps) {
const [preview, setPreview] = useState(initialPreview);
const { images, index, navigateImage } = useExpandedImagePreviewNavigation(initialPreview);
// Sync when the parent hands us a new preview reference.
useEffect(() => {
setPreview(initialPreview);
}, [initialPreview]);
useExpandedImageKeyboardShortcuts({
imageCount: images.length,
navigateImage,
onClose,
});
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 };
});
}, []);
useEffect(() => {
const onKeyDown = (event: globalThis.KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
onClose();
return;
}
if (preview.images.length <= 1) return;
if (event.key === "ArrowLeft") {
event.preventDefault();
event.stopPropagation();
navigateImage(-1);
return;
}
if (event.key !== "ArrowRight") return;
event.preventDefault();
event.stopPropagation();
navigateImage(1);
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [navigateImage, onClose, preview.images.length]);
const item = preview.images[preview.index];
const item = images[index];
if (!item) return null;
return (
@@ -69,7 +99,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
aria-label="Close image preview"
onClick={onClose}
/>
{preview.images.length > 1 && (
{images.length > 1 && (
<Button
type="button"
size="icon"
@@ -100,10 +130,10 @@ 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})` : ""}
{images.length > 1 ? ` (${index + 1}/${images.length})` : ""}
</p>
</div>
{preview.images.length > 1 && (
{images.length > 1 && (
<Button
type="button"
size="icon"
Binary file not shown.
+38 -95
View File
@@ -1,103 +1,46 @@
import * as Schema from "effect/Schema";
import * as Data from "effect/Data";
export class SshHostDiscoveryError extends Schema.TaggedErrorClass<SshHostDiscoveryError>()(
"SshHostDiscoveryError",
{
message: Schema.String,
cause: Schema.Defect,
},
) {}
export class SshHostDiscoveryError extends Data.TaggedError("SshHostDiscoveryError")<{
readonly message: string;
readonly cause: unknown;
}> {}
export class SshInvalidTargetError extends Schema.TaggedErrorClass<SshInvalidTargetError>()(
"SshInvalidTargetError",
{
message: Schema.String,
},
) {}
export class SshInvalidTargetError extends Data.TaggedError("SshInvalidTargetError")<{
readonly message: string;
}> {}
export class SshCommandError extends Schema.TaggedErrorClass<SshCommandError>()("SshCommandError", {
message: Schema.String,
command: Schema.Array(Schema.String),
exitCode: Schema.NullOr(Schema.Number),
stderr: Schema.String,
cause: Schema.optional(Schema.Defect),
}) {}
export class SshCommandError extends Data.TaggedError("SshCommandError")<{
readonly message: string;
readonly command: readonly string[];
readonly exitCode: number | null;
readonly stderr: string;
readonly cause?: unknown;
}> {}
export class SshLaunchError extends Schema.TaggedErrorClass<SshLaunchError>()("SshLaunchError", {
message: Schema.String,
stdout: Schema.String,
cause: Schema.optional(Schema.Defect),
}) {}
export class SshLaunchError extends Data.TaggedError("SshLaunchError")<{
readonly message: string;
readonly stdout: string;
readonly cause?: unknown;
}> {}
export class SshPairingError extends Schema.TaggedErrorClass<SshPairingError>()("SshPairingError", {
message: Schema.String,
stdout: Schema.String,
cause: Schema.optional(Schema.Defect),
}) {}
export class SshPairingError extends Data.TaggedError("SshPairingError")<{
readonly message: string;
readonly stdout: string;
readonly cause?: unknown;
}> {}
export class SshHttpBridgeError extends Schema.TaggedErrorClass<SshHttpBridgeError>()(
"SshHttpBridgeError",
{
message: Schema.String,
status: Schema.optional(Schema.Number),
cause: Schema.optional(Schema.Defect),
},
) {}
export class SshHttpBridgeError extends Data.TaggedError("SshHttpBridgeError")<{
readonly message: string;
readonly status?: number;
readonly cause?: unknown;
}> {}
export class SshReadinessProbeFailedError extends Schema.TaggedErrorClass<SshReadinessProbeFailedError>()(
"SshReadinessProbeFailedError",
{
message: Schema.String,
requestUrl: Schema.String,
cause: Schema.optional(Schema.Defect),
},
) {}
export class SshReadinessError extends Data.TaggedError("SshReadinessError")<{
readonly message: string;
readonly cause?: unknown;
}> {}
export class SshReadinessProbeTimedOutError extends Schema.TaggedErrorClass<SshReadinessProbeTimedOutError>()(
"SshReadinessProbeTimedOutError",
{
message: Schema.String,
requestUrl: Schema.String,
attempt: Schema.Number,
probeTimeoutMs: Schema.Number,
},
) {}
export class SshReadinessTimedOutError extends Schema.TaggedErrorClass<SshReadinessTimedOutError>()(
"SshReadinessTimedOutError",
{
message: Schema.String,
baseUrl: Schema.String,
requestUrl: Schema.String,
timeoutMs: Schema.Number,
intervalMs: Schema.Number,
probeTimeoutMs: Schema.Number,
attempts: Schema.Number,
cause: Schema.optional(Schema.Defect),
},
) {}
export type SshReadinessError =
| SshReadinessProbeFailedError
| SshReadinessProbeTimedOutError
| SshReadinessTimedOutError;
const isSshReadinessProbeFailedError = Schema.is(SshReadinessProbeFailedError);
const isSshReadinessProbeTimedOutError = Schema.is(SshReadinessProbeTimedOutError);
const isSshReadinessTimedOutError = Schema.is(SshReadinessTimedOutError);
export function isSshReadinessError(cause: unknown): cause is SshReadinessError {
return (
isSshReadinessProbeFailedError(cause) ||
isSshReadinessProbeTimedOutError(cause) ||
isSshReadinessTimedOutError(cause)
);
}
export class SshPasswordPromptError extends Schema.TaggedErrorClass<SshPasswordPromptError>()(
"SshPasswordPromptError",
{
message: Schema.String,
cause: Schema.optional(Schema.Defect),
},
) {}
export class SshPasswordPromptError extends Data.TaggedError("SshPasswordPromptError")<{
readonly message: string;
readonly cause?: unknown;
}> {}
+3 -3
View File
@@ -242,9 +242,9 @@ describe("ssh tunnel scripts", () => {
Effect.result(
waitForHttpReady({
baseUrl: "http://127.0.0.1:41773/",
timeout: Duration.seconds(1),
interval: Duration.millis(100),
probeTimeout: Duration.millis(250),
timeoutMs: 1_000,
intervalMs: 100,
probeTimeoutMs: 250,
}),
),
);
+46 -86
View File
@@ -45,20 +45,16 @@ import {
SshLaunchError,
SshPairingError,
SshPasswordPromptError,
SshReadinessProbeFailedError,
SshReadinessProbeTimedOutError,
SshReadinessTimedOutError,
isSshReadinessError,
type SshReadinessError,
SshReadinessError,
} from "./errors.ts";
export const DEFAULT_REMOTE_PORT = 3773;
const REMOTE_PORT_SCAN_WINDOW = 200;
const SSH_READY_TIMEOUT = Duration.seconds(20);
const SSH_READY_PROBE_TIMEOUT = Duration.seconds(1);
const TUNNEL_SHUTDOWN_TIMEOUT = Duration.seconds(2);
const REMOTE_READY_TIMEOUT = Duration.seconds(15);
const REMOTE_REUSE_READY_TIMEOUT = Duration.seconds(2);
const SSH_READY_TIMEOUT_MS = 20_000;
const SSH_READY_PROBE_TIMEOUT_MS = 1_000;
const TUNNEL_SHUTDOWN_TIMEOUT_MS = 2_000;
const REMOTE_READY_TIMEOUT_MS = 15_000;
const REMOTE_REUSE_READY_TIMEOUT_MS = 2_000;
export interface RemoteT3RunnerOptions {
readonly packageSpec?: string;
@@ -241,37 +237,12 @@ function applyScriptPlaceholders(
}
export function describeReadinessCause(cause: unknown): unknown {
if (isSshReadinessError(cause)) {
const base = {
if (cause instanceof SshReadinessError) {
return {
_tag: cause._tag,
message: cause.message,
...(cause.cause === undefined ? {} : { cause: describeReadinessCause(cause.cause) }),
};
switch (cause._tag) {
case "SshReadinessProbeFailedError":
return {
...base,
requestUrl: cause.requestUrl,
...(cause.cause === undefined ? {} : { cause: describeReadinessCause(cause.cause) }),
};
case "SshReadinessProbeTimedOutError":
return {
...base,
requestUrl: cause.requestUrl,
attempt: cause.attempt,
probeTimeoutMs: cause.probeTimeoutMs,
};
case "SshReadinessTimedOutError":
return {
...base,
baseUrl: cause.baseUrl,
requestUrl: cause.requestUrl,
timeoutMs: cause.timeoutMs,
intervalMs: cause.intervalMs,
probeTimeoutMs: cause.probeTimeoutMs,
attempts: cause.attempts,
...(cause.cause === undefined ? {} : { cause: describeReadinessCause(cause.cause) }),
};
}
}
if (cause instanceof Error) {
return {
@@ -715,9 +686,9 @@ export function buildRemoteLaunchScript(input?: RemoteT3RunnerOptions): string {
T3_WAIT_READY_SCRIPT: stripTrailingNewlines(REMOTE_WAIT_READY_SCRIPT),
T3_DEFAULT_REMOTE_PORT: String(DEFAULT_REMOTE_PORT),
T3_REMOTE_PORT_SCAN_WINDOW: String(REMOTE_PORT_SCAN_WINDOW),
T3_READY_TIMEOUT_MS: String(Duration.toMillis(REMOTE_READY_TIMEOUT)),
T3_REUSE_READY_TIMEOUT_MS: String(Duration.toMillis(REMOTE_REUSE_READY_TIMEOUT)),
T3_READY_PROBE_TIMEOUT_MS: String(Duration.toMillis(SSH_READY_PROBE_TIMEOUT)),
T3_READY_TIMEOUT_MS: String(REMOTE_READY_TIMEOUT_MS),
T3_REUSE_READY_TIMEOUT_MS: String(REMOTE_REUSE_READY_TIMEOUT_MS),
T3_READY_PROBE_TIMEOUT_MS: String(SSH_READY_PROBE_TIMEOUT_MS),
});
}
@@ -899,21 +870,20 @@ const readRemoteServerLogTail = Effect.fn("ssh/tunnel.readRemoteServerLogTail")(
export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(function* (input: {
readonly baseUrl: string;
readonly timeout?: Duration.Input;
readonly interval?: Duration.Input;
readonly probeTimeout?: Duration.Input;
readonly timeoutMs?: number;
readonly intervalMs?: number;
readonly probeTimeoutMs?: number;
readonly path?: string;
}): Effect.fn.Return<void, SshReadinessError, HttpClient.HttpClient> {
const timeout = Duration.fromInputUnsafe(input.timeout ?? Duration.seconds(30));
const interval = Duration.fromInputUnsafe(input.interval ?? Duration.millis(100));
const probeTimeout = Duration.fromInputUnsafe(input.probeTimeout ?? SSH_READY_PROBE_TIMEOUT);
const timeoutMs = Duration.toMillis(timeout);
const intervalMs = Duration.toMillis(interval);
const probeTimeoutMs = Duration.toMillis(probeTimeout);
const retryPolicy = Schedule.spaced(interval);
const timeoutMs = input.timeoutMs ?? 30_000;
const intervalMs = input.intervalMs ?? 100;
const probeTimeoutMs = input.probeTimeoutMs ?? SSH_READY_PROBE_TIMEOUT_MS;
const retryPolicy = Schedule.spaced(Duration.millis(intervalMs)).pipe(
Schedule.take(Math.max(0, Math.ceil(timeoutMs / intervalMs))),
);
const requestUrl = new URL(input.path ?? "/", input.baseUrl).toString();
const client = yield* HttpClient.HttpClient;
const lastProbeFailure = yield* Ref.make<Option.Option<unknown>>(Option.none());
const lastProbeFailure = yield* Ref.make<unknown>(null);
let attempt = 0;
yield* Effect.logDebug("ssh.tunnel.httpReady.start", {
@@ -930,12 +900,11 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
Effect.gen(function* () {
attempt += 1;
const responseOption = yield* effect.pipe(
Effect.timeoutOption(probeTimeout),
Effect.timeoutOption(Duration.millis(probeTimeoutMs)),
Effect.mapError(
(cause) =>
new SshReadinessProbeFailedError({
new SshReadinessError({
message: `Backend readiness probe failed at ${requestUrl}.`,
requestUrl,
cause,
}),
),
@@ -944,32 +913,30 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
onSome: Effect.succeed,
onNone: () =>
Effect.fail(
new SshReadinessProbeTimedOutError({
new SshReadinessError({
message: `Backend readiness probe exceeded ${probeTimeoutMs}ms at ${requestUrl}.`,
requestUrl,
attempt,
probeTimeoutMs,
cause: {
kind: "probe-timeout",
attempt,
probeTimeoutMs,
},
}),
),
});
}).pipe(
Effect.mapError((cause) =>
isSshReadinessError(cause)
cause instanceof SshReadinessError
? cause
: new SshReadinessProbeFailedError({
: new SshReadinessError({
message: `Backend readiness probe failed at ${requestUrl}.`,
requestUrl,
cause,
}),
),
Effect.tapError((cause) =>
Ref.set(
lastProbeFailure,
Option.some({
attempt,
cause: describeReadinessCause(cause),
}),
),
Ref.set(lastProbeFailure, {
attempt,
cause: describeReadinessCause(cause),
}),
),
),
),
@@ -979,15 +946,14 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
const result = yield* readinessClient.execute(HttpClientRequest.get(requestUrl)).pipe(
Effect.mapError((cause) =>
isSshReadinessError(cause)
cause instanceof SshReadinessError
? cause
: new SshReadinessProbeFailedError({
: new SshReadinessError({
message: `Backend readiness probe failed at ${requestUrl}.`,
requestUrl,
cause,
}),
),
Effect.timeoutOption(timeout),
Effect.timeoutOption(Duration.millis(timeoutMs)),
);
return yield* Option.match(result, {
@@ -1007,17 +973,11 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
intervalMs,
probeTimeoutMs,
attempts: attempt,
lastFailure: Option.getOrUndefined(lastFailure),
lastFailure,
});
return yield* new SshReadinessTimedOutError({
return yield* new SshReadinessError({
message: `Timed out waiting ${timeoutMs}ms for backend readiness at ${input.baseUrl}.`,
baseUrl: input.baseUrl,
requestUrl,
timeoutMs,
intervalMs,
probeTimeoutMs,
attempts: attempt,
...(Option.isSome(lastFailure) ? { cause: lastFailure.value } : {}),
cause: lastFailure,
});
}),
});
@@ -1276,7 +1236,7 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
yield* Effect.raceFirst(
waitForHttpReady({
baseUrl: input.httpBaseUrl,
timeout: SSH_READY_TIMEOUT,
timeoutMs: SSH_READY_TIMEOUT_MS,
}),
exitFailure,
).pipe(
@@ -1336,7 +1296,7 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
: child
.kill({
killSignal: "SIGTERM",
forceKillAfter: Duration.toMillis(TUNNEL_SHUTDOWN_TIMEOUT),
forceKillAfter: TUNNEL_SHUTDOWN_TIMEOUT_MS,
})
.pipe(Effect.ignore),
),
@@ -1580,7 +1540,7 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
[
tunnelEntry.process.kill({
killSignal: "SIGTERM",
forceKillAfter: Duration.toMillis(TUNNEL_SHUTDOWN_TIMEOUT),
forceKillAfter: TUNNEL_SHUTDOWN_TIMEOUT_MS,
}),
stopRemoteServer(
tunnelEntry.target,
@@ -1634,7 +1594,7 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
remotePort: entry.remotePort,
});
const readinessExit = yield* Effect.exit(
waitForHttpReady({ baseUrl: entry.httpBaseUrl, timeout: Duration.seconds(2) }),
waitForHttpReady({ baseUrl: entry.httpBaseUrl, timeoutMs: 2_000 }),
);
if (Exit.isSuccess(readinessExit)) {
yield* Effect.logDebug("ssh.environment.tunnel.reused", {