ServerStats.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React, { PureComponent } from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import { connect } from 'react-redux';
  4. import { NavModel, StoreState } from 'app/types';
  5. import { getNavModel } from 'app/core/selectors/navModel';
  6. import { getServerStats, ServerStat } from './state/apis';
  7. import Page from 'app/core/components/Page/Page';
  8. interface Props {
  9. navModel: NavModel;
  10. getServerStats: () => Promise<ServerStat[]>;
  11. }
  12. interface State {
  13. stats: ServerStat[];
  14. isLoading: boolean;
  15. }
  16. export class ServerStats extends PureComponent<Props, State> {
  17. constructor(props: Props) {
  18. super(props);
  19. this.state = {
  20. stats: [],
  21. isLoading: false,
  22. };
  23. }
  24. async componentDidMount() {
  25. try {
  26. this.setState({ isLoading: true });
  27. const stats = await this.props.getServerStats();
  28. this.setState({ stats, isLoading: false });
  29. } catch (error) {
  30. console.error(error);
  31. }
  32. }
  33. render() {
  34. const { navModel } = this.props;
  35. const { stats, isLoading } = this.state;
  36. return (
  37. <Page navModel={navModel}>
  38. <Page.Contents isLoading={isLoading}>
  39. <table className="filter-table form-inline">
  40. <thead>
  41. <tr>
  42. <th>Name</th>
  43. <th>Value</th>
  44. </tr>
  45. </thead>
  46. <tbody>{stats.map(StatItem)}</tbody>
  47. </table>
  48. </Page.Contents>
  49. </Page>
  50. );
  51. }
  52. }
  53. function StatItem(stat: ServerStat) {
  54. return (
  55. <tr key={stat.name}>
  56. <td>{stat.name}</td>
  57. <td>{stat.value}</td>
  58. </tr>
  59. );
  60. }
  61. const mapStateToProps = (state: StoreState) => ({
  62. navModel: getNavModel(state.navIndex, 'server-stats'),
  63. getServerStats: getServerStats,
  64. });
  65. export default hot(module)(connect(mapStateToProps)(ServerStats));