rail.tsx 10 KB

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