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

threadContentPresentation.ts

70 lines2.3 KB

1import { type EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2import type { EnvironmentThreadStatus } from "@t3tools/client-runtime/state/threads";
3import { msg } from "gt-react-native";
4​
5import { m, type MessageResolver } from "../../i18n/messages";
6​
7export type ThreadContentPresentation =
8 | { readonly kind: "ready" }
9 | { readonly kind: "loading" }
10 | {
11 readonly kind: "unavailable";
12 readonly title: string;
13 readonly detail: string;
14 };
15​
16// Shared so the thread screen's props keep their identity across renders.
17export const THREAD_CONTENT_READY: ThreadContentPresentation = { kind: "ready" };
18const THREAD_CONTENT_LOADING: ThreadContentPresentation = { kind: "loading" };
19​
20export function projectThreadContentPresentation(input: {
21 readonly hasDetail: boolean;
22 readonly detailError: string | null;
23 readonly detailDeleted: boolean;
24 readonly connectionState: EnvironmentConnectionPhase;
25 readonly resolve?: MessageResolver;
26}): ThreadContentPresentation {
27 const resolve = input.resolve ?? m;
28 if (input.hasDetail) {
29 return THREAD_CONTENT_READY;
30 }
31 if (input.detailDeleted) {
32 return {
33 kind: "unavailable",
34 title: resolve(msg("Thread unavailable")),
35 detail: resolve(msg("This thread was deleted or is no longer available.")),
36 };
37 }
38 if (input.detailError !== null) {
39 return {
40 kind: "unavailable",
41 title: resolve(msg("Could not load conversation")),
42 detail: input.detailError,
43 };
44 }
45 if (
46 input.connectionState === "connected" ||
47 input.connectionState === "connecting" ||
48 input.connectionState === "reconnecting"
49 ) {
50 return THREAD_CONTENT_LOADING;
51 }
52 return {
53 kind: "unavailable",
54 title: resolve(msg("Messages not cached")),
55 detail: resolve(msg("Reconnect this environment to load the conversation.")),
56 };
58​
59export function threadSyncStatusLabel(
60 status: EnvironmentThreadStatus,
61 presentation: ThreadContentPresentation["kind"],
62 hasError: boolean,
63 resolve: MessageResolver = m,
64): string | null {
65 if (status !== "empty" && status !== "cached" && status !== "synchronizing") return null;
66 if (presentation === "ready") {
67 return hasError ? resolve(msg("Could not sync messages")) : resolve(msg("Syncing messages…"));
68 }
69 return presentation === "loading" ? resolve(msg("Loading messages…")) : null;