import * as Dialog from "../../components/ui/Dialog";
import { useAtomRefresh, useAtomSet, useAtomValue } from "@effect/atom-react";
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
import type { PullRequestDetail, PullRequestMergeMethod } from "@t3tools/contracts";
import { AsyncResult } from "effect/reactivity";
import { useRef, useState } from "react";
import { View } from "react-native";
import { AppText as Text } from "../../components/ui/AppText";
import { Button } from "../../components/ui/Button";
import { SplitButton } from "../../components/ui/SplitButton";
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
import { composerPullRequests } from "../../state/pull-requests";
import { useAtomCommand } from "../../state/use-atom-command";
import {
allowedMergeMethods,
canPerformPullRequestAction,
mergeUnavailableReason,
MERGE_METHODS,
PR_ACTION_ICONS,
pullRequestActionLabel,
} from "./pullRequestActions";
import { pullRequestStackMerge, singlePullRequestMergeAllowed } from "./pullRequestStack";
import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
import { usePullRequestAction } from "./usePullRequestAction";
import { usePullRequestPermissions } from "./usePullRequestPermissions";
import { PullRequestGlyph } from "./pullRequestGlyphs";
import type { AppSymbolName } from "../../components/ui/AppSymbol";
import type { PullRequestStackState } from "./usePullRequestStack";
export function PullRequestMergeControls(props: {
readonly pr: PullRequestDetail;
readonly params: PullRequestParams;
readonly stack: PullRequestStackState;
}) {
const { pr, stack } = props;
const terminology = sourceControlClients.get(pr.provider).changeRequest;
const target = pullRequestTarget(props.params);
const permissions = usePullRequestPermissions(props.params, pr);
const stateAction = usePullRequestAction(props.params, pr);
const preferences = useAtomValue(mobilePreferencesAtom);
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
const runAction = useAtomCommand(composerPullRequests.runAction, { reportFailure: false });
const refreshDetail = useAtomRefresh(composerPullRequests.detail(target));
const refreshActivity = useAtomRefresh(composerPullRequests.activity(target));
const [selectedMethod, setSelectedMethod] = useState<PullRequestMergeMethod | null>(null);
const [busy, setBusy] = useState(false);
const pending = useRef(false);
const methods = allowedMergeMethods(pr);
const preferred =
selectedMethod ??
(AsyncResult.isSuccess(preferences) ? preferences.value.pullRequestMergeMethod : undefined) ??
"squash";
const method = methods.includes(preferred)
? preferred
: methods.includes("squash")
? "squash"
: methods[0];
const scope = stack.stack ? pullRequestStackMerge(stack.stack, pr.number, terminology) : null;
const canMergeSingle = singlePullRequestMergeAllowed({
supportsStackActions: stack.supportsStackActions,
hasStack: stack.stack !== null,
isFresh: stack.isFresh,
});
const checkingStack =
permissions.connected && stack.supportsStackActions && !stack.isFresh && !stack.error;
const reason =
(!permissions.connected
? "Reconnect to merge."
: mergeUnavailableReason(pr, permissions.canWrite, terminology)) ??
(stack.stack
? !stack.supportsStackActions
? "This environment can display stacks but cannot merge them."
: !stack.isFresh
? "Refresh the stack before merging."
: scope?.reason
: !canMergeSingle
? stack.error
? "Couldn't check stack membership. Refresh before merging."
: "Checking stack membership…"
: null);
const canMerge = !busy && method !== undefined && !reason;
const label = method
? scope && scope.layers.length > 1
? `${MERGE_METHODS[method].title} stack (${scope.layers.length})`
: MERGE_METHODS[method].title
: "Merge unavailable";
const nextAction =
pr.state === "closed" ? "reopen" : pr.state === "open" && pr.isDraft ? "ready" : null;
// States the button itself names, instead of a reason line under it. Draft and
// closed offer their way forward when the viewer can take it.
const status: {
label: string;
icon: AppSymbolName;
final?: boolean;
action?: "ready" | "reopen";
} | null =
nextAction && stateAction.available(nextAction)
? {
label: pullRequestActionLabel(nextAction, terminology),
icon: PR_ACTION_ICONS[nextAction],
final: nextAction === "reopen",
action: nextAction,
}
: pr.state === "merged"
? { label: "Merged", icon: PullRequestGlyph.merged, final: true }
: pr.state === "closed"
? { label: "Closed", icon: PullRequestGlyph.closed, final: true }
: pr.isDraft
? { label: "Draft", icon: PullRequestGlyph.draft }
: pr.mergeability === "conflicting"
? { label: "Resolve conflicts to merge", icon: PullRequestGlyph.conflicting }
: pr.autoMergeEnabled
? {
label: `Auto-merge pending${pr.autoMergeMethod ? ` (${pr.autoMergeMethod})` : ""}`,
icon: "clock",
}
: checkingStack && !busy
? { label: "Checking stack…", icon: PullRequestGlyph.merged }
: null;
const autoAction = pr.autoMergeEnabled ? "disable-auto-merge" : "enable-auto-merge";
const canAutoMerge =
!busy &&
(autoAction === "disable-auto-merge" || (!stack.stack && canMergeSingle)) &&
permissions.canWrite &&
canPerformPullRequestAction(pr, autoAction);
const perform = async (action: "merge" | "enable-auto-merge" | "disable-auto-merge") => {
if (
pending.current ||
(action !== "disable-auto-merge" && !method) ||
(action === "merge" ? !canMerge : !canAutoMerge)
)
return;
pending.current = true;
setBusy(true);
const response = await runAction({
...target,
input: {
...target.input,
action,
...(action === "merge" && method && stack.stack && scope
? {
stackNumber: stack.stack.number,
expectedStackHeads: scope.heads,
mergeMethod: method,
}
: action === "merge" && method
? {
resolveMergeMethod: (fresh) => {
if (
!canPerformPullRequestAction(fresh, "merge") ||
!allowedMergeMethods(fresh).includes(method)
)
throw new Error(`The ${terminology.singular} changed. Refresh before merging.`);
return method;
},
}
: action === "enable-auto-merge" && method
? { mergeMethod: method }
: {}),
},
});
// A stack can partly merge before the provider reports a failure.
if (!AsyncResult.isSuccess(response)) refreshDetail();
refreshActivity();
stack.refresh();
pending.current = false;
setBusy(false);
if (AsyncResult.isFailure(response)) {
const cause = squashAtomCommandFailure(response);
Dialog.alert(
"Couldn't complete the merge action",
cause instanceof Error ? cause.message : "Try again.",
);
}
};
const confirm = (action: "merge" | "enable-auto-merge" | "disable-auto-merge") => {
const title =
action === "merge"
? scope && scope.layers.length > 1
? `Merge ${scope.layers.length} stack layers?`
: `${pullRequestActionLabel("merge", terminology)}?`
: action === "enable-auto-merge"
? "Enable auto-merge?"
: "Disable auto-merge?";
const message =
action === "merge" && scope
? `Merge ${scope.layers.map((layer) => `#${layer.number}`).join(" → ")} into ${stack.stack?.base} using ${method}. Layers above #${pr.number} stay open.`
: action === "enable-auto-merge"
? `Merge #${pr.number} using ${method} when its requirements are met. This may happen immediately.`
: action === "disable-auto-merge"
? `Stop the provider from automatically merging this ${terminology.singular}.`
: `Merge #${pr.number} into ${pr.baseBranch} using ${method}.`;
Dialog.alert(title, message, [
{ text: "Cancel", style: "cancel" },
{
text: action === "merge" ? label : title.replace("?", ""),
onPress: () => void perform(action),
},
]);
};
return (
<View className="gap-2 border-t border-border-subtle p-3">
<SplitButton
label={
busy || stateAction.busy
? "Working…"
: !permissions.connected
? "Reconnect to merge"
: (status?.label ?? label)
}
accessibilityLabel={
status
? status.label
: scope
? `${label}, ${scope.layers.length} stack layers through #${pr.number}`
: label
}
icon={status?.icon ?? PullRequestGlyph.merged}
disabled={status?.action ? stateAction.busy : !canMerge || status !== null}
loading={busy || stateAction.busy || (!status?.action && checkingStack)}
onPress={() =>
status?.action ? void stateAction.perform(status.action) : confirm("merge")
}
menuTitle="Merge options"
actions={
status?.final
? []
: [
...methods.map((id) => ({
id,
title: MERGE_METHODS[id].title,
state: id === method ? ("on" as const) : ("off" as const),
attributes: { disabled: !canMerge && !canAutoMerge },
})),
...(canAutoMerge
? [
{
id: autoAction,
title: pr.autoMergeEnabled ? "Disable auto-merge" : "Enable auto-merge",
image: "clock",
},
]
: []),
]
}
onPressAction={(id) => {
const selected = methods.find((method) => method === id);
if (selected) {
setSelectedMethod(selected);
savePreferences({ pullRequestMergeMethod: selected });
} else if (id === autoAction && canAutoMerge) confirm(autoAction);
}}
/>
{permissions.connected && !status && reason ? (
<Text className="px-1 text-xs text-foreground-muted">{reason}</Text>
) : null}
{stack.error && stack.supportsStackActions ? (
<Button label="Retry stack lookup" tone="text" onPress={stack.refresh} />
) : null}
</View>
);
}