pdf-logs-button.js 1.4 KB

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