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(web): stop display-name refetch loop after a failed batch (#7884)

teob720f9c562d8
diff --git a/apps/web/src/lib/core/user/displayName-failure.test.ts b/apps/web/src/lib/core/user/displayName-failure.test.ts
new file mode 100644
index 0000000000..0da0393af9
--- /dev/null
+++ b/apps/web/src/lib/core/user/displayName-failure.test.ts
@@ -0,0 +1,72 @@
+import { err, ok } from 'neverthrow';
+import { createRenderEffect, createRoot } from 'solid-js';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { getDisplayName } from './displayName';
+import type { MacroId } from './macroId';
+
+const mocks = vi.hoisted(() => ({ getUserNamesWithEmail: vi.fn() }));
+vi.mock('@service-auth/client', () => ({
+ authServiceClient: { getUserNamesWithEmail: mocks.getUserNamesWithEmail },
+}));
+
+const unauthorized = err([
+ { code: 'UNAUTHORIZED', message: 'Unauthorized access' },
+]);
+
+/** Renders `getDisplayName(id)` in a tracked scope, like a JSX label. */
+function renderLabel(id: MacroId) {
+ const labels: string[] = [];
+ const dispose = createRoot((dispose) => {
+ createRenderEffect(() => labels.push(getDisplayName(id)));
+ return dispose;
+ });
+ return { labels, dispose };
+}
+
+beforeEach(() => {
+ vi.useFakeTimers();
+ mocks.getUserNamesWithEmail.mockReset();
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+});
+
+describe('getDisplayName after a failed batch', () => {
+ it('does not refetch while the label stays rendered', async () => {
+ const id = 'macro|[email protected]' as MacroId;
+ mocks.getUserNamesWithEmail.mockResolvedValue(unauthorized);
+ const label = renderLabel(id);
+
+ await vi.advanceTimersByTimeAsync(5 * 60 * 1000);
+
+ expect(mocks.getUserNamesWithEmail).toHaveBeenCalledTimes(1);
+ expect(label.labels.at(-1)).toBe('[email protected]');
+ label.dispose();
+ });
+
+ it('retries on a later access once the failure has cooled down', async () => {
+ const id = 'macro|[email protected]' as MacroId;
+ mocks.getUserNamesWithEmail
+ .mockResolvedValueOnce(unauthorized)
+ .mockResolvedValueOnce(
+ ok({ names: [{ id, first_name: 'Ada', last_name: 'Lovelace' }] })
+ );
+ const first = renderLabel(id);
+ await vi.advanceTimersByTimeAsync(1000);
+ first.dispose();
+
+ const tooSoon = renderLabel(id);
+ await vi.advanceTimersByTimeAsync(1000);
+ tooSoon.dispose();
+ expect(mocks.getUserNamesWithEmail).toHaveBeenCalledTimes(1);
+
+ await vi.advanceTimersByTimeAsync(60 * 1000);
+ const later = renderLabel(id);
+ await vi.advanceTimersByTimeAsync(1000);
+
+ expect(mocks.getUserNamesWithEmail).toHaveBeenCalledTimes(2);
+ expect(later.labels.at(-1)).toBe('Ada Lovelace');
+ later.dispose();
+ });
+});
diff --git a/apps/web/src/lib/core/user/displayName.ts b/apps/web/src/lib/core/user/displayName.ts
index f2487a50c0..c0850a3666 100644
--- a/apps/web/src/lib/core/user/displayName.ts
+++ b/apps/web/src/lib/core/user/displayName.ts
@@ -42,6 +42,7 @@ export function getInitialsFromName(
}
const DEFAULT_CACHE_TIME_SECONDS = 60 * 10;
+const FAILED_FETCH_RETRY_SECONDS = 30;
type DisplayNameStore = Record<string, UserNameItem>;
@@ -115,14 +116,15 @@ async function fetchDisplayNames(ids: string[]): Promise<UserNameItem[]> {
});
if (result.isErr()) {
console.error('Failed to fetch user display names');
- // Mark as not-loading with an expired timestamp so the next access
- // retries the fetch instead of getting stuck showing the email
- // fallback forever (see `ensureUserNameItem`'s `cacheExpired` check).
- return ids.map((id) => ({
- _createdAt: new Date(0),
- id,
- loading: false,
- }));
+ // Labels read this store in tracked scopes, so an entry written already
+ // expired re-queues the failed request the moment it lands. Backdate it to
+ // expire after FAILED_FETCH_RETRY_SECONDS so a later access retries
+ // instead of the label sticking on the email fallback.
+ const expiresSoon = new Date(
+ Date.now() -
+ (DEFAULT_CACHE_TIME_SECONDS - FAILED_FETCH_RETRY_SECONDS) * 1000
+ );
+ return ids.map((id) => ({ _createdAt: expiresSoon, id, loading: false }));
}
const data = result.value;