refactor(cache): replace window.ipcRenderer calls with redux actions

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.
This commit is contained in:
lohit-bruno
2026-03-03 12:58:21 +05:30
parent 795fb08d1f
commit 05bbb54df2
2 changed files with 45 additions and 19 deletions

View File

@@ -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'));
};

View File

@@ -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;