Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadContentPresentation.ts

57 lines1.7 KB

1import { type EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2import type { EnvironmentThreadStatus } from "@t3tools/client-runtime/state/threads";
3​
4export type ThreadContentPresentation =
5 | { readonly kind: "ready" }
6 | { readonly kind: "loading" }
7 | {
8 readonly kind: "unavailable";
9 readonly title: string;
10 readonly detail: string;
11 };
12​
13export function projectThreadContentPresentation(input: {
14 readonly hasDetail: boolean;
15 readonly detailError: string | null;
16 readonly detailDeleted: boolean;
17 readonly connectionState: EnvironmentConnectionPhase;
18}): ThreadContentPresentation {
19 if (input.hasDetail) {
20 return { kind: "ready" };
21 }
22 if (input.detailDeleted) {
23 return {
24 kind: "unavailable",
25 title: "Thread unavailable",
26 detail: "This thread was deleted or is no longer available.",
27 };
28 }
29 if (input.detailError !== null) {
30 return {
31 kind: "unavailable",
32 title: "Could not load conversation",
33 detail: input.detailError,
34 };
35 }
36 if (
37 input.connectionState === "connected" ||
38 input.connectionState === "connecting" ||
39 input.connectionState === "reconnecting"
40 ) {
41 return { kind: "loading" };
42 }
43 return {
44 kind: "unavailable",
45 title: "Messages not cached",
46 detail: "Reconnect this environment to load the conversation.",
47 };
49​
50export function threadSyncStatusLabel(
51 status: EnvironmentThreadStatus,
52 presentation: ThreadContentPresentation["kind"],
53): string | null {
54 if (status !== "empty" && status !== "cached" && status !== "synchronizing") return null;
55 if (presentation === "ready") return "Syncing messages…";
56 return presentation === "loading" ? "Loading messages…" : null;