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

useProactivePanels.ts

208 lines7.5 KB

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 activeSurfaceKind: openSurface?.kind ?? null,
173 })
174 : "ignore";
175 proactivePanelObservationRef.current = {
176 ...proactivePanelObservationRef.current,
177 // Preserve first-entry eligibility while capabilities, checkpoint or repository load.
178 runningTurnId:
179 diffAction === "defer" || shouldDeferLink ? previousRunningTurnId : activeRunningTurnId,
180 };
181 if (diffAction !== "open" || newlyCompletedTurnId === null) return;
182 if (!panels.openProactive(threadRef, { id: "diff", kind: "diff" }, userActionRevision)) {
183 return;
184 }
185 useDiffPanelStore.getState().selectGitScope(threadRef, "branch");
186 onDiffPanelOpen?.();
187 }, [
188 activeLatestRun?.runId,
189 activeLatestRun?.status,
190 activeRunningTurnId,
191 clientSettingsHydrated,
192 hasLinkedPullRequestDetail,
193 isGitRepo,
194 latestRunSettled,
195 linkedThreadPullRequest,
196 onDiffPanelOpen,
197 proactivePanelsEnabledSetting,
198 proactivePullRequestsKey,
199 pullRequestsCapabilityKnown,
200 pullRequestsSurfaceAvailable,
201 supportsPullRequests,
202 threadDetailLoading,
203 threadKey,
204 threadRef,
205 turnDiffSummaries,
206 visiblePullRequestCount,
207 ]);