TypeScript
refactor(web): move proactive right-panel opening out of ChatView (#20309)
Julius Marminge4283e23392f2
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex b84862b526f..5352615f7bb 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -62,7 +62,6 @@ import {type ServerProvider,type ScopedThreadRef,type ThreadId,- type ThreadLinkedPullRequest,type RunId,type RuntimeRequestId,type KeybindingCommand,@@ -225,7 +224,6 @@ import {} from "../rightPanelLayout";import { PopoverCreateHandle } from "./ui/popover";import {- pullRequestSurface,selectActiveRightPanel,selectActiveRightPanelSurface,selectThreadPanelOpen,@@ -463,6 +461,7 @@ import {} from "./chat/ThreadErrorBanner";import { QueuedRunsControl, type QueuedRunsControlHandle } from "./chat/QueuedRunsControl";import { useThreadLinkedPullRequest } from "./pullRequest/useThreadLinkedPullRequest";+import { useProactivePanels } from "../hooks/useProactivePanels";import type { ComposerBannerStackItem } from "./chat/ComposerBannerStack";import { ThreadStatusLine } from "./chat/ThreadStatusLine";import { formatRelativeTimeLabel, formatRelativeTimeUntilLabel } from "../timestampFormat";@@ -502,9 +501,6 @@ import {isBranchMismatchDismissedForSession,shouldShowBranchMismatchBanner,shouldShowPlanFollowUpPrompt,- shouldOpenProactivePullRequest,- shouldRetargetThreadPullRequestPanel,- shouldOpenProactiveTurnDiff,shouldReleaseTimelineAnchorForToolActivity,shouldRenderPreviewMiniPlayer,getStartedThreadModelChangeBlockReason,@@ -522,8 +518,6 @@ import {resolveComposerInteractionMode,resolveComposerProviderSelection,getAntigravitySendBlockReason,- observeProactivePanelUserChoice,- resolveProactiveTurnDiffAction,resolveThreadMetadataUpdateForNextTurn,resolveSendEnvMode,revokeBlobPreviewUrl,@@ -4270,147 +4264,25 @@ export default function ChatView(props: ChatViewProps) {): linkedThreadPullRequestKey;const activeRunningTurnId = activeRuntime?.activeRunId ?? null;- const proactivePanelObservationRef = useRef<ReturnType<- typeof observeProactivePanelUserChoice- > | null>(null);- const observedThreadPullRequestRef = useRef<{- readonly threadKey: string;- readonly reference: ThreadLinkedPullRequest | null;- } | null>(null);-- useEffect(() => {- if (!isServerThread || activeThreadKey === null || activeThreadRef === null) {- proactivePanelObservationRef.current = null;- observedThreadPullRequestRef.current = null;- return;- }- const panels = useRightPanelStore.getState();- const observation = observeProactivePanelUserChoice(proactivePanelObservationRef.current, {- threadKey: activeThreadKey,- runningTurnId: activeRunningTurnId,- userActionRevision: panels.getUserActionRevision(activeThreadRef),- });- proactivePanelObservationRef.current = observation;- const {- runningTurnId: previousRunningTurnId,- targetKey: previousTargetKey,- userActionRevision,- } = observation;- const openSurface = selectActiveRightPanelSurface(panels.byThreadKey, activeThreadRef);- const previousPullRequest = observedThreadPullRequestRef.current;- observedThreadPullRequestRef.current = {- threadKey: activeThreadKey,- reference: linkedThreadPullRequest,- };- const followSelectedPullRequest =- previousPullRequest?.threadKey === activeThreadKey &&- shouldRetargetThreadPullRequestPanel(- previousPullRequest.reference,- linkedThreadPullRequest,- openSurface,- );- if (followSelectedPullRequest && linkedThreadPullRequest !== null) {- panels.openProactive(- activeThreadRef,- pullRequestSurface(linkedThreadPullRequest),- userActionRevision,- );- }- if (!clientSettingsHydrated) return;-- const proactivePanelsEnabled = settings.proactivePanelsEnabled && !shouldUsePlanSidebarSheet;- const eligibleLink =- proactivePanelsEnabled &&- shouldOpenProactivePullRequest(previousTargetKey, proactivePullRequestsKey);- const shouldDeferLink = eligibleLink && !pullRequestsCapabilityKnown;- proactivePanelObservationRef.current = {- ...observation,- targetKey: shouldDeferLink ? (previousTargetKey ?? null) : proactivePullRequestsKey,- };- if (eligibleLink && pullRequestsCapabilityKnown) {- if (- pullRequestsSurfaceAvailable &&- (visiblePullRequestCount > 1 || !hasLinkedPullRequestDetail || !supportsPullRequests)- ) {- panels.openProactive(- activeThreadRef,- { id: "pull-requests", kind: "pull-requests" },- userActionRevision,- );- } else if (- !followSelectedPullRequest &&- supportsPullRequests &&- linkedThreadPullRequest !== null- ) {- panels.openProactive(- activeThreadRef,- pullRequestSurface(linkedThreadPullRequest),- userActionRevision,- );- }- }- if (threadDetailLoading) return;-- const settledTurnId = latestRunSettled ? (activeLatestRun?.runId ?? null) : null;- const newlyCompletedTurnId = shouldOpenProactiveTurnDiff({- previousRunningTurnId,- runningTurnId: activeRunningTurnId,- settledTurnId,- turnCompleted: activeLatestRun?.status === "completed",- })- ? settledTurnId- : null;- const eligibleCompletion =- proactivePanelsEnabled &&- newlyCompletedTurnId !== null &&- !(- proactivePullRequestsKey !== null &&- (!pullRequestsCapabilityKnown || supportsPullRequests || pullRequestsSurfaceAvailable)- );- const completedCheckpoint = eligibleCompletion- ? turnDiffSummaries.find((checkpoint) => checkpoint.runId === newlyCompletedTurnId)- : undefined;- const diffAction = eligibleCompletion- ? resolveProactiveTurnDiffAction({- checkpoint: completedCheckpoint,- isGitRepo: gitStatusQuery.data?.isRepo,- activeSurfaceKind: openSurface?.kind ?? null,- })- : "ignore";- proactivePanelObservationRef.current = {- ...proactivePanelObservationRef.current,- runningTurnId:- diffAction === "defer" || shouldDeferLink ? previousRunningTurnId : activeRunningTurnId,- };- if (diffAction !== "open" || newlyCompletedTurnId === null) return;- if (!panels.openProactive(activeThreadRef, { id: "diff", kind: "diff" }, userActionRevision)) {- return;- }- useDiffPanelStore.getState().selectGitScope(activeThreadRef, "branch");- onDiffPanelOpen?.();- }, [- turnDiffSummaries,- activeLatestRun?.runId,- activeLatestRun?.status,- activeRunningTurnId,- activeThreadKey,- activeThreadRef,- clientSettingsHydrated,- gitStatusQuery.data?.isRepo,- isServerThread,+ useProactivePanels({+ threadRef: isServerThread ? activeThreadRef : null,+ enabled: settings.proactivePanelsEnabled && !shouldUsePlanSidebarSheet,+ settingsHydrated: clientSettingsHydrated,+ threadDetailLoading,+ runningTurnId: activeRunningTurnId,+ latestRun: activeLatestRun,latestRunSettled,- linkedThreadPullRequest,- proactivePullRequestsKey,+ turnDiffSummaries,+ isGitRepo: gitStatusQuery.data?.isRepo,+ linkedPullRequest: linkedThreadPullRequest,hasLinkedPullRequestDetail,- onDiffPanelOpen,+ pullRequestsKey: proactivePullRequestsKey,pullRequestsCapabilityKnown,+ supportsPullRequests,pullRequestsSurfaceAvailable,visiblePullRequestCount,-- settings.proactivePanelsEnabled,- shouldUsePlanSidebarSheet,- threadDetailLoading,- ]);+ onDiffPanelOpen,+ });const closePreviewPanel = () => {if (activeThreadRef) {if (activeRightPanelSurface?.kind === "preview" && activeRightPanelSurface.resourceId) {diff --git a/apps/web/src/hooks/useProactivePanels.ts b/apps/web/src/hooks/useProactivePanels.tsnew file mode 100644index 00000000000..6f7e64f3fe3--- /dev/null+++ b/apps/web/src/hooks/useProactivePanels.ts@@ -0,0 +1,208 @@+import type { RunId, ScopedThreadRef, ThreadLinkedPullRequest } from "@t3tools/contracts";+import { scopedThreadKey } from "@t3tools/client-runtime/environment";+import { useEffect, useRef } from "react";+import { useDiffPanelStore } from "../diffPanelStore";+import {+ selectActiveRightPanelSurface,+ pullRequestSurface,+ useRightPanelStore,+} from "../rightPanelStore";+import type { TurnDiffSummary } from "../types";+import {+ observeProactivePanelUserChoice,+ resolveProactiveTurnDiffAction,+ shouldOpenProactivePullRequest,+ shouldOpenProactiveTurnDiff,+ shouldRetargetThreadPullRequestPanel,+} from "../components/ChatView.logic";++/**+ * Opens the right panel on its own when there is something new to look at: a+ * change request linked to the thread, or the diff of a turn that just+ * completed. A panel the user opened or closed since wins; see+ * observeProactivePanelUserChoice.+ */+export function useProactivePanels(input: {+ // Null for drafts and while the thread is unavailable.+ readonly threadRef: ScopedThreadRef | null;+ readonly enabled: boolean;+ readonly settingsHydrated: boolean;+ readonly threadDetailLoading: boolean;+ readonly runningTurnId: RunId | null;+ readonly latestRun: { readonly runId: RunId; readonly status: string } | null;+ readonly latestRunSettled: boolean;+ readonly turnDiffSummaries: ReadonlyArray<TurnDiffSummary>;+ readonly isGitRepo: boolean | undefined;+ readonly linkedPullRequest: ThreadLinkedPullRequest | null;+ readonly hasLinkedPullRequestDetail: boolean;+ // Identifies the change requests the thread shows, so a new one can open the panel.+ readonly pullRequestsKey: string | null;+ readonly pullRequestsCapabilityKnown: boolean;+ readonly supportsPullRequests: boolean;+ readonly pullRequestsSurfaceAvailable: boolean;+ readonly visiblePullRequestCount: number;+ readonly onDiffPanelOpen: (() => void) | undefined;+}) {+ const {+ threadRef,+ enabled: proactivePanelsEnabledSetting,+ settingsHydrated: clientSettingsHydrated,+ threadDetailLoading,+ runningTurnId: activeRunningTurnId,+ latestRun: activeLatestRun,+ latestRunSettled,+ turnDiffSummaries,+ isGitRepo,+ linkedPullRequest: linkedThreadPullRequest,+ hasLinkedPullRequestDetail,+ pullRequestsKey: proactivePullRequestsKey,+ pullRequestsCapabilityKnown,+ supportsPullRequests,+ pullRequestsSurfaceAvailable,+ visiblePullRequestCount,+ onDiffPanelOpen,+ } = input;+ const proactivePanelObservationRef = useRef<ReturnType<+ typeof observeProactivePanelUserChoice+ > | null>(null);+ const observedThreadPullRequestRef = useRef<{+ readonly threadKey: string;+ readonly reference: ThreadLinkedPullRequest | null;+ } | null>(null);+ const threadKey = threadRef ? scopedThreadKey(threadRef) : null;++ useEffect(() => {+ if (threadKey === null || threadRef === null) {+ proactivePanelObservationRef.current = null;+ observedThreadPullRequestRef.current = null;+ return;+ }+ const panels = useRightPanelStore.getState();+ const observation = observeProactivePanelUserChoice(proactivePanelObservationRef.current, {+ threadKey: threadKey,+ runningTurnId: activeRunningTurnId,+ userActionRevision: panels.getUserActionRevision(threadRef),+ });+ proactivePanelObservationRef.current = observation;+ const {+ runningTurnId: previousRunningTurnId,+ targetKey: previousTargetKey,+ userActionRevision,+ } = observation;+ const openSurface = selectActiveRightPanelSurface(panels.byThreadKey, threadRef);+ const previousPullRequest = observedThreadPullRequestRef.current;+ observedThreadPullRequestRef.current = {+ threadKey: threadKey,+ reference: linkedThreadPullRequest,+ };+ const followSelectedPullRequest =+ previousPullRequest?.threadKey === threadKey &&+ shouldRetargetThreadPullRequestPanel(+ previousPullRequest.reference,+ linkedThreadPullRequest,+ openSurface,+ );+ // Following the selected linked PR does not open an unrelated panel, so it+ // remains available with proactive panels off. It still respects a later choice.+ if (followSelectedPullRequest && linkedThreadPullRequest !== null) {+ panels.openProactive(+ threadRef,+ pullRequestSurface(linkedThreadPullRequest),+ userActionRevision,+ );+ }+ if (!clientSettingsHydrated) return;++ const proactivePanelsEnabled = proactivePanelsEnabledSetting;+ const eligibleLink =+ proactivePanelsEnabled &&+ shouldOpenProactivePullRequest(previousTargetKey, proactivePullRequestsKey);+ const shouldDeferLink = eligibleLink && !pullRequestsCapabilityKnown;+ proactivePanelObservationRef.current = {+ ...observation,+ targetKey: shouldDeferLink ? (previousTargetKey ?? null) : proactivePullRequestsKey,+ };+ if (eligibleLink && pullRequestsCapabilityKnown) {+ if (+ pullRequestsSurfaceAvailable &&+ (visiblePullRequestCount > 1 || !hasLinkedPullRequestDetail || !supportsPullRequests)+ ) {+ panels.openProactive(+ threadRef,+ { id: "pull-requests", kind: "pull-requests" },+ userActionRevision,+ );+ } else if (+ !followSelectedPullRequest &&+ supportsPullRequests &&+ linkedThreadPullRequest !== null+ ) {+ panels.openProactive(+ threadRef,+ pullRequestSurface(linkedThreadPullRequest),+ userActionRevision,+ );+ }+ }+ if (threadDetailLoading) return;++ const settledTurnId = latestRunSettled ? (activeLatestRun?.runId ?? null) : null;+ const newlyCompletedTurnId = shouldOpenProactiveTurnDiff({+ previousRunningTurnId,+ runningTurnId: activeRunningTurnId,+ settledTurnId,+ turnCompleted: activeLatestRun?.status === "completed",+ })+ ? settledTurnId+ : null;+ const eligibleCompletion =+ proactivePanelsEnabled &&+ newlyCompletedTurnId !== null &&+ !(+ proactivePullRequestsKey !== null &&+ (!pullRequestsCapabilityKnown || supportsPullRequests || pullRequestsSurfaceAvailable)+ );+ const completedCheckpoint = eligibleCompletion+ ? turnDiffSummaries.find((checkpoint) => checkpoint.runId === newlyCompletedTurnId)+ : undefined;+ const diffAction = eligibleCompletion+ ? resolveProactiveTurnDiffAction({+ checkpoint: completedCheckpoint,+ isGitRepo,+ activeSurfaceKind: openSurface?.kind ?? null,+ })+ : "ignore";+ proactivePanelObservationRef.current = {+ ...proactivePanelObservationRef.current,+ // Preserve first-entry eligibility while capabilities, checkpoint or repository load.+ runningTurnId:+ diffAction === "defer" || shouldDeferLink ? previousRunningTurnId : activeRunningTurnId,+ };+ if (diffAction !== "open" || newlyCompletedTurnId === null) return;+ if (!panels.openProactive(threadRef, { id: "diff", kind: "diff" }, userActionRevision)) {+ return;+ }+ useDiffPanelStore.getState().selectGitScope(threadRef, "branch");+ onDiffPanelOpen?.();+ }, [+ activeLatestRun?.runId,+ activeLatestRun?.status,+ activeRunningTurnId,+ clientSettingsHydrated,+ hasLinkedPullRequestDetail,+ isGitRepo,+ latestRunSettled,+ linkedThreadPullRequest,+ onDiffPanelOpen,+ proactivePanelsEnabledSetting,+ proactivePullRequestsKey,+ pullRequestsCapabilityKnown,+ pullRequestsSurfaceAvailable,+ supportsPullRequests,+ threadDetailLoading,+ threadKey,+ threadRef,+ turnDiffSummaries,+ visiblePullRequestCount,+ ]);+}diff --git a/vite.config.ts b/vite.config.tsindex 3fb47a01270..aec8e595ac9 100644--- a/vite.config.ts+++ b/vite.config.ts@@ -332,6 +332,7 @@ export default defineConfig({"apps/web/src/components/chat/useUsageLimitsPanel.ts","apps/web/src/components/pullRequest/useThreadLinkedPullRequest.ts","apps/web/src/components/terminal/**",+ "apps/web/src/hooks/useProactivePanels.ts",],rules: {"react/exhaustive-effect-dependencies": "error",