summaryrefslogtreecommitdiff
path: root/src/components/Rules.js
blob: cdb86c778e2fa3b892553d05e2c6bdb97d8e5738 (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
import React from 'react';
import { FixedSizeList as List, areEqual } from 'react-window';
import { RotateCw } from 'react-feather';

import { Fab, position as fabPosition } from './shared/Fab';
import { connect } from './StateProvider';
import { getClashAPIConfig } from '../store/app';
import ContentHeader from './ContentHeader';
import Rule from './Rule';
import RuleSearch from './RuleSearch';
import useRemainingViewPortHeight from '../hooks/useRemainingViewPortHeight';

import { getRules, fetchRules, fetchRulesOnce } from '../store/rules';

const { memo, useEffect, useMemo, useCallback } = React;

// import s from './Rules.module.css';
const paddingBottom = 30;

function itemKey(index, data) {
  const item = data[index];
  return item.id;
}

const Row = memo(({ index, style, data }) => {
  const r = data[index];
  return (
    <div style={style}>
      <Rule {...r} />
    </div>
  );
}, areEqual);

const mapState = (s) => ({
  apiConfig: getClashAPIConfig(s),
  rules: getRules(s),
});

export default connect(mapState)(Rules);

function Rules({ dispatch, apiConfig, rules }) {
  const fetchRulesHooked = useCallback(() => {
    dispatch(fetchRules(apiConfig));
  }, [apiConfig, dispatch]);
  useEffect(() => {
    dispatch(fetchRulesOnce(apiConfig));
  }, [dispatch, apiConfig]);
  const [refRulesContainer, containerHeight] = useRemainingViewPortHeight();
  const refreshIcon = useMemo(() => <RotateCw width={16} />, []);
  return (
    <div>
      <ContentHeader title="Rules" />
      <RuleSearch />
      <div ref={refRulesContainer} style={{ paddingBottom }}>
        <List
          height={containerHeight - paddingBottom}
          width="100%"
          itemCount={rules.length}
          itemSize={80}
          itemData={rules}
          itemKey={itemKey}
        >
          {Row}
        </List>
      </div>

      <Fab
        icon={refreshIcon}
        text="Refresh"
        onClick={fetchRulesHooked}
        position={fabPosition}
      />
    </div>
  );
}