Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
107 lines
2.7 KiB
TypeScript
107 lines
2.7 KiB
TypeScript
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
|
|
import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell";
|
|
import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
|
|
import type { DraftId } from "./composerDraftStore";
|
|
|
|
export type ThreadRouteTarget =
|
|
| {
|
|
kind: "server";
|
|
threadRef: ScopedThreadRef;
|
|
}
|
|
| {
|
|
kind: "draft";
|
|
draftId: DraftId;
|
|
};
|
|
|
|
type DraftThreadRouteState = {
|
|
environmentId: EnvironmentId;
|
|
threadId: ThreadId;
|
|
promotedTo?: ScopedThreadRef | null;
|
|
};
|
|
|
|
export type ThreadRouteRenderState = "loading" | "ready" | "missing";
|
|
|
|
export function isThreadRouteSnapshotAuthoritative(
|
|
status: EnvironmentShellStatus | undefined,
|
|
): boolean {
|
|
return status === "live";
|
|
}
|
|
|
|
export function resolveThreadRouteRenderState(input: {
|
|
bootstrapComplete: boolean;
|
|
serverThreadExists: boolean;
|
|
serverThreadDeleted: boolean;
|
|
draftThreadExists: boolean;
|
|
}): ThreadRouteRenderState {
|
|
if (input.draftThreadExists || (input.serverThreadExists && !input.serverThreadDeleted)) {
|
|
return "ready";
|
|
}
|
|
if (!input.bootstrapComplete) {
|
|
return "loading";
|
|
}
|
|
return "missing";
|
|
}
|
|
|
|
export function buildThreadRouteParams(ref: ScopedThreadRef): {
|
|
environmentId: EnvironmentId;
|
|
threadId: ThreadId;
|
|
} {
|
|
return {
|
|
environmentId: ref.environmentId,
|
|
threadId: ref.threadId,
|
|
};
|
|
}
|
|
|
|
export function buildDraftThreadRouteParams(draftId: DraftId): {
|
|
draftId: DraftId;
|
|
} {
|
|
return { draftId };
|
|
}
|
|
|
|
export function resolveThreadRouteRef(
|
|
params: Partial<Record<"environmentId" | "threadId", string | undefined>>,
|
|
): ScopedThreadRef | null {
|
|
if (!params.environmentId || !params.threadId) {
|
|
return null;
|
|
}
|
|
|
|
return scopeThreadRef(params.environmentId as EnvironmentId, params.threadId as ThreadId);
|
|
}
|
|
|
|
export function resolveThreadRouteTarget(
|
|
params: Partial<Record<"environmentId" | "threadId" | "draftId", string | undefined>>,
|
|
): ThreadRouteTarget | null {
|
|
if (params.environmentId && params.threadId) {
|
|
return {
|
|
kind: "server",
|
|
threadRef: scopeThreadRef(params.environmentId as EnvironmentId, params.threadId as ThreadId),
|
|
};
|
|
}
|
|
|
|
if (!params.draftId) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
kind: "draft",
|
|
draftId: params.draftId as DraftId,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Resolves the thread represented by either a canonical thread route or a
|
|
* draft route whose promotion to a server thread has been recorded.
|
|
*/
|
|
export function resolveActiveThreadRouteRef(
|
|
target: ThreadRouteTarget | null,
|
|
draftThread: DraftThreadRouteState | null,
|
|
): ScopedThreadRef | null {
|
|
if (target?.kind === "server") {
|
|
return target.threadRef;
|
|
}
|
|
if (target?.kind !== "draft" || !draftThread?.promotedTo) {
|
|
return null;
|
|
}
|
|
return draftThread.promotedTo;
|
|
}
|