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.

chg(soup/properties): clean up property handling in soup (#1511)

teoc0ea14ade187
diff --git a/js/app/packages/core/component/Properties/hooks/index.ts b/js/app/packages/core/component/Properties/hooks/index.ts
index b808f8c793..b8f18c6f3c 100644
--- a/js/app/packages/core/component/Properties/hooks/index.ts
+++ b/js/app/packages/core/component/Properties/hooks/index.ts
@@ -4,4 +4,3 @@ export { useInlineEditor } from './useInlineEditor';
export { usePropertyEditor } from './usePropertyEditor';
export { usePropertyEntityDisplay } from './usePropertyEntityDisplay';
export { usePropertySelection } from './usePropertySelection';
-export { useTaskProperties } from './useTaskProperties';
diff --git a/js/app/packages/core/component/Properties/hooks/useTaskProperties.ts b/js/app/packages/core/component/Properties/hooks/useTaskProperties.ts
deleted file mode 100644
index b6f8007748..0000000000
--- a/js/app/packages/core/component/Properties/hooks/useTaskProperties.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import type { SubType } from '@entity';
-import { useBulkEntityPropertiesQuery } from '@queries/properties/bulk';
-import { createMemo } from 'solid-js';
-import { SYSTEM_PROPERTY_IDS } from '../constants';
-import type { Property } from '../types';
-
-export type TaskPropertiesStore = Record<string, Property[]>;
-
-interface Entity {
- id: string;
- type: string;
- subType?: SubType;
-}
-
-const TASK_PROPERTY_DEFINITION_IDS = [
- SYSTEM_PROPERTY_IDS.ASSIGNEES,
- SYSTEM_PROPERTY_IDS.STATUS,
- SYSTEM_PROPERTY_IDS.PRIORITY,
-];
-
-function isTaskEntity(
- entity: Entity
-): entity is Entity & { type: 'document'; subType: { type: 'task' } } {
- return entity.type === 'document' && entity.subType?.type === 'task';
-}
-
-export function useTaskProperties(
- entities: () => Entity[] | undefined
-): () => TaskPropertiesStore {
- const taskEntityIds = createMemo(() => {
- const allEntities = entities() ?? [];
- return allEntities.filter(isTaskEntity).map((e) => e.id);
- });
-
- const query = useBulkEntityPropertiesQuery(
- 'TASK',
- taskEntityIds,
- TASK_PROPERTY_DEFINITION_IDS
- );
-
- return () => {
- if (query.isLoading || !query.data) return {};
- return query.data;
- };
-}
diff --git a/js/app/packages/queries/properties/bulk.ts b/js/app/packages/queries/properties/bulk.ts
deleted file mode 100644
index 6a507ba524..0000000000
--- a/js/app/packages/queries/properties/bulk.ts
+++ /dev/null
@@ -1,72 +0,0 @@
-import { useQuery } from '@tanstack/solid-query';
-import type { Accessor } from 'solid-js';
-import { entityPropertyFromApi } from '../../core/component/Properties/api/converters';
-import type { Property } from '../../core/component/Properties/types';
-import { isErr } from '../../core/util/maybeResult';
-import { propertiesServiceClient } from '../../service-clients/service-properties/client';
-import type { EntityType } from '../../service-clients/service-properties/generated/schemas/entityType';
-import { queryClient } from '../client';
-import { propertiesKeys } from './keys';
-
-export type BulkEntityPropertiesData = Record<string, Property[]>;
-
-/**
- * Fetches properties for multiple entities of the same type.
- * No caching partitioning - just a simple bulk fetch.
- */
-async function fetchBulkProperties(
- entityType: EntityType,
- entityIds: readonly string[],
- propertyDefinitionIds: readonly string[]
-): Promise<BulkEntityPropertiesData> {
- if (entityIds.length === 0) return {};
-
- const entities = entityIds.map((id) => ({
- entity_type: entityType,
- entity_id: id,
- }));
-
- const result = await propertiesServiceClient.getBulkEntityProperties({
- body: {
- entities,
- property_ids: [...propertyDefinitionIds],
- },
- });
-
- if (isErr(result)) {
- throw new Error('Failed to fetch entity properties', { cause: result[0] });
- }
-
- const [, data] = result;
- const out: BulkEntityPropertiesData = {};
-
- for (const entityId of entityIds) {
- const response = data[entityId];
- out[entityId] = response?.properties.map(entityPropertyFromApi) ?? [];
- }
-
- return out;
-}
-
-/**
- * Hook for fetching properties for multiple entities.
- * Simple query without complex caching logic.
- */
-export function useBulkEntityPropertiesQuery(
- entityType: EntityType,
- entityIds: Accessor<readonly string[]>,
- propertyDefinitionIds: readonly string[]
-) {
- return useQuery(
- () => ({
- queryKey: propertiesKeys.bulk({
- entityType,
- entityIds: entityIds(),
- }).queryKey,
- queryFn: () =>
- fetchBulkProperties(entityType, entityIds(), propertyDefinitionIds),
- enabled: entityIds().length > 0,
- }),
- () => queryClient
- );
-}
diff --git a/js/app/packages/queries/properties/entity.ts b/js/app/packages/queries/properties/entity.ts
index f973b4a9e2..2dd6a6f4a0 100644
--- a/js/app/packages/queries/properties/entity.ts
+++ b/js/app/packages/queries/properties/entity.ts
@@ -18,18 +18,20 @@ import {
type PropertiesEntityType,
propertiesServiceClient,
} from '../../service-clients/service-properties/client';
-import type { EntityType } from '../../service-clients/service-properties/generated/schemas/entityType';
+import { EntityType } from '../../service-clients/service-properties/generated/schemas/entityType';
import type { SoupPropertyValue } from '../../service-clients/service-storage/generated/schemas/soupPropertyValue';
import { queryClient } from '../client';
import { type MutationCallbacks, withCallbacks } from '../utils';
import { propertiesKeys } from './keys';
-import type { BulkEntityPropertiesData } from './bulk';
import {
getSoupEntityById,
optimisticUpdateSoupEntity,
invalidateSoupEntity,
type SoupTransaction,
+ refetchSoupEntity,
+ type SoupEntityTag,
} from '../soup/cache';
+import { match, P } from 'ts-pattern';
export function useEntityPropertiesQuery(
entityType: Accessor<EntityType>,
@@ -63,16 +65,6 @@ export function useEntityPropertiesQuery(
);
}
-function bulkIncludesEntityPredicate(queryKey: QueryKey, entityId: string) {
- return (
- queryKey.includes('properties') &&
- queryKey.includes('bulk') &&
- queryKey.some(
- (subKey) => Array.isArray(subKey) && subKey.includes(entityId)
- )
- );
-}
-
export function invalidatePropertiesForEntity(
entityType: EntityType,
entityId: string
@@ -80,12 +72,6 @@ export function invalidatePropertiesForEntity(
queryClient.invalidateQueries({
queryKey: propertiesKeys.entity({ entityType, entityId }).queryKey,
});
-
- // This invalidates any bulk query including this entity
- queryClient.invalidateQueries({
- predicate: ({ queryKey }) =>
- bulkIncludesEntityPredicate(queryKey, entityId),
- });
}
export type SaveEntityPropertyParams = {
@@ -296,7 +282,6 @@ export type SetPropertyStatusCompleteParams = {
type SetPropertyStatusCompleteContext = {
previousEntityProperties: [QueryKey, Property[] | undefined][];
- previousBulkProperties: [QueryKey, BulkEntityPropertiesData | undefined][];
soupTxn?: SoupTransaction;
};
@@ -336,6 +321,33 @@ function updateStatusPropertyToCompleted<
});
}
+function propertyEntityTypeToSoupTag(
+ entityType: EntityType
+): SoupEntityTag | null {
+ return match(entityType)
+ .with(EntityType.CHANNEL, () => 'channel' as const)
+ .with(EntityType.THREAD, () => 'emailThread' as const)
+ .with(EntityType.CHAT, () => 'chat' as const)
+ .with(P.union(EntityType.COMPANY, EntityType.USER), () => null)
+ .with(
+ P.union(EntityType.DOCUMENT, EntityType.TASK),
+ () => 'document' as const
+ )
+ .with(EntityType.PROJECT, () => 'project' as const)
+
+ .exhaustive();
+}
+
+function withValidSoupTag(
+ entityType: EntityType,
+ callback: (tag: SoupEntityTag) => void
+) {
+ const tag = propertyEntityTypeToSoupTag(entityType);
+ if (tag) {
+ callback(tag);
+ }
+}
+
/** Sets the status property to complete for an entity (mark as done) */
export function useSetPropertyStatusCompleteMutation(
callbacks?: MutationCallbacks<
@@ -366,10 +378,6 @@ export function useSetPropertyStatusCompleteMutation(
entityId: vars.entityId,
}).queryKey,
}),
- queryClient.cancelQueries({
- predicate: ({ queryKey }) =>
- bulkIncludesEntityPredicate(queryKey, vars.entityId),
- }),
]);
// Snapshot previous property data for rollback
@@ -380,12 +388,6 @@ export function useSetPropertyStatusCompleteMutation(
}).queryKey,
});
- const previousBulkProperties =
- queryClient.getQueriesData<BulkEntityPropertiesData>({
- predicate: ({ queryKey }) =>
- bulkIncludesEntityPredicate(queryKey, vars.entityId),
- });
-
// Optimistically update entity properties query
queryClient.setQueriesData<Property[]>(
{
@@ -397,23 +399,6 @@ export function useSetPropertyStatusCompleteMutation(
(old) => (old ? updateStatusPropertyToCompleted(old) : old)
);
- // Optimistically update bulk properties queries
- queryClient.setQueriesData<BulkEntityPropertiesData>(
- {
- predicate: ({ queryKey }) =>
- bulkIncludesEntityPredicate(queryKey, vars.entityId),
- },
- (old) => {
- if (!old || !old[vars.entityId]) return old;
- return {
- ...old,
- [vars.entityId]: updateStatusPropertyToCompleted(
- old[vars.entityId]
- ),
- };
- }
- );
-
// Optimistically update soup queries (embedded properties on entities)
const current = getSoupEntityById(vars.entityId);
@@ -433,7 +418,6 @@ export function useSetPropertyStatusCompleteMutation(
return {
previousEntityProperties,
- previousBulkProperties,
soupTxn,
};
},
@@ -449,14 +433,13 @@ export function useSetPropertyStatusCompleteMutation(
for (const [key, data] of context.previousEntityProperties) {
queryClient.setQueryData(key, data);
}
- for (const [key, data] of context.previousBulkProperties) {
- queryClient.setQueryData(key, data);
- }
}
},
onSettled: (_data, _error, variables) => {
invalidatePropertiesForEntity(variables.entityType, variables.entityId);
- invalidateSoupEntity(variables.entityId);
+ withValidSoupTag(variables.entityType, (tag) =>
+ refetchSoupEntity(variables.entityId, tag)
+ );
},
...(callbacks
? withCallbacks<
@@ -530,7 +513,9 @@ export function useBulkSaveEntityPropertiesMutation(
entityGroups.forEach((entityIds, entityType) => {
entityIds.forEach((entityId) => {
invalidatePropertiesForEntity(entityType, entityId);
- invalidateSoupEntity(entityId);
+ withValidSoupTag(entityType, (tag) =>
+ refetchSoupEntity(entityId, tag)
+ );
});
});
},
diff --git a/js/app/packages/queries/properties/keys.ts b/js/app/packages/queries/properties/keys.ts
index c5c478ce50..8088294208 100644
--- a/js/app/packages/queries/properties/keys.ts
+++ b/js/app/packages/queries/properties/keys.ts
@@ -7,9 +7,6 @@ export const propertiesKeys = createQueryKeys('properties', {
entity: (params: { entityType: EntityType; entityId: string }) => ({
queryKey: ['entity', params.entityType, params.entityId],
}),
- bulk: (params: { entityType: EntityType; entityIds: readonly string[] }) => ({
- queryKey: ['bulk', params.entityType, [...params.entityIds].sort()],
- }),
options: (params: { propertyDefinitionId: string }) => ({
queryKey: ['options', params.propertyDefinitionId],
}),