1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
import React from 'react';
import { ChevronDown, RotateCw, Zap } from 'react-feather';
import { formatDistance } from 'date-fns';
import ResizeObserver from 'resize-observer-polyfill';
import { motion } from 'framer-motion';
import cx from 'classnames';
import { connect } from './StateProvider';
import { SectionNameType } from './shared/Basic';
import { ProxyList, ProxyListSummaryView } from './ProxyGroup';
import Button from './Button';
import { getClashAPIConfig } from '../store/app';
import {
updateProviderByName,
healthcheckProviderByName
} from '../store/proxies';
import s from './ProxyProvider.module.css';
const { memo, useState, useRef, useEffect, useCallback } = React;
type Props = {
item: Array<{
name: string,
proxies: Array<string>,
type: 'Proxy' | 'Rule',
vehicleType: 'HTTP' | 'File' | 'Compatible',
updatedAt?: string
}>,
proxies: {
[string]: any
},
dispatch: any => void
};
function ProxyProvider({ item, dispatch, apiConfig }: Props) {
const [isHealthcheckLoading, setIsHealthcheckLoading] = useState(false);
const updateProvider = useCallback(
() => dispatch(updateProviderByName(apiConfig, item.name)),
[apiConfig, dispatch, item.name]
);
const healthcheckProvider = useCallback(async () => {
setIsHealthcheckLoading(true);
await dispatch(healthcheckProviderByName(apiConfig, item.name));
setIsHealthcheckLoading(false);
}, [apiConfig, dispatch, item.name, setIsHealthcheckLoading]);
const [isCollapsibleOpen, setCollapsibleOpen] = useState(false);
const toggle = useCallback(() => setCollapsibleOpen(x => !x), []);
const timeAgo = formatDistance(new Date(item.updatedAt), new Date());
return (
<div className={s.body}>
<div className={s.header} onClick={toggle}>
<SectionNameType name={item.name} type={item.vehicleType} />
<Button kind="minimal">
<span className={cx(s.arrow, { [s.isOpen]: isCollapsibleOpen })}>
<ChevronDown />
</span>
</Button>
</div>
<div className={s.updatedAt}>
<small>Updated {timeAgo} ago</small>
</div>
<Collapsible2 isOpen={isCollapsibleOpen}>
<ProxyList all={item.proxies} />
<div className={s.actionFooter}>
<Button text="Update" start={<Refresh />} onClick={updateProvider} />
<Button
text="Health Check"
start={<Zap size={16} />}
onClick={healthcheckProvider}
isLoading={isHealthcheckLoading}
/>
</div>
</Collapsible2>
<Collapsible2 isOpen={!isCollapsibleOpen}>
<ProxyListSummaryView all={item.proxies} />
</Collapsible2>
</div>
);
}
const button = {
rest: { scale: 1 },
// hover: { scale: 1.1 },
pressed: { scale: 0.95 }
};
const arrow = {
rest: { rotate: 0 },
hover: { rotate: 360, transition: { duration: 0.3 } }
};
function Refresh() {
return (
<motion.div
className={s.refresh}
variants={button}
initial="rest"
whileHover="hover"
whileTap="pressed"
>
<motion.div className="flexCenter" variants={arrow}>
<RotateCw size={16} />
</motion.div>
</motion.div>
);
}
function usePrevious(value) {
const ref = useRef();
useEffect(() => void (ref.current = value), [value]);
return ref.current;
}
function useMeasure() {
const ref = useRef();
const [bounds, set] = useState({ height: 0 });
useEffect(() => {
const ro = new ResizeObserver(([entry]) => set(entry.contentRect));
if (ref.current) ro.observe(ref.current);
return () => ro.disconnect();
}, []);
return [ref, bounds];
}
const variantsCollpapsibleWrap = {
initialOpen: {
height: 'auto',
transition: { duration: 0 }
},
open: height => ({
height,
opacity: 1,
visibility: 'visible',
transition: { duration: 0.3 }
}),
closed: {
height: 0,
opacity: 0,
visibility: 'hidden',
transition: { duration: 0.3 }
}
};
const variantsCollpapsibleChildContainer = {
open: {
x: 0
},
closed: {
x: 20
}
};
const Collapsible2 = memo(({ children, isOpen }) => {
const previous = usePrevious(isOpen);
const [refToMeature, { height }] = useMeasure();
return (
<div>
<motion.div
animate={
isOpen && previous === isOpen
? 'initialOpen'
: isOpen
? 'open'
: 'closed'
}
custom={height}
variants={variantsCollpapsibleWrap}
>
<motion.div
variants={variantsCollpapsibleChildContainer}
ref={refToMeature}
>
{children}
</motion.div>
</motion.div>
</div>
);
});
const mapState = s => ({
apiConfig: getClashAPIConfig(s)
});
export default connect(mapState)(ProxyProvider);
|