|
1 | | -import React, { useEffect, useCallback, useRef } from 'react'; |
2 | | -import { useFormik } from 'formik'; |
| 1 | +import React, { useEffect, useState, useCallback } from 'react'; |
3 | 2 | import { useSelector, useDispatch } from 'react-redux'; |
4 | | -import { |
5 | | - savePreferences, |
6 | | - clearHttpHttpsAgentCache |
7 | | -} from 'providers/ReduxStore/slices/app'; |
| 3 | +import { savePreferences, clearHttpHttpsAgentCache } from 'providers/ReduxStore/slices/app'; |
8 | 4 | import toast from 'react-hot-toast'; |
9 | | -import StyledWrapper from './StyledWrapper'; |
10 | | -import * as Yup from 'yup'; |
11 | | -import debounce from 'lodash/debounce'; |
12 | 5 | import get from 'lodash/get'; |
13 | | - |
14 | | -const cacheSchema = Yup.object().shape({ |
15 | | - sslSession: Yup.object({ |
16 | | - enabled: Yup.boolean() |
17 | | - }) |
18 | | -}); |
| 6 | +import { IconEraser } from '@tabler/icons'; |
| 7 | +import { useTheme } from 'providers/Theme'; |
| 8 | +import ToggleSwitch from 'components/ToggleSwitch'; |
| 9 | +import ActionIcon from 'ui/ActionIcon'; |
| 10 | +import StyledWrapper from './StyledWrapper'; |
| 11 | +import { formatSize } from 'utils/common'; |
19 | 12 |
|
20 | 13 | const Cache = () => { |
21 | 14 | const preferences = useSelector((state) => state.app.preferences); |
22 | 15 | const dispatch = useDispatch(); |
| 16 | + const { theme } = useTheme(); |
| 17 | + const { ipcRenderer } = window; |
23 | 18 |
|
24 | | - const handleSave = useCallback( |
25 | | - (newCachePreferences) => { |
26 | | - dispatch( |
27 | | - savePreferences({ |
28 | | - ...preferences, |
29 | | - cache: newCachePreferences |
30 | | - }) |
31 | | - ).catch(() => toast.error('Failed to update cache preferences')); |
32 | | - }, |
33 | | - [dispatch, preferences] |
34 | | - ); |
35 | | - |
36 | | - const handleSaveRef = useRef(handleSave); |
37 | | - handleSaveRef.current = handleSave; |
| 19 | + const fileCacheEnabled = get(preferences, 'cache.file.enabled', false); |
| 20 | + const sslSessionEnabled = get(preferences, 'cache.sslSession.enabled', false); |
38 | 21 |
|
39 | | - const formik = useFormik({ |
40 | | - initialValues: { |
41 | | - sslSession: { |
42 | | - enabled: get(preferences, 'cache.sslSession.enabled', false) |
43 | | - } |
44 | | - }, |
45 | | - validationSchema: cacheSchema, |
46 | | - onSubmit: async (values) => { |
47 | | - try { |
48 | | - const newPreferences = await cacheSchema.validate(values, { abortEarly: true }); |
49 | | - handleSave(newPreferences); |
50 | | - } catch (error) { |
51 | | - console.error('Cache preferences validation error:', error.message); |
52 | | - } |
53 | | - } |
54 | | - }); |
| 22 | + const [fileCacheSize, setFileCacheSize] = useState(null); |
55 | 23 |
|
56 | | - const debouncedSave = useCallback( |
57 | | - debounce((values) => { |
58 | | - cacheSchema |
59 | | - .validate(values, { abortEarly: true }) |
60 | | - .then((validatedValues) => handleSaveRef.current(validatedValues)) |
61 | | - .catch(() => {}); |
62 | | - }, 500), |
63 | | - [] |
64 | | - ); |
| 24 | + const refreshFileCacheSize = useCallback(() => { |
| 25 | + if (!ipcRenderer) return; |
| 26 | + ipcRenderer |
| 27 | + .invoke('renderer:get-file-cache-size') |
| 28 | + .then((size) => setFileCacheSize(size)) |
| 29 | + .catch(() => setFileCacheSize(null)); |
| 30 | + }, [ipcRenderer]); |
65 | 31 |
|
66 | 32 | useEffect(() => { |
67 | | - if (formik.dirty && formik.isValid) { |
68 | | - debouncedSave(formik.values); |
69 | | - } |
70 | | - return () => { |
71 | | - debouncedSave.flush(); |
72 | | - }; |
73 | | - }, [formik.values, formik.dirty, formik.isValid, debouncedSave]); |
| 33 | + refreshFileCacheSize(); |
| 34 | + }, [refreshFileCacheSize, fileCacheEnabled]); |
74 | 35 |
|
75 | | - const handleAgentCachingChange = (e) => { |
76 | | - formik.handleChange(e); |
77 | | - // Immediately evict all cached agents when caching is disabled |
78 | | - if (!e.target.checked) { |
| 36 | + const persist = (next) => { |
| 37 | + dispatch(savePreferences({ ...preferences, cache: next })).catch(() => { |
| 38 | + toast.error('Failed to update cache preferences'); |
| 39 | + }); |
| 40 | + }; |
| 41 | + |
| 42 | + const handleToggleFileCache = () => { |
| 43 | + persist({ |
| 44 | + ...preferences.cache, |
| 45 | + file: { enabled: !fileCacheEnabled } |
| 46 | + }); |
| 47 | + }; |
| 48 | + |
| 49 | + const handleToggleSslSession = () => { |
| 50 | + const next = !sslSessionEnabled; |
| 51 | + persist({ |
| 52 | + ...preferences.cache, |
| 53 | + sslSession: { enabled: next } |
| 54 | + }); |
| 55 | + if (!next) { |
79 | 56 | dispatch(clearHttpHttpsAgentCache()).catch(() => {}); |
80 | 57 | } |
81 | 58 | }; |
82 | 59 |
|
83 | | - const handleResetCache = () => { |
| 60 | + const handleClearFileCache = () => { |
| 61 | + if (!ipcRenderer) return; |
| 62 | + ipcRenderer |
| 63 | + .invoke('renderer:clear-file-cache') |
| 64 | + .then((size) => { |
| 65 | + setFileCacheSize(size); |
| 66 | + toast.success('File cache cleared'); |
| 67 | + }) |
| 68 | + .catch(() => toast.error('Failed to clear file cache')); |
| 69 | + }; |
| 70 | + |
| 71 | + const handleClearSslSession = () => { |
84 | 72 | dispatch(clearHttpHttpsAgentCache()) |
85 | | - .then(() => toast.success('ssl session cache cleared')) |
86 | | - .catch(() => toast.error('Failed to clear ssl session cache')); |
| 73 | + .then(() => toast.success('SSL session cache cleared')) |
| 74 | + .catch(() => toast.error('Failed to clear SSL session cache')); |
87 | 75 | }; |
88 | 76 |
|
89 | 77 | return ( |
90 | 78 | <StyledWrapper className="w-full"> |
91 | | - <form className="bruno-form" onSubmit={formik.handleSubmit}> |
92 | | - <div className="section-title mt-6 mb-3">Cache SSL Session</div> |
| 79 | + <div className="cache-section-title">Cache</div> |
93 | 80 |
|
94 | | - <div className="flex items-center my-2"> |
95 | | - <input |
96 | | - id="sslSession.enabled" |
97 | | - type="checkbox" |
98 | | - name="sslSession.enabled" |
99 | | - checked={formik.values.sslSession.enabled} |
100 | | - onChange={handleAgentCachingChange} |
101 | | - className="mousetrap mr-0" |
| 81 | + <div className="cache-item"> |
| 82 | + <div className="cache-item-header"> |
| 83 | + <div className="cache-item-title-group"> |
| 84 | + <span className="cache-item-title">File cache</span> |
| 85 | + <span className="beta-badge">Beta</span> |
| 86 | + </div> |
| 87 | + <ToggleSwitch |
| 88 | + data-testid="cache.file.enabled" |
| 89 | + isOn={fileCacheEnabled} |
| 90 | + handleToggle={handleToggleFileCache} |
| 91 | + size="2xs" |
| 92 | + activeColor={theme.primary.solid} |
102 | 93 | /> |
103 | | - <label className="block ml-2 select-none" htmlFor="sslSession.enabled"> |
104 | | - Enable SSL session caching |
105 | | - </label> |
106 | 94 | </div> |
107 | | - <div className="text-xs mt-1 ml-6 opacity-70"> |
108 | | - Reuses TLS sessions and connections across requests for faster handshakes. Disable to create a fresh connection for every |
109 | | - request. |
| 95 | + <div className="cache-item-body"> |
| 96 | + <div className="cache-item-body-text"> |
| 97 | + <p className="cache-item-description"> |
| 98 | + Loads your workspace faster by caching opened collections. Bruno refreshes the cache when your collection |
| 99 | + changes. Clearing it won't affect your original files. |
| 100 | + </p> |
| 101 | + <p className="cache-item-size"> |
| 102 | + Cache size <strong>{fileCacheSize == null ? '—' : formatSize(fileCacheSize)}</strong> |
| 103 | + </p> |
| 104 | + </div> |
| 105 | + <ActionIcon |
| 106 | + label="Clear cache" |
| 107 | + onClick={handleClearFileCache} |
| 108 | + disabled={!fileCacheSize} |
| 109 | + colorOnHover={theme.colors.text.danger} |
| 110 | + > |
| 111 | + <IconEraser size={16} strokeWidth={1.5} /> |
| 112 | + </ActionIcon> |
110 | 113 | </div> |
| 114 | + </div> |
111 | 115 |
|
112 | | - <div className="mt-6"> |
113 | | - <button type="button" className="text-link cursor-pointer hover:underline" onClick={handleResetCache}> |
114 | | - Clear |
115 | | - </button> |
| 116 | + <div className="cache-item"> |
| 117 | + <div className="cache-item-header"> |
| 118 | + <div className="cache-item-title-group"> |
| 119 | + <span className="cache-item-title">SSL session cache</span> |
| 120 | + </div> |
| 121 | + <ToggleSwitch |
| 122 | + data-testid="sslSession.enabled" |
| 123 | + isOn={sslSessionEnabled} |
| 124 | + handleToggle={handleToggleSslSession} |
| 125 | + size="2xs" |
| 126 | + activeColor={theme.primary.solid} |
| 127 | + /> |
| 128 | + </div> |
| 129 | + <div className="cache-item-body"> |
| 130 | + <div className="cache-item-body-text"> |
| 131 | + <p className="cache-item-description"> |
| 132 | + Reuses TLS sessions and connections across requests for faster handshakes. Disable to create a fresh |
| 133 | + connection for every request. |
| 134 | + </p> |
| 135 | + </div> |
| 136 | + <ActionIcon |
| 137 | + label="Clear cache" |
| 138 | + onClick={handleClearSslSession} |
| 139 | + colorOnHover={theme.colors.text.danger} |
| 140 | + > |
| 141 | + <IconEraser size={16} strokeWidth={1.5} /> |
| 142 | + </ActionIcon> |
116 | 143 | </div> |
117 | | - </form> |
| 144 | + </div> |
118 | 145 | </StyledWrapper> |
119 | 146 | ); |
120 | 147 | }; |
|
0 commit comments