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

useHandleNewThread.ts

123 lines4.2 KB

1import { DEFAULT_RUNTIME_MODE, type ProjectId, ThreadId } from "@t3tools/contracts";
2import { useNavigate, useParams } from "@tanstack/react-router";
3import { useCallback } from "react";
4import {
5 type DraftThreadEnvMode,
6 type DraftThreadState,
7 useComposerDraftStore,
8} from "../composerDraftStore";
9import { newThreadId } from "../lib/utils";
10import { useStore } from "../store";
11​
12export function useHandleNewThread() {
13 const projects = useStore((store) => store.projects);
14 const threads = useStore((store) => store.threads);
15 const navigate = useNavigate();
16 const routeThreadId = useParams({
17 strict: false,
18 select: (params) => (params.threadId ? ThreadId.makeUnsafe(params.threadId) : null),
19 });
20 const activeDraftThread = useComposerDraftStore((store) =>
21 routeThreadId ? (store.draftThreadsByThreadId[routeThreadId] ?? null) : null,
22 );
23 const activeComposerDraft = useComposerDraftStore((store) =>
24 routeThreadId ? (store.draftsByThreadId[routeThreadId] ?? null) : null,
25 );
26​
27 const activeThread = routeThreadId
28 ? threads.find((thread) => thread.id === routeThreadId)
29 : undefined;
30​
31 const handleNewThread = useCallback(
32 (
33 projectId: ProjectId,
34 options?: {
35 branch?: string | null;
36 worktreePath?: string | null;
37 envMode?: DraftThreadEnvMode;
38 },
39 ): Promise<void> => {
40 const {
41 clearProjectDraftThreadId,
42 getDraftThread,
43 getDraftThreadByProjectId,
44 copyThreadSettings,
45 setDraftThreadContext,
46 setProjectDraftThreadId,
47 } = useComposerDraftStore.getState();
48 const hasBranchOption = options?.branch !== undefined;
49 const hasWorktreePathOption = options?.worktreePath !== undefined;
50 const hasEnvModeOption = options?.envMode !== undefined;
51 const storedDraftThread = getDraftThreadByProjectId(projectId);
52 const latestActiveDraftThread: DraftThreadState | null = routeThreadId
53 ? getDraftThread(routeThreadId)
54 : null;
55 if (storedDraftThread) {
56 return (async () => {
57 if (hasBranchOption || hasWorktreePathOption || hasEnvModeOption) {
58 setDraftThreadContext(storedDraftThread.threadId, {
59 ...(hasBranchOption ? { branch: options?.branch ?? null } : {}),
60 ...(hasWorktreePathOption ? { worktreePath: options?.worktreePath ?? null } : {}),
61 ...(hasEnvModeOption ? { envMode: options?.envMode } : {}),
62 });
63 }
64 setProjectDraftThreadId(projectId, storedDraftThread.threadId);
65 if (routeThreadId === storedDraftThread.threadId) {
66 return;
67 }
68 await navigate({
69 to: "/$threadId",
70 params: { threadId: storedDraftThread.threadId },
71 });
72 })();
73 }
74​
75 clearProjectDraftThreadId(projectId);
76​
77 if (
78 latestActiveDraftThread &&
79 routeThreadId &&
80 latestActiveDraftThread.projectId === projectId
81 ) {
82 if (hasBranchOption || hasWorktreePathOption || hasEnvModeOption) {
83 setDraftThreadContext(routeThreadId, {
84 ...(hasBranchOption ? { branch: options?.branch ?? null } : {}),
85 ...(hasWorktreePathOption ? { worktreePath: options?.worktreePath ?? null } : {}),
86 ...(hasEnvModeOption ? { envMode: options?.envMode } : {}),
87 });
88 }
89 setProjectDraftThreadId(projectId, routeThreadId);
90 return Promise.resolve();
91 }
92​
93 const threadId = newThreadId();
94 const createdAt = new Date().toISOString();
95 return (async () => {
96 setProjectDraftThreadId(projectId, threadId, {
97 createdAt,
98 branch: options?.branch ?? null,
99 worktreePath: options?.worktreePath ?? null,
100 envMode: options?.envMode ?? "local",
101 runtimeMode: DEFAULT_RUNTIME_MODE,
102 });
103 if (routeThreadId && activeComposerDraft) {
104 copyThreadSettings(routeThreadId, threadId);
105 }
106​
107 await navigate({
108 to: "/$threadId",
109 params: { threadId },
110 });
111 })();
112 },
113 [activeComposerDraft, navigate, routeThreadId],
114 );
115​
116 return {
117 activeDraftThread,
118 activeThread,
119 handleNewThread,
120 projects,
121 routeThreadId,
122 };