All files / client/components/ExternalShare AccessLogModal.tsx

0% Statements 0/38
0% Branches 0/24
0% Functions 0/10
0% Lines 0/38

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214                                                                                                                                                                                                                                                                                                                                                                                                                                           
import React from 'react'
import { withTranslation, WithTranslation } from 'react-i18next'
import moment from 'moment'
import platform from 'platform'
import { Modal, ModalHeader, ModalBody, Table, Alert } from 'reactstrap'
import Pagination from 'components/Common/Pagination'
import Crowi from 'client/util/Crowi'
import { Share, ShareAccess } from 'client/types/crowi'
 
interface Props extends WithTranslation {
  show: boolean
  onHide: () => void
  pageId: string | null
  crowi: Crowi
}
 
interface State {
  requesting: boolean
  pageId: string | null
  shares: []
  pagination: {
    total: number
    current: number
    count: number
    limit: number
  }
  error: boolean
}
 
class AccessLogModal extends React.Component<Props, State> {
  constructor(props: Props) {
    super(props)
 
    this.state = {
      requesting: false,
      pageId: null,
      shares: [],
      pagination: {
        total: 0,
        current: 0,
        count: 0,
        limit: 20,
      },
      error: false,
    }
 
    this.getPage = this.getPage.bind(this)
    this.movePage = this.movePage.bind(this)
    this.renderAccessLogTable = this.renderAccessLogTable.bind(this)
  }
 
  renderShareInfo(uuid: string, username: string, name: string, createdAt: string, isActive: boolean) {
    const { t } = this.props
    const date = moment(createdAt).format('llll')
    return (
      <div>
        <h4>
          {(isActive && (
            <span>
              {t('Share ID')}: <a href={`/_share/${uuid}`}>{uuid}</a> <span className="badge badge-success">Active</span>
            </span>
          )) || (
            <span>
              {t('Share ID')}: {uuid} <span className="badge badge-danger">Inactive</span>
            </span>
          )}
        </h4>
        <dl className="share-info">
          <div>
            <dt>{t('Creator')}</dt>
            <dd>
              <a href={`/user/${username}`}>{name}</a>
            </dd>
          </div>
          <div>
            <dt>{t('Created')}</dt>
            <dd>{date}</dd>
          </div>
        </dl>
      </div>
    )
  }
 
  renderTableHeader() {
    const { t } = this.props
    return (
      <thead>
        <tr>
          <th>#</th>
          <th>{t('Browser')}</th>
          <th>OS</th>
          <th>{t('IP Address')}</th>
          <th>{t('Last Accessed')}</th>
        </tr>
      </thead>
    )
  }
 
  static renderTableBody(accesses: ShareAccess, i: number) {
    const {
      tracking: { userAgent, remoteAddress },
      lastAccessedAt,
    } = accesses
    const index = i + 1
    const { name: platformName, os } = platform.parse ? platform.parse(userAgent) : { name: '', os: '' }
    const date = moment(lastAccessedAt).format('llll')
    return (
      <tr key={i}>
        <td>{index}</td>
        <td>{platformName}</td>
        <td>{os}</td>
        <td>{remoteAddress}</td>
        <td>{date}</td>
      </tr>
    )
  }
 
  renderAccessLogTable(share: Share, i: number) {
    const { t } = this.props
    const {
      uuid,
      creator: { name, username },
      createdAt,
      accesses,
      status,
    } = share
    return (
      <div key={i}>
        {this.renderShareInfo(uuid, username, name, createdAt, status === 'active')}
        {accesses.length > 0 ? (
          <Table bordered hover condensed>
            {this.renderTableHeader()}
            <tbody>{accesses.map(AccessLogModal.renderTableBody)}</tbody>
          </Table>
        ) : (
          <Alert color="info">{t('No one accessed yet')}</Alert>
        )}
      </div>
    )
  }
 
  async getPage(pageId: string | null, options = {}) {
    const limit = this.state.pagination.limit
    if (!this.state.error && !this.state.requesting) {
      this.setState({ requesting: true })
 
      try {
        const { share } = await this.props.crowi.apiGet('/shares.list', {
          limit: 5,
          page_id: pageId,
          populate_accesses: true,
          ...options,
        })
        const { docs: shares, total, page: current, pages: count } = share
        const pagination = { total, current, count, limit }
        this.setState({ pageId, shares, pagination, requesting: false })
      } catch (err) {
        this.setState({ error: true, requesting: false })
      }
    }
  }
 
  movePage(i: number) {
    if (i !== this.state.pagination.current) {
      this.getPage(this.state.pageId, { page: i })
    }
  }
 
  componentDidMount() {
    const { pageId = null } = this.props
    if (pageId !== this.state.pageId) {
      this.getPage(pageId)
    }
  }
 
  componentDidUpdate() {
    const { pageId = null } = this.props
    if (pageId !== this.state.pageId) {
      this.getPage(pageId)
    }
  }
 
  render() {
    const { t, show, onHide } = this.props
    const {
      pagination: { total, current, count },
      error,
    } = this.state
    return (
      <Modal className="access-log-modal" isOpen={show} toggle={onHide} size="lg">
        <ModalHeader>{t('Access Log')}</ModalHeader>
        <ModalBody>
          {error ? (
            <Alert color="danger">
              <p>{t('modal_access_log.error.message')}</p>
            </Alert>
          ) : total === 0 ? (
            <Alert color="info">
              <p>{t('modal_access_log.no_access_log_is_exists_yet')}</p>
            </Alert>
          ) : (
            <div>
              {this.state.shares.map(this.renderAccessLogTable)}
              <Pagination current={current} count={count} onClick={this.movePage} />
            </div>
          )}
        </ModalBody>
      </Modal>
    )
  }
}
 
export default withTranslation()(AccessLogModal)