154 lines · 5.8 KB
import { createFileRoute, redirect, useNavigate } from "@tanstack/react-router";
import { getBranches } from "~/fns/browse";
import { compareRefs } from "~/fns/git-write";
import { getPullTemplate } from "~/fns/pull-review";
import { useViewer } from "~/hooks/use-viewer";
import { useRepo } from "~/hooks/use-repo";
import { CommitList } from "~/components/CommitList";
import { CreatePullForm } from "~/components/CreatePullForm";
import { SignInPrompt } from "~/components/SignInPrompt";
import { DiffView } from "~/components/DiffView";
import { Badge } from "~/ui/Badge";
import { EmptyState } from "~/ui/EmptyState";
import { Panel } from "~/ui/Panel";
import { SelectField } from "~/ui/Field";
import { TextLink } from "~/ui/TextLink";

function parseSplat(splat: string): { base: string | null; head: string | null } {
  if (!splat) return { base: null, head: null };
  if (!splat.includes("...")) return { base: null, head: splat };
  const [base, head] = splat.split("...", 2);
  return { base: base || null, head: head || null };
}

export const Route = createFileRoute("/$owner/$repo/compare/$")({
  loader: async ({ params }) => {
    const ref = { owner: params.owner, repo: params.repo };
    const parsed = parseSplat(params._splat ?? "");
    let base = parsed.base;
    let head = parsed.head;
    if (!base || !head || base === head) {
      const listing = await getBranches({ data: ref });
      base ??= listing.defaultBranch;
      const names = listing.branches.map((branch) => branch.name);
      if (!head) {
        const recent = names.find((name) => name !== base);
        if (recent)
          throw redirect({ to: "/$owner/$repo/compare/$", params: { ...ref, _splat: `${base}...${recent}` } });
      }
      if (!head || head === base) return { kind: "pick" as const, base, head: head ?? base, branches: names };
    }