preview-logs-toggle-button.js 3.5 KB

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