Dashboard.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import React, { useState, useMemo } from 'react';
  2. import {
  3. Table, Button, Space, message, Layout, Typography,
  4. Card, Tooltip, Badge, Row, Col, Statistic, Dropdown, Avatar
  5. } from 'antd';
  6. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  7. import {
  8. PlayCircleFilled,
  9. PauseCircleFilled,
  10. ReloadOutlined,
  11. SettingOutlined,
  12. CloudUploadOutlined,
  13. FileTextOutlined,
  14. PlusOutlined,
  15. MoreOutlined,
  16. GlobalOutlined,
  17. UserOutlined,
  18. AppstoreOutlined,
  19. ThunderboltFilled
  20. } from '@ant-design/icons';
  21. // API & Components
  22. import { getStatus, startGroup, stopGroup, restartGroup } from '../api';
  23. import ConfigModal from '../components/ConfigModal';
  24. import UpgradeModal from '../components/UpgradeModal';
  25. import LogViewer from '../components/LogViewer';
  26. import CreateGroupModal from '../components/CreateGroupModal';
  27. const { Header, Content } = Layout;
  28. const { Title, Text } = Typography;
  29. const Dashboard = () => {
  30. const queryClient = useQueryClient();
  31. // State
  32. const [configGroupId, setConfigGroupId] = useState(null);
  33. const [logGroupId, setLogGroupId] = useState(null);
  34. const [isUpgradeOpen, setIsUpgradeOpen] = useState(false);
  35. const [isCreateOpen, setIsCreateOpen] = useState(false);
  36. // 1. Data Fetching
  37. const { data: statusResp, isLoading } = useQuery({
  38. queryKey: ['status'],
  39. queryFn: getStatus,
  40. refetchInterval: 3000,
  41. refetchOnWindowFocus: true,
  42. });
  43. const dataSource = statusResp?.data || [];
  44. // 2. Statistics Calculation
  45. const stats = useMemo(() => {
  46. const total = dataSource.length;
  47. const running = dataSource.filter(d => d.running).length;
  48. const stopped = total - running;
  49. const totalInstances = dataSource.reduce((acc, curr) => acc + (curr.instances || 0), 0);
  50. return { total, running, stopped, totalInstances };
  51. }, [dataSource]);
  52. // 3. Actions
  53. const actionMutation = useMutation({
  54. mutationFn: ({ fn, id }) => fn(id),
  55. onSuccess: () => {
  56. message.success('Command sent successfully');
  57. queryClient.invalidateQueries(['status']);
  58. },
  59. onError: (err) => message.error('Operation failed: ' + err.message),
  60. });
  61. const handleAction = (fn, id) => actionMutation.mutate({ fn, id });
  62. // 4. Columns Definition
  63. const columns = [
  64. {
  65. title: 'Identity',
  66. key: 'identity',
  67. width: 220,
  68. render: (_, record) => (
  69. <Space>
  70. <Avatar shape="square" size="large" icon={<AppstoreOutlined />} style={{ backgroundColor: record.running ? '#e6f7ff' : '#f5f5f5', color: record.running ? '#1890ff' : '#ccc' }} />
  71. <div style={{ display: 'flex', flexDirection: 'column' }}>
  72. <Text strong style={{ fontSize: '15px' }}>{record.id}</Text>
  73. <Text type="secondary" style={{ fontSize: '12px' }}>{record.plugin}</Text>
  74. </div>
  75. </Space>
  76. )
  77. },
  78. {
  79. title: 'Status',
  80. key: 'status',
  81. width: 120,
  82. render: (_, record) => (
  83. <Badge
  84. status={record.running ? "processing" : "default"}
  85. text={
  86. <span style={{
  87. color: record.running ? '#52c41a' : '#999',
  88. fontWeight: 500
  89. }}>
  90. {record.running ? 'Running' : 'Stopped'}
  91. </span>
  92. }
  93. />
  94. ),
  95. },
  96. {
  97. title: 'Target',
  98. dataIndex: 'instances',
  99. key: 'instances',
  100. width: 100,
  101. align: 'center',
  102. render: (val) => <TagPill value={val} label="Inst" />
  103. },
  104. {
  105. title: 'Resources',
  106. key: 'pools',
  107. render: (_, record) => (
  108. <Space direction="vertical" size={0}>
  109. <Space size={4}>
  110. <UserOutlined style={{ color: '#8c8c8c', fontSize: '12px' }} />
  111. <Text style={{ fontSize: '13px', color: record.local_account_pool ? '#595959' : '#d9d9d9' }}>
  112. {record.local_account_pool || 'N/A'}
  113. </Text>
  114. </Space>
  115. <Space size={4}>
  116. <GlobalOutlined style={{ color: '#8c8c8c', fontSize: '12px' }} />
  117. <Text style={{ fontSize: '13px', color: record.proxies_pool ? '#595959' : '#d9d9d9' }}>
  118. {record.proxies_pool || 'N/A'}
  119. </Text>
  120. </Space>
  121. </Space>
  122. )
  123. },
  124. {
  125. title: 'Actions',
  126. key: 'action',
  127. width: 200,
  128. align: 'right',
  129. render: (_, record) => {
  130. // Dropdown Menu for secondary actions
  131. const menuItems = [
  132. { key: 'logs', label: 'View Logs', icon: <FileTextOutlined />, onClick: () => setLogGroupId(record.id) },
  133. { key: 'config', label: 'Configuration', icon: <SettingOutlined />, onClick: () => setConfigGroupId(record.id) },
  134. ];
  135. return (
  136. <Space size="small">
  137. {record.running ? (
  138. <Tooltip title="Stop">
  139. <Button
  140. type="text"
  141. danger
  142. icon={<PauseCircleFilled style={{ fontSize: '18px' }} />}
  143. onClick={() => handleAction(stopGroup, record.id)}
  144. style={{ background: '#fff1f0', border: '1px solid #ffa39e' }}
  145. />
  146. </Tooltip>
  147. ) : (
  148. <Tooltip title="Start">
  149. <Button
  150. type="text"
  151. icon={<PlayCircleFilled style={{ fontSize: '18px', color: '#52c41a' }} />}
  152. onClick={() => handleAction(startGroup, record.id)}
  153. style={{ background: '#f6ffed', border: '1px solid #b7eb8f' }}
  154. />
  155. </Tooltip>
  156. )}
  157. <Tooltip title="Restart">
  158. <Button
  159. icon={<ReloadOutlined />}
  160. onClick={() => handleAction(restartGroup, record.id)}
  161. />
  162. </Tooltip>
  163. <Dropdown menu={{ items: menuItems }} trigger={['click']}>
  164. <Button icon={<MoreOutlined />} />
  165. </Dropdown>
  166. </Space>
  167. );
  168. },
  169. },
  170. ];
  171. return (
  172. <Layout style={{ minHeight: '100vh', background: '#f0f2f5' }}>
  173. {/* 1. Modern Header */}
  174. <Header style={{
  175. background: '#fff',
  176. padding: '0 24px',
  177. boxShadow: '0 2px 8px #f0f1f2',
  178. display: 'flex',
  179. alignItems: 'center',
  180. justifyContent: 'space-between',
  181. zIndex: 10
  182. }}>
  183. <div style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
  184. <div style={{
  185. width: '36px', height: '36px',
  186. background: 'linear-gradient(135deg, #1890ff 0%, #096dd9 100%)',
  187. borderRadius: '8px',
  188. display: 'flex', alignItems: 'center', justifyContent: 'center',
  189. color: '#fff', fontSize: '20px'
  190. }}>
  191. <ThunderboltFilled />
  192. </div>
  193. <Title level={4} style={{ margin: 0, fontWeight: 600 }}>Visa Manager</Title>
  194. </div>
  195. <Space size="middle">
  196. <Button
  197. onClick={() => setIsUpgradeOpen(true)}
  198. icon={<CloudUploadOutlined />}
  199. >
  200. OTA Update
  201. </Button>
  202. <Button
  203. type="primary"
  204. icon={<PlusOutlined />}
  205. onClick={() => setIsCreateOpen(true)}
  206. style={{ borderRadius: '6px', height: '36px', padding: '0 20px' }}
  207. >
  208. Create Group
  209. </Button>
  210. </Space>
  211. </Header>
  212. <Content style={{ padding: '24px', maxWidth: '1600px', margin: '0 auto', width: '100%' }}>
  213. {/* 2. Stats Overview Cards */}
  214. <Row gutter={16} style={{ marginBottom: '24px' }}>
  215. <Col span={6}>
  216. <StatCard title="Total Groups" value={stats.total} icon={<AppstoreOutlined />} color="#1890ff" />
  217. </Col>
  218. <Col span={6}>
  219. <StatCard title="Running" value={stats.running} icon={<PlayCircleFilled />} color="#52c41a" />
  220. </Col>
  221. <Col span={6}>
  222. <StatCard title="Stopped" value={stats.stopped} icon={<PauseCircleFilled />} color="#ff4d4f" />
  223. </Col>
  224. <Col span={6}>
  225. <StatCard title="Total Instances" value={stats.totalInstances} icon={<ThunderboltFilled />} color="#faad14" />
  226. </Col>
  227. </Row>
  228. {/* 3. Main Data Table */}
  229. <Card
  230. bordered={false}
  231. bodyStyle={{ padding: '0' }}
  232. style={{ borderRadius: '12px', boxShadow: '0 2px 8px rgba(0,0,0,0.04)', overflow: 'hidden' }}
  233. >
  234. <Table
  235. dataSource={dataSource}
  236. columns={columns}
  237. loading={isLoading}
  238. rowKey="id"
  239. pagination={false}
  240. rowClassName="align-middle"
  241. size="middle"
  242. />
  243. </Card>
  244. </Content>
  245. {/* Modals */}
  246. {configGroupId && <ConfigModal groupId={configGroupId} open={!!configGroupId} onClose={() => setConfigGroupId(null)} />}
  247. {logGroupId && <LogViewer groupId={logGroupId} open={!!logGroupId} onClose={() => setLogGroupId(null)} />}
  248. <UpgradeModal open={isUpgradeOpen} onClose={() => setIsUpgradeOpen(false)} />
  249. <CreateGroupModal open={isCreateOpen} onClose={() => setIsCreateOpen(false)} />
  250. </Layout>
  251. );
  252. };
  253. // --- Helper Components ---
  254. const StatCard = ({ title, value, icon, color }) => (
  255. <Card bordered={false} style={{ borderRadius: '12px', boxShadow: '0 2px 8px rgba(0,0,0,0.02)' }}>
  256. <Statistic
  257. title={<span style={{ fontSize: '13px', fontWeight: 500, color: '#8c8c8c' }}>{title}</span>}
  258. value={value}
  259. valueStyle={{ fontWeight: 'bold', fontSize: '24px', color: '#262626' }}
  260. prefix={<span style={{ color, marginRight: '8px', fontSize: '20px', position: 'relative', top: '2px' }}>{icon}</span>}
  261. />
  262. </Card>
  263. );
  264. const TagPill = ({ value, label }) => (
  265. <div style={{
  266. background: '#f5f5f5', borderRadius: '4px',
  267. padding: '2px 8px', display: 'inline-block',
  268. fontSize: '12px', color: '#595959', border: '1px solid #d9d9d9'
  269. }}>
  270. <span style={{ fontWeight: 'bold', marginRight: '4px' }}>{value}</span>
  271. <span style={{ fontSize: '10px', color: '#8c8c8c' }}>{label}</span>
  272. </div>
  273. );
  274. export default Dashboard;