feat: useOptimisticMutation#10103
Draft
xiaodemen wants to merge 1 commit into
Draft
Conversation
b2977c7 to
aa409da
Compare
✅ Circular References ReportGenerated at: 2026-06-17T08:01:58.544Z Summary
Click to view all circular references in PR (9)Click to view all circular references in base branch (9)Analysis✅ No Change: This PR does not introduce or remove any circular references. This report was generated automatically by comparing against the |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a small React Query-based “store” layer for app settings, including a reusable useOptimisticMutation hook to support optimistic cache updates and rollback.
Changes:
- Added
useOptimisticMutationhelper to perform optimisticsetQueryDataupdates and invalidate/refetch after mutation settles. - Added
useSettingsStorehook backed by React Query, with DB change listening + semantic settings mutation helpers (hotkeys, plugin toggles, etc.).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| packages/insomnia/src/ui/stores/use-optimistic-mutation.ts | New optimistic-mutation helper built on React Query. |
| packages/insomnia/src/ui/stores/settings-store.ts | New settings “store” hook using React Query + optimistic updates and DB-change invalidation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+6
to
+9
| export function useOptimisticMutation( | ||
| options: Parameters<typeof useMutation>[0] & { invalidateKey: any[]; patch: boolean }, | ||
| ) { | ||
| const { invalidateKey, mutationFn, onMutate, onSettled, patch, ...rest } = options; |
Comment on lines
+12
to
+18
| mutationFn: (...args: any) => { | ||
| const [variables, { client }] = args; | ||
| const previousSettings = client.getQueryData(invalidateKey); | ||
| client.setQueryData(invalidateKey, patch ? { ...previousSettings, ...variables } : variables); | ||
|
|
||
| return mutationFn?.(...args); | ||
| }, |
Comment on lines
+20
to
+25
| onMutate: (...args: any) => { | ||
| const [_, { client }] = args; | ||
| const previous = client.getQueryData(invalidateKey); | ||
| const result = onMutate?.(...args) || {}; | ||
| return { _previous: previous, ...result }; | ||
| }, |
Comment on lines
+26
to
+35
| onSettled: (...args: any) => { | ||
| const [data, error, variables, { _previous }, { client }] = args; | ||
| if (error && _previous) { | ||
| // rollback first | ||
| client.setQueryData(invalidateKey, _previous); | ||
| } | ||
| onSettled?.(...args); | ||
| // Always refetch after error or success: | ||
| client.invalidateQueries({ queryKey: invalidateKey }); | ||
| }, |
Comment on lines
+42
to
+56
| export function useSettingsStore(selector?: (settings: Settings) => any) { | ||
| const { data: settings, ...rest } = useQuery({ | ||
| queryKey: SETTINGS_STORE_KEY, | ||
| queryFn: async () => { | ||
| const settings = await services.settings.get(); | ||
| return settings; | ||
| }, | ||
| selector, | ||
| }); | ||
|
|
||
| // computed | ||
| const httpProxyHasCredentials = settings?.httpProxy?.includes('@'); | ||
| const isProxyConfigured = Boolean(settings?.proxyEnabled && (settings.httpProxy || settings.httpsProxy)); | ||
| const hasCustomPluginPath = Boolean(settings?.pluginPath); | ||
| const hasDataFolderRestrictions = Boolean(settings?.dataFolders && settings.dataFolders.length > 0); |
Comment on lines
+58
to
+68
| const { mutateResult, ...mutation } = useOptimisticMutation({ | ||
| mutationFn: async (patch: Partial<Settings>, { client }) => { | ||
| const updatedSettings = await services.settings.patch(patch); | ||
| client.setQueryData(SETTINGS_STORE_KEY, updatedSettings); | ||
| if ('enableAnalytics' in patch && !patch.enableAnalytics) { | ||
| window.main.trackAnalyticsEvent({ event: AnalyticsEvent.analyticsDisabled }); | ||
| } | ||
| return updatedSettings; | ||
| }, | ||
| invalidateKey: SETTINGS_STORE_KEY, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.