detach-compile-button.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(
  17. () => startOrTriggerCompile(),
  18. [startOrTriggerCompile]
  19. )
  20. return (
  21. <div
  22. className={classnames({
  23. 'btn-recompile-group': true,
  24. 'btn-recompile-group-has-changes': hasChanges,
  25. })}
  26. >
  27. <PdfCompileButtonInner
  28. startCompile={handleStartCompile}
  29. compiling={compiling}
  30. />
  31. </div>
  32. )
  33. }
  34. export function DetachCompileButtonWrapper() {
  35. const { detachRole, detachIsLinked } = useLayoutContext(
  36. layoutContextPropTypes
  37. )
  38. if (detachRole !== 'detacher' || !detachIsLinked) {
  39. return null
  40. }
  41. return <DetachCompileButton />
  42. }
  43. const layoutContextPropTypes = {
  44. detachRole: PropTypes.string,
  45. detachIsLinked: PropTypes.bool,
  46. }
  47. export default memo(DetachCompileButtonWrapper)