170 lines · 6.3 KB
import { useState } from "react";
import { mergePull, recheckMergeability, updatePull } from "~/fns/pulls";
import { useAction } from "~/hooks/use-action";
import { StatusList } from "./StatusList";
import { MergeAutomation } from "./MergeAutomation";
import { StackMergeControls } from "./StackMergeControls";
import { Badge } from "~/ui/Badge";
import { Panel } from "~/ui/Panel";
import { StatusDot } from "~/ui/StatusDot";
import { Button } from "~/ui/Button";
import { CheckboxField, SelectField, TextAreaField } from "~/ui/Field";
import { ErrorBanner } from "~/ui/Callout";
import type { usePull } from "~/hooks/use-pull";
import { useViewer } from "~/hooks/use-viewer";

type PullData = ReturnType<typeof usePull>;

const METHODS = [
  { value: "merge", label: "Create a merge commit" },
  { value: "squash", label: "Squash and merge" },
  { value: "rebase", label: "Rebase and merge" },
] as const;

/** The browser's UTC offset as git's `+HHMM`, so rebased commits carry the merger's local timezone. */
function browserTimezone(): string {
  const offset = -new Date().getTimezoneOffset();
  const absolute = Math.abs(offset);
  const hours = String(Math.floor(absolute / 60)).padStart(2, "0");
  const minutes = String(absolute % 60).padStart(2, "0");
  return `${offset < 0 ? "-" : "+"}${hours}${minutes}`;
}

export function MergeBox({ owner, repo, data }: { owner: string; repo: string; data: PullData }) {
  const viewer = useViewer();
  const { pull, gate, checks, checksState } = data;
  const [method, setMethod] = useState<(typeof METHODS)[number]["value"]>("merge");
  const merge = useAction(mergePull);
  const recheck = useAction(recheckMergeability);
  const ready = useAction(updatePull);
  const number = pull.number;