From 05bbb54df2f25545c8d2eeeffcab436527e28f01 Mon Sep 17 00:00:00 2001 From: lohit-bruno Date: Tue, 3 Mar 2026 12:58:21 +0530 Subject: [PATCH] refactor(cache): replace window.ipcRenderer calls with redux actions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add getCacheStats, purgeCache, and clearHttpHttpsAgentCache thunks to the app slice. Update the Cache preferences component to dispatch these actions instead of calling window.ipcRenderer directly. Also move handleSave and handleSaveRef above useFormik to fix declaration order — onSubmit closes over handleSaveRef, so the ref must be initialized before useFormik is called. --- .../src/components/Preferences/Cache/index.js | 43 +++++++++++-------- .../src/providers/ReduxStore/slices/app.js | 21 +++++++++ 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/packages/bruno-app/src/components/Preferences/Cache/index.js b/packages/bruno-app/src/components/Preferences/Cache/index.js index 57e1ae49f..ab369b824 100644 --- a/packages/bruno-app/src/components/Preferences/Cache/index.js +++ b/packages/bruno-app/src/components/Preferences/Cache/index.js @@ -1,7 +1,10 @@ -import React, { useEffect, useCallback } from 'react'; +import React, { useEffect, useCallback, useRef } from 'react'; import { useFormik } from 'formik'; import { useSelector, useDispatch } from 'react-redux'; -import { savePreferences } from 'providers/ReduxStore/slices/app'; +import { + savePreferences, + clearHttpHttpsAgentCache +} from 'providers/ReduxStore/slices/app'; import toast from 'react-hot-toast'; import StyledWrapper from './StyledWrapper'; import * as Yup from 'yup'; @@ -18,6 +21,21 @@ const Cache = () => { const preferences = useSelector((state) => state.app.preferences); const dispatch = useDispatch(); + const handleSave = useCallback( + (newCachePreferences) => { + dispatch( + savePreferences({ + ...preferences, + cache: newCachePreferences + }) + ).catch(() => toast.error('Failed to update cache preferences')); + }, + [dispatch, preferences] + ); + + const handleSaveRef = useRef(handleSave); + handleSaveRef.current = handleSave; + const formik = useFormik({ initialValues: { httpHttpsAgents: { @@ -35,26 +53,14 @@ const Cache = () => { } }); - const handleSave = useCallback( - (newCachePreferences) => { - dispatch( - savePreferences({ - ...preferences, - cache: newCachePreferences - }) - ).catch(() => toast.error('Failed to update cache preferences')); - }, - [dispatch, preferences] - ); - const debouncedSave = useCallback( debounce((values) => { cacheSchema .validate(values, { abortEarly: true }) - .then((validatedValues) => handleSave(validatedValues)) + .then((validatedValues) => handleSaveRef.current(validatedValues)) .catch(() => {}); }, 500), - [handleSave] + [] ); useEffect(() => { @@ -70,13 +76,12 @@ const Cache = () => { formik.handleChange(e); // Immediately evict all cached agents when caching is disabled if (!e.target.checked) { - window.ipcRenderer.invoke('renderer:clear-http-https-agent-cache').catch(() => {}); + dispatch(clearHttpHttpsAgentCache()).catch(() => {}); } }; const handleResetCache = () => { - window.ipcRenderer - .invoke('renderer:clear-http-https-agent-cache') + dispatch(clearHttpHttpsAgentCache()) .then(() => toast.success('Agent cache cleared')) .catch(() => toast.error('Failed to clear agent cache')); }; diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/app.js b/packages/bruno-app/src/providers/ReduxStore/slices/app.js index 3474187ef..a674c7968 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/app.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/app.js @@ -306,4 +306,25 @@ export const refreshSystemProxy = () => (dispatch, getState) => { }); }; +export const getCacheStats = () => () => { + return new Promise((resolve, reject) => { + const { ipcRenderer } = window; + ipcRenderer.invoke('renderer:get-cache-stats').then(resolve).catch(reject); + }); +}; + +export const purgeCache = () => () => { + return new Promise((resolve, reject) => { + const { ipcRenderer } = window; + ipcRenderer.invoke('renderer:purge-cache').then(resolve).catch(reject); + }); +}; + +export const clearHttpHttpsAgentCache = () => () => { + return new Promise((resolve, reject) => { + const { ipcRenderer } = window; + ipcRenderer.invoke('renderer:clear-http-https-agent-cache').then(resolve).catch(reject); + }); +}; + export default appSlice.reducer;