Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.0 KB
Raw Blame
import { throwOnErr } from '@core/util/result';
import { authServiceClient } from '@service-auth/client';
import { useQuery } from '@tanstack/solid-query';
import type { Accessor } from 'solid-js';
import {
  parseGithubPrUrl,
  prDisplayName,
  prHtmlUrl,
  toGithubKey,
} from '../../block-pr/util/prKey';
import { changesKeys } from './keys';

export function pullRequestStatsQueryOptions(
  url: string | undefined,
  captureId?: string
) {
  const reference = url ? parseGithubPrUrl(url) : null;
  const githubKey = reference ? toGithubKey(reference) : '';
  return {
    queryKey: changesKeys.pullRequestStats(githubKey, captureId ?? '').queryKey,
    enabled: reference !== null,
    queryFn: async () => {
      if (!reference) return null;
      // The auth-service proxy calls GitHub with the viewer's linked account.
      const response = await throwOnErr(() =>
        authServiceClient.enrichGithubPullRequests({
          pullRequests: [
            {
              ...reference,
              githubKey,
              displayName: prDisplayName(reference),
              url: prHtmlUrl(reference),
            },
          ],
        })
      );
      const pr = response.pullRequests.find((pr) => pr.githubKey === githubKey);
      if (!pr) return null;
      // A title can be known even when GitHub totals are unavailable.
      return {
        title: pr.name ?? undefined,
        counts:
          pr.additions != null && pr.deletions != null
            ? { additions: pr.additions, deletions: pr.deletions }
            : undefined,
      };
    },
    staleTime: 30_000,
    refetchInterval: 30_000,
    retry: false,
  };
}

/** One guarded source shared by the header and sidebar; never suspends them. */
export function createPullRequestStatsSource(
  url: Accessor<string | undefined>,
  captureId: Accessor<string | undefined>
) {
  const query = useQuery(() =>
    pullRequestStatsQueryOptions(url(), captureId())
  );
  return () => (query.isSuccess ? (query.data ?? undefined) : undefined);
}