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.

fix(email): use the Soup favorites query in the Favorites view (#7071)

teob82642c4422e
diff --git a/apps/web/src/features/email-view/queries/email-query.test.ts b/apps/web/src/features/email-view/queries/email-query.test.ts
index a6015d025c..5d18618d8b 100644
--- a/apps/web/src/features/email-view/queries/email-query.test.ts
+++ b/apps/web/src/features/email-view/queries/email-query.test.ts
@@ -91,29 +91,32 @@ describe('buildEmailQuery', () => {
);
expect(args.transport).toBeUndefined();
expect(args.body.emailView).toBe('all');
- expect(serialize(args.body.ef)).toContain(
- serialize({ l: { ThreadId: 'starred-a' } })
- );
+ expect(args.body.favorites_only).toBe(true);
+ expect(serialize(args.body)).not.toContain('starred-a');
const input = makeGraphqlSoupInput(args);
expect(input.initial?.emailView).toBe('ALL');
const filter = serialize(input.initial?.filters?.emailFilter);
- expect(filter).toContain(serialize({ literal: { threadId: 'starred-a' } }));
- expect(filter).toContain(serialize({ literal: { threadId: 'starred-b' } }));
+ expect(input.initial?.filters?.favoritesOnly).toBe(true);
expect(filter).toContain(serialize({ literal: { owner: 'inbox-a' } }));
expect(filter).toContain(serialize({ literal: { read: false } }));
expect(filter).not.toContain('inboxVisible');
});
- it('matches no email when favorites are empty or unresolved', () => {
- for (const favoriteThreadIds of [undefined, []]) {
- const args = buildEmailQuery(
- contextFor({ tab: 'favorites', favoriteThreadIds })
- );
- expect(args.body.ef).toEqual({ l: { ThreadId: NIL } });
- expect(makeGraphqlSoupInput(args).initial?.filters?.emailFilter).toEqual({
- tree: { literal: { threadId: NIL } },
- });
+ it('keeps the Soup query identical as favorite membership changes', () => {
+ const args = buildEmailQuery(contextFor({ tab: 'favorites' }));
+ for (const favoriteThreadIds of [[], ['a'], ['a', 'b']]) {
+ expect(
+ buildEmailQuery(contextFor({ tab: 'favorites', favoriteThreadIds }))
+ ).toEqual(args);
}
+ expect(args.body.favorites_only).toBe(true);
+ const admitted = buildEmailQuery(
+ contextFor({ tab: 'favorites', facets: { read: ['unread'] } }),
+ ['a']
+ );
+ expect(admitted.body.favorites_only).toBe(true);
+ expect(serialize(admitted.body.ef)).toContain('a');
+ expect(serialize(admitted.body.ef)).not.toContain('Read');
});
it('keeps a full admission batch below REST and GraphQL ingress recursion limits', () => {
diff --git a/apps/web/src/features/email-view/queries/email-query.ts b/apps/web/src/features/email-view/queries/email-query.ts
index b8716e33cf..f35f0de19c 100644
--- a/apps/web/src/features/email-view/queries/email-query.ts
+++ b/apps/web/src/features/email-view/queries/email-query.ts
@@ -19,7 +19,7 @@ export type EmailQueryContext = {
inboxIds: string[] | undefined;
facets: FacetSelection;
facetContext: EmailFacetContext;
- /** Macro favorites, resolved before listing or searching the Favorites tab. */
+ /** Macro favorite IDs used only by the separate text-search service. */
favoriteThreadIds?: readonly string[];
};
@@ -56,7 +56,6 @@ function threadIdsClause(ids: readonly string[]): TargetExpr {
function tabClause(context: EmailQueryContext): TargetExpr {
return (
match(context.tab)
- .with('favorites', () => threadIdsClause(context.favoriteThreadIds ?? []))
.with('important', () =>
clause.and(
clause.eq('emailImportance', true),
@@ -78,7 +77,7 @@ function tabClause(context: EmailQueryContext): TargetExpr {
.with('shared', () => clause.eq('emailShared', 'only'))
// Sent and Drafts are scoped entirely by `emailView`; the server's sent
// view already covers every linked inbox, so no sender filter is needed.
- .with('drafts', 'scheduled', 'sent', 'all', anyThread)
+ .with('drafts', 'scheduled', 'sent', 'favorites', 'all', anyThread)
.exhaustive()
);
}
@@ -117,6 +116,7 @@ export function buildEmailQuery(
const body: SoupAstBody = {
...mergeAst(base, refinements),
emailView: emailViewForTab(context.tab),
+ ...(context.tab === 'favorites' ? { favorites_only: true } : {}),
};
// Newest activity first: `updated_at` is the thread's latest message time.
diff --git a/apps/web/src/features/email-view/queries/use-email-query.test.ts b/apps/web/src/features/email-view/queries/use-email-query.test.ts
index 73e69bbe4a..e2e7f6f824 100644
--- a/apps/web/src/features/email-view/queries/use-email-query.test.ts
+++ b/apps/web/src/features/email-view/queries/use-email-query.test.ts
@@ -269,44 +269,45 @@ describe('Email list query transitions', () => {
});
afterEach(() => dispose?.());
- it('waits for favorites and uses only their IDs in the Soup query', () => {
- const { source, setState, setEntities, setFavoriteIds } = mount();
+ it('uses a stable Soup filter and renders without a separate favorites response', () => {
+ const { source, setState, setEntities, setFavoriteIds, setFavoritesError } =
+ mount();
setFavoriteIds(undefined);
setState('tab', 'favorites');
- setEntities([email('starred'), email('ordinary')]);
- expect(source.isLoading()).toBe(true);
- expect(ids(source)).toEqual([]);
- expect(vi.mocked(useSoupAstItemsQuery).mock.calls[0][1]?.().enabled).toBe(
- false
- );
- setFavoriteIds(['starred']);
+ setEntities([{ ...email('starred'), isFavorited: true }]);
+ const args = vi.mocked(useSoupAstItemsQuery).mock.calls[0][0];
+ const original = JSON.stringify(args());
expect(source.isLoading()).toBe(false);
expect(ids(source)).toEqual(['starred']);
+ expect(args().body.favorites_only).toBe(true);
+ expect(args().transport).toBeUndefined();
expect(vi.mocked(useSoupAstItemsQuery).mock.calls[0][1]?.().enabled).toBe(
true
);
- expect(
- vi.mocked(useSoupAstItemsQuery).mock.calls[0][0]().transport
- ).toBeUndefined();
- expect(
- JSON.stringify(vi.mocked(useSoupAstItemsQuery).mock.calls[0][0]().body.ef)
- ).toContain('starred');
- expect(favoritesQueryMock).toHaveBeenCalledWith({
- entityType: ['email_thread'],
- });
setFavoriteIds([]);
- expect(ids(source)).toEqual([]);
+ expect(JSON.stringify(args())).toBe(original);
+ expect(ids(source)).toEqual(['starred']);
+ setFavoritesError(new Error('Favorites unavailable'));
+ expect(source.error()).toBeUndefined();
+ expect(ids(source)).toEqual(['starred']);
});
- it('surfaces a favorites failure without an endless loading state', () => {
- const { source, setState, setFavoriteIds, setFavoritesError } = mount();
- setFavoriteIds(undefined);
+ it('removes only the unfavorited row and restores it on rollback without loading', () => {
+ const { source, setState, setEntities } = mount();
setState('tab', 'favorites');
- const error = new Error('Favorites unavailable');
- setFavoritesError(error);
- expect(source.error()).toBe(error);
+ const first = { ...email('first'), isFavorited: true };
+ const second = { ...email('second'), isFavorited: true };
+ setEntities([first, second]);
+ const args = vi.mocked(useSoupAstItemsQuery).mock.calls[0][0];
+ const original = JSON.stringify(args());
+ expect(ids(source)).toEqual(['first', 'second']);
+ setEntities([{ ...first, isFavorited: false }, second]);
+ expect(ids(source)).toEqual(['second']);
+ expect(source.isLoading()).toBe(false);
+ expect(JSON.stringify(args())).toBe(original);
+ setEntities([first, second]);
+ expect(ids(source)).toEqual(['first', 'second']);
expect(source.isLoading()).toBe(false);
- expect(ids(source)).toEqual([]);
});
it('lists the scheduled source on the Scheduled tab instead of soup rows', () => {
diff --git a/apps/web/src/features/email-view/queries/use-email-query.ts b/apps/web/src/features/email-view/queries/use-email-query.ts
index b3cee4ded8..fd91c5720b 100644
--- a/apps/web/src/features/email-view/queries/use-email-query.ts
+++ b/apps/web/src/features/email-view/queries/use-email-query.ts
@@ -121,18 +121,23 @@ export function useEmailDataSource(
inboxIds: state.inboxIds === undefined ? undefined : [...state.inboxIds],
facets: state.facets,
facetContext: facetContext(),
- ...(showsFavorites() ? { favoriteThreadIds: favoriteThreadIds() } : {}),
})
);
// Discovery must apply read status before pagination, not scan through
// pages of read mail to find an unread thread.
const queryArgs = createMemo(() => buildEmailQuery(queryContext()));
- // Resolve tags and favorite membership before either list or search fetches.
+ const tagsReady = () => tagFacetReady(state.facets, options.tagSetsReady());
+ // Soup owns favorite filtering; only the separate search service needs IDs.
+ const searchFavoritesReady = () =>
+ !showsFavorites() || favoriteThreadIds() !== undefined;
const filtersReady = () =>
- tagFacetReady(state.facets, options.tagSetsReady()) &&
- (!showsFavorites() || favoriteThreadIds() !== undefined);
- const sourceEnabled = () => filtersReady() && state.tab !== 'scheduled';
+ tagsReady() && (!state.search.trim() || searchFavoritesReady());
+ const sourceEnabled = () => tagsReady() && state.tab !== 'scheduled';
+ const searchContext = (): EmailQueryContext => ({
+ ...queryContext(),
+ ...(showsFavorites() ? { favoriteThreadIds: favoriteThreadIds() } : {}),
+ });
const query = useSoupAstItemsQuery(queryArgs, () => ({
enabled: sourceEnabled(),
}));
@@ -145,7 +150,11 @@ export function useEmailDataSource(
for (const entity of entities) {
if (!isEmailEntity(entity)) continue;
if (!emailMatchesTab(entity, context.tab, userId())) continue;
- if (showsFavorites() && !favoriteIds().has(entity.id)) continue;
+ if (showsFavorites()) {
+ if (entity.isFavorited === false) continue;
+ // Search results have no Soup favorite field and use search's ID scope.
+ if (search.isSearching() && !favoriteIds().has(entity.id)) continue;
+ }
selected.push(entity);
}
@@ -156,10 +165,10 @@ export function useEmailDataSource(
// every search result comes from the search service.
const search = createSearchState({
text: () => state.search,
- // Wait for the same tag and favorites scope as the list query.
- enabled: sourceEnabled,
+ enabled: () => sourceEnabled() && searchFavoritesReady(),
disableLocalSearch: () => true,
- buildRequest: (request) => buildEmailSearchRequest(queryContext(), request),
+ buildRequest: (request) =>
+ buildEmailSearchRequest(searchContext(), request),
});
const rawEntities = createMemo<EntityData[]>(() => {
@@ -243,7 +252,7 @@ export function useEmailDataSource(
const searchQuery = useSearchSoupQuery(
() =>
buildEmailSearchRequest(
- queryContext(),
+ searchContext(),
{
query: state.search.trim(),
matchType: soupSearchMatchType(state.search),
@@ -306,7 +315,11 @@ export function useEmailDataSource(
const email = current.has(snapshot.id)
? current.get(snapshot.id)
: snapshot;
- return email && matchesOtherFacets(email) ? [email] : [];
+ return email &&
+ selectEmails([email]).length > 0 &&
+ matchesOtherFacets(email)
+ ? [email]
+ : [];
}),
};
},
@@ -353,7 +366,8 @@ export function useEmailDataSource(
});
const isLoading = () => {
- if (showsFavorites() && favorites.isError) return false;
+ if (search.isSearching() && showsFavorites() && favorites.isError)
+ return false;
if (!filtersReady()) return true;
if (!search.isSearching()) {
// A query held back for the tag sets is loading, not empty.
@@ -375,7 +389,9 @@ export function useEmailDataSource(
error: () => {
if (showsScheduled()) return scheduled.error();
return (
- (showsFavorites() ? favorites.error : undefined) ??
+ (search.isSearching() && showsFavorites()
+ ? favorites.error
+ : undefined) ??
(usesServiceSearch() ? search.error() : query.error) ??
retainedQueries()
.map((lookup) => lookup.error())
@@ -398,7 +414,7 @@ export function useEmailDataSource(
await scheduled.refresh();
return;
}
- if (showsFavorites()) await favorites.refetch();
+ if (showsFavorites() && search.isSearching()) await favorites.refetch();
await Promise.all([
usesServiceSearch() ? search.refetch() : query.refresh(),
...retainedQueries().map((lookup) => lookup.refresh()),
diff --git a/docs/AGENT_GUIDE/surfaces.md b/docs/AGENT_GUIDE/surfaces.md
index f0be58fee2..62b81401ae 100644
--- a/docs/AGENT_GUIDE/surfaces.md
+++ b/docs/AGENT_GUIDE/surfaces.md
@@ -313,9 +313,10 @@ connected account. Search is `Ctrl+F` within the surface.
Noise, and archived mail. It respects the selected inboxes and filters; search
within the tab is also restricted to favorites. Removing a star removes the row
from this view. The tab persists across reloads. With `enable-graphql-soup` on,
-favorite membership comes from the filtered GraphQL favorites query, then thread
-IDs scope the paginated GraphQL Soup query. With the flag off, the same scope
-uses the REST queries. An empty favorites list shows `No favorite emails`.
+the paginated GraphQL Soup query uses `favoritesOnly: true`. With the flag off,
+REST Soup uses `favorites_only: true`. Starring changes membership without
+changing the list query. Text search still resolves favorite IDs for the search
+service. An empty favorites list shows `No favorite emails`.
On desktop, a favorited email keeps a filled, muted star just before its
timestamp. Other rows reserve only that small star slot. Hovering reveals