Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
perf(search): move search provider to use quick access entities since soup is slow (#1694)
gbirman1b588275ab21
diff --git a/js/app/packages/app/component/next-soup/search-context.tsx b/js/app/packages/app/component/next-soup/search-context.tsxindex e08c1d7226..0c07fb45ed 100644--- a/js/app/packages/app/component/next-soup/search-context.tsx+++ b/js/app/packages/app/component/next-soup/search-context.tsx@@ -1,42 +1,13 @@import type { EntityData } from '@entity';-import {- type SoupItemsQueryArgs,- useSoupItemsQuery,-} from '@queries/soup/items';-import { EXCLUDE } from '@app/component/next-soup/filters/filters';import {type Accessor,createContext,- createDeferred,createMemo,type FlowComponent,useContext,} from 'solid-js';-import { throttle } from '@solid-primitives/scheduled';--export const DEFAULT_SEARCH_SORT = 'updated_at';--const CHANNEL_PRELOAD_ARGS: SoupItemsQueryArgs = {- params: { limit: 500, sort_method: DEFAULT_SEARCH_SORT },- body: {- chat_filters: { chat_ids: EXCLUDE },- document_filters: { document_ids: EXCLUDE },- email_filters: { recipients: EXCLUDE },- project_filters: { project_ids: EXCLUDE },- channel_filters: { channel_ids: [] },- },-};--const ITEM_PRELOAD_ARGS: SoupItemsQueryArgs = {- params: { limit: 500, sort_method: DEFAULT_SEARCH_SORT },- body: {- chat_filters: { chat_ids: [] },- document_filters: { document_ids: [] },- email_filters: { recipients: EXCLUDE },- project_filters: { project_ids: [] },- channel_filters: { channel_ids: EXCLUDE },- },-};+import { useQuickAccessEntities } from '@core/component/Properties/component/modal';+import { EntityType } from '@service-properties/generated/schemas/entityType';interface SearchContextValue {entityPool: Accessor<EntityData[]>;@@ -53,42 +24,16 @@ export const useSearchContext = () => {};export const SearchProvider: FlowComponent = (props) => {- const itemsQuery = useSoupItemsQuery(- () => ITEM_PRELOAD_ARGS,- () => ({- staleTime: 'static',- })- );- const itemsFetchNextPage = throttle(() => itemsQuery.fetchNextPage(), 2000);- createDeferred(() => {- if (itemsQuery.hasNextPage && !itemsQuery.isFetchingNextPage) {- itemsFetchNextPage();- }- });+ const { items } = useQuickAccessEntities(() => [+ EntityType.CHANNEL,+ EntityType.CHAT,+ EntityType.DOCUMENT,+ EntityType.PROJECT,+ ]);- const channelItemsQuery = useSoupItemsQuery(- () => CHANNEL_PRELOAD_ARGS,- () => ({- staleTime: 'static',- })- );- const channelItemsFetchNextPage = throttle(- () => channelItemsQuery.fetchNextPage(),- 2000+ const entityPool = createMemo<EntityData[]>(() =>+ items().map((item) => item.data));- createDeferred(() => {- if (- channelItemsQuery.hasNextPage &&- !channelItemsQuery.isFetchingNextPage- ) {- channelItemsFetchNextPage();- }- });-- const entityPool = createMemo<EntityData[]>(() => [- ...(itemsQuery.data ?? []),- ...(channelItemsQuery.data ?? []),- ]);return (<SearchContext.Provider value={{ entityPool }}>diff --git a/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.ts b/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.tsindex 4cdd38d1b3..0bacc4d9e2 100644--- a/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.ts+++ b/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.ts@@ -1,62 +1,77 @@import type { IUser } from '@core/user';-import type { EntityData, EmailEntity } from '@entity';+import type {+ EntityData,+ ChannelEntity,+ ChatEntity,+ DocumentEntity,+ TaskEntity,+ EmailEntity,+ ProjectEntity,+} from '@entity';import type { EntityType } from '@service-properties/generated/schemas/entityType';import type { Accessor } from 'solid-js';import type { FreshSortConfig, TimestampedItem } from '@core/util/freshSort';import {useQuickAccess,type QuickAccessItem,+ type EntityItem,+ type UserItem,type Bucket,} from '@core/context/quickAccess';+import { match } from 'ts-pattern';++export type EntityTypeItemMap = {+ USER: UserItem;+ CHANNEL: EntityItem<ChannelEntity>;+ DOCUMENT: EntityItem<DocumentEntity>;+ PROJECT: EntityItem<ProjectEntity>;+ CHAT: EntityItem<ChatEntity>;+ TASK: EntityItem<TaskEntity>;+ THREAD: EntityItem<EmailEntity>;+ COMPANY: never;+};/*** Maps EntityType to quickAccess buckets*/-export function entityTypeToBuckets(- entityType: EntityType | null | undefined-): Bucket[] | null {- if (!entityType) return null; // null means "all"- switch (entityType) {- case 'USER':- return ['person'];- case 'CHANNEL':- return ['channel', 'dm'];- case 'DOCUMENT':- return ['document', 'note'];- case 'PROJECT':- return ['project'];- case 'CHAT':- return ['chat'];- case 'TASK':- return ['task'];- case 'THREAD':- return ['email']; // Note: emails aren't in quickAccess yet, handled separately- case 'COMPANY':- return []; // Companies aren't in quickAccess- default:- return null;- }+export function entityTypeToBuckets(entityType: EntityType): readonly Bucket[] {+ const buckets = match(entityType)+ .with('USER', () => ['person'] as const)+ .with('CHANNEL', () => ['channel', 'dm'] as const)+ .with('DOCUMENT', () => ['document', 'note'] as const)+ .with('PROJECT', () => ['project'] as const)+ .with('CHAT', () => ['chat'] as const)+ .with('TASK', () => ['task'] as const)+ .with('THREAD', () => ['email'] as const) // Note: emails aren't in quickAccess yet, handled separately+ .with('COMPANY', () => [] as const) // Companies aren't in quickAccess+ .exhaustive();+ return buckets;}/*** Hook to get QuickAccessItems for a given EntityType.* Returns items from the appropriate buckets based on entity type.*/-export function useQuickAccessEntities(- entityType: Accessor<EntityType | null | undefined>-): { items: Accessor<QuickAccessItem[]>; isLoading: Accessor<boolean> } {+export function useQuickAccessEntities<T extends EntityType>(+ entityType: Accessor<T | T[] | null | undefined>+): { items: Accessor<EntityTypeItemMap[T][]>; isLoading: Accessor<boolean> } {const quickAccess = useQuickAccess();- const buckets = () => entityTypeToBuckets(entityType());- const items = (): QuickAccessItem[] => {+ const buckets = () => {+ const entityType_ = entityType();+ if (!entityType_) return null;+ if (!Array.isArray(entityType_)) return entityTypeToBuckets(entityType_);+ return entityType_.flatMap(entityTypeToBuckets);+ };+ const items = (): EntityTypeItemMap[T][] => {const b = buckets();if (b === null) {- return quickAccess.useList()();+ return quickAccess.useList()() as EntityTypeItemMap[T][];}if (b.length === 0) {return [];}- return quickAccess.useList(...b)();+ return quickAccess.useList(...b)() as EntityTypeItemMap[T][];};return { items, isLoading: quickAccess.isLoading };}diff --git a/js/app/packages/core/component/Properties/component/modal/shared/index.ts b/js/app/packages/core/component/Properties/component/modal/shared/index.tsindex b33a6d9fd4..f5a6cf81e1 100644--- a/js/app/packages/core/component/Properties/component/modal/shared/index.ts+++ b/js/app/packages/core/component/Properties/component/modal/shared/index.ts@@ -2,6 +2,7 @@ export type { DropdownOption } from './Dropdown';export { Dropdown } from './Dropdown';export {type CombinedEntity,+ type EntityTypeItemMap,createEntitySearchConfig,entityTypeToBuckets,useQuickAccessEntities,