Open History to see the last commit
1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell";
3import type { EnvironmentThreadState } from "@t3tools/client-runtime/state/threads";
4import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
5import * as Option from "effect/Option";
6import type { DraftId } from "./composerDraftStore";
7
8export type ThreadRouteTarget =
9 | {
10 kind: "server";
11 threadRef: ScopedThreadRef;
12 }
13 | {
14 kind: "draft";
15 draftId: DraftId;
16 };
17
18type DraftThreadRouteState = {
19 environmentId: EnvironmentId;
20 threadId: ThreadId;
21 promotedTo?: ScopedThreadRef | null;
22};
23
24export type ThreadRouteRenderState = "loading" | "ready" | "missing";
25
26export function isThreadRouteSnapshotAuthoritative(
27 status: EnvironmentShellStatus | null | undefined,
28): boolean {
29 return status === "live";
30}
31
32export function resolveThreadRouteRenderState(input: {
33 bootstrapComplete: boolean;
34 serverThreadExists: boolean;
35 serverThreadDeleted: boolean;
36 unlistedThreadDetail: EnvironmentThreadState;
37 draftThreadExists: boolean;
38}): ThreadRouteRenderState {
39 if (input.draftThreadExists || (input.serverThreadExists && !input.serverThreadDeleted)) {
40 return "ready";
41 }
42 if (!input.bootstrapComplete) {
43 return "loading";
44 }
45 if (input.serverThreadExists || input.unlistedThreadDetail.status === "deleted") {
46 return "missing";
47 }
48 // Shell membership can lag a new thread; its detail decides.
49 const projection = Option.getOrNull(input.unlistedThreadDetail.data);
50 if (projection === null) {
51 return "loading";
52 }
53 return projection.thread.deletedAt !== null || projection.thread.archivedAt !== null
54 ? "missing"
55 : "ready";
56}
57
58export function buildThreadRouteParams(ref: ScopedThreadRef): {
59 environmentId: EnvironmentId;
60 threadId: ThreadId;
61} {
62 return {
63 environmentId: ref.environmentId,
64 threadId: ref.threadId,
65 };
66}
67
68/** Whether a (decoded) location pathname is `ref`'s own thread route, `/$environmentId/$threadId`. */
69export function isThreadRoutePathname(pathname: string, ref: ScopedThreadRef): boolean {
70 const { environmentId, threadId } = buildThreadRouteParams(ref);
71 return pathname.replace(/\/$/, "") === `/${environmentId}/${threadId}`;
72}
73
74export function buildDraftThreadRouteParams(draftId: DraftId): {
75 draftId: DraftId;
76} {
77 return { draftId };
78}
79
80export function resolveThreadRouteRef(
81 params: Partial<Record<"environmentId" | "threadId", string | undefined>>,
82): ScopedThreadRef | null {
83 if (!params.environmentId || !params.threadId) {
84 return null;
85 }
86
87 return scopeThreadRef(params.environmentId as EnvironmentId, params.threadId as ThreadId);
88}
89
90export function resolveThreadRouteTarget(
91 params: Partial<Record<"environmentId" | "threadId" | "draftId", string | undefined>>,
92): ThreadRouteTarget | null {
93 if (params.environmentId && params.threadId) {
94 return {
95 kind: "server",
96 threadRef: scopeThreadRef(params.environmentId as EnvironmentId, params.threadId as ThreadId),
97 };
98 }
99
100 if (!params.draftId) {
101 return null;
102 }
103
104 return {
105 kind: "draft",
106 draftId: params.draftId as DraftId,
107 };
108}
109
110export function resolveActiveThreadRouteRef(
111 target: ThreadRouteTarget | null,
112 draftThread: DraftThreadRouteState | null,
113): ScopedThreadRef | null {
114 if (target?.kind === "server") {
115 return target.threadRef;
116 }
117 if (target?.kind !== "draft" || !draftThread?.promotedTo) {
118 return null;
119 }
120 return draftThread.promotedTo;
121}