|
1 | 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { AuthOrchestrationOperateScope } from "@t3tools/contracts"; |
| 3 | +import { Atom } from "effect/reactivity"; |
2 | 4 | import { useMemo } from "react"; |
3 | 5 | import { threadKey } from "@t3tools/client-runtime/state/entities"; |
4 | 6 | import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; |
5 | | -import { buildPendingNewTasks, type PendingNewTask } from "./pending-new-tasks-model"; |
| 7 | +import { |
| 8 | + buildPendingNewTasks, |
| 9 | + isQueuedCreationSending, |
| 10 | + type PendingNewTask, |
| 11 | + type ThreadOutboxSendGate, |
| 12 | +} from "./pending-new-tasks-model"; |
6 | 13 | import { pendingThreadCreationOutcomesAtom, withLaunchingThreads } from "./pending-thread-creation"; |
7 | | -import { connectedEnvironmentIdsAtom } from "./presentation"; |
8 | 14 | import { flattenQueuedThreadMessages } from "./thread-outbox-model"; |
9 | 15 | import { environmentThreadShells } from "./threads"; |
10 | 16 | import { newTaskComposerDraftsAtom } from "./use-composer-drafts"; |
11 | | -import { useThreadOutboxMessages } from "./use-thread-outbox"; |
| 17 | +import { threadOutboxShellStatusesAtom, useThreadOutboxMessages } from "./use-thread-outbox"; |
| 18 | +import { environmentSession, sessionHasScope } from "./session"; |
| 19 | +import { workspaceConnections } from "./workspace"; |
12 | 20 |
|
13 | 21 | export type { |
14 | 22 | PendingDraftTask, |
15 | 23 | PendingNewTask, |
16 | 24 | PendingQueuedTask, |
17 | 25 | } from "./pending-new-tasks-model"; |
18 | 26 |
|
| 27 | +const threadOutboxSendGateAtom = Atom.make((get): ThreadOutboxSendGate => { |
| 28 | + const connected = get(workspaceConnections.environmentsAtom).flatMap((environment) => |
| 29 | + environment.connectionState === "connected" ? [environment.environmentId] : [], |
| 30 | + ); |
| 31 | + return { |
| 32 | + connectedEnvironmentIds: new Set(connected), |
| 33 | + operableEnvironmentIds: new Set( |
| 34 | + connected.filter((environmentId) => |
| 35 | + sessionHasScope( |
| 36 | + get(environmentSession.sessionStateAtom(environmentId)), |
| 37 | + AuthOrchestrationOperateScope, |
| 38 | + ), |
| 39 | + ), |
| 40 | + ), |
| 41 | + shellStatuses: get(threadOutboxShellStatusesAtom), |
| 42 | + }; |
| 43 | +}).pipe(Atom.withLabel("mobile:pending-new-tasks:send-gate")); |
| 44 | + |
19 | 45 | export function usePendingNewTasks(): ReadonlyArray<PendingNewTask> { |
20 | 46 | const queuedMessagesByThreadKey = useThreadOutboxMessages(); |
21 | 47 | const drafts = useAtomValue(newTaskComposerDraftsAtom); |
22 | 48 | const hiddenKeys = useAtomValue(environmentThreadShells.hiddenThreadKeysAtom); |
23 | | - const connectedEnvironmentIds = useAtomValue(connectedEnvironmentIdsAtom); |
| 49 | + const sendGate = useAtomValue(threadOutboxSendGateAtom); |
24 | 50 | // A delivered creation can stay queued after its thread is live and hidden. |
25 | 51 | const queuedMessages = flattenQueuedThreadMessages(queuedMessagesByThreadKey).filter( |
26 | 52 | (message) => !hiddenKeys.has(threadKey(message)), |
27 | 53 | ); |
28 | 54 | return buildPendingNewTasks({ |
29 | 55 | queuedMessages, |
30 | 56 | drafts, |
31 | | - connectedEnvironmentIds: new Set(connectedEnvironmentIds), |
| 57 | + sendGate, |
32 | 58 | }); |
33 | 59 | } |
34 | 60 |
|
35 | | -/** The list's threads, with creations a connected environment is delivering placed as working. */ |
| 61 | +/** The list's threads, with creations the outbox is sending placed as working. */ |
36 | 62 | export function useLaunchingThreads( |
37 | 63 | threads: ReadonlyArray<EnvironmentThreadShell>, |
38 | 64 | ): ReadonlyArray<EnvironmentThreadShell> { |
39 | 65 | const queuedMessagesByThreadKey = useThreadOutboxMessages(); |
40 | 66 | const outcomes = useAtomValue(pendingThreadCreationOutcomesAtom); |
41 | 67 | const hiddenThreadKeys = useAtomValue(environmentThreadShells.hiddenThreadKeysAtom); |
42 | | - const connectedEnvironmentIds = useAtomValue(connectedEnvironmentIdsAtom); |
| 68 | + const sendGate = useAtomValue(threadOutboxSendGateAtom); |
43 | 69 | return useMemo(() => { |
44 | | - const connected = new Set(connectedEnvironmentIds); |
45 | 70 | const launches = [ |
46 | 71 | ...flattenQueuedThreadMessages(queuedMessagesByThreadKey).filter((message) => |
47 | | - connected.has(message.environmentId), |
| 72 | + isQueuedCreationSending(message, sendGate), |
48 | 73 | ), |
49 | 74 | ...Object.values(outcomes).flatMap((outcome) => |
50 | 75 | outcome.kind === "delivered" ? [outcome.message] : [], |
51 | 76 | ), |
52 | 77 | ]; |
53 | 78 | return withLaunchingThreads({ threads, launches, hiddenThreadKeys }); |
54 | | - }, [threads, queuedMessagesByThreadKey, outcomes, hiddenThreadKeys, connectedEnvironmentIds]); |
| 79 | + }, [threads, queuedMessagesByThreadKey, outcomes, hiddenThreadKeys, sendGate]); |
55 | 80 | } |
0 commit comments