From b5d7c8f13804115167fba61db00af09b8e1f5132 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Sun, 29 Nov 2026 18:37:45 +0900 Subject: [PATCH 1/5] refactor(components): move UI side effects from 'useMutation' callbacks to 'mutate' options --- src/components/DocFeedbackNote.tsx | 40 +++++---- src/components/DocFeedbackProvider.tsx | 38 +++++---- src/components/FeedbackModerationPage.tsx | 60 +++++++------- src/components/NewsletterSignup.tsx | 59 +++++++------- src/components/ShowcaseModerationPage.tsx | 98 +++++++++++++---------- src/components/ShowcaseSubmitForm.tsx | 15 ++-- 6 files changed, 175 insertions(+), 135 deletions(-) diff --git a/src/components/DocFeedbackNote.tsx b/src/components/DocFeedbackNote.tsx index a0918a5d8..652e07a13 100644 --- a/src/components/DocFeedbackNote.tsx +++ b/src/components/DocFeedbackNote.tsx @@ -136,8 +136,6 @@ export function DocFeedbackNote({ queryClient.setQueryData(queryKey, data) }) } - setDeleteError(error.message) - setIsDeleting(false) }, onSettled: () => { queryClient.invalidateQueries({ queryKey: ['docFeedback'] }) @@ -199,10 +197,6 @@ export function DocFeedbackNote({ return { previousData } }, - onSuccess: () => { - setIsSaving(false) - setSaveError(null) - }, onError: (error: Error, variables, context) => { // Rollback on error if (context?.previousData) { @@ -210,8 +204,6 @@ export function DocFeedbackNote({ queryClient.setQueryData(queryKey, data) }) } - setSaveError(error.message) - setIsSaving(false) }, onSettled: () => { queryClient.invalidateQueries({ queryKey: ['docFeedback'] }) @@ -291,7 +283,15 @@ export function DocFeedbackNote({ if (!isDeleting && confirm('Are you sure you want to delete this note?')) { setIsDeleting(true) setDeleteError(null) - deleteMutation.mutate({ data: { feedbackId: note.id } }) + deleteMutation.mutate( + { data: { feedbackId: note.id } }, + { + onError: (error: Error) => { + setDeleteError(error.message) + setIsDeleting(false) + }, + }, + ) } } @@ -303,12 +303,24 @@ export function DocFeedbackNote({ setIsSaving(true) setSaveError(null) - updateMutation.mutate({ - data: { - feedbackId: note.id, - content: content.trim(), + updateMutation.mutate( + { + data: { + feedbackId: note.id, + content: content.trim(), + }, }, - }) + { + onSuccess: () => { + setIsSaving(false) + setSaveError(null) + }, + onError: (error: Error) => { + setSaveError(error.message) + setIsSaving(false) + }, + }, + ) } const handleCancel = () => { diff --git a/src/components/DocFeedbackProvider.tsx b/src/components/DocFeedbackProvider.tsx index 26125bcba..2a53f0eee 100644 --- a/src/components/DocFeedbackProvider.tsx +++ b/src/components/DocFeedbackProvider.tsx @@ -650,9 +650,6 @@ function CreatingFeedbackNote({ return { previousData } }, - onSuccess: () => { - onClose() - }, onError: (error: Error, variables, context) => { // Rollback on error if (context?.previousData) { @@ -660,8 +657,6 @@ function CreatingFeedbackNote({ queryClient.setQueryData(queryKey, data) }) } - setError(error.message) - setIsSaving(false) }, onSettled: () => { queryClient.invalidateQueries({ queryKey: ['docFeedback'] }) @@ -684,18 +679,29 @@ function CreatingFeedbackNote({ setIsSaving(true) setError(null) - createMutation.mutate({ - data: { - type, - content: content.trim(), - pagePath, - libraryId, - libraryVersion, - blockSelector, - blockContentHash, - blockMarkdown, + createMutation.mutate( + { + data: { + type, + content: content.trim(), + pagePath, + libraryId, + libraryVersion, + blockSelector, + blockContentHash, + blockMarkdown, + }, }, - }) + { + onSuccess: () => { + onClose() + }, + onError: (error: Error) => { + setError(error.message) + setIsSaving(false) + }, + }, + ) } const charCount = content.length diff --git a/src/components/FeedbackModerationPage.tsx b/src/components/FeedbackModerationPage.tsx index d22514821..2d408d226 100644 --- a/src/components/FeedbackModerationPage.tsx +++ b/src/components/FeedbackModerationPage.tsx @@ -33,29 +33,9 @@ export function FeedbackModerationPage() { const moderateMutation = useMutation({ mutationFn: moderateDocFeedback, - onSuccess: (_, variables) => { - notify( -
-
Feedback moderated
-
- Feedback{' '} - {variables.data.action === 'approve' ? 'approved' : 'denied'}{' '} - successfully. -
-
, - ) + onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['docFeedback', 'moderation'] }) }, - onError: (error: Error) => { - notify( -
-
Moderation failed
-
- {error.message} -
-
, - ) - }, }) const handleFilterChange = (filters: Partial) => { @@ -89,13 +69,39 @@ export function FeedbackModerationPage() { action: 'approve' | 'deny', moderationNote?: string, ) => { - moderateMutation.mutate({ - data: { - feedbackId, - action, - moderationNote, + moderateMutation.mutate( + { + data: { + feedbackId, + action, + moderationNote, + }, }, - }) + { + onSuccess: (_, variables) => { + notify( +
+
Feedback moderated
+
+ Feedback{' '} + {variables.data.action === 'approve' ? 'approved' : 'denied'}{' '} + successfully. +
+
, + ) + }, + onError: (error: Error) => { + notify( +
+
Moderation failed
+
+ {error.message} +
+
, + ) + }, + }, + ) } return ( diff --git a/src/components/NewsletterSignup.tsx b/src/components/NewsletterSignup.tsx index 26fd70b83..486864247 100644 --- a/src/components/NewsletterSignup.tsx +++ b/src/components/NewsletterSignup.tsx @@ -31,30 +31,7 @@ export function NewsletterSignup({ const { mutate, isPending } = useMutation({ mutationFn: () => addUserSignupSource({ data: { source: 'newsletter' } }), - onSuccess: async (result) => { - await queryClient.invalidateQueries(currentUserQueryOptions) - - notify( -
-
- {result.alreadyTagged ? 'Already subscribed' : "You're subscribed"} -
-
- We'll use your TanStack login email. -
-
, - ) - }, - onError: () => { - notify( -
-
Subscription failed
-
- Please try again in a moment -
-
, - ) - }, + onSuccess: () => queryClient.invalidateQueries(currentUserQueryOptions), }) const handleSubscribe = React.useCallback(() => { @@ -62,15 +39,43 @@ export function NewsletterSignup({ return } + const subscribe = () => + mutate(undefined, { + onSuccess: (result) => { + notify( +
+
+ {result.alreadyTagged + ? 'Already subscribed' + : "You're subscribed"} +
+
+ We'll use your TanStack login email. +
+
, + ) + }, + onError: () => { + notify( +
+
Subscription failed
+
+ Please try again in a moment +
+
, + ) + }, + }) + if (!user) { openLoginModal({ - onSuccess: () => mutate(), + onSuccess: subscribe, }) return } - mutate() - }, [isPending, isSubscribed, mutate, openLoginModal, user]) + subscribe() + }, [isPending, isSubscribed, mutate, notify, openLoginModal, user]) if (isSubscribed) { return ( diff --git a/src/components/ShowcaseModerationPage.tsx b/src/components/ShowcaseModerationPage.tsx index 68024b685..cea5f58dc 100644 --- a/src/components/ShowcaseModerationPage.tsx +++ b/src/components/ShowcaseModerationPage.tsx @@ -35,50 +35,16 @@ export function ShowcaseModerationPage() { const featuredMutation = useMutation({ mutationFn: setShowcaseFeatured, - onSuccess: (_, variables) => { - notify( -
-
Featured status updated
-
- Showcase {variables.data.isFeatured ? 'featured' : 'unfeatured'}{' '} - successfully. -
-
, - ) + onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['showcases'] }) }, - onError: (error: Error) => { - notify( -
-
Update failed
-
- {error.message} -
-
, - ) - }, }) const deleteMutation = useMutation({ mutationFn: adminDeleteShowcase, onSuccess: () => { - notify( -
-
Showcase deleted
-
, - ) queryClient.invalidateQueries({ queryKey: ['showcases'] }) }, - onError: (error: Error) => { - notify( -
-
Delete failed
-
- {error.message} -
-
, - ) - }, }) const voteMutation = useMutation({ @@ -115,18 +81,64 @@ export function ShowcaseModerationPage() { } const handleToggleFeatured = (showcaseId: string, isFeatured: boolean) => { - featuredMutation.mutate({ - data: { - showcaseId, - isFeatured, + featuredMutation.mutate( + { + data: { + showcaseId, + isFeatured, + }, }, - }) + { + onSuccess: (_, variables) => { + notify( +
+
Featured status updated
+
+ Showcase {variables.data.isFeatured ? 'featured' : 'unfeatured'}{' '} + successfully. +
+
, + ) + }, + onError: (error: Error) => { + notify( +
+
Update failed
+
+ {error.message} +
+
, + ) + }, + }, + ) } const handleDelete = (showcaseId: string) => { - deleteMutation.mutate({ - data: { showcaseId }, - }) + deleteMutation.mutate( + { + data: { showcaseId }, + }, + { + onSuccess: () => { + notify( +
+
Showcase deleted
+
, + ) + }, + onError: (error: Error) => { + notify( +
+
Delete failed
+
+ {error.message} +
+
, + ) + }, + }, + ) } const handleVote = (showcaseId: string, value: 1 | -1) => { diff --git a/src/components/ShowcaseSubmitForm.tsx b/src/components/ShowcaseSubmitForm.tsx index 1e37322ad..30911ea20 100644 --- a/src/components/ShowcaseSubmitForm.tsx +++ b/src/components/ShowcaseSubmitForm.tsx @@ -91,14 +91,10 @@ export function ShowcaseSubmitForm({ showcase }: ShowcaseSubmitFormProps) { const createMutation = useMutation({ mutationFn: submitShowcase, - onSuccess, - onError, }) const editMutation = useMutation({ mutationFn: updateShowcase, - onSuccess, - onError, }) const isPending = createMutation.isPending || editMutation.isPending @@ -179,11 +175,14 @@ export function ShowcaseSubmitForm({ showcase }: ShowcaseSubmitFormProps) { } if (isEditMode) { - editMutation.mutate({ - data: { ...formData, showcaseId: showcase.id }, - }) + editMutation.mutate( + { + data: { ...formData, showcaseId: showcase.id }, + }, + { onSuccess, onError }, + ) } else { - createMutation.mutate({ data: formData }) + createMutation.mutate({ data: formData }, { onSuccess, onError }) } } From 84b457c2754eab9b304ce88d221875af2934a2a3 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Sun, 29 Nov 2026 18:37:45 +0900 Subject: [PATCH 2/5] refactor(routes/account): move 'notify' and state updates from 'useMutation' callbacks to 'mutate' options --- src/routes/account/integrations.tsx | 126 +++++++++++++++++----------- src/routes/account/submissions.tsx | 38 +++++---- 2 files changed, 100 insertions(+), 64 deletions(-) diff --git a/src/routes/account/integrations.tsx b/src/routes/account/integrations.tsx index dfe58ae16..0d47d1204 100644 --- a/src/routes/account/integrations.tsx +++ b/src/routes/account/integrations.tsx @@ -50,30 +50,8 @@ function IntegrationsPage() { const createMutation = useMutation({ mutationFn: createMcpApiKey, - onSuccess: (data) => { + onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['api-keys'] }) - setNewlyCreatedKey(data.rawKey) - setIsCreating(false) - setNewKeyName('') - setExpiresInDays(undefined) - notify( -
-
API key created
-
- Copy it now, you won't see it again -
-
, - ) - }, - onError: (error) => { - notify( -
-
Error
-
- {error instanceof Error ? error.message : 'Failed to create key'} -
-
, - ) }, }) @@ -81,11 +59,6 @@ function IntegrationsPage() { mutationFn: revokeMcpApiKey, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['api-keys'] }) - notify( -
-
API key revoked
-
, - ) }, }) @@ -93,11 +66,6 @@ function IntegrationsPage() { mutationFn: deleteMcpApiKey, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['api-keys'] }) - notify( -
-
API key deleted
-
, - ) }, }) @@ -110,22 +78,47 @@ function IntegrationsPage() { mutationFn: revokeConnectedApp, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['connected-apps'] }) - notify( -
-
App access revoked
-
, - ) }, }) const handleCreate = () => { if (!newKeyName.trim()) return - createMutation.mutate({ - data: { - name: newKeyName.trim(), - expiresInDays, + createMutation.mutate( + { + data: { + name: newKeyName.trim(), + expiresInDays, + }, }, - }) + { + onSuccess: (data) => { + setNewlyCreatedKey(data.rawKey) + setIsCreating(false) + setNewKeyName('') + setExpiresInDays(undefined) + notify( +
+
API key created
+
+ Copy it now, you won't see it again +
+
, + ) + }, + onError: (error) => { + notify( +
+
Error
+
+ {error instanceof Error + ? error.message + : 'Failed to create key'} +
+
, + ) + }, + }, + ) } const handleCopy = async () => { @@ -349,7 +342,20 @@ function IntegrationsPage() { {key.isActive && (