detach-compile-button.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { memo, useCallback } from 'react'
  2. import PropTypes from 'prop-types'
  3. import classnames from 'classnames'
  4. import { useLayoutContext } from '../../../shared/context/layout-context'
  5. import { useCompileContext } from '../../../shared/context/compile-context'
  6. import useDetachAction from '../../../shared/hooks/use-detach-action'
  7. import PdfCompileButtonInner from './pdf-compile-button-inner'
  8. export function DetachCompileButton() {
  9. const { compiling, hasChanges, startCompile } = useCompileContext()
  10. const startOrTriggerCompile = useDetachAction(
  11. 'start-compile',
  12. startCompile,
  13. 'detacher',
  14. 'detached'
  15. )
  16. const handleStartCompile = useCallback(() => startOrTriggerCompile(), [
  17. startOrTriggerCompile,
  18. ])
  19. return (
  20. <div
  21. className={classnames({
  22. 'btn-recompile-group': true,
  23. 'btn-recompile-group-has-changes': hasChanges,
  24. })}
  25. >
  26. <PdfCompileButtonInner
  27. startCompile={handleStartCompile}
  28. compiling={compiling}
  29. />
  30. </div>
  31. )
  32. }
  33. export function DetachCompileButtonWrapper() {
  34. const { detachRole, detachIsLinked } = useLayoutContext(
  35. layoutContextPropTypes
  36. )
  37. if (detachRole !== 'detacher' || !detachIsLinked) {
  38. return null
  39. }
  40. return <DetachCompileButton />
  41. }
  42. const layoutContextPropTypes = {
  43. detachRole: PropTypes.string,
  44. detachIsLinked: PropTypes.bool,
  45. }
  46. export default memo(DetachCompileButtonWrapper)