preview-logs-toggle-button.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import React from 'react'
  2. import PropTypes from 'prop-types'
  3. import classNames from 'classnames'
  4. import { useTranslation } from 'react-i18next'
  5. import Icon from '../../../shared/components/icon'
  6. function PreviewLogsToggleButton({
  7. onToggle,
  8. showLogs,
  9. logsState: { nErrors, nWarnings }
  10. }) {
  11. const toggleButtonClasses = classNames('btn', 'btn-xs', 'btn-toggle-logs', {
  12. 'btn-danger': !showLogs && nErrors,
  13. 'btn-warning': !showLogs && !nErrors && nWarnings,
  14. 'btn-default': showLogs || (!nErrors && !nWarnings)
  15. })
  16. function handleOnClick(e) {
  17. e.currentTarget.blur()
  18. onToggle()
  19. }
  20. return (
  21. <button
  22. type="button"
  23. className={toggleButtonClasses}
  24. onClick={handleOnClick}
  25. >
  26. {showLogs ? (
  27. <ViewPdfButton />
  28. ) : (
  29. <CompilationResultIndicator nErrors={nErrors} nWarnings={nWarnings} />
  30. )}
  31. </button>
  32. )
  33. }
  34. function CompilationResultIndicator({ nErrors, nWarnings }) {
  35. if (nErrors || nWarnings) {
  36. return (
  37. <LogsCompilationResultIndicator
  38. logType={nErrors ? 'errors' : 'warnings'}
  39. nLogs={nErrors || nWarnings}
  40. />
  41. )
  42. } else {
  43. return <ViewLogsButton />
  44. }
  45. }
  46. function LogsCompilationResultIndicator({ logType, nLogs }) {
  47. const { t } = useTranslation()
  48. const label =
  49. logType === 'errors' ? t('your_project_has_errors') : t('view_warnings')
  50. return (
  51. <>
  52. <Icon type="file-text-o" />
  53. <span className="btn-toggle-logs-label" aria-label={label}>
  54. {`${label} (${nLogs > 9 ? '9+' : nLogs})`}
  55. </span>
  56. </>
  57. )
  58. }
  59. function ViewLogsButton() {
  60. const { t } = useTranslation()
  61. return (
  62. <>
  63. <Icon type="file-text-o" />
  64. <span className="btn-toggle-logs-label">{t('view_logs')}</span>
  65. </>
  66. )
  67. }
  68. function ViewPdfButton() {
  69. const { t } = useTranslation()
  70. return (
  71. <>
  72. <Icon type="file-pdf-o" />
  73. <span className="btn-toggle-logs-label">{t('view_pdf')}</span>
  74. </>
  75. )
  76. }
  77. PreviewLogsToggleButton.propTypes = {
  78. onToggle: PropTypes.func.isRequired,
  79. logsState: PropTypes.shape({
  80. nErrors: PropTypes.number.isRequired,
  81. nWarnings: PropTypes.number.isRequired,
  82. nLogEntries: PropTypes.number.isRequired
  83. }),
  84. showLogs: PropTypes.bool.isRequired
  85. }
  86. LogsCompilationResultIndicator.propTypes = {
  87. logType: PropTypes.string.isRequired,
  88. nLogs: PropTypes.number.isRequired
  89. }
  90. export default PreviewLogsToggleButton