| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- import { OverlayTrigger, Tooltip } from 'react-bootstrap'
- import PropTypes from 'prop-types'
- import classNames from 'classnames'
- import { useTranslation } from 'react-i18next'
- import Icon from '../../../shared/components/icon'
- const MAX_ERRORS_COUNT = 99
- function PreviewLogsToggleButton({
- onToggle,
- showLogs,
- autoCompileLintingError = false,
- compileFailed = false,
- logsState: { nErrors, nWarnings },
- showText,
- }) {
- const { t } = useTranslation()
- let textStyle = {}
- let btnColorCssClass = 'btn-default'
- let buttonContents
- if (!showText) {
- textStyle = {
- position: 'absolute',
- right: '-100vw',
- }
- }
- function handleOnClick(e) {
- e.currentTarget.blur()
- onToggle()
- }
- if (showLogs) {
- buttonContents = <ViewPdf textStyle={textStyle} />
- } else {
- buttonContents = (
- <CompilationResult
- textStyle={textStyle}
- autoCompileLintingError={autoCompileLintingError}
- nErrors={nErrors}
- nWarnings={nWarnings}
- />
- )
- if (autoCompileLintingError || nErrors > 0) {
- btnColorCssClass = 'btn-danger'
- } else if (nWarnings > 0) {
- btnColorCssClass = 'btn-warning'
- }
- }
- const buttonClasses = classNames(
- 'btn',
- 'btn-xs',
- 'btn-toggle-logs',
- 'toolbar-item',
- btnColorCssClass
- )
- const buttonElement = (
- <button
- id="logs-toggle"
- type="button"
- disabled={compileFailed}
- className={buttonClasses}
- onClick={handleOnClick}
- >
- {buttonContents}
- </button>
- )
- return showText ? (
- buttonElement
- ) : (
- <OverlayTrigger
- placement="bottom"
- overlay={
- <Tooltip id="tooltip-logs-toggle">
- {showLogs ? t('view_pdf') : t('view_logs')}
- </Tooltip>
- }
- >
- {buttonElement}
- </OverlayTrigger>
- )
- }
- function CompilationResult({
- textStyle,
- autoCompileLintingError,
- nErrors,
- nWarnings,
- }) {
- if (autoCompileLintingError) {
- return <AutoCompileLintingError textStyle={textStyle} />
- } else if (nErrors || nWarnings) {
- return (
- <LogsCompilationResult
- logType={nErrors ? 'errors' : 'warnings'}
- nLogs={nErrors || nWarnings}
- textStyle={textStyle}
- />
- )
- } else {
- return <ViewLogs textStyle={textStyle} />
- }
- }
- function ViewPdf({ textStyle }) {
- const { t } = useTranslation()
- return (
- <>
- <Icon type="file-pdf-o" />
- <span className="toolbar-text" style={textStyle}>
- {t('view_pdf')}
- </span>
- </>
- )
- }
- function LogsCompilationResult({ textStyle, logType, nLogs }) {
- const { t } = useTranslation()
- const label =
- logType === 'errors' ? t('your_project_has_errors') : t('view_warnings')
- return (
- <>
- <Icon type="file-text-o" />
- <span
- className="btn-toggle-logs-label toolbar-text"
- aria-label={label}
- style={textStyle}
- >
- {`${label} (${
- nLogs > MAX_ERRORS_COUNT ? `${MAX_ERRORS_COUNT}+` : nLogs
- })`}
- </span>
- </>
- )
- }
- function AutoCompileLintingError({ textStyle }) {
- const { t } = useTranslation()
- return (
- <>
- <Icon type="exclamation-triangle" />
- <span className="toolbar-text" style={textStyle}>
- {t('code_check_failed')}
- </span>
- </>
- )
- }
- function ViewLogs({ textStyle }) {
- const { t } = useTranslation()
- return (
- <>
- <Icon type="file-text-o" />
- <span className="toolbar-text" style={textStyle}>
- {t('view_logs')}
- </span>
- </>
- )
- }
- PreviewLogsToggleButton.propTypes = {
- onToggle: PropTypes.func.isRequired,
- logsState: PropTypes.shape({
- nErrors: PropTypes.number.isRequired,
- nWarnings: PropTypes.number.isRequired,
- }),
- showLogs: PropTypes.bool.isRequired,
- showText: PropTypes.bool.isRequired,
- compileFailed: PropTypes.bool,
- autoCompileLintingError: PropTypes.bool,
- }
- CompilationResult.propTypes = {
- textStyle: PropTypes.object.isRequired,
- autoCompileLintingError: PropTypes.bool,
- nErrors: PropTypes.number.isRequired,
- nWarnings: PropTypes.number.isRequired,
- }
- LogsCompilationResult.propTypes = {
- logType: PropTypes.string.isRequired,
- nLogs: PropTypes.number.isRequired,
- textStyle: PropTypes.object.isRequired,
- }
- AutoCompileLintingError.propTypes = {
- textStyle: PropTypes.object.isRequired,
- }
- ViewLogs.propTypes = {
- textStyle: PropTypes.object.isRequired,
- }
- ViewPdf.propTypes = {
- textStyle: PropTypes.object.isRequired,
- }
- export default PreviewLogsToggleButton
|