All files / client/components/Admin/Share AccessLog.tsx

0% Statements 0/26
0% Branches 0/19
0% Functions 0/8
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                     
import React from 'react'
import { withTranslation, WithTranslation } from 'react-i18next'
import moment from 'moment'
import platform from 'platform'
import { Table, Alert } from 'reactstrap'
import Pagination from 'components/Common/Pagination'
import Crowi from 'client/util/Crowi'
import { ShareAccess } from 'client/types/crowi'
 
interface Props extends WithTranslation {
  crowi: Crowi
}
 
interface State {
  accesses: ShareAccess[]
  pagination: {
    current: number
    count: number
    limit: number
  }
  error: boolean
}
 
interface Record {
  index: number
  path: string
  info: Platform | undefined
  remoteAddress: string
  date: string
}
 
class AccessLog extends React.Component<Props, State> {
  constructor(props: Props) {
    super(props)
 
    this.state = {
      accesses: [],
      pagination: {
        current: 0,
        count: 0,
        limit: 20,
      },
      error: false,
    }
 
    this.getPage = this.getPage.bind(this)
    this.movePage = this.movePage.bind(this)
    this.renderTableBody = this.renderTableBody.bind(this)
  }
 
  async getPage(options = {}) {
    const limit = this.state.pagination.limit
    try {
      const { shareAccess } = await this.props.crowi.apiGet('/shares/accesses.list', { ...options, limit })
      const { docs: accesses, page: current, pages: count } = shareAccess
      const pagination = { current, count, limit }
      this.setState({ accesses, pagination })
    } catch (err) {
      this.setState({ error: true })
    }
  }
 
  movePage(i: number) {
    if (i !== this.state.pagination.current) {
      this.getPage({ page: i })
    }
  }
 
  componentDidMount() {
    this.getPage()
  }
 
  renderRecord({ index, path, info, remoteAddress, date }: Record) {
    const { name: platformName = '', os = '' } = info || {}
    return (
      <tr key={index}>
        <td>{index}</td>
        <td>{path ? <a href={path}>{path}</a> : '(Deleted)'}</td>
        <td>{platformName}</td>
        <td>{os.toString()}</td>
        <td>{remoteAddress}</td>
        <td>{date}</td>
      </tr>
    )
  }
 
  renderTableBody() {
    const { current, limit } = this.state.pagination
    const start = (current - 1) * limit + 1
    return (
      <tbody>
        {this.state.accesses.map(({ share, tracking: { userAgent, remoteAddress }, lastAccessedAt }, i) => {
          const { page = { path: '' } } = share || {}
          const { path = '' } = page || {}
          return this.renderRecord({
            index: start + i,
            path,
            info: platform.parse ? platform.parse(userAgent) : undefined,
            remoteAddress,
            date: moment(lastAccessedAt).format('llll'),
          })
        })}
      </tbody>
    )
  }
 
  render() {
    const { t } = this.props
    const {
      pagination: { current, count },
      error,
    } = this.state
    return error ? (
      <Alert color="danger">
        <p>{t('access_log.error.message')}</p>
      </Alert>
    ) : (
      <div>
        <Table bordered hover size="sm">
          <thead>
            <tr>
              <th>#</th>
              <th>{t('Page name')}</th>
              <th>{t('Browser')}</th>
              <th>OS</th>
              <th>{t('IP Address')}</th>
              <th>{t('Last Accessed')}</th>
            </tr>
          </thead>
          {this.renderTableBody()}
        </Table>
        <Pagination current={current} count={count} onClick={this.movePage} />
      </div>
    )
  }
}
 
export default withTranslation()(AccessLog)