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

threadRoutes.ts

59 lines1.4 KB

1import { scopeThreadRef } from "@v12/client-runtime/environment";
2import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@v12/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​
15export function buildThreadRouteParams(ref: ScopedThreadRef): {
16 environmentId: EnvironmentId;
17 threadId: ThreadId;
18} {
19 return {
20 environmentId: ref.environmentId,
21 threadId: ref.threadId,
22 };
24​
25export function buildDraftThreadRouteParams(draftId: DraftId): {
26 draftId: DraftId;
27} {
28 return { draftId };
30​
31export function resolveThreadRouteRef(
32 params: Partial<Record<"environmentId" | "threadId", string | undefined>>,
33): ScopedThreadRef | null {
34 if (!params.environmentId || !params.threadId) {
35 return null;
36 }
37​
38 return scopeThreadRef(params.environmentId as EnvironmentId, params.threadId as ThreadId);
40​
41export function resolveThreadRouteTarget(
42 params: Partial<Record<"environmentId" | "threadId" | "draftId", string | undefined>>,
43): ThreadRouteTarget | null {
44 if (params.environmentId && params.threadId) {
45 return {
46 kind: "server",
47 threadRef: scopeThreadRef(params.environmentId as EnvironmentId, params.threadId as ThreadId),
48 };
49 }
50​
51 if (!params.draftId) {
52 return null;
53 }
54​
55 return {
56 kind: "draft",
57 draftId: params.draftId as DraftId,
58 };