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.5 KB
Raw Blame
import { throwOnErr } from '@core/util/result';
import { queryClient } from '@queries/client';
import type { GithubPullRequestWithDetails } from '@queries/storage/github-pull-requests';
import { storageServiceClient } from '@service-storage/client';
import type { StoredGithubPullRequest } from '@service-storage/generated/schemas/storedGithubPullRequest';
import { queryOptions, useQuery } from '@tanstack/solid-query';
import type { Accessor } from 'solid-js';

import type { PrRef } from '../util/prKey';
import { prDisplayName } from '../util/prKey';

const PR_STALE_TIME = 60 * 1000;

export type PrForeignEntityData = {
  id: string;
  prRef: PrRef;
  pullRequest: GithubPullRequestWithDetails;
  /** The branch carrying the pull request's changes, when stored. */
  headBranch?: string;
};

export function prForeignEntityQueryKey(id: string): string[] {
  return ['github-pr', 'foreign-entity', id];
}

/** Refetch one PR detail after it was merged. */
export function invalidatePrForeignEntity(id: string): void {
  queryClient.invalidateQueries({ queryKey: prForeignEntityQueryKey(id) });
}

function prForeignEntityDataFromStored(
  pullRequest: StoredGithubPullRequest
): PrForeignEntityData {
  const prRef: PrRef = {
    owner: pullRequest.owner,
    repo: pullRequest.repo,
    number: pullRequest.number,
  };

  return {
    id: pullRequest.id,
    prRef,
    headBranch: pullRequest.head?.name ?? undefined,
    pullRequest: {
      additions: pullRequest.additions,
      authorLogin: pullRequest.authorLogin,
      description: pullRequest.description,
      checks: pullRequest.checks,
      comments: pullRequest.comments,
      draft: pullRequest.draft,
      deletions: pullRequest.deletions,
      displayName: prDisplayName(prRef),
      foreignEntityId: pullRequest.id,
      githubKey: pullRequest.githubKey,
      labels: pullRequest.labels,
      name: pullRequest.title,
      number: pullRequest.number,
      owner: pullRequest.owner,
      repo: pullRequest.repo,
      status: pullRequest.status,
      url: pullRequest.url,
    },
  };
}

export function prForeignEntityQueryOptions(id: string) {
  return queryOptions({
    queryKey: prForeignEntityQueryKey(id),
    queryFn: async (): Promise<PrForeignEntityData> =>
      prForeignEntityDataFromStored(
        await throwOnErr(() =>
          storageServiceClient.getGithubPullRequest({ id })
        )
      ),
    staleTime: PR_STALE_TIME,
    retry: 1,
  });
}

export function usePrForeignEntityQuery(id: Accessor<string>) {
  return useQuery(() => prForeignEntityQueryOptions(id()));
}