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

threadRoutes.ts

82 lines2.1 KB

1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
3import type { DraftId } from "./composerDraftStore";
4​
5export type ThreadRouteTarget =
6 | {
7 kind: "server";
8 threadRef: ScopedThreadRef;
9 }
10 | {
11 kind: "draft";
12 draftId: DraftId;
13 };
14​
15type DraftThreadRouteState = {
16 environmentId: EnvironmentId;
17 threadId: ThreadId;
18 promotedTo?: ScopedThreadRef | null;
19};
20​
21export function buildThreadRouteParams(ref: ScopedThreadRef): {
22 environmentId: EnvironmentId;
23 threadId: ThreadId;
24} {
25 return {
26 environmentId: ref.environmentId,
27 threadId: ref.threadId,
28 };
30​
31export function buildDraftThreadRouteParams(draftId: DraftId): {
32 draftId: DraftId;
33} {
34 return { draftId };
36​
37export function resolveThreadRouteRef(
38 params: Partial<Record<"environmentId" | "threadId", string | undefined>>,
39): ScopedThreadRef | null {
40 if (!params.environmentId || !params.threadId) {
41 return null;
42 }
43​
44 return scopeThreadRef(params.environmentId as EnvironmentId, params.threadId as ThreadId);
46​
47export function resolveThreadRouteTarget(
48 params: Partial<Record<"environmentId" | "threadId" | "draftId", string | undefined>>,
49): ThreadRouteTarget | null {
50 if (params.environmentId && params.threadId) {
51 return {
52 kind: "server",
53 threadRef: scopeThreadRef(params.environmentId as EnvironmentId, params.threadId as ThreadId),
54 };
55 }
56​
57 if (!params.draftId) {
58 return null;
59 }
60​
61 return {
62 kind: "draft",
63 draftId: params.draftId as DraftId,
64 };
66​
67/**
68 * Resolves the thread represented by either a canonical thread route or a
69 * draft route whose promotion to a server thread has been recorded.
70 */
71export function resolveActiveThreadRouteRef(
72 target: ThreadRouteTarget | null,
73 draftThread: DraftThreadRouteState | null,
74): ScopedThreadRef | null {
75 if (target?.kind === "server") {
76 return target.threadRef;
77 }
78 if (target?.kind !== "draft" || !draftThread?.promotedTo) {
79 return null;
80 }
81 return draftThread.promotedTo;