274 lines · 8.4 KB · TypeScript
RawDownload
import {  type GitActionProgressEvent,  type GitStackedAction,  type ThreadId,} from "@t3tools/contracts";import {  infiniteQueryOptions,  mutationOptions,  queryOptions,  type QueryClient,} from "@tanstack/react-query";import { ensureNativeApi } from "../nativeApi";import { getWsRpcClient } from "../wsRpcClient"; const GIT_STATUS_STALE_TIME_MS = 5_000;const GIT_STATUS_REFETCH_INTERVAL_MS = 15_000;const GIT_BRANCHES_STALE_TIME_MS = 15_000;const GIT_BRANCHES_REFETCH_INTERVAL_MS = 60_000;const GIT_BRANCHES_PAGE_SIZE = 100; export const gitQueryKeys = {  all: ["git"] as const,  status: (cwd: string | null) => ["git", "status", cwd] as const,  branches: (cwd: string | null) => ["git", "branches", cwd] as const,  branchSearch: (cwd: string | null, query: string) =>    ["git", "branches", cwd, "search", query] as const,}; export const gitMutationKeys = {  init: (cwd: string | null) => ["git", "mutation", "init", cwd] as const,  checkout: (cwd: string | null) => ["git", "mutation", "checkout", cwd] as const,  runStackedAction: (cwd: string | null) => ["git", "mutation", "run-stacked-action", cwd] as const,  pull: (cwd: string | null) => ["git", "mutation", "pull", cwd] as const,  preparePullRequestThread: (cwd: string | null) =>    ["git", "mutation", "prepare-pull-request-thread", cwd] as const,}; export function invalidateGitQueries(queryClient: QueryClient, input?: { cwd?: string | null }) {  const cwd = input?.cwd ?? null;  if (cwd !== null) {    return Promise.all([      queryClient.invalidateQueries({ queryKey: gitQueryKeys.status(cwd) }),      queryClient.invalidateQueries({ queryKey: gitQueryKeys.branches(cwd) }),    ]);  }   return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });} export function invalidateGitStatusQuery(queryClient: QueryClient, cwd: string | null) {  if (cwd === null) {    return Promise.resolve();  }   return queryClient.invalidateQueries({ queryKey: gitQueryKeys.status(cwd) });} export function gitStatusQueryOptions(cwd: string | null) {  return queryOptions({    queryKey: gitQueryKeys.status(cwd),    queryFn: async () => {      const api = ensureNativeApi();      if (!cwd) throw new Error("Git status is unavailable.");      return api.git.status({ cwd });    },    enabled: cwd !== null,    staleTime: GIT_STATUS_STALE_TIME_MS,    refetchOnWindowFocus: "always",    refetchOnReconnect: "always",    refetchInterval: GIT_STATUS_REFETCH_INTERVAL_MS,  });} export function gitBranchSearchInfiniteQueryOptions(input: {  cwd: string | null;  query: string;  enabled?: boolean;}) {  const normalizedQuery = input.query.trim();   return infiniteQueryOptions({    queryKey: gitQueryKeys.branchSearch(input.cwd, normalizedQuery),    initialPageParam: 0,    queryFn: async ({ pageParam }) => {      const api = ensureNativeApi();      if (!input.cwd) throw new Error("Git branches are unavailable.");      return api.git.listBranches({        cwd: input.cwd,        ...(normalizedQuery.length > 0 ? { query: normalizedQuery } : {}),        cursor: pageParam,        limit: GIT_BRANCHES_PAGE_SIZE,      });    },    getNextPageParam: (lastPage) => lastPage.nextCursor ?? undefined,    enabled: input.cwd !== null && (input.enabled ?? true),    staleTime: GIT_BRANCHES_STALE_TIME_MS,    refetchOnWindowFocus: true,    refetchOnReconnect: true,    refetchInterval: GIT_BRANCHES_REFETCH_INTERVAL_MS,  });} export function gitResolvePullRequestQueryOptions(input: {  cwd: string | null;  reference: string | null;}) {  return queryOptions({    queryKey: ["git", "pull-request", input.cwd, input.reference] as const,    queryFn: async () => {      const api = ensureNativeApi();      if (!input.cwd || !input.reference) {        throw new Error("Pull request lookup is unavailable.");      }      return api.git.resolvePullRequest({ cwd: input.cwd, reference: input.reference });    },    enabled: input.cwd !== null && input.reference !== null,    staleTime: 30_000,    refetchOnWindowFocus: false,    refetchOnReconnect: false,  });} export function gitInitMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {  return mutationOptions({    mutationKey: gitMutationKeys.init(input.cwd),    mutationFn: async () => {      const api = ensureNativeApi();      if (!input.cwd) throw new Error("Git init is unavailable.");      return api.git.init({ cwd: input.cwd });    },    onSuccess: async () => {      await invalidateGitQueries(input.queryClient);    },  });} export function gitCheckoutMutationOptions(input: {  cwd: string | null;  queryClient: QueryClient;}) {  return mutationOptions({    mutationKey: gitMutationKeys.checkout(input.cwd),    mutationFn: async (branch: string) => {      const api = ensureNativeApi();      if (!input.cwd) throw new Error("Git checkout is unavailable.");      return api.git.checkout({ cwd: input.cwd, branch });    },    onSuccess: async () => {      await invalidateGitQueries(input.queryClient);    },  });} export function gitRunStackedActionMutationOptions(input: {  cwd: string | null;  queryClient: QueryClient;}) {  return mutationOptions({    mutationKey: gitMutationKeys.runStackedAction(input.cwd),    mutationFn: async ({      actionId,      action,      commitMessage,      featureBranch,      filePaths,      onProgress,    }: {      actionId: string;      action: GitStackedAction;      commitMessage?: string;      featureBranch?: boolean;      filePaths?: string[];      onProgress?: (event: GitActionProgressEvent) => void;    }) => {      if (!input.cwd) throw new Error("Git action is unavailable.");      return getWsRpcClient().git.runStackedAction(        {          actionId,          cwd: input.cwd,          action,          ...(commitMessage ? { commitMessage } : {}),          ...(featureBranch ? { featureBranch } : {}),          ...(filePaths ? { filePaths } : {}),        },        ...(onProgress ? [{ onProgress }] : []),      );    },    onSettled: async () => {      await invalidateGitQueries(input.queryClient);    },  });} export function gitPullMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {  return mutationOptions({    mutationKey: gitMutationKeys.pull(input.cwd),    mutationFn: async () => {      const api = ensureNativeApi();      if (!input.cwd) throw new Error("Git pull is unavailable.");      return api.git.pull({ cwd: input.cwd });    },    onSettled: async () => {      await invalidateGitQueries(input.queryClient);    },  });} export function gitCreateWorktreeMutationOptions(input: { queryClient: QueryClient }) {  return mutationOptions({    mutationFn: async ({      cwd,      branch,      newBranch,      path,    }: {      cwd: string;      branch: string;      newBranch: string;      path?: string | null;    }) => {      const api = ensureNativeApi();      if (!cwd) throw new Error("Git worktree creation is unavailable.");      return api.git.createWorktree({ cwd, branch, newBranch, path: path ?? null });    },    mutationKey: ["git", "mutation", "create-worktree"] as const,    onSettled: async () => {      await invalidateGitQueries(input.queryClient);    },  });} export function gitRemoveWorktreeMutationOptions(input: { queryClient: QueryClient }) {  return mutationOptions({    mutationFn: async ({ cwd, path, force }: { cwd: string; path: string; force?: boolean }) => {      const api = ensureNativeApi();      if (!cwd) throw new Error("Git worktree removal is unavailable.");      return api.git.removeWorktree({ cwd, path, force });    },    mutationKey: ["git", "mutation", "remove-worktree"] as const,    onSettled: async () => {      await invalidateGitQueries(input.queryClient);    },  });} export function gitPreparePullRequestThreadMutationOptions(input: {  cwd: string | null;  queryClient: QueryClient;}) {  return mutationOptions({    mutationFn: async ({      reference,      mode,      threadId,    }: {      reference: string;      mode: "local" | "worktree";      threadId?: ThreadId;    }) => {      const api = ensureNativeApi();      if (!input.cwd) throw new Error("Pull request thread preparation is unavailable.");      return api.git.preparePullRequestThread({        cwd: input.cwd,        reference,        mode,        ...(threadId ? { threadId } : {}),      });    },    mutationKey: gitMutationKeys.preparePullRequestThread(input.cwd),    onSettled: async () => {      await invalidateGitQueries(input.queryClient);    },  });}