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

useReviewSections.ts

190 lines6.1 KB

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