@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 56 lines 2.09 KB
Raw
1 import { EnvironmentRegistry, removedWithRelay } from "@t3tools/client-runtime/connection";
2 import { createRuntimeCommand } from "@t3tools/client-runtime/state/runtime";
3 import * as Effect from "effect/Effect";
4 import * as Schema from "effect/Schema";
5 import * as SubscriptionRef from "effect/SubscriptionRef";
6 import { msg } from "gt-react-native";
7
8 import { connectionAtomRuntime } from "../../connection/runtime";
9 import { m } from "../../i18n/messages";
10 import { archiveCloudComposerDrafts } from "../../state/use-composer-drafts";
11
12 export class CloudDraftArchiveError extends Schema.TaggedError<CloudDraftArchiveError>()(
13 "CloudDraftArchiveError",
14 {
15 environmentCount: Schema.Number,
16 hasAccountId: Schema.Boolean,
17 cause: Schema.Defect(),
18 },
19 ) {
20 override get message(): string {
21 return m(
22 msg(
23 "{count, plural, other {Could not preserve local drafts for {count} cloud environments before sign-out.}}",
24 { count: this.environmentCount },
25 ),
26 );
27 }
28 }
29
30 export const removeCloudEnvironments = createRuntimeCommand(connectionAtomRuntime, {
31 label: "cloud:preserve-drafts-and-remove-environments",
32 execute: Effect.fn("removeCloudEnvironments")(function* (accountId: string | null) {
33 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
34 const entries = yield* SubscriptionRef.get(registry.entries);
35 const environmentIds = new Set(
36 [...entries.values()].filter(removedWithRelay).map((entry) => entry.target.environmentId),
37 );
38 // Backup failure must retain local file owners so a later sign-in can retry.
39 yield* Effect.tryPromise({
40 // Resolve draft owners against every environment, not just the removed ones.
41 try: () =>
42 archiveCloudComposerDrafts(
43 accountId,
44 environmentIds,
45 [...entries.values()].map((entry) => entry.target.environmentId),
46 ),
47 catch: (cause) =>
48 new CloudDraftArchiveError({
49 environmentCount: environmentIds.size,
50 hasAccountId: accountId !== null,
51 cause,
52 }),
53 });
54 yield* registry.removeRelayEnvironments();
55 }),
56 });