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.3 KB
Raw Blame
/**
 * Adapts the pull request changes queries to the Changes pane's
 * `ChangesSource`: wire DTOs become core types here, and a reason GitHub
 * gave no changes becomes a not-ready attempt the pane explains.
 */

import type {
  ChangesSource,
  PatchRead,
} from '@app/features/changes/context/changes-context';
import type { ChangesSummary } from '@app/features/changes/core/changeset';
import {
  decodeChangeset,
  queryStatus,
} from '@app/features/changes/queries/changes-adapter';
import { fireAndForget } from '@core/util/fireAndForget';
import {
  useGithubPullRequestChangesPatchQuery,
  useGithubPullRequestChangesQuery,
} from '@queries/storage/github-pull-request-changes';
import type { GithubPullRequestChangesResponse } from '@service-storage/generated/schemas/githubPullRequestChangesResponse';
import type { Accessor } from 'solid-js';

export function decodePrChanges(
  dto: GithubPullRequestChangesResponse,
  now = new Date().toISOString()
): ChangesSummary {
  if (dto.changeset) {
    return { changeset: decodeChangeset(dto.changeset), capturing: false };
  }
  return {
    attempt: {
      startedAt: now,
      finishedAt: now,
      outcome: 'not_ready',
      error: dto.error ?? undefined,
    },
    capturing: false,
  };
}

/** The changes of the pull request behind the foreign entity record `id`. */
export function createPrChangesSource(
  foreignEntityId: Accessor<string | undefined>
): ChangesSource {
  const summaryQuery = useGithubPullRequestChangesQuery(foreignEntityId);

  // Gate every `data` read on status: an unguarded read suspends the
  // nearest boundary (apps/web/AGENTS.md).
  const summary = () =>
    summaryQuery.isSuccess ? decodePrChanges(summaryQuery.data) : undefined;

  const patch = (
    changesetId: Accessor<string | undefined>,
    enabled: Accessor<boolean>
  ): PatchRead => {
    const query = useGithubPullRequestChangesPatchQuery(foreignEntityId, () =>
      enabled() ? changesetId() : undefined
    );
    return {
      text: () => (query.isSuccess ? query.data.patch : undefined),
      status: () => queryStatus(query),
      retry: () => fireAndForget(query.refetch(), 'pr-changes'),
    };
  };

  return {
    summary,
    summaryStatus: () => queryStatus(summaryQuery),
    patch,
    refresh: async () => {
      await summaryQuery.refetch();
    },
  };
}