useInboundColumns.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { useMemo, type ReactElement } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { Popover, Switch, Tag, type TableColumnType } from 'antd';
  4. import { TeamOutlined } from '@ant-design/icons';
  5. import { SizeFormatter, IntlUtil, ColorUtils } from '@/utils';
  6. import { InfinityIcon } from '@/components/ui';
  7. import { useDatepicker } from '@/hooks/useDatepicker';
  8. import type { NodeRecord } from '@/api/queries/useNodesQuery';
  9. import { RowActionsCell } from './RowActions';
  10. import {
  11. readStreamHints,
  12. networkLabel,
  13. networkL4,
  14. shadowsocksNetworkLabel,
  15. tunnelNetworkLabel,
  16. mixedNetworkLabel,
  17. } from './helpers';
  18. import type { ClientCountEntry, DBInboundRecord, RowAction } from './types';
  19. interface UseInboundColumnsParams {
  20. hasAnyRemark: boolean;
  21. hasActiveNode: boolean;
  22. nodesById: Map<number, NodeRecord>;
  23. clientCount: Record<number, ClientCountEntry>;
  24. subEnable: boolean;
  25. expireDiff: number;
  26. trafficDiff: number;
  27. onRowAction: (action: { key: RowAction; dbInbound: DBInboundRecord }) => void;
  28. onSwitchEnable: (dbInbound: DBInboundRecord, next: boolean) => void;
  29. }
  30. export function useInboundColumns({
  31. hasAnyRemark,
  32. hasActiveNode,
  33. nodesById,
  34. clientCount,
  35. subEnable,
  36. expireDiff,
  37. trafficDiff,
  38. onRowAction,
  39. onSwitchEnable,
  40. }: UseInboundColumnsParams): TableColumnType<DBInboundRecord>[] {
  41. const { t } = useTranslation();
  42. const { datepicker } = useDatepicker();
  43. return useMemo(() => {
  44. const cols: TableColumnType<DBInboundRecord>[] = [
  45. {
  46. title: 'ID',
  47. dataIndex: 'id',
  48. key: 'id',
  49. align: 'right',
  50. width: 30,
  51. },
  52. {
  53. title: t('pages.inbounds.operate'),
  54. key: 'action',
  55. align: 'center',
  56. width: 60,
  57. render: (_, record) => (
  58. <RowActionsCell
  59. record={record}
  60. subEnable={subEnable}
  61. hasClients={(clientCount[record.id]?.clients || 0) > 0}
  62. onClick={(key) => onRowAction({ key, dbInbound: record })}
  63. />
  64. ),
  65. },
  66. {
  67. title: t('pages.inbounds.enable'),
  68. key: 'enable',
  69. align: 'center',
  70. width: 35,
  71. render: (_, record) => (
  72. <Switch
  73. checked={record.enable}
  74. onChange={(next) => onSwitchEnable(record, next)}
  75. />
  76. ),
  77. },
  78. ];
  79. if (hasAnyRemark) {
  80. cols.push({
  81. title: t('pages.inbounds.remark'),
  82. dataIndex: 'remark',
  83. key: 'remark',
  84. align: 'center',
  85. width: 60,
  86. });
  87. }
  88. if (hasActiveNode) {
  89. cols.push({
  90. title: t('pages.inbounds.node'),
  91. key: 'node',
  92. align: 'center',
  93. width: 60,
  94. render: (_, record) => {
  95. if (record.nodeId == null) {
  96. return <Tag color="default">{t('pages.inbounds.localPanel')}</Tag>;
  97. }
  98. const node = nodesById.get(record.nodeId);
  99. if (!node) {
  100. return <Tag color="orange">node #{record.nodeId}</Tag>;
  101. }
  102. return (
  103. <Tag color={node.status === 'online' ? 'blue' : 'red'}>{node.name}</Tag>
  104. );
  105. },
  106. });
  107. }
  108. cols.push(
  109. {
  110. title: t('pages.inbounds.port'),
  111. dataIndex: 'port',
  112. key: 'port',
  113. align: 'center',
  114. width: 40,
  115. },
  116. {
  117. title: t('pages.inbounds.protocol'),
  118. key: 'protocol',
  119. align: 'left',
  120. width: 130,
  121. render: (_, record) => {
  122. const tags: ReactElement[] = [<Tag key="p" color="purple">{record.protocol}</Tag>];
  123. if (record.isWireguard || record.isHysteria) {
  124. tags.push(<Tag key="n" color="green">UDP</Tag>);
  125. } else if (record.isSS) {
  126. const stream = readStreamHints(record.streamSettings);
  127. tags.push(<Tag key="n" color="green">{shadowsocksNetworkLabel(record.settings)}</Tag>);
  128. if (stream.isTls) tags.push(<Tag key="tls" color="blue">TLS</Tag>);
  129. } else if (record.isTunnel) {
  130. tags.push(<Tag key="n" color="green">{tunnelNetworkLabel(record.settings)}</Tag>);
  131. } else if (record.isMixed) {
  132. tags.push(<Tag key="n" color="green">{mixedNetworkLabel(record.settings)}</Tag>);
  133. } else if (record.isVMess || record.isVLess || record.isTrojan) {
  134. const stream = readStreamHints(record.streamSettings);
  135. tags.push(<Tag key="n" color="green">{networkLabel(stream.network)}</Tag>);
  136. const l4 = networkL4(stream.network);
  137. if (l4) tags.push(<Tag key="l4" color="green">{l4}</Tag>);
  138. if (stream.isTls) tags.push(<Tag key="tls" color="blue">TLS</Tag>);
  139. if (stream.isReality) tags.push(<Tag key="reality" color="blue">Reality</Tag>);
  140. }
  141. return <div className="protocol-tags">{tags}</div>;
  142. },
  143. },
  144. {
  145. title: t('clients'),
  146. key: 'clients',
  147. align: 'left',
  148. width: 110,
  149. render: (_, record) => {
  150. const cc = clientCount[record.id];
  151. if (!cc) return null;
  152. return (
  153. <>
  154. <Tag className="client-count-tag" style={{ margin: 0, padding: '0 2px' }}>
  155. <TeamOutlined /> {cc.clients}
  156. </Tag>
  157. {cc.active.length > 0 && (
  158. <Popover
  159. title={t('subscription.active')}
  160. content={(
  161. <div className="client-email-list">
  162. {cc.active.map((e) => <div key={e}>{e}</div>)}
  163. </div>
  164. )}
  165. >
  166. <Tag color="green" className="client-count-tag" style={{ margin: 0, padding: '0 2px' }}>{cc.active.length}</Tag>
  167. </Popover>
  168. )}
  169. {cc.deactive.length > 0 && (
  170. <Popover
  171. title={t('disabled')}
  172. content={(
  173. <div className="client-email-list">
  174. {cc.deactive.map((e) => <div key={e}>{e}</div>)}
  175. </div>
  176. )}
  177. >
  178. <Tag className="client-count-tag" style={{ margin: 0, padding: '0 2px' }}>{cc.deactive.length}</Tag>
  179. </Popover>
  180. )}
  181. {cc.depleted.length > 0 && (
  182. <Popover
  183. title={t('depleted')}
  184. content={(
  185. <div className="client-email-list">
  186. {cc.depleted.map((e) => <div key={e}>{e}</div>)}
  187. </div>
  188. )}
  189. >
  190. <Tag color="red" className="client-count-tag" style={{ margin: 0, padding: '0 2px' }}>{cc.depleted.length}</Tag>
  191. </Popover>
  192. )}
  193. {cc.online.length > 0 && (
  194. <Popover
  195. title={t('online')}
  196. content={(
  197. <div className="client-email-list">
  198. {cc.online.map((e) => <div key={e}>{e}</div>)}
  199. </div>
  200. )}
  201. >
  202. <Tag color="blue" className="client-count-tag" style={{ margin: 0, padding: '0 2px' }}>{cc.online.length}</Tag>
  203. </Popover>
  204. )}
  205. </>
  206. );
  207. },
  208. },
  209. {
  210. title: t('pages.inbounds.traffic'),
  211. key: 'traffic',
  212. align: 'center',
  213. width: 90,
  214. render: (_, record) => (
  215. <Popover
  216. content={(
  217. <table cellPadding={2}>
  218. <tbody>
  219. <tr>
  220. <td>↑ {SizeFormatter.sizeFormat(record.up)}</td>
  221. <td>↓ {SizeFormatter.sizeFormat(record.down)}</td>
  222. </tr>
  223. {record.total > 0 && record.up + record.down < record.total && (
  224. <tr>
  225. <td>{t('remained')}</td>
  226. <td>{SizeFormatter.sizeFormat(record.total - record.up - record.down)}</td>
  227. </tr>
  228. )}
  229. </tbody>
  230. </table>
  231. )}
  232. >
  233. <Tag color={ColorUtils.usageColor(record.up + record.down, trafficDiff, record.total)}>
  234. {SizeFormatter.sizeFormat(record.up + record.down)} /
  235. {' '}
  236. {record.total > 0 ? SizeFormatter.sizeFormat(record.total) : <InfinityIcon />}
  237. </Tag>
  238. </Popover>
  239. ),
  240. },
  241. {
  242. title: t('pages.inbounds.expireDate'),
  243. key: 'expiryTime',
  244. align: 'center',
  245. width: 40,
  246. render: (_, record) => {
  247. if (record.expiryTime > 0) {
  248. return (
  249. <Popover content={IntlUtil.formatDate(record.expiryTime, datepicker)}>
  250. <Tag color={ColorUtils.usageColor(Date.now(), expireDiff, record._expiryTime)} style={{ minWidth: 50 }}>
  251. {IntlUtil.formatRelativeTime(record.expiryTime)}
  252. </Tag>
  253. </Popover>
  254. );
  255. }
  256. return <Tag color="purple"><InfinityIcon /></Tag>;
  257. },
  258. },
  259. );
  260. return cols;
  261. }, [t, hasAnyRemark, hasActiveNode, nodesById, clientCount, subEnable, expireDiff, trafficDiff, datepicker, onRowAction, onSwitchEnable]);
  262. }