pdf-logs-button.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 { codeCheckFailed, error, logEntries, showLogs, setShowLogs } =
  8. useCompileContext()
  9. const buttonStyle = useMemo(() => {
  10. if (showLogs) {
  11. return 'default'
  12. }
  13. if (codeCheckFailed) {
  14. return 'danger'
  15. }
  16. if (logEntries) {
  17. if (logEntries.errors?.length) {
  18. return 'danger'
  19. }
  20. if (logEntries.warnings?.length) {
  21. return 'warning'
  22. }
  23. }
  24. return 'default'
  25. }, [codeCheckFailed, logEntries, showLogs])
  26. const handleClick = useCallback(() => {
  27. setShowLogs(value => {
  28. if (!value) {
  29. sendMBOnce('ide-open-logs-once')
  30. }
  31. return !value
  32. })
  33. }, [setShowLogs])
  34. return (
  35. <Button
  36. bsSize="xsmall"
  37. bsStyle={buttonStyle}
  38. disabled={Boolean(error)}
  39. className="btn-toggle-logs toolbar-item"
  40. onClick={handleClick}
  41. >
  42. <PdfLogsButtonContent
  43. showLogs={showLogs}
  44. logEntries={logEntries}
  45. codeCheckFailed={codeCheckFailed}
  46. />
  47. </Button>
  48. )
  49. }
  50. export default memo(PdfLogsButton)