sailfork / t3code-github / apps/ mobile/ src/ state/ use-thread-pr.ts

import { useAtomValue } from "@effect/atom-react";
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
import {
  createLinkedPullRequestSummaryAtomFamily,
  pullRequestDetailToVcsStatus,
} from "@t3tools/client-runtime/state/pull-requests";
import type { EnvironmentId, PullRequestSummary } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import { Atom } from "effect/reactivity";
import { use, useMemo } from "react";

import { connectionAtomRuntime } from "../connection/runtime";
import { appAtomRegistry } from "./atom-registry";
import { serverEnvironment } from "./server";
import { environmentThreadShells } from "./threads";
import { useEnvironmentQuery } from "./query";
import { ThreadPrDemandContext } from "./thread-pr-demand";
import {
  knownPullRequestSummary,
  rememberBounded,
  resolveThreadPrSource,
  presentThreadPr,
  type ThreadPrPresentation,
} from "./thread-pr-presentation";

const MAX_PULL_REQUEST_SNAPSHOTS = 500;

// One bounded cache survives row virtualization (the summary query is dropped once idle) without
// retaining one live atom for every pull request ever seen.
const pullRequestSnapshotsAtom = Atom.make<ReadonlyMap<string, PullRequestSummary>>(new Map()).pipe(
  Atom.keepAlive,
  Atom.withLabel("mobile:thread-pr-snapshots"),
);

function snapshotKey(
  environmentId: EnvironmentId,
  reference: { readonly projectId: string; readonly repository: string; readonly number: number },
): string {
  return JSON.stringify([
    environmentId,
    reference.projectId,
    reference.repository,
    reference.number,
  ]);
}

const pullRequestSummaryAtom = createLinkedPullRequestSummaryAtomFamily(
  connectionAtomRuntime,
  undefined,
  ({ environmentId, input }, summary, registry) =>
    Effect.sync(() =>
      registry.update(pullRequestSnapshotsAtom, (current) =>
        rememberBounded(
          current,
          snapshotKey(environmentId, input),
          summary,
          MAX_PULL_REQUEST_SNAPSHOTS,
        ),
      ),
    ),
);

export { type ThreadPr, type ThreadPrPresentation } from "./thread-pr-presentation";

/** Seeds a pull request page with a summary already in memory, never another pull request's. */
export function withKnownPullRequestSummary<
  Params extends { readonly host?: string; readonly repository: string; readonly number: number },
>(params: Params) {
  const initialSummary =
    params.host === undefined
      ? undefined
      : knownPullRequestSummary(
          { host: params.host, repository: params.repository, number: params.number },
          appAtomRegistry.get(environmentThreadShells.threadShellsAtom),
          appAtomRegistry.get(pullRequestSnapshotsAtom).values(),
        );
  return initialSummary === undefined ? params : { ...params, initialSummary };
}

export function useThreadPr(thread: EnvironmentThreadShell): ThreadPrPresentation | null {
  const demand = use(ThreadPrDemandContext);
  const supportsLinks = useAtomValue(
    serverEnvironment.configValueAtom(thread.environmentId),
    (config) => config !== null,
  );
  const { linkedPresentation, pullRequestRef } = useMemo(
    () =>
      resolveThreadPrSource(
        {
          pullRequests: thread.pullRequests,
          linkedPullRequest: thread.linkedPullRequest,
          branchPullRequest: thread.branchPullRequest,
        },
        supportsLinks,
      ),
    [thread.pullRequests, thread.linkedPullRequest, thread.branchPullRequest, supportsLinks],
  );
  const key = pullRequestRef === null ? null : snapshotKey(thread.environmentId, pullRequestRef);
  // Select this row's entry so answers for other pull requests do not re-render it.
  const snapshot = useAtomValue(pullRequestSnapshotsAtom, (current) =>
    key === null ? undefined : current.get(key),
  );
  const pullRequestSummary = useEnvironmentQuery(
    pullRequestRef === null || !demand
      ? null
      : pullRequestSummaryAtom({
          environmentId: thread.environmentId,
          input: {
            projectId: pullRequestRef.projectId,
            repository: pullRequestRef.repository,
            number: pullRequestRef.number,
          },
        }),
  );
  const summary = pullRequestRef === null ? undefined : (pullRequestSummary.data ?? snapshot);
  // Rows pass `pr` into callbacks; a fresh object per render would invalidate them.
  const summaryPresentation =
    summary === undefined
      ? null
      : presentThreadPr(pullRequestDetailToVcsStatus(summary), {
          kind: summary.provider,
          name: summary.provider,
          baseUrl: "",
        });
  return linkedPresentation ?? summaryPresentation;
}