Open History to see the last commit
1import { EnvironmentRegistry } from "@t3tools/client-runtime/connection";
2import { createRuntimeCommand } from "@t3tools/client-runtime/state/runtime";
3import * as Effect from "effect/Effect";
4import * as Schema from "effect/Schema";
5import * as SubscriptionRef from "effect/SubscriptionRef";
6
7import { connectionAtomRuntime } from "../../connection/runtime";
8import { archiveCloudComposerDrafts } from "../../state/use-composer-drafts";
9
10export class CloudDraftArchiveError extends Schema.TaggedError<CloudDraftArchiveError>()(
11 "CloudDraftArchiveError",
12 {
13 environmentCount: Schema.Number,
14 hasAccountId: Schema.Boolean,
15 cause: Schema.Defect(),
16 },
17) {
18 override get message(): string {
19 return `Could not preserve local drafts for ${this.environmentCount} cloud environments before sign-out.`;
20 }
21}
22
23export const removeCloudEnvironments = createRuntimeCommand(connectionAtomRuntime, {
24 label: "cloud:preserve-drafts-and-remove-environments",
25 execute: Effect.fn("removeCloudEnvironments")(function* (accountId: string | null) {
26 const registry = yield* EnvironmentRegistry;
27 const entries = yield* SubscriptionRef.get(registry.entries);
28 const environmentIds = new Set(
29 [...entries.values()]
30 .filter((entry) => entry.target._tag === "RelayConnectionTarget")
31 .map((entry) => entry.target.environmentId),
32 );
33 // Credentials are already revoked. A failed backup must leave the local
34 // owners intact so a later sign-in can retry without losing their files.
35 yield* Effect.tryPromise({
36 try: () => archiveCloudComposerDrafts(accountId, environmentIds),
37 catch: (cause) =>
38 new CloudDraftArchiveError({
39 environmentCount: environmentIds.size,
40 hasAccountId: accountId !== null,
41 cause,
42 }),
43 });
44 yield* registry.removeRelayEnvironments();
45 }),
46});