preview-logs-toggle-button.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import React from 'react'
  2. import { OverlayTrigger, Tooltip } from 'react-bootstrap'
  3. import PropTypes from 'prop-types'
  4. import classNames from 'classnames'
  5. import { useTranslation } from 'react-i18next'
  6. import Icon from '../../../shared/components/icon'
  7. const MAX_ERRORS_COUNT = 99
  8. function PreviewLogsToggleButton({
  9. onToggle,
  10. showLogs,
  11. compileFailed = false,
  12. logsState: { nErrors, nWarnings },
  13. showText
  14. }) {
  15. const { t } = useTranslation()
  16. const toggleButtonClasses = classNames(
  17. 'btn',
  18. 'btn-xs',
  19. 'btn-toggle-logs',
  20. 'toolbar-item',
  21. {
  22. 'btn-danger': !showLogs && nErrors,
  23. 'btn-warning': !showLogs && !nErrors && nWarnings,
  24. 'btn-default': showLogs || (!nErrors && !nWarnings)
  25. }
  26. )
  27. let textStyle = {}
  28. if (!showText) {
  29. textStyle = {
  30. position: 'absolute',
  31. right: '-100vw'
  32. }
  33. }
  34. function handleOnClick(e) {
  35. e.currentTarget.blur()
  36. onToggle()
  37. }
  38. const buttonElement = (
  39. <button
  40. id="logs-toggle"
  41. type="button"
  42. disabled={compileFailed}
  43. className={toggleButtonClasses}
  44. onClick={handleOnClick}
  45. >
  46. {showLogs ? (
  47. <ViewPdfButton textStyle={textStyle} />
  48. ) : (
  49. <CompilationResultIndicator
  50. textStyle={textStyle}
  51. nErrors={nErrors}
  52. nWarnings={nWarnings}
  53. />
  54. )}
  55. </button>
  56. )
  57. return showText ? (
  58. buttonElement
  59. ) : (
  60. <OverlayTrigger
  61. placement="bottom"
  62. overlay={
  63. <Tooltip id="tooltip-logs-toggle">
  64. {showLogs ? t('view_pdf') : t('view_logs')}
  65. </Tooltip>
  66. }
  67. >
  68. {buttonElement}
  69. </OverlayTrigger>
  70. )
  71. }
  72. function CompilationResultIndicator({ textStyle, nErrors, nWarnings }) {
  73. if (nErrors || nWarnings) {
  74. return (
  75. <LogsCompilationResultIndicator
  76. logType={nErrors ? 'errors' : 'warnings'}
  77. nLogs={nErrors || nWarnings}
  78. textStyle={textStyle}
  79. />
  80. )
  81. } else {
  82. return <ViewLogsButton textStyle={textStyle} />
  83. }
  84. }
  85. function LogsCompilationResultIndicator({ textStyle, logType, nLogs }) {
  86. const { t } = useTranslation()
  87. const label =
  88. logType === 'errors' ? t('your_project_has_errors') : t('view_warnings')
  89. return (
  90. <>
  91. <Icon type="file-text-o" />
  92. <span
  93. className="btn-toggle-logs-label toolbar-text"
  94. aria-label={label}
  95. style={textStyle}
  96. >
  97. {`${label} (${
  98. nLogs > MAX_ERRORS_COUNT ? `${MAX_ERRORS_COUNT}+` : nLogs
  99. })`}
  100. </span>
  101. </>
  102. )
  103. }
  104. function ViewLogsButton({ textStyle }) {
  105. const { t } = useTranslation()
  106. return (
  107. <>
  108. <Icon type="file-text-o" />
  109. <span className="toolbar-text" style={textStyle}>
  110. {t('view_logs')}
  111. </span>
  112. </>
  113. )
  114. }
  115. function ViewPdfButton({ textStyle }) {
  116. const { t } = useTranslation()
  117. return (
  118. <>
  119. <Icon type="file-pdf-o" />
  120. <span className="toolbar-text" style={textStyle}>
  121. {t('view_pdf')}
  122. </span>
  123. </>
  124. )
  125. }
  126. PreviewLogsToggleButton.propTypes = {
  127. onToggle: PropTypes.func.isRequired,
  128. logsState: PropTypes.shape({
  129. nErrors: PropTypes.number.isRequired,
  130. nWarnings: PropTypes.number.isRequired,
  131. nLogEntries: PropTypes.number.isRequired
  132. }),
  133. showLogs: PropTypes.bool.isRequired,
  134. showText: PropTypes.bool.isRequired,
  135. compileFailed: PropTypes.bool
  136. }
  137. LogsCompilationResultIndicator.propTypes = {
  138. logType: PropTypes.string.isRequired,
  139. nLogs: PropTypes.number.isRequired,
  140. textStyle: PropTypes.object.isRequired
  141. }
  142. ViewLogsButton.propTypes = {
  143. textStyle: PropTypes.object.isRequired
  144. }
  145. ViewPdfButton.propTypes = {
  146. textStyle: PropTypes.object.isRequired
  147. }
  148. export default PreviewLogsToggleButton