import { type JSXElementConstructor, useCallback, useState } from 'react' import classnames from 'classnames' import { useTranslation } from 'react-i18next' import getMeta from '../../../utils/meta' import NewProjectButtonModal, { NewProjectButtonModalVariant, } from './new-project-button/new-project-button-modal' import AddAffiliation, { useAddAffiliation } from './add-affiliation' import { Nullable } from '../../../../../types/utils' import { sendMB } from '../../../infrastructure/event-tracking' import importOverleafModules from '../../../../macros/import-overleaf-module.macro' import { Dropdown, DropdownDivider, DropdownHeader, DropdownItem, DropdownMenu, DropdownToggle, } from '@/shared/components/dropdown/dropdown-menu' import type { DropdownProps } from '@/shared/components/types/dropdown-menu-props' import { useSendProjectListMB } from '@/features/project-list/components/project-list-events' import type { PortalTemplate } from '../../../../../types/portal-template' import { useFeatureFlag } from '@/shared/context/split-test-context' import MaterialIcon from '@/shared/components/material-icon' import { useProjectListContext } from '@/features/project-list/context/project-list-context' import { isSplitTestEnabled } from '@/utils/splitTestUtils' type SendTrackingEvent = { dropdownMenu: string dropdownOpen: boolean institutionTemplateName?: string } type Segmentation = SendTrackingEvent & { 'welcome-page-redesign': 'default' } type ModalMenuClickOptions = { modalVariant: NewProjectButtonModalVariant dropdownMenuEvent: string } type NewProjectButtonProps = { id: string buttonText?: string className?: string trackingKey?: string showAddAffiliationWidget?: boolean align?: DropdownProps['align'] } function NewProjectButton({ id, buttonText, className, trackingKey, showAddAffiliationWidget, align, }: NewProjectButtonProps) { const { t } = useTranslation() const { templateLinks } = getMeta('ol-ExposedSettings') const [modal, setModal] = useState>(null) const portalTemplates = getMeta('ol-portalTemplates') || [] const { show: enableAddAffiliationWidget } = useAddAffiliation() const sendProjectListMB = useSendProjectListMB() const docxImportEnabled = useFeatureFlag('import-docx') && getMeta('ol-ExposedSettings').enablePandocConversions const markdownImportEnabled = useFeatureFlag('import-markdown') && getMeta('ol-ExposedSettings').enablePandocConversions const { selectedTagId, tags } = useProjectListContext() const isLibraryEnabled = isSplitTestEnabled('overleaf-library') const initialTags = isLibraryEnabled && selectedTagId ? tags.filter(tag => tag._id === selectedTagId) : [] const sendTrackingEvent = useCallback( ({ dropdownMenu, dropdownOpen, institutionTemplateName, }: SendTrackingEvent) => { if (trackingKey) { let segmentation: Segmentation = { 'welcome-page-redesign': 'default', dropdownMenu, dropdownOpen, } if (institutionTemplateName) { segmentation = { ...segmentation, institutionTemplateName, } } sendMB(trackingKey, segmentation) } }, [trackingKey] ) const handleMainButtonClick = useCallback( (dropdownOpen: boolean) => { sendTrackingEvent({ dropdownMenu: 'main-button', dropdownOpen, }) }, [sendTrackingEvent] ) const handleModalMenuClick = useCallback( ( e: React.MouseEvent, { modalVariant, dropdownMenuEvent }: ModalMenuClickOptions ) => { // avoid invoking the "onClick" callback on the main dropdown button e.stopPropagation() sendTrackingEvent({ dropdownMenu: dropdownMenuEvent, dropdownOpen: true, }) sendProjectListMB('new-project-click', { item: dropdownMenuEvent }) setModal(modalVariant) }, [sendProjectListMB, sendTrackingEvent] ) const handlePortalTemplateClick = useCallback( (e: React.MouseEvent, template: PortalTemplate) => { // avoid invoking the "onClick" callback on the main dropdown button e.stopPropagation() sendTrackingEvent({ dropdownMenu: 'institution-template', dropdownOpen: true, institutionTemplateName: template.name, }) sendProjectListMB('new-project-click', { item: template.name, destinationURL: template.url, }) }, [sendProjectListMB, sendTrackingEvent] ) const handleStaticTemplateClick = useCallback( (e: React.MouseEvent, template: { trackingKey: string; url: string }) => { // avoid invoking the "onClick" callback on the main dropdown button e.stopPropagation() sendTrackingEvent({ dropdownMenu: template.trackingKey, dropdownOpen: true, }) sendProjectListMB('new-project-click', { item: template.trackingKey, destinationURL: template.url, }) }, [sendProjectListMB, sendTrackingEvent] ) const [importProjectFromGithubMenu] = importOverleafModules( 'importProjectFromGithubMenu' ) const ImportProjectFromGithubMenu: JSXElementConstructor<{ onClick: (e: React.MouseEvent) => void }> = importProjectFromGithubMenu?.import.default return ( <> { if (nextShow) sendProjectListMB('new-project-expand', undefined) }} > {buttonText || t('new_project')}
  • handleModalMenuClick(e, { modalVariant: 'blank_project', dropdownMenuEvent: 'blank-project', }) } > {t('blank_project')}
  • handleModalMenuClick(e, { modalVariant: 'example_project', dropdownMenuEvent: 'example-project', }) } > {t('example_project')}
  • handleModalMenuClick(e, { modalVariant: 'upload_project', dropdownMenuEvent: 'upload-project', }) } > {t('upload_project')}
  • {docxImportEnabled && (
  • handleModalMenuClick(e, { modalVariant: 'import_docx', dropdownMenuEvent: 'import-docx', }) } trailingIcon={} > {t('import_word_document')}
  • )} {markdownImportEnabled && (
  • handleModalMenuClick(e, { modalVariant: 'import_markdown', dropdownMenuEvent: 'import-markdown', }) } trailingIcon={} > {t('import_markdown_file')}
  • )}
  • {ImportProjectFromGithubMenu && ( handleModalMenuClick(e, { modalVariant: 'import_from_github', dropdownMenuEvent: 'import-from-github', }) } /> )}
  • {portalTemplates.length > 0 ? ( <> {portalTemplates.map((portalTemplate, index) => (
  • handlePortalTemplateClick(e, portalTemplate)} aria-label={`${portalTemplate.name} ${t('template')}`} > {portalTemplate.name}
  • ))} ) : null} {templateLinks && templateLinks.length > 0 && ( <> )} {templateLinks?.map((templateLink, index) => (
  • handleStaticTemplateClick(e, templateLink)} aria-label={`${templateLink.name} ${t('template')}`} > {templateLink.name === 'view_all' ? t('view_all') : templateLink.name}
  • ))} {showAddAffiliationWidget && enableAddAffiliationWidget ? ( <>
  • ) : null}
    setModal(null)} initialTags={initialTags} /> ) } export default NewProjectButton