view-only-access-modal.tsx 2.3 KB

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