rail.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import { FC, RefObject, useCallback, useEffect, useMemo, useRef } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { Nav, TabContainer } from 'react-bootstrap'
  4. import { useLayoutContext } from '@/shared/context/layout-context'
  5. import { RailTabKey, useRailContext } from '../../contexts/rail-context'
  6. import FileTreeOutlinePanel from '../file-tree/file-tree-outline-panel'
  7. import { ChatIndicator, ChatPane } from '../chat/chat'
  8. import getMeta from '@/utils/meta'
  9. import classNames from 'classnames'
  10. import IntegrationsPanel from '../integrations-panel/integrations-panel'
  11. import { useChatContext } from '@/features/chat/context/chat-context'
  12. import { useEditorAnalytics } from '@/shared/hooks/use-editor-analytics'
  13. import {
  14. FullProjectSearchPanel,
  15. hasFullProjectSearch,
  16. } from '../full-project-search-panel'
  17. import { sendSearchEvent } from '@/features/event-tracking/search-events'
  18. import { useProjectContext } from '@/shared/context/project-context'
  19. import { useCommandProvider } from '@/features/ide-react/hooks/use-command-provider'
  20. import RailHelpDropdown from './rail-help-dropdown'
  21. import RailTab from './rail-tab'
  22. import RailActionElement, { RailAction } from './rail-action-element'
  23. import { RailElement } from '../../utils/rail-types'
  24. import RailPanel from './rail-panel'
  25. import RailResizeHandle from './rail-resize-handle'
  26. import RailModals from './rail-modals'
  27. import RailOverflowDropdown from './rail-overflow-dropdown'
  28. import useRailOverflow from '../../hooks/use-rail-overflow'
  29. import EditorTourRailTooltip from '../editor-tour/editor-tour-rail-tooltip'
  30. import importOverleafModules from '../../../../../macros/import-overleaf-module.macro'
  31. import EditorTourThemeTooltip from '../editor-tour/editor-tour-theme-tooltip'
  32. import EditorTourSwitchBackTooltip from '../editor-tour/editor-tour-switch-back-tooltip'
  33. import { shouldIncludeElement } from '../../utils/rail-utils'
  34. import { useEditorContext } from '@/shared/context/editor-context'
  35. const moduleRailEntries = (
  36. importOverleafModules('railEntries') as {
  37. import: { default: RailElement }
  38. path: string
  39. }[]
  40. ).map(({ import: { default: element } }) => element)
  41. const moduleRailPopovers = (
  42. importOverleafModules('railPopovers') as {
  43. import: {
  44. default: {
  45. key: string
  46. Component: FC<{ ref: RefObject<HTMLAnchorElement> }>
  47. ref: RefObject<HTMLAnchorElement>
  48. hide: boolean | (() => boolean)
  49. }
  50. }
  51. path: string
  52. }[]
  53. ).map(({ import: { default: element } }) => element)
  54. export const RailLayout = () => {
  55. const { sendEvent } = useEditorAnalytics()
  56. const { t } = useTranslation()
  57. const { selectedTab, openTab, isOpen, togglePane } = useRailContext()
  58. const { features } = useProjectContext()
  59. const { isRestrictedTokenMember } = useEditorContext()
  60. const { view, setLeftMenuShown } = useLayoutContext()
  61. const { markMessagesAsRead } = useChatContext()
  62. const isHistoryView = view === 'history'
  63. const fileTreeRef = useRef<HTMLAnchorElement>(null)
  64. const settingsRef = useRef<HTMLButtonElement>(null)
  65. const railTabs: RailElement[] = useMemo(
  66. () => [
  67. {
  68. key: 'file-tree',
  69. icon: 'description',
  70. title: t('file_tree'),
  71. component: <FileTreeOutlinePanel />,
  72. // NOTE: We always need to mount the file tree on first load
  73. // since it is responsible for opening the initial document.
  74. mountOnFirstLoad: true,
  75. ref: fileTreeRef,
  76. },
  77. {
  78. key: 'full-project-search',
  79. icon: 'search',
  80. title: t('project_search'),
  81. component: <FullProjectSearchPanel />,
  82. hide: !hasFullProjectSearch,
  83. },
  84. {
  85. key: 'integrations',
  86. icon: 'integration_instructions',
  87. title: t('integrations'),
  88. component: <IntegrationsPanel />,
  89. },
  90. {
  91. key: 'review-panel',
  92. icon: 'rate_review',
  93. title: t('review_panel'),
  94. component: null,
  95. hide: !features.trackChangesVisible,
  96. disabled: view !== 'editor',
  97. },
  98. {
  99. key: 'chat',
  100. icon: 'forum',
  101. component: <ChatPane />,
  102. indicator: <ChatIndicator />,
  103. title: t('chat'),
  104. hide:
  105. !getMeta('ol-capabilities')?.includes('chat') ||
  106. isRestrictedTokenMember,
  107. },
  108. ...moduleRailEntries,
  109. ],
  110. [t, features.trackChangesVisible, view, isRestrictedTokenMember]
  111. )
  112. const railActions: RailAction[] = useMemo(
  113. () => [
  114. {
  115. key: 'support',
  116. icon: 'help',
  117. title: t('help'),
  118. dropdown: <RailHelpDropdown />,
  119. },
  120. {
  121. key: 'settings',
  122. icon: 'settings',
  123. title: t('settings'),
  124. action: () => {
  125. sendEvent('rail-click', { tab: 'settings' })
  126. setLeftMenuShown(true)
  127. },
  128. ref: settingsRef,
  129. },
  130. ],
  131. [setLeftMenuShown, t, sendEvent]
  132. )
  133. useCommandProvider(
  134. () => [
  135. {
  136. id: 'open-settings',
  137. handler: () => {
  138. setLeftMenuShown(true)
  139. },
  140. label: t('settings'),
  141. },
  142. ],
  143. [t, setLeftMenuShown]
  144. )
  145. const onTabSelect = useCallback(
  146. (key: string | null) => {
  147. if (key === selectedTab) {
  148. togglePane()
  149. sendEvent('rail-click', { tab: key, type: 'toggle' })
  150. } else {
  151. // HACK: Apparently the onSelect event is triggered with href attributes
  152. // from DropdownItems
  153. if (
  154. !railTabs.some(tab =>
  155. typeof tab.hide === 'function'
  156. ? !tab.hide()
  157. : !tab.hide && tab.key === key
  158. )
  159. ) {
  160. // Attempting to open a non-existent tab
  161. return
  162. }
  163. const keyOrDefault = (key ?? 'file-tree') as RailTabKey
  164. // Change the selected tab and make sure it's open
  165. openTab(keyOrDefault)
  166. sendEvent('rail-click', { tab: keyOrDefault })
  167. if (keyOrDefault === 'full-project-search') {
  168. sendSearchEvent('search-open', {
  169. searchType: 'full-project',
  170. method: 'button',
  171. location: 'rail',
  172. })
  173. }
  174. if (key === 'chat') {
  175. markMessagesAsRead()
  176. }
  177. }
  178. },
  179. [openTab, togglePane, selectedTab, railTabs, sendEvent, markMessagesAsRead]
  180. )
  181. useEffect(() => {
  182. const validTabKeys = railTabs
  183. .filter(shouldIncludeElement)
  184. .map(tab => tab.key)
  185. if (!validTabKeys.includes(selectedTab) && isOpen) {
  186. // If the selected tab is no longer valid (e.g. due to permissions changes),
  187. // switch back to the file tree
  188. openTab('file-tree')
  189. }
  190. }, [railTabs, selectedTab, openTab, isOpen])
  191. const isReviewPanelOpen =
  192. selectedTab === 'review-panel' && isOpen && !isHistoryView
  193. const { tabsInRail, tabsInOverflow, tabWrapperRef } =
  194. useRailOverflow(railTabs)
  195. const moreOptionsAction: RailAction = useMemo(() => {
  196. return {
  197. key: 'more-options',
  198. icon: 'more_vert',
  199. title: t('more_options'),
  200. hide: tabsInOverflow.length === 0,
  201. dropdown: (
  202. <RailOverflowDropdown
  203. tabs={tabsInOverflow}
  204. isOpen={isOpen}
  205. selectedTab={selectedTab}
  206. />
  207. ),
  208. }
  209. }, [t, isOpen, selectedTab, tabsInOverflow])
  210. return (
  211. <TabContainer
  212. mountOnEnter // Only render when necessary (so that we can lazy load tab content)
  213. unmountOnExit={false} // TODO: Should we unmount the tabs when they're not used?
  214. transition={false}
  215. activeKey={selectedTab}
  216. onSelect={onTabSelect}
  217. id="ide-rail-tabs"
  218. >
  219. {/* The <Nav> element is a "div" and has a "role="tablist"".
  220. But it should be identified as a navigation landmark.
  221. Therefore, we nest them: the parent <nav> is the landmark, and its child gets the "role="tablist"". */}
  222. <nav
  223. className={classNames('ide-rail', { hidden: isHistoryView })}
  224. aria-label={t('sidebar')}
  225. >
  226. <Nav activeKey={selectedTab} className="ide-rail-tabs-nav">
  227. <div className="ide-rail-tabs-wrapper" ref={tabWrapperRef}>
  228. {tabsInRail
  229. .filter(shouldIncludeElement)
  230. .map(({ icon, key, indicator, title, disabled, ref }) => (
  231. <RailTab
  232. open={isOpen && selectedTab === key}
  233. key={key}
  234. eventKey={key}
  235. icon={icon}
  236. indicator={indicator}
  237. title={title}
  238. disabled={disabled}
  239. ref={ref}
  240. />
  241. ))}
  242. <RailActionElement key="more-options" action={moreOptionsAction} />
  243. </div>
  244. <nav aria-label={t('help_editor_settings')}>
  245. {railActions.map(action => (
  246. <RailActionElement
  247. key={action.key}
  248. action={action}
  249. ref={action.ref}
  250. />
  251. ))}
  252. </nav>
  253. </Nav>
  254. </nav>
  255. <EditorTourRailTooltip target={fileTreeRef.current} />
  256. <EditorTourThemeTooltip target={settingsRef.current} />
  257. <EditorTourSwitchBackTooltip target={settingsRef.current} />
  258. {moduleRailPopovers
  259. .filter(shouldIncludeElement)
  260. .map(({ key, Component, ref }) => (
  261. <Component key={key} ref={ref} />
  262. ))}
  263. <RailPanel
  264. isReviewPanelOpen={isReviewPanelOpen}
  265. isHistoryView={isHistoryView}
  266. railTabs={railTabs}
  267. />
  268. <RailResizeHandle isReviewPanelOpen={isReviewPanelOpen} />
  269. <RailModals />
  270. </TabContainer>
  271. )
  272. }