pdf-logs-button.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { memo, useCallback, useMemo } from 'react'
  2. import { Button } from 'react-bootstrap'
  3. import { usePdfPreviewContext } from '../contexts/pdf-preview-context'
  4. import { PdfLogsButtonContent } from './pdf-logs-button-content'
  5. import { sendMBOnce } from '../../../infrastructure/event-tracking'
  6. function PdfLogsButton() {
  7. const {
  8. autoCompileLintingError,
  9. stopOnValidationError,
  10. error,
  11. logEntries,
  12. showLogs,
  13. setShowLogs,
  14. } = usePdfPreviewContext()
  15. const buttonStyle = useMemo(() => {
  16. if (showLogs) {
  17. return 'default'
  18. }
  19. if (autoCompileLintingError && stopOnValidationError) {
  20. return 'danger'
  21. }
  22. if (logEntries) {
  23. if (logEntries.errors?.length) {
  24. return 'danger'
  25. }
  26. if (logEntries.warnings?.length) {
  27. return 'warning'
  28. }
  29. }
  30. return 'default'
  31. }, [autoCompileLintingError, logEntries, showLogs, stopOnValidationError])
  32. const handleClick = useCallback(() => {
  33. setShowLogs(value => {
  34. if (!value) {
  35. sendMBOnce('ide-open-logs-once')
  36. }
  37. return !value
  38. })
  39. }, [setShowLogs])
  40. return (
  41. <Button
  42. bsSize="xsmall"
  43. bsStyle={buttonStyle}
  44. disabled={Boolean(error)}
  45. className="btn-toggle-logs toolbar-item"
  46. onClick={handleClick}
  47. >
  48. <PdfLogsButtonContent
  49. showLogs={showLogs}
  50. logEntries={logEntries}
  51. autoCompileLintingError={
  52. autoCompileLintingError && stopOnValidationError
  53. }
  54. />
  55. </Button>
  56. )
  57. }
  58. export default memo(PdfLogsButton)