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

useHandleNewThread.ts

116 lines3.9 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​
24 const activeThread = routeThreadId
25 ? threads.find((thread) => thread.id === routeThreadId)
26 : undefined;
27​
28 const handleNewThread = useCallback(
29 (
30 projectId: ProjectId,
31 options?: {
32 branch?: string | null;
33 worktreePath?: string | null;
34 envMode?: DraftThreadEnvMode;
35 },
36 ): Promise<void> => {
37 const {
38 clearProjectDraftThreadId,
39 getDraftThread,
40 getDraftThreadByProjectId,
41 setDraftThreadContext,
42 setProjectDraftThreadId,
43 } = useComposerDraftStore.getState();
44 const hasBranchOption = options?.branch !== undefined;
45 const hasWorktreePathOption = options?.worktreePath !== undefined;
46 const hasEnvModeOption = options?.envMode !== undefined;
47 const storedDraftThread = getDraftThreadByProjectId(projectId);
48 const latestActiveDraftThread: DraftThreadState | null = routeThreadId
49 ? getDraftThread(routeThreadId)
50 : null;
51 if (storedDraftThread) {
52 return (async () => {
53 if (hasBranchOption || hasWorktreePathOption || hasEnvModeOption) {
54 setDraftThreadContext(storedDraftThread.threadId, {
55 ...(hasBranchOption ? { branch: options?.branch ?? null } : {}),
56 ...(hasWorktreePathOption ? { worktreePath: options?.worktreePath ?? null } : {}),
57 ...(hasEnvModeOption ? { envMode: options?.envMode } : {}),
58 });
59 }
60 setProjectDraftThreadId(projectId, storedDraftThread.threadId);
61 if (routeThreadId === storedDraftThread.threadId) {
62 return;
63 }
64 await navigate({
65 to: "/$threadId",
66 params: { threadId: storedDraftThread.threadId },
67 });
68 })();
69 }
70​
71 clearProjectDraftThreadId(projectId);
72​
73 if (
74 latestActiveDraftThread &&
75 routeThreadId &&
76 latestActiveDraftThread.projectId === projectId
77 ) {
78 if (hasBranchOption || hasWorktreePathOption || hasEnvModeOption) {
79 setDraftThreadContext(routeThreadId, {
80 ...(hasBranchOption ? { branch: options?.branch ?? null } : {}),
81 ...(hasWorktreePathOption ? { worktreePath: options?.worktreePath ?? null } : {}),
82 ...(hasEnvModeOption ? { envMode: options?.envMode } : {}),
83 });
84 }
85 setProjectDraftThreadId(projectId, routeThreadId);
86 return Promise.resolve();
87 }
88​
89 const threadId = newThreadId();
90 const createdAt = new Date().toISOString();
91 return (async () => {
92 setProjectDraftThreadId(projectId, threadId, {
93 createdAt,
94 branch: options?.branch ?? null,
95 worktreePath: options?.worktreePath ?? null,
96 envMode: options?.envMode ?? "local",
97 runtimeMode: DEFAULT_RUNTIME_MODE,
98 });
99​
100 await navigate({
101 to: "/$threadId",
102 params: { threadId },
103 });
104 })();
105 },
106 [navigate, routeThreadId],
107 );
108​
109 return {
110 activeDraftThread,
111 activeThread,
112 handleNewThread,
113 projects,
114 routeThreadId,
115 };