| 1 | import { type EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection"; |
| 2 | |
| 3 | export type ThreadContentPresentation = |
| 4 | | { readonly kind: "ready" } |
| 5 | | { readonly kind: "loading" } |
| 6 | | { |
| 7 | readonly kind: "unavailable"; |
| 8 | readonly title: string; |
| 9 | readonly detail: string; |
| 10 | }; |
| 11 | |
| 12 | export function projectThreadContentPresentation(input: { |
| 13 | readonly hasDetail: boolean; |
| 14 | readonly detailError: string | null; |
| 15 | readonly detailDeleted: boolean; |
| 16 | readonly connectionState: EnvironmentConnectionPhase; |
| 17 | }): ThreadContentPresentation { |
| 18 | if (input.hasDetail) { |
| 19 | return { kind: "ready" }; |
| 20 | } |
| 21 | if (input.detailDeleted) { |
| 22 | return { |
| 23 | kind: "unavailable", |
| 24 | title: "Thread unavailable", |
| 25 | detail: "This thread was deleted or is no longer available.", |
| 26 | }; |
| 27 | } |
| 28 | if (input.detailError !== null) { |
| 29 | return { |
| 30 | kind: "unavailable", |
| 31 | title: "Could not load conversation", |
| 32 | detail: input.detailError, |
| 33 | }; |
| 34 | } |
| 35 | if ( |
| 36 | input.connectionState === "connected" || |
| 37 | input.connectionState === "connecting" || |
| 38 | input.connectionState === "reconnecting" |
| 39 | ) { |
| 40 | // Messages will arrive once the (re)connection completes — present as |
| 41 | // loading; the composer's connection pill reports the connection phase. |
| 42 | return { kind: "loading" }; |
| 43 | } |
| 44 | return { |
| 45 | kind: "unavailable", |
| 46 | title: "Messages not cached", |
| 47 | detail: "Reconnect this environment to load the conversation.", |
| 48 | }; |
| 49 | } |