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.7 KB
Raw Blame
import { throwOnErr } from '@core/util/result';
import { queryClient } from '@queries/client';
import { storageServiceClient } from '@service-storage/client';
import type { AccountManagerDecision } from '@service-storage/generated/schemas/accountManagerDecision';
import type { AccountManagerGrant } from '@service-storage/generated/schemas/accountManagerGrant';
import type { AccountManagerRole } from '@service-storage/generated/schemas/accountManagerRole';
import { useMutation, useQuery } from '@tanstack/solid-query';
import type { Accessor } from 'solid-js';
import { accountManagerKeys } from './keys';

function storeGrant(grant: AccountManagerGrant) {
  queryClient.setQueryData(accountManagerKeys.grant(grant.id).queryKey, grant);
  return queryClient.invalidateQueries({
    queryKey: accountManagerKeys.list.queryKey,
  });
}

function accountManagersOptions() {
  return {
    queryKey: accountManagerKeys.list.queryKey,
    queryFn: () => throwOnErr(() => storageServiceClient.listAccountManagers()),
  };
}

export function useAccountManagersQuery() {
  return useQuery(accountManagersOptions);
}

function accountManagerGrantOptions(id: string) {
  return {
    queryKey: accountManagerKeys.grant(id).queryKey,
    queryFn: () =>
      throwOnErr(() => storageServiceClient.getAccountManagerGrant({ id })),
    retry: false,
    // The other account changes this grant, so a cached status can be wrong
    // when a card or the approval link opens.
    staleTime: 0,
  };
}

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

export function useRequestAccountManagerMutation() {
  return useMutation(() => ({
    mutationFn: (vars: {
      counterpartyId: string;
      callerRole: AccountManagerRole;
    }) => throwOnErr(() => storageServiceClient.requestAccountManager(vars)),
    onSuccess: storeGrant,
  }));
}

export function useRespondToAccountManagerRequestMutation() {
  return useMutation(() => ({
    mutationFn: (vars: { id: string; decision: AccountManagerDecision }) =>
      throwOnErr(() =>
        storageServiceClient.respondToAccountManagerRequest(vars)
      ),
    onSuccess: storeGrant,
  }));
}

export function useClaimAccountManagerTokenMutation() {
  return useMutation(() => ({
    // The result holds the secret, so the cache must not keep it.
    gcTime: 0,
    mutationFn: (vars: { id: string }) =>
      throwOnErr(() => storageServiceClient.claimAccountManagerToken(vars)),
    onSuccess: (claimed) => storeGrant(claimed.grant),
  }));
}

export function useRevokeAccountManagerGrantMutation() {
  return useMutation(() => ({
    mutationFn: (vars: { id: string }) =>
      throwOnErr(() => storageServiceClient.revokeAccountManagerGrant(vars)),
    onSuccess: storeGrant,
  }));
}