Skip to content

Commit 8c9af54

Browse files
fix(web): disable the git actions menu when nothing in it is usable (#31972)
Co-authored-by: Julius Marminge <[email protected]>
1 parent c50dd8e commit 8c9af54

3 files changed

Lines changed: 62 additions & 9 deletions

File tree

‎apps/web/src/components/GitActionsControl.logic.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { resolveAutoFeatureBranchName } from "@t3tools/shared/git";
44
import { assert, describe, it } from "vite-plus/test";
55
import {
66
buildMenuItems,
7+
hasUsableGitMenuContent,
78
requiresDefaultBranchConfirmation,
89
resolveChangesTotals,
910
resolveDefaultBranchActionDialogCopy,
@@ -77,6 +78,29 @@ describe("when: ref is clean, up to date, and has no open PR", () => {
7778
false,
7879
);
7980
});
81+
82+
it("offers the actions menu only when something in it is usable", () => {
83+
const hasContent = (
84+
gitStatus: VcsStatusResult,
85+
overrides: Partial<Parameters<typeof hasUsableGitMenuContent>[0]> = {},
86+
) =>
87+
hasUsableGitMenuContent({
88+
menuItems: buildMenuItems(gitStatus, false, gitStatus.hasPrimaryRemote),
89+
canWriteSourceControl: true,
90+
canPublishRepository: false,
91+
hasNotice: false,
92+
...overrides,
93+
});
94+
const clean = status();
95+
const dirty = status({ hasWorkingTreeChanges: true });
96+
97+
assert.isFalse(hasContent(clean));
98+
assert.isTrue(hasContent(dirty));
99+
assert.isTrue(hasContent(status({ aheadCount: 1 })));
100+
assert.isFalse(hasContent(dirty, { canWriteSourceControl: false }));
101+
assert.isTrue(hasContent(status({ hasPrimaryRemote: false }), { canPublishRepository: true }));
102+
assert.isTrue(hasContent(clean, { hasNotice: true }));
103+
});
80104
});
81105

82106
describe("when: on default ref without open PR", () => {

‎apps/web/src/components/GitActionsControl.logic.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ export function buildMenuItems(
174174
];
175175
}
176176

177+
export function hasUsableGitMenuContent(input: {
178+
readonly menuItems: readonly GitActionMenuItem[];
179+
readonly canWriteSourceControl: boolean;
180+
readonly canPublishRepository: boolean;
181+
readonly hasNotice: boolean;
182+
}): boolean {
183+
return (
184+
input.canPublishRepository ||
185+
input.hasNotice ||
186+
(input.canWriteSourceControl && input.menuItems.some((item) => !item.disabled))
187+
);
188+
}
189+
177190
export function resolveQuickAction(
178191
gitStatus: VcsStatusResult | null,
179192
isBusy: boolean,

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
buildMenuItems,
4747
formatGitActionElapsed,
4848
GIT_ACTION_SUCCESS_VISIBLE_MS,
49+
hasUsableGitMenuContent,
4950
type GitActionProgressPresentation,
5051
type GitActionIconName,
5152
type GitActionMenuItem,
@@ -1395,6 +1396,23 @@ export default function GitActionsControl({
13951396

13961397
const canPublishRepository =
13971398
canWriteSourceControl && isRepo && gitStatusForActions !== null && !hasPrimaryRemote;
1399+
const isDetachedHead = gitStatusForActions?.refName === null;
1400+
const isCleanAndBehind =
1401+
gitStatusForActions !== null &&
1402+
gitStatusForActions.refName !== null &&
1403+
!gitStatusForActions.hasWorkingTreeChanges &&
1404+
gitStatusForActions.behindCount > 0 &&
1405+
gitStatusForActions.aheadCount === 0;
1406+
const hasGitMenuContent = hasUsableGitMenuContent({
1407+
menuItems: gitActionMenuItems,
1408+
canWriteSourceControl,
1409+
canPublishRepository,
1410+
hasNotice: isDetachedHead || isCleanAndBehind || Boolean(gitStatusError),
1411+
});
1412+
1413+
// A disabled menu cannot refresh on open, and terminal edits or commits publish no status.
1414+
const refreshStatusForEmptyMenu = () =>
1415+
requestVcsStatusRefresh(refreshVcsStatus, activeEnvironmentId, gitCwd);
13981416

13991417
const initializeGit = () => {
14001418
void (async () => {
@@ -1466,19 +1484,15 @@ export default function GitActionsControl({
14661484
<MenuItemLabel>Publish repository...</MenuItemLabel>
14671485
</MenuItem>
14681486
) : null}
1469-
{gitStatusForActions?.refName === null && (
1487+
{isDetachedHead && (
14701488
<p className="px-2 py-1.5 text-xs text-warning">
14711489
Detached HEAD: create and check out a branch to enable push and{" "}
14721490
{changeRequestTerminology.singular} actions.
14731491
</p>
14741492
)}
1475-
{gitStatusForActions &&
1476-
gitStatusForActions.refName !== null &&
1477-
!gitStatusForActions.hasWorkingTreeChanges &&
1478-
gitStatusForActions.behindCount > 0 &&
1479-
gitStatusForActions.aheadCount === 0 && (
1480-
<p className="px-2 py-1.5 text-xs text-warning">Behind upstream. Pull/rebase first.</p>
1481-
)}
1493+
{isCleanAndBehind && (
1494+
<p className="px-2 py-1.5 text-xs text-warning">Behind upstream. Pull/rebase first.</p>
1495+
)}
14821496
{gitStatusError && <p className="px-2 py-1.5 text-xs text-destructive">{gitStatusError}</p>}
14831497
</>
14841498
);
@@ -1506,6 +1520,8 @@ export default function GitActionsControl({
15061520
role="group"
15071521
aria-label="Git actions"
15081522
ref={panelAnchorRef}
1523+
onPointerEnter={hasGitMenuContent ? undefined : refreshStatusForEmptyMenu}
1524+
onFocus={hasGitMenuContent ? undefined : refreshStatusForEmptyMenu}
15091525
className={cn(
15101526
"shrink-0",
15111527
THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS,
@@ -1613,7 +1629,7 @@ export default function GitActionsControl({
16131629
panel
16141630
/>
16151631
}
1616-
disabled={isGitActionRunning}
1632+
disabled={isGitActionRunning || !hasGitMenuContent}
16171633
>
16181634
<ChevronDownIcon
16191635
aria-hidden="true"

0 commit comments

Comments
 (0)