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: clean up multi-inbox settings toast and optimistic add/remove (#3815)

gbirmandf6409b053e1
diff --git a/js/app/packages/app/component/EmailAuth.tsx b/js/app/packages/app/component/EmailAuth.tsx
index 206abc6888..13288ca509 100644
--- a/js/app/packages/app/component/EmailAuth.tsx
+++ b/js/app/packages/app/component/EmailAuth.tsx
@@ -115,7 +115,11 @@ function EmailLinkCallback(props: Pick<EmailAuthParams, 'successPath'>) {
}
await initEmailLink({ linkId }).match(
- () => {
+ async () => {
+ // Pull the newly-provisioned link into the cache before leaving the
+ // callback so the inbox panel shows it immediately on return rather
+ // than flashing a stale list until its own refetch lands.
+ await query.refetch();
toast.success('Inbox connected');
navigateToSuccess();
},
diff --git a/js/app/packages/app/component/settings/Account.tsx b/js/app/packages/app/component/settings/Account.tsx
index e72458ab17..03d7e2cb82 100644
--- a/js/app/packages/app/component/settings/Account.tsx
+++ b/js/app/packages/app/component/settings/Account.tsx
@@ -59,6 +59,7 @@ import PaywallTeamOwnerView from '../paywall/PaywallTeamOwnerView';
import { ROUTER_BASE_CONCAT } from '@app/constants/routerBase';
import { useEmailLinks, useEmailLinksStatus } from '@core/email-link';
import { useInitGmailLink } from '@queries/auth';
+import { useRemoveInboxMutation } from '@queries/email/link';
import {
type SupportedNotificationSettings,
useNotificationSettings,
@@ -142,9 +143,12 @@ export function Account() {
const {
query: emailLinksQuery,
disconnect: disconnectEmail,
- removeInbox,
resyncInbox,
} = useEmailLinks();
+ const removeInboxMutation = useRemoveInboxMutation({
+ onSuccess: () => toast.success('Inbox removed'),
+ onError: () => toast.failure('Failed to remove inbox. Please try again.'),
+ });
const [removeTarget, setRemoveTarget] = createSignal<{
id: string;
email: string;
@@ -218,20 +222,11 @@ export function Account() {
});
};
- const handleRemoveInbox = async () => {
+ const handleRemoveInbox = () => {
const target = removeTarget();
if (!target) return;
setRemoveTarget(null);
- await removeInbox(target.id).match(
- () => {
- toast.success(
- target.isOwn
- ? 'Inbox removed — clearing its data, this may take a moment.'
- : 'Inbox removed.'
- );
- },
- () => toast.failure('Failed to remove inbox. Please try again.')
- );
+ removeInboxMutation.mutate(target.id);
};
const [githubLinkStatus, { refetch: refetchGithubLinkStatus }] =
diff --git a/js/app/packages/core/email-link/index.ts b/js/app/packages/core/email-link/index.ts
index fa9598e20d..dd96dcaaf3 100644
--- a/js/app/packages/core/email-link/index.ts
+++ b/js/app/packages/core/email-link/index.ts
@@ -116,20 +116,6 @@ function disconnectEmail(): ResultAsync<void, 'failed-to-disconnect'> {
);
}
-/**
- * Removes a linked inbox. For an owned inbox the backend cascades the full
- * teardown; for a delegated inbox it only drops the delegation edge.
- *
- * @returns ok if the inbox was removed, err if it failed
- */
-function removeInbox(linkId: string): ResultAsync<void, 'failed-to-remove'> {
- return ResultAsync.fromSafePromise(
- emailClient.deleteLink({ linkId })
- ).andThen((response) =>
- response.isErr() ? err('failed-to-remove') : okAsync(void 0)
- );
-}
-
/**
* Enqueues a fresh backfill for a linked inbox. Idempotent on the backend: a
* no-op when a backfill is already in progress.
@@ -197,7 +183,6 @@ export function useEmailLinks() {
.map(startEmailPolling)
.andTee(invalidations),
disconnect: () => disconnectEmail().andTee(invalidations),
- removeInbox: (linkId: string) => removeInbox(linkId).andTee(invalidations),
resyncInbox: (linkId: string) =>
resyncInbox(linkId).andTee(() => invalidateEmailLinks()),
invalidate: () => invalidateEmailLinks(),
diff --git a/js/app/packages/queries/email/link.ts b/js/app/packages/queries/email/link.ts
index 4c0bff8c3f..05ba25f438 100644
--- a/js/app/packages/queries/email/link.ts
+++ b/js/app/packages/queries/email/link.ts
@@ -1,9 +1,12 @@
import { useEmail } from '@core/context/user';
import { throwOnErr } from '@core/util/result';
+import { invalidateUserInfo } from '@queries/auth/user-info';
import { queryClient } from '@queries/client';
import { emailClient } from '@service-email/client';
-import { useQuery } from '@tanstack/solid-query';
+import type { ListLinksResponse } from '@service-email/generated/schemas';
+import { useMutation, useQuery } from '@tanstack/solid-query';
import { createMemo } from 'solid-js';
+import { type MutationCallbacks, withCallbacks } from '../utils';
import { emailKeys } from './keys';
const LINK_STALE_TIME = 5 * 60 * 1000;
@@ -52,3 +55,70 @@ export function invalidateEmailLinks() {
queryKey: emailKeys.links.queryKey,
});
}
+
+type RemoveInboxContext = { previousLinks: ListLinksResponse | undefined };
+type RemoveInboxCallbacks = MutationCallbacks<
+ void,
+ Error,
+ string,
+ RemoveInboxContext
+>;
+
+/**
+ * Removes a linked inbox, optimistically dropping its row from the cached links
+ * list so the change is reflected immediately. Rolls the cache back on failure
+ * and reconciles with the server on success.
+ */
+export function useRemoveInboxMutation(callbacks?: RemoveInboxCallbacks) {
+ return useMutation(() => ({
+ mutationFn: async (linkId: string) => {
+ await throwOnErr(() => emailClient.deleteLink({ linkId }));
+ },
+
+ ...withCallbacks<void, Error, string, RemoveInboxContext>(
+ {
+ onMutate: async (linkId) => {
+ await queryClient.cancelQueries({
+ queryKey: emailKeys.links.queryKey,
+ });
+
+ const previousLinks = queryClient.getQueryData<ListLinksResponse>(
+ emailKeys.links.queryKey
+ );
+
+ queryClient.setQueryData<ListLinksResponse>(
+ emailKeys.links.queryKey,
+ (old) =>
+ old
+ ? {
+ ...old,
+ links: old.links.filter((link) => link.id !== linkId),
+ }
+ : undefined
+ );
+
+ return { previousLinks };
+ },
+
+ onSuccess: async () => {
+ // Owned inboxes are torn down asynchronously, so the row still appears
+ // in GET /email/links for a short window after the 204. Refetching links
+ // here would resurrect the optimistically-removed row; instead leave the
+ // optimistic cache in place and let the next focus refetch reconcile once
+ // teardown completes.
+ await invalidateUserInfo();
+ },
+
+ onError: (_error, _linkId, context) => {
+ if (context?.previousLinks) {
+ queryClient.setQueryData(
+ emailKeys.links.queryKey,
+ context.previousLinks
+ );
+ }
+ },
+ },
+ callbacks
+ ),
+ }));
+}