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(notifications): centralize entity and query invalidation/refetching (#5210)

dev-rb6a787ff8e42c
diff --git a/apps/web/src/features/next-soup/soup-view/soup-view.tsx b/apps/web/src/features/next-soup/soup-view/soup-view.tsx
index d8f45e19f7..715e1fcc25 100644
--- a/apps/web/src/features/next-soup/soup-view/soup-view.tsx
+++ b/apps/web/src/features/next-soup/soup-view/soup-view.tsx
@@ -96,19 +96,11 @@ import {
type SearchLocation,
} from '@entity';
import SearchIcon from '@icon/macro-magnifying-glass.svg';
-import { createEffectOnEntityTypeNotification } from '@notifications';
import CaretDownIcon from '@phosphor/caret-down.svg';
import ChevronRightIcon from '@phosphor/caret-right.svg';
import CheckIcon from '@phosphor/check.svg';
import InfoIcon from '@phosphor/info.svg';
import Spinner from '@phosphor/spinner.svg';
-import { useQueryClient } from '@queries/client';
-import { emailKeys } from '@queries/email/keys';
-import { invalidateEntityNotifications } from '@queries/notification/user-notifications';
-import {
- invalidateSoupEntity,
- refetchSoupEntity,
-} from '@queries/soup/normalized-cache';
import { createElementSize } from '@solid-primitives/resize-observer';
import { debounce } from '@solid-primitives/scheduled';
import { Button, cn, Layer, Tooltip } from '@ui';
@@ -213,85 +205,6 @@ const MobileTabLoadingBar = () => (
</div>
);
-const useSoupNotificationInvalidators = () => {
- const notificationSource = useGlobalNotificationSource();
- const entityQueryClient = useQueryClient();
-
- createEffectOnEntityTypeNotification(
- notificationSource,
- 'channel',
- (notification) => {
- const meta = notification.notification_metadata;
-
- let threadId;
-
- if (
- meta.tag === 'channel_mention' ||
- meta.tag === 'channel_message_reply'
- ) {
- threadId = meta.content.threadId?.toString();
- }
-
- refetchSoupEntity(notification.entity_id, 'channel');
- invalidateSoupEntity(notification.entity_id);
- invalidateEntityNotifications(notification.entity_id);
-
- // For new inbox, we want to display channel threads so we should refetch the thread
- // item if this notification was for a thread
- if (threadId) {
- refetchSoupEntity(threadId, 'channelThread');
- invalidateSoupEntity(threadId);
- invalidateEntityNotifications(threadId);
- }
- }
- );
-
- createEffectOnEntityTypeNotification(
- notificationSource,
- 'chat',
- (notification) => {
- refetchSoupEntity(notification.entity_id, 'chat');
- invalidateSoupEntity(notification.entity_id);
- invalidateEntityNotifications(notification.entity_id);
- }
- );
-
- createEffectOnEntityTypeNotification(
- notificationSource,
- 'email_thread',
- (notification) => {
- refetchSoupEntity(notification.entity_id, 'emailThread');
- invalidateSoupEntity(notification.entity_id);
- // invalidate thread cache so thread gets fetched (with new message) on next load
- entityQueryClient.invalidateQueries({
- queryKey: emailKeys.threadMessages(notification.entity_id).queryKey,
- });
- }
- );
-
- createEffectOnEntityTypeNotification(
- notificationSource,
- 'document',
- (notification) => {
- if (notification.notification_event_type === 'task_assigned') {
- refetchSoupEntity(notification.entity_id, 'document');
- invalidateSoupEntity(notification.entity_id);
- invalidateEntityNotifications(notification.entity_id);
- }
- }
- );
-
- createEffectOnEntityTypeNotification(
- notificationSource,
- 'foreign_entity',
- (notification) => {
- refetchSoupEntity(notification.entity_id, 'foreignEntity');
- invalidateSoupEntity(notification.entity_id);
- invalidateEntityNotifications(notification.entity_id);
- }
- );
-};
-
const SOUP_LIST_STATE_ENTRY_KEY = 'soup.listState';
type SoupListEntryState = {
focus: string | undefined;
@@ -533,8 +446,6 @@ export const SoupView = (props: SoupViewProps) => {
)
);
- useSoupNotificationInvalidators();
-
const component = createMemo(() => {
const content = panel.handle.content();
diff --git a/apps/web/src/features/notifications/index.ts b/apps/web/src/features/notifications/index.ts
index 8d81a66659..11fd2f86a8 100644
--- a/apps/web/src/features/notifications/index.ts
+++ b/apps/web/src/features/notifications/index.ts
@@ -93,3 +93,7 @@ export type {
UnifiedNotification,
} from './types';
export { compositeEntity, notificationEntity } from './types';
+export {
+ handleNotificationUpdate,
+ useNotificationUpdates,
+} from './use-notification-updates';
diff --git a/apps/web/src/features/notifications/use-notification-updates.ts b/apps/web/src/features/notifications/use-notification-updates.ts
new file mode 100644
index 0000000000..7d2016f4bd
--- /dev/null
+++ b/apps/web/src/features/notifications/use-notification-updates.ts
@@ -0,0 +1,124 @@
+import { queryClient } from '@queries/client';
+import { emailKeys } from '@queries/email/keys';
+import { invalidateEmailLinks } from '@queries/email/link';
+import { invalidateEntityNotifications } from '@queries/notification/user-notifications';
+import {
+ invalidateSoupEntity,
+ refetchSoupEntity,
+} from '@queries/soup/normalized-cache';
+import { teamKeys } from '@queries/team/keys';
+import { onCleanup } from 'solid-js';
+import { match } from 'ts-pattern';
+import type { NotificationSource } from './notification-source';
+import type { UnifiedNotification } from './types';
+
+function refreshSoupEntity(
+ notification: UnifiedNotification,
+ entityType: Parameters<typeof refetchSoupEntity>[1]
+) {
+ void refetchSoupEntity(notification.entity_id, entityType);
+ invalidateSoupEntity(notification.entity_id);
+ void invalidateEntityNotifications(notification.entity_id);
+}
+
+function refreshChannel(
+ notification: UnifiedNotification,
+ threadId?: string | null
+) {
+ refreshSoupEntity(notification, 'channel');
+
+ if (!threadId) return;
+
+ void refetchSoupEntity(threadId, 'channelThread');
+ invalidateSoupEntity(threadId);
+ void invalidateEntityNotifications(threadId);
+}
+
+function refreshEmailThread(notification: UnifiedNotification) {
+ void refetchSoupEntity(notification.entity_id, 'emailThread');
+ invalidateSoupEntity(notification.entity_id);
+ void queryClient.invalidateQueries({
+ queryKey: emailKeys.threadMessages(notification.entity_id).queryKey,
+ });
+}
+
+/**
+ * Applies domain cache updates for a connection-gateway notification.
+ *
+ * Known notification types are handled explicitly. Unknown types are ignored
+ * so a newer backend cannot break clients that have not added support yet.
+ */
+export function handleNotificationUpdate(notification: UnifiedNotification) {
+ match(notification.notification_metadata)
+ .with({ tag: 'channel_mention' }, ({ content }) => {
+ refreshChannel(notification, content.threadId?.toString());
+ })
+ .with({ tag: 'document_mention' }, ({ content }) => {
+ refreshChannel(notification, content.threadId?.toString());
+ })
+ .with({ tag: 'mentioned_in_document_comment' }, () => {
+ refreshSoupEntity(notification, 'document');
+ })
+ .with({ tag: 'replied_to_document_comment_thread' }, () => {
+ refreshSoupEntity(notification, 'document');
+ })
+ .with({ tag: 'commented_on_document' }, () => {
+ refreshSoupEntity(notification, 'document');
+ })
+ .with({ tag: 'channel_invite' }, () => {
+ refreshChannel(notification);
+ })
+ .with({ tag: 'channel_message_send' }, () => {
+ refreshChannel(notification);
+ })
+ .with({ tag: 'channel_message_reply' }, ({ content }) => {
+ refreshChannel(notification, content.threadId?.toString());
+ })
+ .with({ tag: 'call_started' }, () => {
+ refreshChannel(notification);
+ })
+ .with({ tag: 'new_email' }, () => {
+ refreshEmailThread(notification);
+ })
+ .with({ tag: 'inbox_reauth_required' }, () => {
+ invalidateEmailLinks();
+ })
+ .with({ tag: 'invite_to_team' }, () => {
+ void queryClient.invalidateQueries({
+ queryKey: teamKeys.userInvites.queryKey,
+ });
+ })
+ .with({ tag: 'task_assigned' }, () => {
+ refreshSoupEntity(notification, 'document');
+ })
+ .with({ tag: 'ai_response' }, () => {
+ refreshSoupEntity(notification, 'chat');
+ })
+ .with({ tag: 'github_pr_status_changed' }, () => {
+ refreshSoupEntity(notification, 'foreignEntity');
+ })
+ .with({ tag: 'github_pr_check_run' }, () => {
+ refreshSoupEntity(notification, 'foreignEntity');
+ })
+ .with({ tag: 'github_review_requested' }, () => {
+ refreshSoupEntity(notification, 'foreignEntity');
+ })
+ .with({ tag: 'github_pr_comment' }, () => {
+ refreshSoupEntity(notification, 'foreignEntity');
+ })
+ .with({ tag: 'github_pr_mention' }, () => {
+ refreshSoupEntity(notification, 'foreignEntity');
+ })
+ .with({ tag: 'github_pr_review' }, () => {
+ refreshSoupEntity(notification, 'foreignEntity');
+ })
+ .otherwise(() => {
+ // Ignore notification types introduced by a newer backend.
+ });
+}
+
+/** Subscribe globally to notification-driven domain cache updates. */
+export function useNotificationUpdates(notificationSource: NotificationSource) {
+ const unsubscribe = notificationSource.subscribe(handleNotificationUpdate);
+ onCleanup(unsubscribe);
+}
diff --git a/apps/web/src/routes/Root.tsx b/apps/web/src/routes/Root.tsx
index 9d2902d1f4..e4a6ba8099 100644
--- a/apps/web/src/routes/Root.tsx
+++ b/apps/web/src/routes/Root.tsx
@@ -69,6 +69,7 @@ import {
BrowserNotificationModal,
createNotificationSource,
type UnifiedNotification,
+ useNotificationUpdates,
usePlatformNotificationState,
} from '@notifications';
import { maybeHandlePlatformNotification } from '@notifications/notification-platform';
@@ -485,6 +486,7 @@ function ConfiguredGlobalAppStateProvider(props: ParentProps) {
connectionGatewayWebsocket,
onNotification
);
+ useNotificationUpdates(notificationSource);
const blockOrchestrator = createBlockOrchestrator();
usePendingNotificationNavigationEffect(notificationSource);