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: optimistically mark email thread as read (#418)

dev-rb6b4ff52023ab
diff --git a/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx b/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
index 75ca4250af..e8b5545002 100644
--- a/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
+++ b/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
@@ -73,13 +73,13 @@ const getEntityTimestamp = (entity: EntityData): number => {
};
/**
- * Returns true if the new entity should replace the existing one based on timestamp
+ * Returns true if the new entity should replace the existing one based on timestamp. If the timestamp is the same, prefer to use the newer entity to handle optimistic updates
*/
const isNewerEntity = (
newEntity: EntityData,
existing: EntityData
): boolean => {
- return getEntityTimestamp(newEntity) > getEntityTimestamp(existing);
+ return getEntityTimestamp(newEntity) >= getEntityTimestamp(existing);
};
/**
diff --git a/js/app/packages/macro-entity/src/queries/email.ts b/js/app/packages/macro-entity/src/queries/email.ts
index 66e6204e09..89663dac03 100644
--- a/js/app/packages/macro-entity/src/queries/email.ts
+++ b/js/app/packages/macro-entity/src/queries/email.ts
@@ -1,5 +1,9 @@
import type { PreviewViewStandardLabel } from '@service-email/generated/schemas';
-import { useInfiniteQuery } from '@tanstack/solid-query';
+import {
+ type InfiniteData,
+ partialMatchKey,
+ useInfiniteQuery,
+} from '@tanstack/solid-query';
import { SERVER_HOSTS } from 'core/constant/servers';
import { platformFetch } from 'core/util/platformFetch';
import type { ApiPaginatedThreadCursor } from 'service-email/generated/schemas/apiPaginatedThreadCursor';
@@ -7,6 +11,7 @@ import type { PreviewsInboxCursorParams } from 'service-email/generated/schemas/
import { type Accessor, createMemo } from 'solid-js';
import type { EmailEntity } from '../types/entity';
import { createApiTokenQuery } from './auth';
+import { queryClient } from './client';
import { queryKeys } from './key';
export type FetchPaginatedEmailsParams = PreviewsInboxCursorParams & {
@@ -102,3 +107,50 @@ export function createEmailsInfiniteQuery(
};
});
}
+
+export const optimisticMarkEmailAsRead = (emailId: string) => {
+ queryClient.setQueriesData(
+ {
+ predicate(query) {
+ return partialMatchKey(
+ query.queryKey,
+ queryKeys.email({
+ infinite: true,
+ limit: 100,
+ view: 'inbox',
+ })
+ );
+ },
+ },
+ (
+ prev:
+ | InfiniteData<ApiPaginatedThreadCursor>
+ | ApiPaginatedThreadCursor
+ | undefined
+ ) => {
+ if (!prev) return;
+
+ if ('pageParams' in prev) {
+ return {
+ ...prev,
+ pages: prev.pages.map((p) => ({
+ ...p,
+ items: p.items.map((item) => {
+ if (item.id !== emailId) return item;
+ return {
+ ...item,
+ isRead: true,
+ };
+ }),
+ })),
+ };
+ }
+
+ return prev.items.map((item) => {
+ if (item.id !== emailId) return item;
+
+ return { ...item, isRead: true };
+ });
+ }
+ );
+};
diff --git a/js/app/packages/notifications/components/DebouncedNotificationReadMarker.tsx b/js/app/packages/notifications/components/DebouncedNotificationReadMarker.tsx
index 29bf3981b1..db05913919 100644
--- a/js/app/packages/notifications/components/DebouncedNotificationReadMarker.tsx
+++ b/js/app/packages/notifications/components/DebouncedNotificationReadMarker.tsx
@@ -1,6 +1,7 @@
import type { Entity } from '@core/types';
import { emailClient } from '@service-email/client';
import { onCleanup, onMount } from 'solid-js';
+import { optimisticMarkEmailAsRead } from '../../macro-entity/src/queries/email';
import {
markNotificationForEntityIdAsRead,
markNotificationsForEntityAsRead,
@@ -114,6 +115,7 @@ export function EmailDebouncedReadMarker(props: {
<DebouncedMarker
debounceTime={props.debounceTime}
debouncedFn={() => {
+ optimisticMarkEmailAsRead(props.threadId);
emailClient.markThreadAsSeen({
thread_id: props.threadId,
});