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

useHandleNewThread.ts

126 lines4.4 KB

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