Skip to content

Commit

Permalink
ProxyGroup ProxyProvider app index
Browse files Browse the repository at this point in the history
  • Loading branch information
haishanh committed Oct 3, 2023
1 parent a1dd35a commit 57f9d2f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 37 deletions.
20 changes: 10 additions & 10 deletions src/components/proxies/ProxyGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Tooltip } from '@reach/tooltip';
import { useAtom } from 'jotai';
import * as React from 'react';

import Button from '$src/components/Button';
import CollapsibleSectionHeader from '$src/components/CollapsibleSectionHeader';
import { ZapAnimated } from '$src/components/shared/ZapAnimated';
import { connect, useStoreActions } from '$src/components/StateProvider';
import { useState2 } from '$src/hooks/basic';
import { getCollapsibleIsOpen, getHideUnavailableProxies, getProxySortBy } from '$src/store/app';
import { collapsibleIsOpenAtom, getHideUnavailableProxies, getProxySortBy } from '$src/store/app';
import { getProxies, switchProxy } from '$src/store/proxies';
import { DelayMapping, DispatchFn, ProxiesMapping, State } from '$src/store/types';
import { ClashAPIConfig } from '$src/types';
Expand All @@ -26,7 +27,6 @@ type ProxyGroupImplProps = {
proxies: ProxiesMapping;
type: string;
now: string;
isOpen: boolean;
apiConfig: ClashAPIConfig;
dispatch: DispatchFn;
};
Expand All @@ -40,19 +40,22 @@ function ProxyGroupImpl({
proxies,
type,
now,
isOpen,
apiConfig,
dispatch,
}: ProxyGroupImplProps) {
const [collapsibleIsOpen, setCollapsibleIsOpen] = useAtom(collapsibleIsOpenAtom);
const isOpen = collapsibleIsOpen[`proxyGroup:${name}`];
const all = useFilteredAndSorted(allItems, delay, hideUnavailableProxies, proxySortBy, proxies);

const isSelectable = useMemo(() => type === 'Selector', [type]);

const {
app: { updateCollapsibleIsOpen },
proxies: { requestDelayForProxies },
} = useStoreActions();

const updateCollapsibleIsOpen = useCallback(
(prefix: string, name: string, v: boolean) => {
setCollapsibleIsOpen((s) => ({ ...s, [`${prefix}:${name}`]: v }));
},
[setCollapsibleIsOpen],
);
const toggle = useCallback(() => {
updateCollapsibleIsOpen('proxyGroup', name, !isOpen);
}, [isOpen, updateCollapsibleIsOpen, name]);
Expand Down Expand Up @@ -105,10 +108,8 @@ function ProxyGroupImpl({

export const ProxyGroup = connect((s: State, { name, delay }) => {
const proxies = getProxies(s);
const collapsibleIsOpen = getCollapsibleIsOpen(s);
const proxySortBy = getProxySortBy(s);
const hideUnavailableProxies = getHideUnavailableProxies(s);

const group = proxies[name];
const { all, type, now } = group;
return {
Expand All @@ -119,6 +120,5 @@ export const ProxyGroup = connect((s: State, { name, delay }) => {
proxies,
type,
now,
isOpen: collapsibleIsOpen[`proxyGroup:${name}`],
};
})(ProxyGroupImpl);
22 changes: 11 additions & 11 deletions src/components/proxies/ProxyProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Tooltip } from '@reach/tooltip';
import { formatDistance } from 'date-fns';
import { useAtom } from 'jotai';
import * as React from 'react';
import { RotateCw } from 'react-feather';
import Button from 'src/components/Button';
Expand All @@ -8,7 +9,7 @@ import { useUpdateProviderItem } from 'src/components/proxies/proxies.hooks';
import { connect, useStoreActions } from 'src/components/StateProvider';

Check failure on line 9 in src/components/proxies/ProxyProvider.tsx

View workflow job for this annotation

GitHub Actions / install (20.x)

'useStoreActions' is defined but never used
import { framerMotionResource } from 'src/misc/motion';
import {
getCollapsibleIsOpen,
collapsibleIsOpenAtom,
getHideUnavailableProxies,
getProxySortBy,
useApiConfig,
Expand All @@ -35,7 +36,6 @@ type Props = {
vehicleType: 'HTTP' | 'File' | 'Compatible';
updatedAt?: string;
dispatch: (x: any) => Promise<any>;
isOpen: boolean;
};

function ProxyProviderImpl({
Expand All @@ -46,9 +46,10 @@ function ProxyProviderImpl({
proxySortBy,
vehicleType,
updatedAt,
isOpen,
dispatch,
}: Props) {
const [collapsibleIsOpen, setCollapsibleIsOpen] = useAtom(collapsibleIsOpenAtom);
const isOpen = collapsibleIsOpen[`proxyProvider:${name}`];
const apiConfig = useApiConfig();
const proxies = useFilteredAndSorted(all, delay, hideUnavailableProxies, proxySortBy);
const checkingHealth = useState2(false);
Expand All @@ -61,11 +62,12 @@ function ProxyProviderImpl({
const stop = () => checkingHealth.set(false);
dispatch(healthcheckProviderByName(apiConfig, name)).then(stop, stop);
}, [apiConfig, dispatch, name, checkingHealth]);

const {
app: { updateCollapsibleIsOpen },
} = useStoreActions();

const updateCollapsibleIsOpen = useCallback(
(prefix: string, name: string, v: boolean) => {
setCollapsibleIsOpen((s) => ({ ...s, [`${prefix}:${name}`]: v }));
},
[setCollapsibleIsOpen],
);
const toggle = useCallback(() => {
updateCollapsibleIsOpen('proxyProvider', name, !isOpen);
}, [isOpen, updateCollapsibleIsOpen, name]);
Expand Down Expand Up @@ -129,17 +131,15 @@ function Refresh() {
);
}

const mapState = (s: State, { proxies, name }) => {
const mapState = (s: State, { proxies }) => {
const hideUnavailableProxies = getHideUnavailableProxies(s);
const delay = getDelay(s);
const collapsibleIsOpen = getCollapsibleIsOpen(s);
const proxySortBy = getProxySortBy(s);
return {
proxies,
delay,
hideUnavailableProxies,
proxySortBy,
isOpen: collapsibleIsOpen[`proxyProvider:${name}`],
};
};

Expand Down
14 changes: 1 addition & 13 deletions src/store/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const selectedClashAPIConfigIndexAtom = atom<number>(initialState().selec
export const clashAPIConfigsAtom = atom<ClashAPIConfigWithAddedAt[]>(initialState().clashAPIConfigs);
export const selectedChartStyleIndexAtom = atom(initialState().selectedChartStyleIndex);
export const latencyTestUrlAtom = atom(initialState().latencyTestUrl);
export const collapsibleIsOpenAtom = atom(initialState().collapsibleIsOpen);

// hooks

Expand All @@ -66,14 +67,11 @@ export function useApiConfig() {
}

export const getTheme = (s: State) => s.app.theme;
export const getCollapsibleIsOpen = (s: State) => s.app.collapsibleIsOpen;
export const getProxySortBy = (s: State) => s.app.proxySortBy;
export const getHideUnavailableProxies = (s: State) => s.app.hideUnavailableProxies;
export const getAutoCloseOldConns = (s: State) => s.app.autoCloseOldConns;
export const getLogStreamingPaused = (s: State) => s.app.logStreamingPaused;

const saveStateDebounced = debounce(saveState, 600);

export function findClashAPIConfigIndexTmp(
arr: ClashAPIConfigWithAddedAt[],
{ baseURL, secret, metaLabel }: ClashAPIConfig,
Expand Down Expand Up @@ -172,16 +170,6 @@ export function updateAppConfig(name: string, value: unknown) {
};
}

export function updateCollapsibleIsOpen(prefix: string, name: string, v: boolean) {
return (dispatch: DispatchFn, getState: GetStateFn) => {
dispatch('updateCollapsibleIsOpen', (s: State) => {
s.app.collapsibleIsOpen[`${prefix}:${name}`] = v;
});
// side effect
saveStateDebounced(getState().app);
};
}

function parseConfigQueryString() {
const { search } = window.location;
const collector: Record<string, string> = {};
Expand Down
3 changes: 0 additions & 3 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
initialState as app,
updateAppConfig,
updateCollapsibleIsOpen,
} from './app';
import { initialState as configs } from './configs';
import { initialState as logs } from './logs';
Expand All @@ -18,9 +17,7 @@ export const initialState = {

export const actions = {
updateAppConfig,

app: {
updateCollapsibleIsOpen,
updateAppConfig,
},
proxies: proxiesActions,
Expand Down

0 comments on commit 57f9d2f

Please sign in to comment.