| 1 | import * as Effect from "effect/Effect"; |
| 2 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 3 | |
| 4 | import type { SavedRemoteConnection } from "../../lib/connection"; |
| 5 | import { linkEnvironmentToCloudWithPreference } from "../cloud/linkEnvironment"; |
| 6 | import { updateAgentAwarenessRegistrationPreferences } from "./remoteRegistration"; |
| 7 | |
| 8 | export const setLiveActivityUpdatesEnabled = Effect.fn("setLiveActivityUpdatesEnabled")( |
| 9 | function* (input: { |
| 10 | readonly enabled: boolean; |
| 11 | readonly previousEnabled: boolean; |
| 12 | readonly clerkToken: string | null; |
| 13 | readonly connections: ReadonlyArray<SavedRemoteConnection>; |
| 14 | readonly canConfigureEnvironment: (environmentId: EnvironmentId) => boolean; |
| 15 | }) { |
| 16 | const linkedConnections = input.connections.filter( |
| 17 | (connection) => connection.bearerToken !== null, |
| 18 | ); |
| 19 | |
| 20 | const updateEnvironmentPreference = Effect.fn("updateEnvironmentPreference")(function* ( |
| 21 | connection: SavedRemoteConnection, |
| 22 | enabled: boolean, |
| 23 | clerkToken: string, |
| 24 | ) { |
| 25 | if (!input.canConfigureEnvironment(connection.environmentId)) return; |
| 26 | |
| 27 | yield* linkEnvironmentToCloudWithPreference({ |
| 28 | clerkToken, |
| 29 | connection, |
| 30 | liveActivitiesEnabled: enabled, |
| 31 | }); |
| 32 | }); |
| 33 | |
| 34 | const updateRelayPreference = Effect.fn("updateRelayPreference")(function* (enabled: boolean) { |
| 35 | yield* updateAgentAwarenessRegistrationPreferences({ |
| 36 | liveActivitiesEnabled: enabled, |
| 37 | }); |
| 38 | |
| 39 | const clerkToken = input.clerkToken; |
| 40 | if (!clerkToken) return; |
| 41 | |
| 42 | yield* Effect.forEach( |
| 43 | linkedConnections, |
| 44 | (connection) => updateEnvironmentPreference(connection, enabled, clerkToken), |
| 45 | { concurrency: "unbounded" }, |
| 46 | ); |
| 47 | }); |
| 48 | |
| 49 | const restoreRelayPreference = Effect.fn("restoreRelayPreference")(function* () { |
| 50 | yield* updateAgentAwarenessRegistrationPreferences({ |
| 51 | liveActivitiesEnabled: input.previousEnabled, |
| 52 | }).pipe( |
| 53 | Effect.catchCause((cause) => |
| 54 | Effect.logWarning("Could not restore Live Activity device preference.", cause), |
| 55 | ), |
| 56 | ); |
| 57 | |
| 58 | const clerkToken = input.clerkToken; |
| 59 | if (!clerkToken) return; |
| 60 | |
| 61 | yield* Effect.forEach( |
| 62 | linkedConnections, |
| 63 | (connection) => |
| 64 | updateEnvironmentPreference(connection, input.previousEnabled, clerkToken).pipe( |
| 65 | Effect.catchCause((cause) => |
| 66 | Effect.logWarning( |
| 67 | `Could not restore Live Activity preference for environment ${connection.environmentId}.`, |
| 68 | cause, |
| 69 | ), |
| 70 | ), |
| 71 | ), |
| 72 | { concurrency: "unbounded" }, |
| 73 | ); |
| 74 | }); |
| 75 | |
| 76 | yield* updateRelayPreference(input.enabled).pipe( |
| 77 | Effect.onError(() => restoreRelayPreference()), |
| 78 | ); |
| 79 | }, |
| 80 | ); |