Open History to see the last commit
1import { useCallback, useEffect, useMemo } from "react";
2import * as DateTime from "effect/DateTime";
3
4import {
5 deriveThreadCheckpointSummaries,
6 type ThreadCheckpointSummary,
7} from "@t3tools/client-runtime/state/thread-checkpoints";
8import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
9
10import { useCheckpointDiff } from "../../state/queries";
11import { useEnvironmentQuery } from "../../state/query";
12import { reviewEnvironment } from "../../state/review";
13import { useSelectedThreadProjection } from "../../state/use-thread-detail";
14import { useSelectedThreadWorktree } from "../../state/use-selected-thread-worktree";
15import {
16 buildReviewSectionItems,
17 getDefaultReviewSectionId,
18 getReadyReviewCheckpoints,
19 getReviewSectionIdForCheckpoint,
20} from "./reviewModel";
21import {
22 setReviewAsyncError,
23 setReviewGitSections,
24 setReviewSelectedSectionId,
25 setReviewTurnDiff,
26 setReviewTurnDiffLoading,
27 type ReviewCacheForThread,
28} from "./reviewState";
29
30export function useReviewSections(input: {
31 readonly enabled?: boolean;
32 readonly environmentId?: EnvironmentId;
33 readonly threadId?: ThreadId;
34 readonly reviewCache: ReviewCacheForThread;
35}) {
36 const { environmentId, reviewCache, threadId } = input;
37 const enabled = input.enabled ?? true;
38 const selectedThread = useSelectedThreadProjection();
39 const { selectedThreadCwd } = useSelectedThreadWorktree();
40 const diffPreview = useEnvironmentQuery(
41 enabled && environmentId !== undefined && selectedThreadCwd !== null
42 ? reviewEnvironment.diffPreview({
43 environmentId,
44 input: { cwd: selectedThreadCwd },
45 })
46 : null,
47 );
48 const { loadingTurnIds } = reviewCache.asyncState;
49
50 useEffect(() => {
51 if (reviewCache.threadKey && diffPreview.data) {
52 setReviewGitSections(reviewCache.threadKey, diffPreview.data.sources);
53 }
54 }, [diffPreview.data, reviewCache.threadKey]);
55
56 const readyCheckpoints = useMemo(
57 () =>
58 getReadyReviewCheckpoints(
59 selectedThread === null ? [] : deriveThreadCheckpointSummaries(selectedThread.projection),
60 ),
61 [selectedThread],
62 );
63 const checkpointBySectionId = useMemo(
64 () =>
65 Object.fromEntries(
66 readyCheckpoints.map((checkpoint) => [
67 getReviewSectionIdForCheckpoint(checkpoint),
68 checkpoint,
69 ]),
70 ) as Record<string, ThreadCheckpointSummary>,
71 [readyCheckpoints],
72 );
73 const reviewSections = useMemo(
74 () =>
75 buildReviewSectionItems({
76 checkpoints: readyCheckpoints,
77 gitSections: diffPreview.data?.sources ?? reviewCache.gitSections,
78 turnDiffById: reviewCache.turnDiffById,
79 loadingTurnIds,
80 loadingGitSections: diffPreview.isPending,
81 }),
82 [
83 diffPreview.isPending,
84 diffPreview.data?.sources,
85 loadingTurnIds,
86 readyCheckpoints,
87 reviewCache.gitSections,
88 reviewCache.turnDiffById,
89 ],
90 );
91 const selectedSection = useMemo(
92 () =>
93 reviewSections.find((section) => section.id === reviewCache.selectedSectionId) ??
94 reviewSections[0] ??
95 null,
96 [reviewCache.selectedSectionId, reviewSections],
97 );
98 const fallbackSectionId = useMemo(
99 () => getDefaultReviewSectionId(reviewSections),
100 [reviewSections],
101 );
102 const selectedSectionIdExists = useMemo(
103 () =>
104 reviewCache.selectedSectionId
105 ? reviewSections.some((section) => section.id === reviewCache.selectedSectionId)
106 : false,
107 [reviewCache.selectedSectionId, reviewSections],
108 );
109
110 useEffect(() => {
111 if (
112 reviewSections.length > 0 &&
113 reviewCache.threadKey &&
114 (!reviewCache.selectedSectionId || !selectedSectionIdExists)
115 ) {
116 setReviewSelectedSectionId(reviewCache.threadKey, fallbackSectionId);
117 }
118 }, [
119 fallbackSectionId,
120 reviewCache.selectedSectionId,
121 reviewCache.threadKey,
122 reviewSections.length,
123 selectedSectionIdExists,
124 ]);
125
126 let activeCheckpoint = readyCheckpoints[0] ?? null;
127 if (selectedSection?.kind === "turn") {
128 activeCheckpoint = checkpointBySectionId[selectedSection.id] ?? activeCheckpoint;
129 }
130 const activeSectionId = activeCheckpoint
131 ? getReviewSectionIdForCheckpoint(activeCheckpoint)
132 : null;
133 const activeTurnDiff = useCheckpointDiff({
134 environmentId: enabled ? (environmentId ?? null) : null,
135 threadId: enabled ? (threadId ?? null) : null,
136 fromTurnCount:
137 enabled && activeCheckpoint ? Math.max(0, activeCheckpoint.checkpointTurnCount - 1) : null,
138 toTurnCount: enabled ? (activeCheckpoint?.checkpointTurnCount ?? null) : null,
139 ignoreWhitespace: false,
140 });
141
142 useEffect(() => {
143 if (!reviewCache.threadKey || !activeSectionId) {
144 return;
145 }
146 setReviewTurnDiffLoading(reviewCache.threadKey, activeSectionId, activeTurnDiff.isPending);
147 }, [activeSectionId, activeTurnDiff.isPending, reviewCache.threadKey]);
148
149 useEffect(() => {
150 if (!reviewCache.threadKey || !activeSectionId || !activeTurnDiff.data) {
151 return;
152 }
153 setReviewTurnDiff(reviewCache.threadKey, activeSectionId, activeTurnDiff.data.diff);
154 setReviewAsyncError(reviewCache.threadKey, null);
155 }, [activeSectionId, activeTurnDiff.data, reviewCache.threadKey]);
156
157 useEffect(() => {
158 if (reviewCache.threadKey && activeTurnDiff.error) {
159 setReviewAsyncError(reviewCache.threadKey, activeTurnDiff.error);
160 }
161 }, [activeTurnDiff.error, reviewCache.threadKey]);
162
163 const refreshSelectedSection = useCallback(async () => {
164 if (!enabled) {
165 return;
166 }
167 if (selectedSection?.kind === "turn") {
168 activeTurnDiff.refresh();
169 return;
170 }
171 diffPreview.refresh();
172 }, [activeTurnDiff, diffPreview, enabled, selectedSection?.kind]);
173
174 const selectSection = useCallback(
175 (sectionId: string) => {
176 if (reviewCache.threadKey) {
177 setReviewSelectedSectionId(reviewCache.threadKey, sectionId);
178 }
179 },
180 [reviewCache.threadKey],
181 );
182
183 return {
184 error: diffPreview.error ?? activeTurnDiff.error ?? reviewCache.asyncState.error,
185 isSelectedSectionPending:
186 selectedSection?.kind === "turn" ? activeTurnDiff.isPending : diffPreview.isPending,
187 loadingGitDiffs: diffPreview.isPending,
188 diffPreviewRevision: diffPreview.data
189 ? DateTime.formatIso(diffPreview.data.generatedAt)
190 : undefined,
191 loadingTurnIds,
192 reviewSections,
193 selectedSection,
194 refreshSelectedSection,
195 selectSection,
196 };
197}