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