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

useProactivePanels.ts

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