Skip to content

Commit fa7ff8d

Browse files
fix(web): sync sidebar PR state from open panel
Co-authored-by: maria <[email protected]>
1 parent 9d1879b commit fa7ff8d

3 files changed

Lines changed: 134 additions & 1 deletion

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ import { environmentCatalog } from "../connection/catalog";
262262
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
263263
import { useKnownTerminalSessions, useThreadRunningTerminalIds } from "../state/terminalSessions";
264264
import { projectEnvironment } from "../state/projects";
265+
import { linkedPullRequestDetailAtom } from "../state/pullRequests";
265266
import { useEnvironmentQuery } from "../state/query";
266267
import {
267268
environmentServerConfigsAtom,
@@ -317,6 +318,7 @@ import {
317318
} from "./chat/ThreadErrorBanner";
318319
import {
319320
resolveDisplayedThreadPr,
321+
threadPullRequestRefreshSource,
320322
threadChangeRequestSnapshotsAtom,
321323
useLinkedThreadPullRequest,
322324
} from "./ThreadStatusIndicators";
@@ -1745,14 +1747,16 @@ function ChatViewContent(props: ChatViewProps) {
17451747
// the tab is found again whether or not that surface was opened with an environment on it.
17461748
const activePullRequestSurfaceId =
17471749
activeRightPanelSurface?.kind === "pull-request" ? activeRightPanelSurface.id : undefined;
1748-
const handlePullRequestTabStatusChange = useCallback(
1750+
const updatePullRequestTabStatusFromPanel = useCallback(
17491751
(status: PullRequestTabStatus) => {
17501752
const id = activePullRequestSurfaceId;
17511753
if (id === undefined) return;
17521754
setPullRequestTabStatuses((current) => updatePullRequestTabStatus(current, id, status));
17531755
},
17541756
[activePullRequestSurfaceId],
17551757
);
1758+
const refreshVcsStatus = useAtomCommand(vcsEnvironment.refreshStatus, { reportFailure: false });
1759+
const sidebarPrRefreshKeyRef = useRef<string | null>(null);
17561760
const activeFileSurface =
17571761
activeRightPanelSurface?.kind === "file" ? activeRightPanelSurface : null;
17581762
const activePreviewState = useThreadPreviewState(activeThreadRef);
@@ -4594,6 +4598,62 @@ function ChatViewContent(props: ChatViewProps) {
45944598
linkedPullRequest: linkedThreadPullRequest,
45954599
linkedPullRequestStatus,
45964600
});
4601+
const handlePullRequestTabStatusChange = useCallback(
4602+
(status: PullRequestTabStatus) => {
4603+
updatePullRequestTabStatusFromPanel(status);
4604+
const source = threadPullRequestRefreshSource({
4605+
panel: status,
4606+
thread: {
4607+
repository: threadRepository,
4608+
number: linkedThreadPullRequest?.number ?? activeThreadPr?.number ?? null,
4609+
state: activeThreadPr?.state ?? null,
4610+
linked: linkedThreadPullRequest !== null,
4611+
},
4612+
});
4613+
if (source === null) {
4614+
sidebarPrRefreshKeyRef.current = null;
4615+
return;
4616+
}
4617+
const refreshKey = `${source}:${status.repository}#${status.number}:${status.state}`;
4618+
if (sidebarPrRefreshKeyRef.current === refreshKey) return;
4619+
sidebarPrRefreshKeyRef.current = refreshKey;
4620+
4621+
if (source === "linked-detail" && activeThreadRef && linkedThreadPullRequest) {
4622+
appAtomRegistry.refresh(
4623+
linkedPullRequestDetailAtom({
4624+
environmentId: activeThreadRef.environmentId,
4625+
input: {
4626+
projectId: linkedThreadPullRequest.projectId,
4627+
repository: linkedThreadPullRequest.repository,
4628+
number: linkedThreadPullRequest.number,
4629+
},
4630+
}),
4631+
);
4632+
sidebarPrRefreshKeyRef.current = null;
4633+
return;
4634+
}
4635+
if (source === "vcs" && activeThreadRef && gitCwd !== null) {
4636+
void refreshVcsStatus({
4637+
environmentId: activeThreadRef.environmentId,
4638+
input: { cwd: gitCwd },
4639+
}).then(() => {
4640+
if (sidebarPrRefreshKeyRef.current === refreshKey) {
4641+
sidebarPrRefreshKeyRef.current = null;
4642+
}
4643+
});
4644+
}
4645+
},
4646+
[
4647+
activeThreadPr?.number,
4648+
activeThreadPr?.state,
4649+
activeThreadRef,
4650+
gitCwd,
4651+
linkedThreadPullRequest,
4652+
refreshVcsStatus,
4653+
threadRepository,
4654+
updatePullRequestTabStatusFromPanel,
4655+
],
4656+
);
45974657
const activeThreadReferenceCopyTarget = useMemo(
45984658
() =>
45994659
activeThreadId === null || !isServerThread

‎apps/web/src/components/ThreadStatusIndicators.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
resolveDisplayedThreadPrProvider,
1111
resolveThreadPr,
1212
settledPrHoverColorClass,
13+
threadPullRequestRefreshSource,
1314
threadChangeRequestSnapshotsAtom,
1415
type ThreadChangeRequestSnapshot,
1516
} from "./ThreadStatusIndicators";
@@ -56,6 +57,52 @@ function snapshotFor(
5657
return { branch, pr, sourceControlProvider };
5758
}
5859

60+
describe("threadPullRequestRefreshSource", () => {
61+
const panel = { repository: "pingdotgg/t3code", number: 42, state: "merged" as const };
62+
63+
it("refreshes the VCS stream when the open panel is newer than an inferred sidebar PR", () => {
64+
expect(
65+
threadPullRequestRefreshSource({
66+
panel,
67+
thread: { repository: "pingdotgg/t3code", number: 42, state: "open", linked: false },
68+
}),
69+
).toBe("vcs");
70+
});
71+
72+
it("refreshes linked detail when the open panel is newer than a linked sidebar PR", () => {
73+
expect(
74+
threadPullRequestRefreshSource({
75+
panel,
76+
thread: { repository: "pingdotgg/t3code", number: 42, state: "open", linked: true },
77+
}),
78+
).toBe("linked-detail");
79+
});
80+
81+
it("refreshes when the sidebar has not resolved state yet", () => {
82+
expect(
83+
threadPullRequestRefreshSource({
84+
panel,
85+
thread: { repository: "pingdotgg/t3code", number: 42, state: null, linked: false },
86+
}),
87+
).toBe("vcs");
88+
});
89+
90+
it("does nothing once sidebar state matches or the panel shows another PR", () => {
91+
expect(
92+
threadPullRequestRefreshSource({
93+
panel,
94+
thread: { repository: "pingdotgg/t3code", number: 42, state: "merged", linked: false },
95+
}),
96+
).toBeNull();
97+
expect(
98+
threadPullRequestRefreshSource({
99+
panel,
100+
thread: { repository: "pingdotgg/t3code", number: 41, state: "open", linked: false },
101+
}),
102+
).toBeNull();
103+
});
104+
});
105+
59106
describe("resolveThreadPr", () => {
60107
it("keeps local-checkout PR indicators scoped to the stored thread branch", () => {
61108
expect(

‎apps/web/src/components/ThreadStatusIndicators.tsx‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@ export interface TerminalStatusIndicator {
3939

4040
export type ThreadPr = VcsStatusResult["pr"];
4141

42+
export type ThreadPullRequestRefreshSource = "linked-detail" | "vcs";
43+
44+
/** Refresh only when the panel has newer state for this thread's own pull request. */
45+
export function threadPullRequestRefreshSource(input: {
46+
readonly panel: {
47+
readonly repository: string;
48+
readonly number: number;
49+
readonly state: NonNullable<ThreadPr>["state"];
50+
};
51+
readonly thread: {
52+
readonly repository: string | null;
53+
readonly number: number | null;
54+
readonly state: NonNullable<ThreadPr>["state"] | null;
55+
readonly linked: boolean;
56+
};
57+
}): ThreadPullRequestRefreshSource | null {
58+
if (
59+
input.thread.repository !== input.panel.repository ||
60+
input.thread.number !== input.panel.number ||
61+
input.thread.state === input.panel.state
62+
) {
63+
return null;
64+
}
65+
return input.thread.linked ? "linked-detail" : "vcs";
66+
}
67+
4268
export interface LinkedThreadPullRequestStatus {
4369
readonly pr: NonNullable<ThreadPr>;
4470
readonly sourceControlProvider: NonNullable<VcsStatusResult["sourceControlProvider"]>;

0 commit comments

Comments
 (0)