new-project-button.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { useCallback, useState } from 'react'
  2. import { Dropdown, MenuItem } from 'react-bootstrap'
  3. import { useTranslation } from 'react-i18next'
  4. import { ExposedSettings } from '../../../../../types/exposed-settings'
  5. import type { PortalTemplate } from '../../../../../types/portal-template'
  6. import ControlledDropdown from '../../../shared/components/controlled-dropdown'
  7. import getMeta from '../../../utils/meta'
  8. import NewProjectButtonModal, {
  9. NewProjectButtonModalVariant,
  10. } from './new-project-button/new-project-button-modal'
  11. import { Nullable } from '../../../../../types/utils'
  12. import { sendMB } from '../../../infrastructure/event-tracking'
  13. type SendTrackingEvent = {
  14. dropdownMenu: string
  15. dropdownOpen: boolean
  16. institutionTemplateName?: string
  17. }
  18. type Segmentation = SendTrackingEvent & {
  19. 'project-dashboard-react': 'enabled'
  20. }
  21. type ModalMenuClickOptions = {
  22. modalVariant: NewProjectButtonModalVariant
  23. dropdownMenuEvent: string
  24. }
  25. type NewProjectButtonProps = {
  26. id: string
  27. buttonText?: string
  28. className?: string
  29. trackingKey?: string
  30. }
  31. function NewProjectButton({
  32. id,
  33. buttonText,
  34. className,
  35. trackingKey,
  36. }: NewProjectButtonProps) {
  37. const { t } = useTranslation()
  38. const { templateLinks } = getMeta('ol-ExposedSettings') as ExposedSettings
  39. const [modal, setModal] =
  40. useState<Nullable<NewProjectButtonModalVariant>>(null)
  41. const portalTemplates = getMeta('ol-portalTemplates') as PortalTemplate[]
  42. const sendTrackingEvent = useCallback(
  43. ({
  44. dropdownMenu,
  45. dropdownOpen,
  46. institutionTemplateName,
  47. }: SendTrackingEvent) => {
  48. if (trackingKey) {
  49. let segmentation: Segmentation = {
  50. 'project-dashboard-react': 'enabled',
  51. dropdownMenu,
  52. dropdownOpen,
  53. }
  54. if (institutionTemplateName) {
  55. segmentation = {
  56. ...segmentation,
  57. institutionTemplateName,
  58. }
  59. }
  60. sendMB(trackingKey, segmentation)
  61. }
  62. },
  63. [trackingKey]
  64. )
  65. const handleMainButtonClick = useCallback(
  66. (dropdownOpen: boolean) => {
  67. sendTrackingEvent({
  68. dropdownMenu: 'main-button',
  69. dropdownOpen,
  70. })
  71. },
  72. [sendTrackingEvent]
  73. )
  74. const handleModalMenuClick = useCallback(
  75. (
  76. e: React.MouseEvent<Record<string, unknown>>,
  77. { modalVariant, dropdownMenuEvent }: ModalMenuClickOptions
  78. ) => {
  79. // avoid invoking the "onClick" callback on the main dropdown button
  80. e.stopPropagation()
  81. sendTrackingEvent({
  82. dropdownMenu: dropdownMenuEvent,
  83. dropdownOpen: true,
  84. })
  85. setModal(modalVariant)
  86. },
  87. [sendTrackingEvent]
  88. )
  89. const handlePortalTemplateClick = useCallback(
  90. (
  91. e: React.MouseEvent<Record<string, unknown>>,
  92. institutionTemplateName: string
  93. ) => {
  94. // avoid invoking the "onClick" callback on the main dropdown button
  95. e.stopPropagation()
  96. sendTrackingEvent({
  97. dropdownMenu: 'institution-template',
  98. dropdownOpen: true,
  99. institutionTemplateName,
  100. })
  101. },
  102. [sendTrackingEvent]
  103. )
  104. const handleStaticTemplateClick = useCallback(
  105. (
  106. e: React.MouseEvent<Record<string, unknown>>,
  107. templateTrackingKey: string
  108. ) => {
  109. // avoid invoking the "onClick" callback on the main dropdown button
  110. e.stopPropagation()
  111. sendTrackingEvent({
  112. dropdownMenu: templateTrackingKey,
  113. dropdownOpen: true,
  114. })
  115. },
  116. [sendTrackingEvent]
  117. )
  118. return (
  119. <>
  120. <ControlledDropdown
  121. id={id}
  122. className={className}
  123. onMainButtonClick={handleMainButtonClick}
  124. >
  125. <Dropdown.Toggle
  126. noCaret
  127. className="new-project-button"
  128. bsStyle="primary"
  129. >
  130. {buttonText || t('new_project')}
  131. </Dropdown.Toggle>
  132. <Dropdown.Menu>
  133. <MenuItem
  134. onClick={e =>
  135. handleModalMenuClick(e, {
  136. modalVariant: 'blank_project',
  137. dropdownMenuEvent: 'blank-project',
  138. })
  139. }
  140. >
  141. {t('blank_project')}
  142. </MenuItem>
  143. <MenuItem
  144. onClick={e =>
  145. handleModalMenuClick(e, {
  146. modalVariant: 'example_project',
  147. dropdownMenuEvent: 'example-project',
  148. })
  149. }
  150. >
  151. {t('example_project')}
  152. </MenuItem>
  153. <MenuItem
  154. onClick={e =>
  155. handleModalMenuClick(e, {
  156. modalVariant: 'upload_project',
  157. dropdownMenuEvent: 'upload-project',
  158. })
  159. }
  160. >
  161. {t('upload_project')}
  162. </MenuItem>
  163. <MenuItem
  164. onClick={e =>
  165. handleModalMenuClick(e, {
  166. modalVariant: 'import_from_github',
  167. dropdownMenuEvent: 'import-from-github',
  168. })
  169. }
  170. >
  171. {t('import_from_github')}
  172. </MenuItem>
  173. {portalTemplates?.length > 0 ? (
  174. <>
  175. <MenuItem divider />
  176. <MenuItem header>
  177. {`${t('institution')} ${t('templates')}`}
  178. </MenuItem>
  179. {portalTemplates.map((portalTemplate, index) => (
  180. <MenuItem
  181. key={`portal-template-${index}`}
  182. href={`${portalTemplate.url}#templates`}
  183. onClick={e =>
  184. handlePortalTemplateClick(e, portalTemplate.name)
  185. }
  186. >
  187. {portalTemplate.name}
  188. </MenuItem>
  189. ))}
  190. </>
  191. ) : null}
  192. <MenuItem divider />
  193. <MenuItem header>{t('templates')}</MenuItem>
  194. {templateLinks.map((templateLink, index) => (
  195. <MenuItem
  196. key={`new-project-button-template-${index}`}
  197. href={templateLink.url}
  198. onClick={e =>
  199. handleStaticTemplateClick(e, templateLink.trackingKey)
  200. }
  201. >
  202. {templateLink.name === 'view_all'
  203. ? t('view_all')
  204. : templateLink.name}
  205. </MenuItem>
  206. ))}
  207. </Dropdown.Menu>
  208. </ControlledDropdown>
  209. <NewProjectButtonModal modal={modal} onHide={() => setModal(null)} />
  210. </>
  211. )
  212. }
  213. export default NewProjectButton