Open History to see the last commit
1import type { RunId, ScopedThreadRef, ThreadLinkedPullRequest } from "@t3tools/contracts";
2import { scopedThreadKey } from "@t3tools/client-runtime/environment";
3import { useEffect, useRef } from "react";
4import { useDiffPanelStore } from "../diffPanelStore";
5import {
6 selectActiveRightPanelSurface,
7 pullRequestSurface,
8 useRightPanelStore,
9} from "../rightPanelStore";
10import type { TurnDiffSummary } from "../types";
11import {
12 observeProactivePanelUserChoice,
13 resolveProactiveTurnDiffAction,
14 shouldOpenProactivePullRequest,
15 shouldOpenProactiveTurnDiff,
16 shouldRetargetThreadPullRequestPanel,
17} from "../components/ChatView.logic";
18
19/**
20 * Opens the right panel on its own when there is something new to look at: a
21 * change request linked to the thread, or the diff of a turn that just
22 * completed. A panel the user opened or closed since wins; see
23 * observeProactivePanelUserChoice.
24 */
25export function useProactivePanels(input: {
26 // Null for drafts and while the thread is unavailable.
27 readonly threadRef: ScopedThreadRef | null;
28 readonly enabled: boolean;
29 readonly settingsHydrated: boolean;
30 readonly threadDetailLoading: boolean;
31 readonly runningTurnId: RunId | null;
32 readonly latestRun: { readonly runId: RunId; readonly status: string } | null;
33 readonly latestRunSettled: boolean;
34 readonly turnDiffSummaries: ReadonlyArray<TurnDiffSummary>;
35 readonly isGitRepo: boolean | undefined;
36 readonly linkedPullRequest: ThreadLinkedPullRequest | null;
37 readonly hasLinkedPullRequestDetail: boolean;
38 // Identifies the change requests the thread shows, so a new one can open the panel.
39 readonly pullRequestsKey: string | null;
40 readonly pullRequestsCapabilityKnown: boolean;
41 readonly supportsPullRequests: boolean;
42 readonly pullRequestsSurfaceAvailable: boolean;
43 readonly visiblePullRequestCount: number;
44 readonly onDiffPanelOpen: (() => void) | undefined;
45}) {
46 const {
47 threadRef,
48 enabled: proactivePanelsEnabledSetting,
49 settingsHydrated: clientSettingsHydrated,
50 threadDetailLoading,
51 runningTurnId: activeRunningTurnId,
52 latestRun: activeLatestRun,
53 latestRunSettled,
54 turnDiffSummaries,
55 isGitRepo,
56 linkedPullRequest: linkedThreadPullRequest,
57 hasLinkedPullRequestDetail,
58 pullRequestsKey: proactivePullRequestsKey,
59 pullRequestsCapabilityKnown,
60 supportsPullRequests,
61 pullRequestsSurfaceAvailable,
62 visiblePullRequestCount,
63 onDiffPanelOpen,
64 } = input;
65 const proactivePanelObservationRef = useRef<ReturnType<
66 typeof observeProactivePanelUserChoice
67 > | null>(null);
68 const observedThreadPullRequestRef = useRef<{
69 readonly threadKey: string;
70 readonly reference: ThreadLinkedPullRequest | null;
71 } | null>(null);
72 const threadKey = threadRef ? scopedThreadKey(threadRef) : null;
73
74 useEffect(() => {
75 if (threadKey === null || threadRef === null) {
76 proactivePanelObservationRef.current = null;
77 observedThreadPullRequestRef.current = null;
78 return;
79 }
80 const panels = useRightPanelStore.getState();
81 const observation = observeProactivePanelUserChoice(proactivePanelObservationRef.current, {
82 threadKey: threadKey,
83 runningTurnId: activeRunningTurnId,
84 userActionRevision: panels.getUserActionRevision(threadRef),
85 });
86 proactivePanelObservationRef.current = observation;
87 const {
88 runningTurnId: previousRunningTurnId,
89 targetKey: previousTargetKey,
90 userActionRevision,
91 } = observation;
92 const openSurface = selectActiveRightPanelSurface(panels.byThreadKey, threadRef);
93 const previousPullRequest = observedThreadPullRequestRef.current;
94 observedThreadPullRequestRef.current = {
95 threadKey: threadKey,
96 reference: linkedThreadPullRequest,
97 };
98 const followSelectedPullRequest =
99 previousPullRequest?.threadKey === threadKey &&
100 shouldRetargetThreadPullRequestPanel(
101 previousPullRequest.reference,
102 linkedThreadPullRequest,
103 openSurface,
104 );
105 // Following the selected linked PR does not open an unrelated panel, so it
106 // remains available with proactive panels off. It still respects a later choice.
107 if (followSelectedPullRequest && linkedThreadPullRequest !== null) {
108 panels.openProactive(
109 threadRef,
110 pullRequestSurface(linkedThreadPullRequest),
111 userActionRevision,
112 );
113 }
114 if (!clientSettingsHydrated) return;
115
116 const proactivePanelsEnabled = proactivePanelsEnabledSetting;
117 const eligibleLink =
118 proactivePanelsEnabled &&
119 shouldOpenProactivePullRequest(previousTargetKey, proactivePullRequestsKey);
120 const shouldDeferLink = eligibleLink && !pullRequestsCapabilityKnown;
121 proactivePanelObservationRef.current = {
122 ...observation,
123 targetKey: shouldDeferLink ? (previousTargetKey ?? null) : proactivePullRequestsKey,
124 };
125 if (eligibleLink && pullRequestsCapabilityKnown) {
126 if (
127 pullRequestsSurfaceAvailable &&
128 (visiblePullRequestCount > 1 || !hasLinkedPullRequestDetail || !supportsPullRequests)
129 ) {
130 panels.openProactive(
131 threadRef,
132 { id: "pull-requests", kind: "pull-requests" },
133 userActionRevision,
134 );
135 } else if (
136 !followSelectedPullRequest &&
137 supportsPullRequests &&
138 linkedThreadPullRequest !== null
139 ) {
140 panels.openProactive(
141 threadRef,
142 pullRequestSurface(linkedThreadPullRequest),
143 userActionRevision,
144 );
145 }
146 }
147 if (threadDetailLoading) return;
148
149 const settledTurnId = latestRunSettled ? (activeLatestRun?.runId ?? null) : null;
150 const newlyCompletedTurnId = shouldOpenProactiveTurnDiff({
151 previousRunningTurnId,
152 runningTurnId: activeRunningTurnId,
153 settledTurnId,
154 turnCompleted: activeLatestRun?.status === "completed",
155 })
156 ? settledTurnId
157 : null;
158 const eligibleCompletion =
159 proactivePanelsEnabled &&
160 newlyCompletedTurnId !== null &&
161 !(
162 proactivePullRequestsKey !== null &&
163 (!pullRequestsCapabilityKnown || supportsPullRequests || pullRequestsSurfaceAvailable)
164 );
165 const completedCheckpoint = eligibleCompletion
166 ? turnDiffSummaries.find((checkpoint) => checkpoint.runId === newlyCompletedTurnId)
167 : undefined;
168 const diffAction = eligibleCompletion
169 ? resolveProactiveTurnDiffAction({
170 checkpoint: completedCheckpoint,
171 isGitRepo,
172 })
173 : "ignore";
174 proactivePanelObservationRef.current = {
175 ...proactivePanelObservationRef.current,
176 // Preserve first-entry eligibility while capabilities, checkpoint or repository load.
177 runningTurnId:
178 diffAction === "defer" || shouldDeferLink ? previousRunningTurnId : activeRunningTurnId,
179 };
180 if (diffAction !== "open" || newlyCompletedTurnId === null) return;
181 if (
182 !panels.openProactive(
183 threadRef,
184 { kind: "diff", turnId: newlyCompletedTurnId },
185 userActionRevision,
186 )
187 ) {
188 return;
189 }
190 useDiffPanelStore.getState().selectGitScope(threadRef, "branch");
191 onDiffPanelOpen?.();
192 }, [
193 activeLatestRun?.runId,
194 activeLatestRun?.status,
195 activeRunningTurnId,
196 clientSettingsHydrated,
197 hasLinkedPullRequestDetail,
198 isGitRepo,
199 latestRunSettled,
200 linkedThreadPullRequest,
201 onDiffPanelOpen,
202 proactivePanelsEnabledSetting,
203 proactivePullRequestsKey,
204 pullRequestsCapabilityKnown,
205 pullRequestsSurfaceAvailable,
206 supportsPullRequests,
207 threadDetailLoading,
208 threadKey,
209 threadRef,
210 turnDiffSummaries,
211 visiblePullRequestCount,
212 ]);
213}