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