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.

7.3 KB
Raw Blame
import { analytics } from '@app/lib/analytics';
import { ENABLE_PROJECT_SHARING } from '@core/constant/featureFlags';
import { useUserId } from '@core/context/user';
import { compareDateDesc } from '@core/util/date';
import { fireAndForget } from '@core/util/fireAndForget';
import {
  refetchHistory,
  useUpsertToHistoryMutation,
} from '@queries/history/history';
import { setPreviewOnCreate } from '@queries/preview/preview';
import { refetchSoupEntity } from '@queries/soup/cache';
import { type MutationCallbacks, withCallbacks } from '@queries/utils';
import { storageServiceClient } from '@service-storage/client';
import type { Project } from '@service-storage/generated/schemas/project';
import { keepPreviousData, useMutation, useQuery } from '@tanstack/solid-query';
import { v4 as uuidv4 } from 'uuid';
import { queryClient } from '../client';
import { refreshActiveGraphqlFoldersQueries } from './graphql-folders';
import { storageKeys } from './keys';

const PROJECTS_STALE_TIME = 5 * 60 * 1000;
const PROJECTS_GC_TIME = 10 * 60 * 1000;

type ProjectsQueryResponse = {
  projects: Project[];
  pending: Project[];
};

async function fetchProjects(): Promise<ProjectsQueryResponse> {
  const [projectsResult, pendingResult] = await Promise.all([
    storageServiceClient.projects.getAll(),
    storageServiceClient.projects.getPending(),
  ]);

  const projects = projectsResult.isOk() ? projectsResult.value.data : [];
  const pending = pendingResult.isOk() ? pendingResult.value.data : [];

  return { projects, pending };
}

function projectsQueryOptions() {
  return {
    queryKey: storageKeys.projects.list.queryKey,
    queryFn: fetchProjects,
    staleTime: PROJECTS_STALE_TIME,
    gcTime: PROJECTS_GC_TIME,
  };
}

function sortProjects(projects: Project[]): Project[] {
  return [...projects].sort((a, b) =>
    compareDateDesc(a.createdAt, b.createdAt)
  );
}

function filterByUserId(
  projects: Project[],
  userId: string | undefined
): Project[] {
  if (!userId) return projects;
  return projects.filter((project) => project.userId === userId);
}

// Cached callbacks outlive the caller; only the resolved user id enters them.
function projectsListQueryOptions(userId: string | undefined) {
  return {
    ...projectsQueryOptions(),
    placeholderData: keepPreviousData,
    select: (data: ProjectsQueryResponse): Project[] => {
      const allProjects = [...data.projects, ...data.pending];
      const sorted = sortProjects(allProjects);

      if (!ENABLE_PROJECT_SHARING) {
        return filterByUserId(sorted, userId);
      }

      return sorted;
    },
  };
}

export function useProjectsQuery() {
  const userId = useUserId();

  return useQuery(() => projectsListQueryOptions(userId()));
}

export function invalidateProjects() {
  return Promise.all([
    queryClient.invalidateQueries({
      queryKey: storageKeys.projects.list.queryKey,
    }),
    refreshActiveGraphqlFoldersQueries(),
  ]);
}

/**
 * Standalone function for creating projects that can be used outside of component context.
 * Prefer `useCreateProjectMutation` when inside a component.
 */
export async function createProject(params: {
  name: string;
  parentId?: string;
  sharePermission?: null;
  /** UI surface the creation originated from, for analytics. */
  source?: string;
}): Promise<string | undefined> {
  const result = await storageServiceClient.projects.create({
    name: params.name,
    projectParentId: params.parentId,
    sharePermission: params.sharePermission,
  });

  if (result.isOk()) {
    const projectId = result.value.id;
    analytics.track('create_entity', {
      entityType: 'project',
      entityId: projectId,
      parentId: params.parentId,
      source: params.source,
    });
    setPreviewOnCreate({
      itemId: projectId,
      itemType: 'project',
      name: params.name,
    });
    await storageServiceClient.upsertItemToUserHistory({
      itemId: projectId,
      itemType: 'project',
    });
    // Drive's folder contents are a Soup list, not the projects list: insert
    // the new folder there too so it appears in its parent without a reload.
    fireAndForget(
      refetchSoupEntity(projectId, 'project', {
        ownTouch: true,
        refreshGraphql: true,
      }),
      'project-creation'
    );
    await Promise.all([invalidateProjects(), refetchHistory()]);
    return projectId;
  }

  return undefined;
}

type CreateProjectParams = {
  name: string;
  parentId?: string;
  sharePermission?: null;
};

type CreateProjectContext = {
  previousData: ProjectsQueryResponse | undefined;
  tempProjectId: string;
};

function _useCreateProjectMutation(
  callbacks?: MutationCallbacks<
    string | undefined,
    Error,
    CreateProjectParams,
    CreateProjectContext
  >
) {
  const upsertToHistoryMutation = useUpsertToHistoryMutation();

  return useMutation(() => ({
    mutationFn: async (
      params: CreateProjectParams
    ): Promise<string | undefined> => {
      const result = await storageServiceClient.projects.create({
        name: params.name,
        projectParentId: params.parentId,
        sharePermission: params.sharePermission,
      });

      if (result.isOk()) {
        analytics.track('create_entity', {
          entityType: 'project',
          entityId: result.value.id,
          parentId: params.parentId,
        });
        return result.value.id;
      }
      return undefined;
    },
    ...withCallbacks<
      string | undefined,
      Error,
      CreateProjectParams,
      CreateProjectContext
    >(
      {
        onMutate: async (params) => {
          await queryClient.cancelQueries({
            queryKey: storageKeys.projects.list.queryKey,
          });

          const previousData = queryClient.getQueryData<ProjectsQueryResponse>(
            storageKeys.projects.list.queryKey
          );

          const tempProjectId = uuidv4();
          const tempProject: Project = {
            createdAt: new Date().toISOString(),
            id: tempProjectId,
            name: params.name,
            parentId: params.parentId,
            type: 'project',
            userId: '',
            updatedAt: new Date().toISOString(),
            deletedAt: null,
          };

          queryClient.setQueryData<ProjectsQueryResponse>(
            storageKeys.projects.list.queryKey,
            (old) => {
              if (!old) return { projects: [tempProject], pending: [] };
              return {
                ...old,
                projects: [tempProject, ...old.projects],
              };
            }
          );

          return { previousData, tempProjectId };
        },
        onSuccess: (projectId, params, _context) => {
          if (projectId) {
            setPreviewOnCreate({
              itemId: projectId,
              itemType: 'project',
              name: params.name,
            });
            upsertToHistoryMutation.mutate({
              itemId: projectId,
              itemType: 'project',
            });
          }
        },
        onError: (_err, _params, context) => {
          if (context?.previousData) {
            queryClient.setQueryData(
              storageKeys.projects.list.queryKey,
              context.previousData
            );
          }
        },
        onSettled: () => {
          queryClient.invalidateQueries({
            queryKey: storageKeys.projects.list.queryKey,
          });
          fireAndForget(refetchHistory(), 'project-creation');
        },
      },
      callbacks
    ),
  }));
}