Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/renderer/hooks/usePrWriteActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ export interface UsePrWriteActionsArgs {
onRefresh: () => void;
}

/** Which PR write action is currently in flight, so only its button shows a spinner. */
export type PrWriteAction = "merge" | "close" | "ready" | "update";

export interface UsePrWriteActionsResult {
prLoading: boolean;
pendingAction: PrWriteAction | null;
handleMergePr: (method: "merge" | "squash" | "rebase", admin?: boolean) => Promise<void>;
handleClosePr: () => Promise<void>;
handleMarkPrReady: () => Promise<void>;
Expand All @@ -30,7 +34,8 @@ export interface UsePrWriteActionsResult {
*/
export function usePrWriteActions(args: UsePrWriteActionsArgs): UsePrWriteActionsResult {
const { projectLocation, localSyncLocation, prKey, onRefresh } = args;
const [prLoading, setPrLoading] = useState(false);
const [pendingAction, setPendingAction] = useState<PrWriteAction | null>(null);
const prLoading = pendingAction !== null;

function getCurrentPrData() {
if (!prKey) return null;
Expand All @@ -43,7 +48,7 @@ export function usePrWriteActions(args: UsePrWriteActionsArgs): UsePrWriteAction
): Promise<void> {
const prData = getCurrentPrData();
if (!prData) return;
setPrLoading(true);
setPendingAction("merge");
try {
await readBridge().ghMergePr({
projectLocation,
Expand Down Expand Up @@ -72,14 +77,14 @@ export function usePrWriteActions(args: UsePrWriteActionsArgs): UsePrWriteAction
toast.danger(message);
}
} finally {
setPrLoading(false);
setPendingAction(null);
}
}

async function handleClosePr(): Promise<void> {
const prData = getCurrentPrData();
if (!prData) return;
setPrLoading(true);
setPendingAction("close");
try {
await readBridge().ghClosePr({ projectLocation, prNumber: prData.number });
if (prKey) {
Expand All @@ -89,14 +94,14 @@ export function usePrWriteActions(args: UsePrWriteActionsArgs): UsePrWriteAction
console.error("[git] close PR failed", err);
toast.danger(friendlyError(err));
} finally {
setPrLoading(false);
setPendingAction(null);
}
}

async function handleMarkPrReady(): Promise<void> {
const prData = getCurrentPrData();
if (!prData) return;
setPrLoading(true);
setPendingAction("ready");
try {
await readBridge().ghMarkPrReady({ projectLocation, prNumber: prData.number });
if (prKey) {
Expand All @@ -107,14 +112,14 @@ export function usePrWriteActions(args: UsePrWriteActionsArgs): UsePrWriteAction
console.error("[git] mark PR ready failed", err);
toast.danger(friendlyError(err));
} finally {
setPrLoading(false);
setPendingAction(null);
}
}

async function handleUpdatePrBranch(rebase = false): Promise<void> {
const prData = getCurrentPrData();
if (!prData) return;
setPrLoading(true);
setPendingAction("update");
try {
await readBridge().ghUpdatePrBranch({
projectLocation,
Expand All @@ -132,9 +137,16 @@ export function usePrWriteActions(args: UsePrWriteActionsArgs): UsePrWriteAction
console.error("[git] update PR branch failed", err);
toast.danger(friendlyError(err));
} finally {
setPrLoading(false);
setPendingAction(null);
}
}

return { prLoading, handleMergePr, handleClosePr, handleMarkPrReady, handleUpdatePrBranch };
return {
prLoading,
pendingAction,
handleMergePr,
handleClosePr,
handleMarkPrReady,
handleUpdatePrBranch,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export function GitReviewSidebar(props: {
prTargetBranch,
setPrTargetBranch,
prLoading,
prPendingAction,
isGeneratingPr,
handleCommit,
handleGenerateMessage,
Expand Down Expand Up @@ -463,6 +464,7 @@ export function GitReviewSidebar(props: {
projectId={project.id}
worktreePath={worktreePath}
prLoading={prLoading}
pendingAction={prPendingAction}
handleMergePr={handleMergePr}
handleClosePr={handleClosePr}
handleMarkPrReady={handleMarkPrReady}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from "@heroui/react";
import { readBridge } from "@/renderer/bridge";
import { PixelLoader } from "@/renderer/components/common";
import type { PrWriteAction } from "@/renderer/hooks/usePrWriteActions";
import {
usePrMergeStateStatus,
usePrMergeable,
Expand All @@ -47,6 +48,8 @@ export function PrSection(props: {
projectId: string;
worktreePath?: string | undefined;
prLoading: boolean;
/** Which write action is in flight, so only its button spins (others stay disabled). */
pendingAction?: PrWriteAction | null | undefined;
handleMergePr: (method: "merge" | "squash" | "rebase", admin?: boolean) => Promise<void>;
handleClosePr: () => Promise<void>;
handleMarkPrReady: () => Promise<void>;
Expand All @@ -57,6 +60,7 @@ export function PrSection(props: {
projectId,
worktreePath,
prLoading,
pendingAction,
handleMergePr,
handleClosePr,
handleMarkPrReady,
Expand Down Expand Up @@ -133,7 +137,7 @@ export function PrSection(props: {
variant="tertiary"
className="flex-1"
isDisabled={prLoading}
isPending={prLoading}
isPending={pendingAction === "ready"}
onPress={() => void handleMarkPrReady()}
>
{({ isPending }) => (
Expand Down Expand Up @@ -204,7 +208,7 @@ export function PrSection(props: {
variant="tertiary"
className="flex-1"
isDisabled={prLoading}
isPending={prLoading}
isPending={pendingAction === "update"}
onPress={() => void handleUpdatePrBranch(false)}
>
{({ isPending }) => (
Expand Down Expand Up @@ -248,7 +252,7 @@ export function PrSection(props: {
variant="tertiary"
className="flex-1"
isDisabled={prLoading || (isBlocked && !bypass)}
isPending={prLoading}
isPending={pendingAction === "merge"}
onPress={() => void handleMergePr("squash", bypass)}
>
{({ isPending }) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ export function useGitReviewActions(args: UseGitReviewActionsArgs) {
prTargetBranch,
setPrTargetBranch,
prLoading,
prPendingAction: writeActions.pendingAction,
isGeneratingPr,
handleCommit,
handleGenerateMessage,
Expand Down
19 changes: 13 additions & 6 deletions src/renderer/views/PrReviewOverlay/parts/PrReviewSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ export function PrReviewSidebar(props: {
} = props;
const { isCollapsed, collapse, expand } = useSidebar();
const [expanded, setExpanded] = useState(true);
const { prLoading, handleMergePr, handleClosePr, handleMarkPrReady, handleUpdatePrBranch } =
usePrWriteActions({
projectLocation,
prKey,
onRefresh,
});
const {
prLoading,
pendingAction,
handleMergePr,
handleClosePr,
handleMarkPrReady,
handleUpdatePrBranch,
} = usePrWriteActions({
projectLocation,
prKey,
onRefresh,
});

const sorted = files.toSorted((a, b) =>
compareFilesByDirThenName({ path: a.path }, { path: b.path }),
Expand Down Expand Up @@ -148,6 +154,7 @@ export function PrReviewSidebar(props: {
projectId={projectId}
worktreePath={worktreePath}
prLoading={prLoading}
pendingAction={pendingAction}
handleMergePr={handleMergePr}
handleClosePr={handleClosePr}
handleMarkPrReady={handleMarkPrReady}
Expand Down