@@ -128,18 +128,30 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
128128// updateProxy is a helper function that when called will
129129// update the proxy being used.
130130const updateProxy = useCallback ( ( ) => {
131+ const userProxy = loadUserSelectedProxy ( ) ;
131132// Update the saved user proxy for the caller.
132- setUserSavedProxy ( loadUserSelectedProxy ( ) ) ;
133- setProxy (
134- getPreferredProxy (
135- proxiesResp ?? [ ] ,
136- loadUserSelectedProxy ( ) ,
137- proxyLatencies ,
138- // Do not auto select based on latencies, as inconsistent latencies can cause this
139- // to behave poorly.
140- false ,
141- ) ,
142- ) ;
133+ setUserSavedProxy ( userProxy ) ;
134+
135+ // preferred proxy is the proxy that will be used.
136+ // 1. It first selects from the user selected proxy.
137+ // 2. If no user selected proxy is found, it will select
138+ // the best proxy based on latency.
139+ // 2a. The auto select is saved to local storage. So latency changes will not change
140+ // the selected proxy, unless the user manually changes it from the auto selected.
141+ const preferred = getPreferredProxy (
142+ proxiesResp ?? [ ] ,
143+ userProxy ,
144+ proxyLatencies ,
145+ // Autoselect iff the user has not selected a proxy yet. We will save this to local storage.
146+ // If the user disagrees with the auto selected proxy, they can always change it.
147+ true ,
148+ )
149+
150+ if ( userProxy === undefined ) {
151+ setUserSavedProxy ( preferred . proxy ) ;
152+ }
153+
154+ setProxy ( preferred ) ;
143155} , [ proxiesResp , proxyLatencies ] ) ;
144156
145157// This useEffect ensures the proxy to be used is updated whenever the state changes.