Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
chg(email): make thread seen optimistic (#803)
teof1cd4d338695
diff --git a/js/app/packages/notifications/components/DebouncedNotificationReadMarker.tsx b/js/app/packages/notifications/components/DebouncedNotificationReadMarker.tsxindex 9b6d1c726a..8879107918 100644--- a/js/app/packages/notifications/components/DebouncedNotificationReadMarker.tsx+++ b/js/app/packages/notifications/components/DebouncedNotificationReadMarker.tsx@@ -1,5 +1,6 @@import type { Entity } from '@core/types';import { queryClient } from '@queries/client';+import { emailKeys } from '@queries/email/keys';import { useMarkThreadAsSeenMutation } from '@queries/email/thread';import { onCleanup, onMount } from 'solid-js';import {@@ -111,20 +112,25 @@ export function EmailDebouncedReadMarker(props: {debounceTime?: number;threadId: string;}) {- const markSeenMutation = useMarkThreadAsSeenMutation({- onSuccess() {- queryClient.invalidateQueries({- predicate(query) {- return query.queryKey.includes('dss');- },- });- },- });+ const markSeenMutation = useMarkThreadAsSeenMutation();++ const isAlreadySeen = () => {+ const data = queryClient.getQueryData(+ emailKeys.threadMessages(props.threadId).queryKey+ );+ return (+ (data as { pages?: { is_read?: boolean }[] })?.pages?.[0]?.is_read ===+ true+ );+ };return (<DebouncedMarkerdebounceTime={props.debounceTime}- debouncedFn={() => markSeenMutation.mutate({ threadId: props.threadId })}+ debouncedFn={() => {+ if (isAlreadySeen()) return;+ markSeenMutation.mutate({ threadId: props.threadId });+ }}/>);}diff --git a/js/app/packages/queries/email/thread.ts b/js/app/packages/queries/email/thread.tsindex 0e437917b7..9254fdd6c5 100644--- a/js/app/packages/queries/email/thread.ts+++ b/js/app/packages/queries/email/thread.ts@@ -1,11 +1,13 @@import { DEFAULT_THREAD_MESSAGES_LIMIT } from '@core/constant/pagination';import { catchToResult, isErr, ok, throwOnErr } from '@core/util/maybeResult';+import { queryKeys } from '@macro-entity';import { emailClient } from '@service-email/client';import type {MessageToSend,SendMessageResponse,APIThread as Thread,} from '@service-email/generated/schemas';+import type { SoupPage } from '@service-storage/generated/schemas';import {type InfiniteData,type SolidInfiniteQueryOptions,@@ -146,6 +148,56 @@ export function useThreadQuery<Options extends UseThreadQueryOptions>(type MarkThreadAsSeenParams = { threadId: string };+/**+ * Optimistically update thread and soup queries when marking as seen.+ * Does not await cancelQueries to avoid triggering suspense boundaries.+ */+function threadSeenOnMutate(params: MarkThreadAsSeenParams): void {+ queryClient.cancelQueries({+ queryKey: emailKeys.threadMessages(params.threadId).queryKey,+ });++ queryClient.setQueryData<InfiniteData<Thread, number>>(+ emailKeys.threadMessages(params.threadId).queryKey,+ (old) =>+ old && {+ ...old,+ pages: old.pages.map((page) => ({+ ...page,+ is_read: true,+ })),+ }+ );++ queryClient.setQueriesData<InfiniteData<SoupPage, unknown>>(+ { queryKey: queryKeys.all.dss },+ (old) => {+ if (!old) return old;+ return {+ ...old,+ pages: old.pages.map((page) => ({+ ...page,+ items: page.items.map((item) => {+ if (+ item.tag === 'emailThread' &&+ item.data.id === params.threadId+ ) {+ return {+ ...item,+ data: {+ ...item.data,+ isRead: true,+ },+ };+ }+ return item;+ }),+ })),+ };+ }+ );+}+/*** Mutation to mark a thread as seen.*/@@ -153,19 +205,21 @@ export function useMarkThreadAsSeenMutation(callbacks?: MutationCallbacks<void, Error, MarkThreadAsSeenParams>) {return useMutation(() => ({- mutationFn: async (params: MarkThreadAsSeenParams) =>- void (await throwOnErr(- async () =>- await emailClient.markThreadAsSeen({- thread_id: params.threadId,- })- )),+ mutationFn: async (params: MarkThreadAsSeenParams) => {+ await throwOnErr(() =>+ emailClient.markThreadAsSeen({ thread_id: params.threadId })+ );+ },...withCallbacks<void, Error, MarkThreadAsSeenParams>({+ onMutate: threadSeenOnMutate,onSuccess: (_, params) => {queryClient.invalidateQueries({queryKey: emailKeys.threadMessages(params.threadId).queryKey,});+ queryClient.invalidateQueries({+ queryKey: queryKeys.all.dss,+ });},},callbacks@@ -178,6 +232,31 @@ type ArchiveThreadContext = {previousData: InfiniteData<Thread, number> | undefined;};+/** Optimistically set `inbox_visible` when archiving a thread. */+async function threadArchiveOnMutate(params: ArchiveThreadParams) {+ await queryClient.cancelQueries({+ queryKey: emailKeys.threadMessages(params.threadId).queryKey,+ });++ const previousData = queryClient.getQueryData<InfiniteData<Thread, number>>(+ emailKeys.threadMessages(params.threadId).queryKey+ );++ queryClient.setQueryData<InfiniteData<Thread, number>>(+ emailKeys.threadMessages(params.threadId).queryKey,+ (old) =>+ old && {+ ...old,+ pages: old.pages.map((page) => ({+ ...page,+ inbox_visible: !params.archive,+ })),+ }+ );++ return { previousData };+}+/*** Mutation to archive or unarchive a thread.* Uses optimistic updates to immediately reflect the change in UI.@@ -201,29 +280,7 @@ export function useArchiveThreadMutation(),...withCallbacks<void, Error, ArchiveThreadParams, ArchiveThreadContext>({- onMutate: async (params) => {- await queryClient.cancelQueries({- queryKey: emailKeys.threadMessages(params.threadId).queryKey,- });-- const previousData = queryClient.getQueryData<- InfiniteData<Thread, number>- >(emailKeys.threadMessages(params.threadId).queryKey);-- queryClient.setQueryData<InfiniteData<Thread, number>>(- emailKeys.threadMessages(params.threadId).queryKey,- (old) =>- old && {- ...old,- pages: old.pages.map((page) => ({- ...page,- inbox_visible: !params.archive,- })),- }- );-- return { previousData };- },+ onMutate: async (params) => await threadArchiveOnMutate(params),onError: (_err, params, context) => {if (context?.previousData) {queryClient.setQueryData(