rail.tsx 9.7 KB

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