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.8 KB
Raw Blame
import { catchToResult, type ResultError } from '@core/util/result';
import { storageServiceClient } from '@service-storage/client';
import type { ProjectPreviewData } from '@service-storage/generated/schemas';
import { useQuery } from '@tanstack/solid-query';
import { err, ok, type Result } from 'neverthrow';
import type { Accessor } from 'solid-js';
import { queryClient } from '../client';
import { projectsKeys } from './keys';

const PROJECT_PREVIEW_STALE_TIME = 5 * 60 * 1000; // 5 minutes
const PROJECT_PREVIEW_GC_TIME = 10 * 60 * 1000; // 10 minutes

/**
 * Fetch project preview data from the storage service
 */
async function fetchProjectPreview(
  projectId: string
): Promise<ProjectPreviewData> {
  const result = await storageServiceClient.projects.getPreview({
    projectIds: [projectId],
  });

  if (result.isErr()) {
    throw new Error(`Failed to fetch project preview`);
  }

  const projectPreview = result.value.previews.find(
    (preview) => preview.id === projectId
  );

  if (!projectPreview) {
    throw new Error(`Project ${projectId} not found in preview response`);
  }

  if (projectPreview.access === 'no_access') {
    throw new Error(`No access to folder ${projectId}`);
  }

  if (projectPreview.access === 'does_not_exist') {
    throw new Error(`Project ${projectId} does not exist`);
  }

  return projectPreview;
}

/**
 * Create query options for a project preview
 */
function projectPreviewQueryOptions(projectId: string) {
  return {
    queryKey: projectsKeys.preview(projectId).queryKey,
    queryFn: () => fetchProjectPreview(projectId),
    staleTime: PROJECT_PREVIEW_STALE_TIME,
    gcTime: PROJECT_PREVIEW_GC_TIME,
    enabled: !!projectId,
  };
}

/**
 * Hook to fetch project preview data
 */
export function useProjectPreviewQuery(projectId: Accessor<string>) {
  return useQuery(() => projectPreviewQueryOptions(projectId()));
}

/**
 * Fetch and cache project preview data
 * Useful for prefetching or ensuring data is available
 */
async function _fetchAndCacheProjectPreview(
  projectId: string
): Promise<Result<{ project: ProjectPreviewData }, ResultError<string>[]>> {
  const result = await catchToResult(async () =>
    queryClient.ensureQueryData(projectPreviewQueryOptions(projectId))
  );

  if (result.isErr()) {
    return err(result.error);
  }

  return ok({ project: result.value });
}

/**
 * Invalidate project preview cache
 */
function _invalidateProjectPreview(projectId: string) {
  return queryClient.invalidateQueries({
    queryKey: projectsKeys.preview(projectId).queryKey,
  });
}

/**
 * Set project preview data directly in cache
 */
function _setProjectPreviewData(projectId: string, data: ProjectPreviewData) {
  return queryClient.setQueryData<ProjectPreviewData>(
    projectsKeys.preview(projectId).queryKey,
    data
  );
}