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

threadRoutes.ts

108 lines2.9 KB

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