diff --git a/src/containers/Proxies/components/Group/index.tsx b/src/containers/Proxies/components/Group/index.tsx index 1c670ea..22e3005 100644 --- a/src/containers/Proxies/components/Group/index.tsx +++ b/src/containers/Proxies/components/Group/index.tsx @@ -10,14 +10,14 @@ interface GroupProps { } export function Group (props: GroupProps) { - const { update } = useProxy() + const { markProxySelected } = useProxy() const proxyMap = useRecoilValue(proxyMapping) const { data: Config } = useConfig() const { config } = props async function handleChangeProxySelected (name: string) { await changeProxySelected(props.config.name, name) - await update() + markProxySelected(props.config.name, name) if (Config.breakConnections) { const list: string[] = [] const snapshot = await getConnections() diff --git a/src/stores/recoil.ts b/src/stores/recoil.ts index 4766b9e..49115f5 100644 --- a/src/stores/recoil.ts +++ b/src/stores/recoil.ts @@ -170,7 +170,7 @@ export const proxies = atom({ export function useProxy () { const [allProxy, set] = useRecoilObjectWithImmer(proxies) - const { data, mutate } = swr('/proxies', async () => { + const { mutate } = swr(['/proxies', set], async () => { const allProxies = await API.getProxies() const global = allProxies.data.proxies.GLOBAL as API.Group @@ -183,15 +183,25 @@ export function useProxy () { .filter(key => !unUsedProxy.has(key)) .map(key => ({ ...allProxies.data.proxies[key], name: key })) const [proxy, groups] = partition(proxies, proxy => !policyGroup.has(proxy.type)) - return { proxies: proxy as API.Proxy[], groups: groups as API.Group[], global: global } + set({ proxies: proxy as API.Proxy[], groups: groups as API.Group[], global: global }) }) - useEffect(() => data && set(data), [data, set]) + + const markProxySelected = useCallback((name: string, selected: string) => { + set(draft => { + for (const group of draft.groups) { + if (group.name === name) { + group.now = selected + } + } + }) + }, [set]) return { proxies: allProxy.proxies, groups: allProxy.groups, global: allProxy.global, update: mutate, + markProxySelected, set } }