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.

1.7 KB
Raw Blame
import { QUERY_FILTERS_BASE } from '@app/features/next-soup/filters/query-filters';
import { type CrmCompanyEntity, isCrmCompanyEntity } from '@entity';
import type { useSoupItemsQuery } from '@queries/soup/items';
import { type Accessor, createMemo } from 'solid-js';

const QUICK_ACCESS_COMPANIES_LIMIT = 500;
const STALE_TIME = 5 * 60 * 1000;

/**
 * Quick Access feed of the team's CRM companies, sorted so the user's
 * recently-opened ones bubble to the top (`viewed_updated` =
 * `COALESCE(UserHistory."updatedAt", crm_companies.last_interaction)`).
 *
 * This is the parallel live source feeding the `'crm_company'` bucket
 * — the recently-viewed soup query covers viewed companies with their
 * `viewedAt`, this one widens the pool to all team companies up to a
 * cap so users can `@`-mention companies they've never opened.
 *
 * Every other entity type is filtered out by extending `QUERY_FILTERS_BASE`.
 */
export function useQuickAccessCrmCompaniesQuery(
  useSource: typeof useSoupItemsQuery,
  enabled: Accessor<boolean>
) {
  const query = useSource(
    () => ({
      params: {
        limit: QUICK_ACCESS_COMPANIES_LIMIT,
        sort_method: 'viewed_updated',
      },
      body: {
        ...QUERY_FILTERS_BASE,
        // crm_company_filters intentionally unset = all visible companies
        crm_company_filters: undefined,
      },
    }),
    () => ({ staleTime: STALE_TIME, enabled: enabled() })
  );

  const companies = createMemo<CrmCompanyEntity[]>(() =>
    query.isSuccess
      ? (query.data?.filter(
          (entity): entity is CrmCompanyEntity =>
            isCrmCompanyEntity(entity) && !entity.hidden
        ) ?? [])
      : []
  );

  return { query, companies };
}