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(emails/soup): fix live updating in email block and in soup (#522)

teoc43bcd0a3335
diff --git a/js/app/packages/app/component/Soup.tsx b/js/app/packages/app/component/Soup.tsx
index d7c1d4d143..c2bd6f1189 100644
--- a/js/app/packages/app/component/Soup.tsx
+++ b/js/app/packages/app/component/Soup.tsx
@@ -280,25 +280,25 @@ export function Soup() {
createEffectOnEntityTypeNotification(
notificationSource,
'channel',
- (notifications) => {
+ (notification) => {
entityQueryClient.invalidateQueries({
queryKey: queryKeys.all.channel,
});
- const eventItemIds = new Set(
- notifications.map(({ eventItemId }) => eventItemId)
- );
- eventItemIds.forEach((eventItemId) => {
- entityQueryClient.invalidateQueries({
- queryKey: queryKeys.notification({ eventItemId }),
- });
+ entityQueryClient.invalidateQueries({
+ queryKey: queryKeys.notification({
+ eventItemId: notification.eventItemId,
+ }),
});
}
);
- createEffectOnEntityTypeNotification(notificationSource, 'email', () =>
+
+ createEffectOnEntityTypeNotification(notificationSource, 'email', () => {
entityQueryClient.invalidateQueries({
- queryKey: queryKeys.all.email,
- })
- );
+ // HACK: this needs to be improved, since we use a single query, per entity invalidations
+ // become a little more complicated.
+ queryKey: queryKeys.all.entity,
+ });
+ });
const saveViewMutation = useUpsertSavedViewMutation();
diff --git a/js/app/packages/block-email/component/Email.tsx b/js/app/packages/block-email/component/Email.tsx
index 58eccfe661..a1b2124e4a 100644
--- a/js/app/packages/block-email/component/Email.tsx
+++ b/js/app/packages/block-email/component/Email.tsx
@@ -17,7 +17,7 @@ import {
} from '@core/user';
import {
createEffectOnEntityTypeNotification,
- isNotificationWithMetadata,
+ isNewEmail,
} from '@notifications';
import {
useArchiveThreadMutation,
@@ -558,30 +558,19 @@ export function Email(props: EmailProps) {
});
const notificationSource = useGlobalNotificationSource();
+
createEffectOnEntityTypeNotification(
notificationSource,
'email',
- (notifications) => {
- for (const notification of notifications) {
- if (!isNotificationWithMetadata(notification)) return;
- const metadata = notification.notificationMetadata;
- if (
- !metadata ||
- typeof metadata !== 'object' ||
- !('thread_id' in metadata)
- )
- return;
-
- const notificationThreadId = (metadata as { thread_id: string })
- .thread_id;
-
- if (notificationThreadId === threadData()?.db_id) {
- threadQuery.refetch();
- break;
- }
+ (notification) => {
+ if (!isNewEmail(notification)) return;
+ const notificationThreadId = notification.notificationMetadata.threadId;
+ if (notificationThreadId === threadData()?.db_id) {
+ threadQuery.refetch();
}
}
);
+
let markdownDomRef!: HTMLDivElement;
registerScopeSignalHotkey(scopeId, {
diff --git a/js/app/packages/notifications/notification-helpers.ts b/js/app/packages/notifications/notification-helpers.ts
index 2dddf30fc9..0714f509e1 100644
--- a/js/app/packages/notifications/notification-helpers.ts
+++ b/js/app/packages/notifications/notification-helpers.ts
@@ -1,6 +1,6 @@
import type { Entity, EntityType } from '@core/types';
import type { UnifiedNotification } from '@service-notification/client';
-import { type Accessor, createEffect, createMemo, on } from 'solid-js';
+import { type Accessor, createEffect, createMemo, onCleanup } from 'solid-js';
import type { NotificationSource } from './notification-source';
import { compositeEntity } from './types';
@@ -191,22 +191,17 @@ export function useNotificationsMutedForEntity(
export function createEffectOnEntityTypeNotification(
notificationSource: NotificationSource,
type: EntityType,
- callback: (notifications: UnifiedNotification[]) => void
+ callback: (n: UnifiedNotification) => void
) {
- createEffect(
- on(
- notificationSource.notifications,
- (currentNotifications, oldNotifications) => {
- oldNotifications = oldNotifications || [];
- const newNotifications = currentNotifications.filter(
- (n) =>
- !oldNotifications.includes(n) && notificationIsOfEntityType(n, type)
- );
-
- if (newNotifications.length > 0) {
- callback(newNotifications);
- }
+ createEffect(() => {
+ let cleanup = notificationSource.subscribe((notification) => {
+ if (notificationIsOfEntityType(notification, type)) {
+ callback(notification);
}
- )
- );
+ });
+
+ onCleanup(() => {
+ if (cleanup) cleanup();
+ });
+ });
}
diff --git a/js/app/packages/notifications/notification-source.ts b/js/app/packages/notifications/notification-source.ts
index 823f806117..543a2ca12d 100644
--- a/js/app/packages/notifications/notification-source.ts
+++ b/js/app/packages/notifications/notification-source.ts
@@ -38,6 +38,9 @@ type AssertKey<T, K extends keyof T & string> = K;
const NOTIFICATION_KEY: AssertKey<UnifiedNotification, 'id'> = 'id';
+type UnsubscribeFn = () => void;
+type SubscribeFn = (newNotification: UnifiedNotification) => void;
+
export type NotificationSource = {
readonly store: Store<NotificationStoreInner>;
readonly notifications: Accessor<UnifiedNotification[]>;
@@ -67,6 +70,9 @@ export type NotificationSource = {
/** subscribe to entity notifications */
unmuteEntity: (entity: Entity) => Promise<void>;
+
+ /** subscribe to new notifications */
+ subscribe: (subscribe: SubscribeFn) => UnsubscribeFn;
};
const NOTIFICATION_EVENT_TYPE = 'notification';
@@ -82,6 +88,8 @@ export function createNotificationSource(
Object.values(trackStore(store)).flat()
);
+ let subscriptions: Set<SubscribeFn> = new Set();
+
const [mutedEntities, setMutedEntities] = createSignal<UserUnsubscribe[]>([]);
const notificationsQuery = createNotificationQuery({ limit: QUERY_LIMIT });
@@ -169,6 +177,8 @@ export function createNotificationSource(
}
onNotification?.(parsedNotification);
+ subscriptions.forEach((subscribe) => subscribe(parsedNotification));
+
refetchAndReconcileNotifications([parsedNotification]);
});
@@ -229,6 +239,13 @@ export function createNotificationSource(
await mutedEntitiesQuery.refetch();
};
+ const subscribe = (subscribeFn: SubscribeFn) => {
+ subscriptions.add(subscribeFn);
+ return () => {
+ subscriptions.delete(subscribeFn);
+ };
+ };
+
return {
store,
notifications,
@@ -242,5 +259,6 @@ export function createNotificationSource(
bulkMarkAsDone,
muteEntity,
unmuteEntity,
+ subscribe,
};
}