view-only-access-modal.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { useEffect, useState } from 'react'
  2. import ViewOnlyAccessModalContent from './view-only-access-modal-content'
  3. import customLocalStorage from '@/infrastructure/local-storage'
  4. import { useProjectContext } from '@/shared/context/project-context'
  5. import { useEditorContext } from '@/shared/context/editor-context'
  6. import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context'
  7. import { sendMB } from '@/infrastructure/event-tracking'
  8. import { OLModal } from '@/shared/components/ol/ol-modal'
  9. const ViewOnlyAccessModal = () => {
  10. const [show, setShow] = useState(false)
  11. const { isProjectOwner, isPendingEditor } = useEditorContext()
  12. const { permissionsLevel } = useIdeReactContext()
  13. const { features, projectId, project } = useProjectContext()
  14. const members = project?.members
  15. const handleHide = () => {
  16. setShow(false)
  17. }
  18. // show the view-only access modal if
  19. // is editor on a project over
  20. // collaborator limit (once every week)
  21. useEffect(() => {
  22. const showModalCooldownHours = 24 * 7 // 7 days
  23. const shouldShowToPendingEditor = () => {
  24. if (isProjectOwner || !features) {
  25. return false
  26. }
  27. if (features.collaborators === -1) {
  28. return false
  29. }
  30. return isPendingEditor
  31. }
  32. if (shouldShowToPendingEditor()) {
  33. const localStorageKey = `last-shown-view-only-access-modal.${projectId}`
  34. const lastShownNeedEditModalTime =
  35. customLocalStorage.getItem(localStorageKey)
  36. if (
  37. !lastShownNeedEditModalTime ||
  38. lastShownNeedEditModalTime + showModalCooldownHours * 60 * 60 * 1000 <
  39. Date.now()
  40. ) {
  41. setShow(true)
  42. customLocalStorage.setItem(localStorageKey, Date.now())
  43. sendMB('notification-prompt', {
  44. name: 'link-sharing-collaborator-limit',
  45. })
  46. }
  47. }
  48. }, [
  49. features,
  50. isProjectOwner,
  51. isPendingEditor,
  52. members,
  53. permissionsLevel,
  54. projectId,
  55. ])
  56. return show ? (
  57. <OLModal
  58. animation
  59. show={show}
  60. onHide={() => {
  61. sendMB('notification-dismiss', {
  62. name: 'link-sharing-collaborator-limit',
  63. })
  64. handleHide()
  65. }}
  66. id="editor-over-limit-modal"
  67. >
  68. <ViewOnlyAccessModalContent handleHide={handleHide} />
  69. </OLModal>
  70. ) : null
  71. }
  72. export default ViewOnlyAccessModal