compile-timeout-messages.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { memo, useCallback, useEffect, useMemo, useState } from 'react'
  2. import * as eventTracking from '@/infrastructure/event-tracking'
  3. import { useDetachCompileContext } from '@/shared/context/detach-compile-context'
  4. import usePersistedState from '@/shared/hooks/use-persisted-state'
  5. import { CompileTimeoutWarning } from '@/features/pdf-preview/components/compile-timeout-warning'
  6. import { CompileTimeoutChangingSoon } from '@/features/pdf-preview/components/compile-timeout-changing-soon'
  7. function CompileTimeoutMessages() {
  8. const {
  9. showNewCompileTimeoutUI,
  10. isProjectOwner,
  11. deliveryLatencies,
  12. compiling,
  13. showLogs,
  14. error,
  15. } = useDetachCompileContext()
  16. const [showWarning, setShowWarning] = useState(false)
  17. const [showChangingSoon, setShowChangingSoon] = useState(false)
  18. const [dismissedUntilWarning, setDismissedUntilWarning] = usePersistedState<
  19. Date | undefined
  20. >(`has-dismissed-10s-compile-time-warning-until`)
  21. const segmentation = useMemo(() => {
  22. return {
  23. newCompileTimeout: showNewCompileTimeoutUI,
  24. isProjectOwner,
  25. }
  26. }, [showNewCompileTimeoutUI, isProjectOwner])
  27. const handleNewCompile = useCallback(
  28. compileTime => {
  29. setShowWarning(false)
  30. setShowChangingSoon(false)
  31. if (compileTime > 20000) {
  32. if (showNewCompileTimeoutUI === 'changing') {
  33. setShowChangingSoon(true)
  34. eventTracking.sendMB('compile-time-warning-displayed', {
  35. time: 20,
  36. ...segmentation,
  37. })
  38. }
  39. } else if (compileTime > 10000) {
  40. setShowChangingSoon(false)
  41. if (
  42. (isProjectOwner && showNewCompileTimeoutUI === 'active') ||
  43. showNewCompileTimeoutUI === 'changing'
  44. ) {
  45. if (
  46. !dismissedUntilWarning ||
  47. new Date(dismissedUntilWarning) < new Date()
  48. ) {
  49. setShowWarning(true)
  50. eventTracking.sendMB('compile-time-warning-displayed', {
  51. time: 10,
  52. ...segmentation,
  53. })
  54. }
  55. }
  56. }
  57. },
  58. [
  59. isProjectOwner,
  60. showNewCompileTimeoutUI,
  61. dismissedUntilWarning,
  62. segmentation,
  63. ]
  64. )
  65. const handleDismissWarning = useCallback(() => {
  66. eventTracking.sendMB('compile-time-warning-dismissed', {
  67. time: 10,
  68. ...segmentation,
  69. })
  70. setShowWarning(false)
  71. const until = new Date()
  72. until.setDate(until.getDate() + 1) // 1 day
  73. setDismissedUntilWarning(until)
  74. }, [setDismissedUntilWarning, segmentation])
  75. const handleDismissChangingSoon = useCallback(() => {
  76. eventTracking.sendMB('compile-time-warning-dismissed', {
  77. time: 20,
  78. ...segmentation,
  79. })
  80. }, [segmentation])
  81. useEffect(() => {
  82. if (compiling || error || showLogs) return
  83. handleNewCompile(deliveryLatencies.compileTimeServerE2E)
  84. }, [compiling, error, showLogs, deliveryLatencies, handleNewCompile])
  85. if (!window.ExposedSettings.enableSubscriptions) {
  86. return null
  87. }
  88. if (compiling || error || showLogs) {
  89. return null
  90. }
  91. if (!showWarning && !showChangingSoon) {
  92. return null
  93. }
  94. // if showWarning is true then the 10s warning is shown
  95. // and if showChangingSoon is true then the 20s-60s should show
  96. return (
  97. <div>
  98. {showWarning && isProjectOwner && (
  99. <CompileTimeoutWarning
  100. showNewCompileTimeoutUI={showNewCompileTimeoutUI}
  101. handleDismissWarning={handleDismissWarning}
  102. />
  103. )}
  104. {showChangingSoon && (
  105. <CompileTimeoutChangingSoon
  106. isProjectOwner={isProjectOwner}
  107. handleDismissChangingSoon={handleDismissChangingSoon}
  108. />
  109. )}
  110. </div>
  111. )
  112. }
  113. export default memo(CompileTimeoutMessages)