~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/pds.js

An atproto PDS in JavaScript, for Cloudflare Workers and Node

0
0
main
88 lines · 2.8 KB Raw History
import { useQuery } from '@tanstack/react-query';
import { Skeleton } from '#/components/atoms/skeleton.jsx';
import { ListSkeleton } from '#/components/molecules/list-skeleton.jsx';
import { PageNotice } from '#/components/molecules/page-notice.jsx';
import { Screen } from '#/components/molecules/screen.jsx';
import { diffHref, loadPulls, pullKey } from '#/lib/collab.js';
import { repoRecord } from '#/lib/git.js';
import { CommitPage } from '#/pages/commit.jsx';

const none = [];

/**
 * One pull request's landing page, addressed by what identifies it: the DID of
 * the account whose copy holds the pulls, and the branch. Both survive a
 * rebase, so the address keeps working while the shas under it move.
 *
 * The pair names nothing directly; the pull request is found among the open
 * and merged pulls, which is the same read the list screen makes and shares its
 * cache.
 */
export function PullPage({ repo, did, branch, tab, commit, space }) {
  const record = repoRecord(repo, space);
  const { data: pulls, error } = useQuery({
    queryKey: ['pulls', repo, space ?? ''],
    enabled: Boolean(record),
    queryFn: () => loadPulls(repo, space),
  });

  if (!record) {
    return <PageNotice>No such repository.</PageNotice>;
  }
  if (error) {
    return <PageNotice tone="error">{error.message}</PageNotice>;
  }
  if (!pulls) {
    // The head the pull request opens on, in outline, so the page keeps its
    // shape while the list that names it arrives.
    return (
      <Screen>
        <div className="flex flex-col gap-3 px-(--gutter) pt-4 pb-5">
          <Skeleton className="h-6 w-1/2 max-w-md" />
          <Skeleton className="h-5 w-3/4 max-w-lg" />
        </div>
        <ListSkeleton rows={3} />
      </Screen>
    );
  }

  const id = pullKey(did, branch);
  const pull = [...pulls.open, ...pulls.merged].find(
    (candidate) => candidate.id === id,
  );
  if (!pull) {
    return (
      <PageNotice>
        No branch of that name is in that account's copy. It may have been
        withdrawn, or renamed.
      </PageNotice>
    );
  }

  // One of its own commits, read under the pull request that carries it, so
  // the section a reader is in is the section they came from. It reads as a
  // commit rather than as a proposal: it is one step of the argument above,
  // not the argument.
  if (commit) {
    return (
      <CommitPage
        repo={repo}
        sha={commit}
        from={pull.author.did}
        backTo={{ href: diffHref(repo, pull), title: pull.title }}
      />
    );
  }

  return (
    <CommitPage
      repo={repo}
      sha={pull.base ? `${pull.base}..${pull.sha}` : pull.sha}
      from={pull.author.did}
      pull={pull}
      tab={tab}
      checks={pulls.checks.get(pull.id) ?? none}
      reviews={pulls.reviews.get(pull.id) ?? none}
    />
  );
}