Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
57 changes: 35 additions & 22 deletions apps/web/src/components/ProjectCloneToastCoordinator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ interface TrackedToast {
readonly toastId: ReturnType<typeof toastManager.add>;
readonly renderedKey: string;
readonly phase: ProjectCloneSnapshot["phase"];
readonly startedAt: ProjectCloneSnapshot["startedAt"];
readonly closed?: true;
}

function renderKey(clone: ProjectCloneSnapshot): string {
Expand Down Expand Up @@ -98,6 +100,7 @@ function EnvironmentCloneToasts({ environmentId }: { environmentId: EnvironmentI
seen.add(clone.projectId);
const key = renderKey(clone);
const tracked = toasts.current.get(clone.projectId);
const sameAttempt = tracked?.startedAt === clone.startedAt;
const name = projectCloneDisplayName(clone);
const closeToast = () => {
const current = toasts.current.get(clone.projectId);
Expand All @@ -111,14 +114,42 @@ function EnvironmentCloneToasts({ environmentId }: { environmentId: EnvironmentI
closeToast();
continue;
}
if (tracked?.renderedKey === key) continue;
if (sameAttempt && tracked.renderedKey === key) continue;
if (
isProjectCloneToastSilenced(ref) &&
(clone.phase === "running" || clone.phase === "done")
) {
closeToast();
continue;
}
if (
sameAttempt &&
tracked.closed &&
(tracked.phase !== "running" || clone.phase !== "failed")
) {
continue;
}

const showToast = (options: ReturnType<typeof stackedThreadToast>) => {
const rendered = { renderedKey: key, phase: clone.phase, startedAt: clone.startedAt };
if (tracked && !tracked.closed) {
toastManager.update(tracked.toastId, options);
toasts.current.set(clone.projectId, { toastId: tracked.toastId, ...rendered });
return;
}

const toastId = toastManager.add({
...options,
// Updates to a closing toast are ignored, even before it has been removed.
onClose: () => {
const current = toasts.current.get(clone.projectId);
if (current?.toastId === toastId) {
toasts.current.set(clone.projectId, { ...current, closed: true });
}
},
});
toasts.current.set(clone.projectId, { toastId, ...rendered });
};

if (clone.phase === "running") {
const options = stackedThreadToast({
Expand All @@ -136,13 +167,7 @@ function EnvironmentCloneToasts({ environmentId }: { environmentId: EnvironmentI
},
data: { hideCopyButton: true },
});
if (tracked) {
toastManager.update(tracked.toastId, options);
toasts.current.set(clone.projectId, { ...tracked, renderedKey: key, phase: "running" });
} else {
const toastId = toastManager.add(options);
toasts.current.set(clone.projectId, { toastId, renderedKey: key, phase: "running" });
}
showToast(options);
continue;
}

Expand All @@ -161,13 +186,7 @@ function EnvironmentCloneToasts({ environmentId }: { environmentId: EnvironmentI
},
data: { hideCopyButton: true },
});
if (tracked) {
toastManager.update(tracked.toastId, options);
toasts.current.set(clone.projectId, { ...tracked, renderedKey: key, phase: "done" });
} else {
const toastId = toastManager.add(options);
toasts.current.set(clone.projectId, { toastId, renderedKey: key, phase: "done" });
}
showToast(options);
continue;
}

Expand Down Expand Up @@ -199,13 +218,7 @@ function EnvironmentCloneToasts({ environmentId }: { environmentId: EnvironmentI
},
},
});
if (tracked) {
toastManager.update(tracked.toastId, options);
toasts.current.set(clone.projectId, { ...tracked, renderedKey: key, phase: clone.phase });
} else {
const toastId = toastManager.add(options);
toasts.current.set(clone.projectId, { toastId, renderedKey: key, phase: clone.phase });
}
showToast(options);
}

for (const [projectId, tracked] of toasts.current) {
Expand Down
113 changes: 113 additions & 0 deletions scripts/scenarios/clone-failure-after-dismissing-progress.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// @effect-diagnostics-next-line nodeBuiltinImport:off -- The fixture holds Git's HTTP request open.
import * as NodeHttp from "node:http";
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import { ProjectId } from "@t3tools/contracts";
import { expect } from "e2e";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as Stream from "effect/Stream";

import { T3_HOME } from "../e2e.config.ts";
import { rpcClient, runPromise, test, WEB } from "./t3.ts";

test(
"Dismissing clone progress leaves the later failure notification visible",
WEB,
async ({ t3, app, screen, browser }) => {
const project = await t3.project("demo");
const thread = await t3.thread({ project, title: "Other work", messages: ["Start"] });
const gitRequest = Promise.withResolvers<NodeHttp.ServerResponse>();
const remote = NodeHttp.createServer((_request, response) => {
gitRequest.resolve(response);
});

try {
await new Promise<void>((resolve) => remote.listen(0, "127.0.0.1", resolve));
const address = remote.address();
if (!address || typeof address === "string") throw new Error("No Git fixture port");
const projectId = ProjectId.make(
await runPromise(
SecureRandom.SecureRandom.pipe(
Effect.flatMap((random) => random.uuidV4()),
Effect.provide(NodeCrypto.layer),
),
),
);
const name = "pending-clone";
const origin = app.baseUrl ?? "";
await t3.open(thread);
await screen.getByText("Start", { exact: true }).waitFor();

await runPromise(
Effect.gen(function* () {
const client = yield* rpcClient(T3_HOME, origin);
const createdAt = DateTime.formatIso(yield* DateTime.now);
yield* client["projectClone.start"]({
projectId,
title: name,
createdAt,
remoteUrl: `http://127.0.0.1:${address.port}/${name}.git`,
destinationPath: `${project}/../${name}`,
});
}).pipe(Effect.scoped),
);
const response = await gitRequest.promise;

const toastTitle = (title: string) =>
browser.locator(`[data-slot="toast-title"]:text-is("${title}")`);
const dismiss = async (title: string) => {
await browser
.locator(
`[data-position]:has(> div > [data-slot="toast-close"]):has([data-slot="toast-title"]:text-is("${title}"))`,
)
.getByRole("button", "Dismiss notification", { exact: true })
.tap();
await toastTitle(title).waitFor({ state: "detached" });
};
const progress = toastTitle(`Cloning ${name}`);
const failure = toastTitle(`Failed to clone ${name}`);

await progress.waitFor();
await dismiss(`Cloning ${name}`);

response.writeHead(404).end();
await runPromise(
Effect.gen(function* () {
const client = yield* rpcClient(T3_HOME, origin);
yield* client.subscribeProjectClones({}).pipe(
Stream.filter((clones) =>
clones.some((clone) => clone.projectId === projectId && clone.phase === "failed"),
),
Stream.runHead,
);
}).pipe(Effect.scoped, Effect.timeout("10 seconds")),
);

try {
await expect(failure).toBeVisible();
} finally {
await app.screenshot("after-background-clone-fails");
}

await dismiss(`Failed to clone ${name}`);

const retryRequest = Promise.withResolvers<NodeHttp.ServerResponse>();
remote.once("request", (_request, response) => retryRequest.resolve(response));
const retry = await runPromise(
Effect.gen(function* () {
const client = yield* rpcClient(T3_HOME, origin);
return yield* client["projectClone.retry"]({ projectId });
}).pipe(Effect.scoped),
);
await expect(retry.applied).toBe(true);
const retryResponse = await retryRequest.promise;
await expect(progress).toBeVisible();
retryResponse.writeHead(404).end();
await expect(failure).toBeVisible();
} finally {
remote.close();
remote.closeAllConnections();
}
},
);
Loading