summaryrefslogtreecommitdiff
path: root/src/components/ConnectionCard.tsx
blob: bf16eb6451872df3c403f683e1db22ba4755d85f (plain)
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
import { formatDistance, Locale } from 'date-fns';
import { enUS, zhCN, zhTW } from 'date-fns/locale';
import React from 'react';
import { ArrowDown, ArrowDownCircle, ArrowUp, X } from '~/components/shared/FeatherIcons';
import { useTranslation } from 'react-i18next';

import { FormattedConn } from '~/store/connections';

import prettyBytes from '../misc/pretty-bytes';

import s from './ConnectionCard.module.scss';

interface Props {
  conn: FormattedConn;
  onDisconnect: (id: string, e: React.MouseEvent) => void;
  onClick: () => void;
}

const ConnectionCard = React.memo(function ConnectionCard({ conn, onDisconnect, onClick }: Props) {
  const { i18n } = useTranslation();

  let locale: Locale;
  if (i18n.language === 'zh-CN') {
    locale = zhCN;
  } else if (i18n.language === 'zh-TW') {
    locale = zhTW;
  } else {
    locale = enUS;
  }

  const timeAgo = formatDistance(conn.start, 0, { locale });

  return (
    <div
      className={s.card}
      onClick={onClick}
      role="button"
      tabIndex={0}
      onKeyDown={(e) => {
        if (e.key === 'Enter' || e.key === ' ') {
          e.preventDefault();
          onClick();
        }
      }}
    >
      <div className={s.row}>
        <div className={s.host}>{conn.host}</div>
        <div className={s.time}>{timeAgo}</div>
      </div>
      <div className={s.row}>
        <div className={s.typeProtocol}>{conn.type.replace(/\((.*)\)/, ' | $1')}</div>
        <div className={s.totals}>
          <span>
            {prettyBytes(conn.download)} <ArrowDown size={12} />
          </span>
          <span>
            {prettyBytes(conn.upload)} <ArrowUp size={12} />
          </span>
        </div>
      </div>
      <div className={s.row}>
        <div className={s.ruleChain}>
          <span className={s.rule}>{conn.rule}</span>
          <span className={s.arrow}>→</span>
          <span className={s.chains}>{conn.chains}</span>
        </div>
        <div className={s.speedAndAction}>
          <div className={s.speed}>
            {prettyBytes(conn.downloadSpeedCurr)}/s
            <ArrowDownCircle size={16} className={s.speedIcon} />
          </div>
          <button
            className={s.closeBtn}
            onClick={(e) => {
              e.stopPropagation();
              onDisconnect(conn.id, e);
            }}
          >
            <X size={16} />
          </button>
        </div>
      </div>
    </div>
  );
});

export default ConnectionCard;