2020-09-29 07:08:49 -04:00
|
|
|
import React from 'react'
|
2020-11-09 09:52:22 -05:00
|
|
|
import { OverlayTrigger, Tooltip } from 'react-bootstrap'
|
2020-09-29 07:08:49 -04:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import classNames from 'classnames'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import Icon from '../../../shared/components/icon'
|
|
|
|
|
2021-02-17 09:07:21 -05:00
|
|
|
const MAX_ERRORS_COUNT = 99
|
|
|
|
|
2020-09-29 07:08:49 -04:00
|
|
|
function PreviewLogsToggleButton({
|
|
|
|
onToggle,
|
|
|
|
showLogs,
|
2021-03-18 09:49:01 -04:00
|
|
|
autoCompileLintingError = false,
|
2020-11-26 04:58:42 -05:00
|
|
|
compileFailed = false,
|
2020-11-09 09:52:22 -05:00
|
|
|
logsState: { nErrors, nWarnings },
|
|
|
|
showText
|
2020-09-29 07:08:49 -04:00
|
|
|
}) {
|
2020-11-09 09:52:22 -05:00
|
|
|
const { t } = useTranslation()
|
|
|
|
let textStyle = {}
|
2021-03-18 09:49:01 -04:00
|
|
|
let btnColorCssClass = 'btn-default'
|
|
|
|
let buttonContents
|
|
|
|
|
2020-11-09 09:52:22 -05:00
|
|
|
if (!showText) {
|
|
|
|
textStyle = {
|
|
|
|
position: 'absolute',
|
|
|
|
right: '-100vw'
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 07:08:49 -04:00
|
|
|
|
|
|
|
function handleOnClick(e) {
|
|
|
|
e.currentTarget.blur()
|
|
|
|
onToggle()
|
|
|
|
}
|
|
|
|
|
2021-03-18 09:49:01 -04:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2020-11-09 09:52:22 -05:00
|
|
|
const buttonElement = (
|
2020-09-29 07:08:49 -04:00
|
|
|
<button
|
2020-11-09 09:52:22 -05:00
|
|
|
id="logs-toggle"
|
2020-09-29 07:08:49 -04:00
|
|
|
type="button"
|
2020-11-26 04:58:42 -05:00
|
|
|
disabled={compileFailed}
|
2021-03-18 09:49:01 -04:00
|
|
|
className={buttonClasses}
|
2020-09-29 07:08:49 -04:00
|
|
|
onClick={handleOnClick}
|
|
|
|
>
|
2021-03-18 09:49:01 -04:00
|
|
|
{buttonContents}
|
2020-09-29 07:08:49 -04:00
|
|
|
</button>
|
|
|
|
)
|
2020-11-09 09:52:22 -05:00
|
|
|
|
|
|
|
return showText ? (
|
|
|
|
buttonElement
|
|
|
|
) : (
|
|
|
|
<OverlayTrigger
|
|
|
|
placement="bottom"
|
|
|
|
overlay={
|
|
|
|
<Tooltip id="tooltip-logs-toggle">
|
|
|
|
{showLogs ? t('view_pdf') : t('view_logs')}
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{buttonElement}
|
|
|
|
</OverlayTrigger>
|
|
|
|
)
|
2020-09-29 07:08:49 -04:00
|
|
|
}
|
|
|
|
|
2021-03-18 09:49:01 -04:00
|
|
|
function CompilationResult({
|
|
|
|
textStyle,
|
|
|
|
autoCompileLintingError,
|
|
|
|
nErrors,
|
|
|
|
nWarnings
|
|
|
|
}) {
|
|
|
|
if (autoCompileLintingError) {
|
|
|
|
return <AutoCompileLintingError textStyle={textStyle} />
|
|
|
|
} else if (nErrors || nWarnings) {
|
2020-10-06 11:40:16 -04:00
|
|
|
return (
|
2021-03-18 09:49:01 -04:00
|
|
|
<LogsCompilationResult
|
2020-10-06 11:40:16 -04:00
|
|
|
logType={nErrors ? 'errors' : 'warnings'}
|
|
|
|
nLogs={nErrors || nWarnings}
|
2020-11-09 09:52:22 -05:00
|
|
|
textStyle={textStyle}
|
2020-10-06 11:40:16 -04:00
|
|
|
/>
|
|
|
|
)
|
2020-09-29 07:08:49 -04:00
|
|
|
} else {
|
2021-03-18 09:49:01 -04:00
|
|
|
return <ViewLogs textStyle={textStyle} />
|
2020-09-29 07:08:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 09:49:01 -04:00
|
|
|
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 }) {
|
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-11-09 09:52:22 -05:00
|
|
|
<span
|
|
|
|
className="btn-toggle-logs-label toolbar-text"
|
|
|
|
aria-label={label}
|
|
|
|
style={textStyle}
|
|
|
|
>
|
2021-02-17 09:07:21 -05:00
|
|
|
{`${label} (${
|
|
|
|
nLogs > MAX_ERRORS_COUNT ? `${MAX_ERRORS_COUNT}+` : nLogs
|
|
|
|
})`}
|
2020-09-29 07:08:49 -04:00
|
|
|
</span>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-18 09:49:01 -04:00
|
|
|
function AutoCompileLintingError({ textStyle }) {
|
2020-09-29 07:08:49 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
return (
|
|
|
|
<>
|
2021-03-18 09:49:01 -04:00
|
|
|
<Icon type="exclamation-triangle" />
|
2020-11-09 09:52:22 -05:00
|
|
|
<span className="toolbar-text" style={textStyle}>
|
2021-03-18 09:49:01 -04:00
|
|
|
{t('code_check_failed')}
|
2020-11-09 09:52:22 -05:00
|
|
|
</span>
|
2020-09-29 07:08:49 -04:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-18 09:49:01 -04:00
|
|
|
function ViewLogs({ textStyle }) {
|
2020-09-29 07:08:49 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
return (
|
|
|
|
<>
|
2021-03-18 09:49:01 -04:00
|
|
|
<Icon type="file-text-o" />
|
2020-11-09 09:52:22 -05:00
|
|
|
<span className="toolbar-text" style={textStyle}>
|
2021-03-18 09:49:01 -04:00
|
|
|
{t('view_logs')}
|
2020-11-09 09:52:22 -05:00
|
|
|
</span>
|
2020-09-29 07:08:49 -04:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
PreviewLogsToggleButton.propTypes = {
|
|
|
|
onToggle: PropTypes.func.isRequired,
|
|
|
|
logsState: PropTypes.shape({
|
|
|
|
nErrors: PropTypes.number.isRequired,
|
2021-03-18 09:49:01 -04:00
|
|
|
nWarnings: PropTypes.number.isRequired
|
2020-09-29 07:08:49 -04:00
|
|
|
}),
|
2020-11-09 09:52:22 -05:00
|
|
|
showLogs: PropTypes.bool.isRequired,
|
2020-11-26 04:58:42 -05:00
|
|
|
showText: PropTypes.bool.isRequired,
|
2021-03-18 09:49:01 -04:00
|
|
|
compileFailed: PropTypes.bool,
|
|
|
|
autoCompileLintingError: PropTypes.bool
|
2020-09-29 07:08:49 -04:00
|
|
|
}
|
|
|
|
|
2021-03-18 09:49:01 -04:00
|
|
|
CompilationResult.propTypes = {
|
|
|
|
textStyle: PropTypes.object.isRequired,
|
|
|
|
autoCompileLintingError: PropTypes.bool,
|
|
|
|
nErrors: PropTypes.number.isRequired,
|
|
|
|
nWarnings: PropTypes.number.isRequired
|
|
|
|
}
|
|
|
|
|
|
|
|
LogsCompilationResult.propTypes = {
|
2020-10-06 11:40:16 -04:00
|
|
|
logType: PropTypes.string.isRequired,
|
2020-11-09 09:52:22 -05:00
|
|
|
nLogs: PropTypes.number.isRequired,
|
|
|
|
textStyle: PropTypes.object.isRequired
|
|
|
|
}
|
|
|
|
|
2021-03-18 09:49:01 -04:00
|
|
|
AutoCompileLintingError.propTypes = {
|
|
|
|
textStyle: PropTypes.object.isRequired
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewLogs.propTypes = {
|
2020-11-09 09:52:22 -05:00
|
|
|
textStyle: PropTypes.object.isRequired
|
|
|
|
}
|
|
|
|
|
2021-03-18 09:49:01 -04:00
|
|
|
ViewPdf.propTypes = {
|
2020-11-09 09:52:22 -05:00
|
|
|
textStyle: PropTypes.object.isRequired
|
2020-09-29 07:08:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default PreviewLogsToggleButton
|