2020-09-29 07:08:49 -04:00
|
|
|
import React from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import classNames from 'classnames'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import Icon from '../../../shared/components/icon'
|
|
|
|
|
|
|
|
function PreviewLogsToggleButton({
|
|
|
|
onToggle,
|
|
|
|
showLogs,
|
|
|
|
logsState: { nErrors, nWarnings }
|
|
|
|
}) {
|
|
|
|
const toggleButtonClasses = classNames('btn', 'btn-xs', 'btn-toggle-logs', {
|
|
|
|
'btn-danger': !showLogs && nErrors,
|
|
|
|
'btn-warning': !showLogs && !nErrors && nWarnings,
|
|
|
|
'btn-default': showLogs || (!nErrors && !nWarnings)
|
|
|
|
})
|
|
|
|
|
|
|
|
function handleOnClick(e) {
|
|
|
|
e.currentTarget.blur()
|
|
|
|
onToggle()
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className={toggleButtonClasses}
|
|
|
|
onClick={handleOnClick}
|
|
|
|
>
|
|
|
|
{showLogs ? (
|
|
|
|
<ViewPdfButton />
|
|
|
|
) : (
|
|
|
|
<CompilationResultIndicator nErrors={nErrors} nWarnings={nWarnings} />
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function CompilationResultIndicator({ nErrors, nWarnings }) {
|
2020-10-06 11:40:16 -04:00
|
|
|
if (nErrors || nWarnings) {
|
|
|
|
return (
|
|
|
|
<LogsCompilationResultIndicator
|
|
|
|
logType={nErrors ? 'errors' : 'warnings'}
|
|
|
|
nLogs={nErrors || nWarnings}
|
|
|
|
/>
|
|
|
|
)
|
2020-09-29 07:08:49 -04:00
|
|
|
} else {
|
|
|
|
return <ViewLogsButton />
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 11:40:16 -04:00
|
|
|
function LogsCompilationResultIndicator({ logType, nLogs }) {
|
2020-09-29 07:08:49 -04:00
|
|
|
const { t } = useTranslation()
|
2020-10-06 11:40:16 -04:00
|
|
|
const label =
|
|
|
|
logType === 'errors' ? t('your_project_has_errors') : t('view_warnings')
|
2020-09-29 07:08:49 -04:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Icon type="file-text-o" />
|
2020-10-06 11:40:16 -04:00
|
|
|
<span className="btn-toggle-logs-label" aria-label={label}>
|
|
|
|
{`${label} (${nLogs > 9 ? '9+' : nLogs})`}
|
2020-09-29 07:08:49 -04:00
|
|
|
</span>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function ViewLogsButton() {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Icon type="file-text-o" />
|
|
|
|
<span className="btn-toggle-logs-label">{t('view_logs')}</span>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function ViewPdfButton() {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Icon type="file-pdf-o" />
|
|
|
|
<span className="btn-toggle-logs-label">{t('view_pdf')}</span>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
PreviewLogsToggleButton.propTypes = {
|
|
|
|
onToggle: PropTypes.func.isRequired,
|
|
|
|
logsState: PropTypes.shape({
|
|
|
|
nErrors: PropTypes.number.isRequired,
|
|
|
|
nWarnings: PropTypes.number.isRequired,
|
|
|
|
nLogEntries: PropTypes.number.isRequired
|
|
|
|
}),
|
|
|
|
showLogs: PropTypes.bool.isRequired
|
|
|
|
}
|
|
|
|
|
2020-10-06 11:40:16 -04:00
|
|
|
LogsCompilationResultIndicator.propTypes = {
|
|
|
|
logType: PropTypes.string.isRequired,
|
|
|
|
nLogs: PropTypes.number.isRequired
|
2020-09-29 07:08:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default PreviewLogsToggleButton
|