ChangeLog.tsx 5.0 KB
Newer Older
Z
zengqiao 已提交
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
import { Collapse, Divider, Spin, Utils } from 'knowdesign';
import * as React from 'react';
import API from '../../api';
import { Link, useParams } from 'react-router-dom';
import InfiniteScroll from 'react-infinite-scroll-component';
import moment from 'moment';
import { timeFormat } from '../../constants/common';
import { DownOutlined } from '@ant-design/icons';
import { renderToolTipValue } from './config';

const { Panel } = Collapse;

interface ILog {
  clusterPhyId: number;
  createTime: number;
  operateTime: string;
  resName: string;
  resTypeCode: number;
  resTypeName: string;
  updateTime: number;
}

const ChangeLog = () => {
  const { clusterId } = useParams<{ clusterId: string }>();
  const [data, setData] = React.useState<ILog[]>([]);
  const [loading, setLoading] = React.useState<boolean>(true);
  const [pagination, setPagination] = React.useState({
    pageNo: 0,
    pageSize: 10,
    total: 0,
  });

  const getChangeLog = () => {
    const promise = Utils.request(API.getClusterChangeLog(+clusterId), {
      params: {
        pageNo: pagination.pageNo + 1,
        pageSize: 10,
      },
    });
    promise.then((res: any) => {
      setData((cur) => cur.concat(res?.bizData));
      setPagination(res?.pagination);
    });
    return promise;
  };

  React.useEffect(() => {
    getChangeLog().then(
      () => setLoading(false),
      () => setLoading(false)
    );
  }, []);

  const renderEmpty = () => {
    return (
      <>
        <div className="empty-panel">
          <div className="img" />
          <div className="text">暂无配置记录</div>
        </div>
      </>
    );
  };

  const getHref = (item: any) => {
    if (item.resTypeName.toLowerCase().includes('topic')) return `/cluster/${clusterId}/topic/list#topicName=${item.resName}`;
    if (item.resTypeName.toLowerCase().includes('broker')) return `/cluster/${clusterId}/broker/list#brokerId=${item.resName}`;
    return '';
  };

  return (
    <>
      <div className="change-log-panel">
        <div className="title">历史变更记录</div>
        {!loading && !data.length ? (
          renderEmpty()
        ) : (
          <div id="changelog-scroll-box">
            <Spin spinning={loading} style={{ paddingLeft: '42%', marginTop: 100 }} />
            <InfiniteScroll
              dataLength={data.length}
              next={getChangeLog as any}
              hasMore={data.length < pagination.total}
              loader={<Spin style={{ paddingLeft: '42%', paddingTop: 10 }} spinning={true} />}
              endMessage={
                !pagination.total ? (
                  ''
                ) : (
                  <Divider className="load-completed-tip" plain>
                    加载完成 共 {pagination.total}
                  </Divider>
                )
              }
              scrollableTarget="changelog-scroll-box"
            >
              <Collapse defaultActiveKey={['log-0']} accordion>
                {data.map((item, index) => {
                  return (
                    <Panel
                      header={
                        <>
                          <div className="header">
                            <div className="label">{renderToolTipValue(`[${item.resTypeName}] ${item.resName}`, 24)}</div>
                            <span className="icon">
                              <DownOutlined />
                            </span>
                          </div>
                          <div className="header-time">{moment(item.updateTime).format(timeFormat)}</div>
                        </>
                      }
                      key={`log-${index}`}
                      showArrow={false}
                    >
                      <div className="log-item">
                        <span>名称</span>
                        <div className="value">
                          {getHref(item) ? (
                            <Link to={getHref(item)}>{renderToolTipValue(item.resName, 18)}</Link>
                          ) : (
                            renderToolTipValue(item.resName, 18)
                          )}
                        </div>
                      </div>
                      <Divider />
                      <div className="log-item">
                        <span>时间</span>
                        <span className="value">{moment(item.updateTime).format(timeFormat)}</span>
                      </div>
                      <Divider />
                      <div className="log-item">
                        <span>内容</span>
                        <span className="value">{'修改配置'}</span>
                      </div>
                      <Divider />
                      <div className="log-item">
                        <span>类型</span>
                        <span className="value">{item.resTypeName}</span>
                      </div>
                    </Panel>
                  );
                })}
              </Collapse>
            </InfiniteScroll>
          </div>
        )}
      </div>
    </>
  );
};

export default ChangeLog;