rail.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. import { FC, ReactElement, useCallback, useMemo } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { Nav, NavLink, Tab, TabContainer } from 'react-bootstrap'
  4. import MaterialIcon, {
  5. AvailableUnfilledIcon,
  6. } from '@/shared/components/material-icon'
  7. import { Panel } from 'react-resizable-panels'
  8. import { useLayoutContext } from '@/shared/context/layout-context'
  9. import ErrorIndicator from './error-logs/error-indicator'
  10. import {
  11. RailModalKey,
  12. RailTabKey,
  13. useRailContext,
  14. } from '../contexts/rail-context'
  15. import FileTreeOutlinePanel from './file-tree/file-tree-outline-panel'
  16. import { ChatIndicator, ChatPane } from './chat/chat'
  17. import getMeta from '@/utils/meta'
  18. import { HorizontalResizeHandle } from '@/features/ide-react/components/resize/horizontal-resize-handle'
  19. import { HorizontalToggler } from '@/features/ide-react/components/resize/horizontal-toggler'
  20. import classNames from 'classnames'
  21. import IntegrationsPanel from './integrations-panel/integrations-panel'
  22. import {
  23. Dropdown,
  24. DropdownDivider,
  25. DropdownItem,
  26. DropdownMenu,
  27. DropdownToggle,
  28. } from '@/features/ui/components/bootstrap-5/dropdown-menu'
  29. import { RailHelpShowHotkeysModal } from './help/keyboard-shortcuts'
  30. import { RailHelpContactUsModal } from './help/contact-us'
  31. import { HistorySidebar } from '@/features/ide-react/components/history-sidebar'
  32. import DictionarySettingsModal from './settings/editor-settings/dictionary-settings-modal'
  33. import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
  34. import OLIconButton from '@/features/ui/components/ol/ol-icon-button'
  35. import { useChatContext } from '@/features/chat/context/chat-context'
  36. import { useEditorAnalytics } from '@/shared/hooks/use-editor-analytics'
  37. import {
  38. FullProjectSearchPanel,
  39. hasFullProjectSearch,
  40. } from './full-project-search-panel'
  41. import { sendSearchEvent } from '@/features/event-tracking/search-events'
  42. import ErrorLogsPanel from './error-logs/error-logs-panel'
  43. import { useDetachCompileContext as useCompileContext } from '@/shared/context/detach-compile-context'
  44. import OldErrorPane from './error-logs/old-error-pane'
  45. import { useFeatureFlag } from '@/shared/context/split-test-context'
  46. type RailElement = {
  47. icon: AvailableUnfilledIcon
  48. key: RailTabKey
  49. component: ReactElement | null
  50. indicator?: ReactElement
  51. title: string
  52. hide?: boolean
  53. disabled?: boolean
  54. }
  55. type RailActionButton = {
  56. key: string
  57. icon: AvailableUnfilledIcon
  58. title: string
  59. action: () => void
  60. }
  61. type RailDropdown = {
  62. key: string
  63. icon: AvailableUnfilledIcon
  64. title: string
  65. dropdown: ReactElement
  66. }
  67. type RailAction = RailDropdown | RailActionButton
  68. const RAIL_MODALS: {
  69. key: RailModalKey
  70. modalComponentFunction: FC<{ show: boolean }>
  71. }[] = [
  72. {
  73. key: 'keyboard-shortcuts',
  74. modalComponentFunction: RailHelpShowHotkeysModal,
  75. },
  76. {
  77. key: 'contact-us',
  78. modalComponentFunction: RailHelpContactUsModal,
  79. },
  80. {
  81. key: 'dictionary',
  82. modalComponentFunction: DictionarySettingsModal,
  83. },
  84. ]
  85. export const RailLayout = () => {
  86. const { sendEvent } = useEditorAnalytics()
  87. const { t } = useTranslation()
  88. const {
  89. activeModal,
  90. selectedTab,
  91. openTab,
  92. isOpen,
  93. setIsOpen,
  94. panelRef,
  95. handlePaneCollapse,
  96. handlePaneExpand,
  97. togglePane,
  98. setResizing,
  99. } = useRailContext()
  100. const { logEntries } = useCompileContext()
  101. const errorLogsDisabled = !logEntries
  102. const { view, setLeftMenuShown } = useLayoutContext()
  103. const { markMessagesAsRead } = useChatContext()
  104. const isHistoryView = view === 'history'
  105. const newErrorlogs = useFeatureFlag('new-editor-error-logs-redesign')
  106. const railTabs: RailElement[] = useMemo(
  107. () => [
  108. {
  109. key: 'file-tree',
  110. icon: 'description',
  111. title: t('file_tree'),
  112. component: <FileTreeOutlinePanel />,
  113. },
  114. {
  115. key: 'full-project-search',
  116. icon: 'search',
  117. title: t('project_search'),
  118. component: <FullProjectSearchPanel />,
  119. hide: !hasFullProjectSearch,
  120. },
  121. {
  122. key: 'integrations',
  123. icon: 'integration_instructions',
  124. title: t('integrations'),
  125. component: <IntegrationsPanel />,
  126. },
  127. {
  128. key: 'review-panel',
  129. icon: 'rate_review',
  130. title: t('review_panel'),
  131. component: null,
  132. },
  133. {
  134. key: 'chat',
  135. icon: 'forum',
  136. component: <ChatPane />,
  137. indicator: <ChatIndicator />,
  138. title: t('chat'),
  139. hide: !getMeta('ol-chatEnabled'),
  140. },
  141. {
  142. key: 'errors',
  143. icon: 'report',
  144. title: t('error_log'),
  145. component: newErrorlogs ? <ErrorLogsPanel /> : <OldErrorPane />,
  146. indicator: <ErrorIndicator />,
  147. disabled: errorLogsDisabled,
  148. },
  149. ],
  150. [t, errorLogsDisabled, newErrorlogs]
  151. )
  152. const railActions: RailAction[] = useMemo(
  153. () => [
  154. {
  155. key: 'support',
  156. icon: 'help',
  157. title: t('help'),
  158. dropdown: <RailHelpDropdown />,
  159. },
  160. {
  161. key: 'settings',
  162. icon: 'settings',
  163. title: t('settings'),
  164. action: () => {
  165. sendEvent('rail-click', { tab: 'settings' })
  166. setLeftMenuShown(true)
  167. },
  168. },
  169. ],
  170. [setLeftMenuShown, t, sendEvent]
  171. )
  172. const onTabSelect = useCallback(
  173. (key: string | null) => {
  174. if (key === selectedTab) {
  175. togglePane()
  176. sendEvent('rail-click', { tab: key, type: 'toggle' })
  177. } else {
  178. // HACK: Apparently the onSelect event is triggered with href attributes
  179. // from DropdownItems
  180. if (!railTabs.some(tab => !tab.hide && tab.key === key)) {
  181. // Attempting to open a non-existent tab
  182. return
  183. }
  184. const keyOrDefault = (key ?? 'file-tree') as RailTabKey
  185. // Change the selected tab and make sure it's open
  186. openTab(keyOrDefault)
  187. sendEvent('rail-click', { tab: keyOrDefault })
  188. if (keyOrDefault === 'full-project-search') {
  189. sendSearchEvent('search-open', {
  190. searchType: 'full-project',
  191. method: 'button',
  192. location: 'rail',
  193. })
  194. }
  195. if (key === 'chat') {
  196. markMessagesAsRead()
  197. }
  198. }
  199. },
  200. [openTab, togglePane, selectedTab, railTabs, sendEvent, markMessagesAsRead]
  201. )
  202. const isReviewPanelOpen = selectedTab === 'review-panel'
  203. return (
  204. <TabContainer
  205. mountOnEnter // Only render when necessary (so that we can lazy load tab content)
  206. unmountOnExit={false} // TODO: Should we unmount the tabs when they're not used?
  207. transition={false}
  208. activeKey={selectedTab}
  209. onSelect={onTabSelect}
  210. id="ide-rail-tabs"
  211. >
  212. <div className={classNames('ide-rail', { hidden: isHistoryView })}>
  213. <Nav activeKey={selectedTab} className="ide-rail-tabs-nav">
  214. {railTabs
  215. .filter(({ hide }) => !hide)
  216. .map(({ icon, key, indicator, title, disabled }) => (
  217. <RailTab
  218. open={isOpen && selectedTab === key}
  219. key={key}
  220. eventKey={key}
  221. icon={icon}
  222. indicator={indicator}
  223. title={title}
  224. disabled={disabled}
  225. />
  226. ))}
  227. <div className="flex-grow-1" />
  228. {railActions?.map(action => (
  229. <RailActionElement key={action.key} action={action} />
  230. ))}
  231. </Nav>
  232. </div>
  233. <Panel
  234. id={
  235. newErrorlogs
  236. ? `ide-redesign-sidebar-panel-${selectedTab}`
  237. : 'ide-redesign-sidebar-panel'
  238. }
  239. className={classNames({ hidden: isReviewPanelOpen })}
  240. order={1}
  241. defaultSize={15}
  242. minSize={5}
  243. maxSize={80}
  244. ref={panelRef}
  245. collapsible
  246. onCollapse={handlePaneCollapse}
  247. onExpand={handlePaneExpand}
  248. >
  249. {isHistoryView && <HistorySidebar />}
  250. <div
  251. className={classNames('ide-rail-content', {
  252. hidden: isHistoryView,
  253. })}
  254. >
  255. <Tab.Content className="ide-rail-tab-content">
  256. {railTabs
  257. .filter(({ hide }) => !hide)
  258. .map(({ key, component }) => (
  259. <Tab.Pane eventKey={key} key={key}>
  260. {component}
  261. </Tab.Pane>
  262. ))}
  263. </Tab.Content>
  264. </div>
  265. </Panel>
  266. <HorizontalResizeHandle
  267. className={classNames({ hidden: isReviewPanelOpen })}
  268. resizable
  269. hitAreaMargins={{ coarse: 0, fine: 0 }}
  270. onDoubleClick={togglePane}
  271. onDragging={setResizing}
  272. >
  273. <HorizontalToggler
  274. id="ide-redesign-sidebar-panel"
  275. togglerType="west"
  276. isOpen={isOpen}
  277. setIsOpen={setIsOpen}
  278. tooltipWhenOpen={t('tooltip_hide_panel')}
  279. tooltipWhenClosed={t('tooltip_show_panel')}
  280. />
  281. </HorizontalResizeHandle>
  282. {RAIL_MODALS.map(({ key, modalComponentFunction: Component }) => (
  283. <Component key={key} show={activeModal === key} />
  284. ))}
  285. </TabContainer>
  286. )
  287. }
  288. const RailTab = ({
  289. icon,
  290. eventKey,
  291. open,
  292. indicator,
  293. title,
  294. disabled = false,
  295. }: {
  296. icon: AvailableUnfilledIcon
  297. eventKey: string
  298. open: boolean
  299. indicator?: ReactElement
  300. title: string
  301. disabled?: boolean
  302. }) => {
  303. return (
  304. <OLTooltip
  305. id={`rail-tab-tooltip-${eventKey}`}
  306. description={title}
  307. overlayProps={{ delay: 0, placement: 'right' }}
  308. >
  309. <NavLink
  310. eventKey={eventKey}
  311. className={classNames('ide-rail-tab-link', {
  312. 'open-rail': open,
  313. })}
  314. disabled={disabled}
  315. >
  316. {open ? (
  317. <MaterialIcon
  318. className="ide-rail-tab-link-icon"
  319. type={icon}
  320. accessibilityLabel={title}
  321. />
  322. ) : (
  323. <MaterialIcon
  324. className="ide-rail-tab-link-icon"
  325. type={icon}
  326. accessibilityLabel={title}
  327. unfilled
  328. />
  329. )}
  330. {indicator}
  331. </NavLink>
  332. </OLTooltip>
  333. )
  334. }
  335. const RailActionElement = ({ action }: { action: RailAction }) => {
  336. const onActionClick = useCallback(() => {
  337. if ('action' in action) {
  338. action.action()
  339. }
  340. }, [action])
  341. if ('dropdown' in action) {
  342. return (
  343. <Dropdown align="end" drop="end">
  344. <OLTooltip
  345. id={`rail-dropdown-tooltip-${action.key}`}
  346. description={action.title}
  347. overlayProps={{ delay: 0, placement: 'right' }}
  348. >
  349. <span>
  350. <DropdownToggle
  351. id="rail-help-dropdown-btn"
  352. className="ide-rail-tab-link ide-rail-tab-button ide-rail-tab-dropdown"
  353. as="button"
  354. aria-label={action.title}
  355. >
  356. <MaterialIcon
  357. className="ide-rail-tab-link-icon"
  358. type={action.icon}
  359. unfilled
  360. />
  361. </DropdownToggle>
  362. </span>
  363. </OLTooltip>
  364. {action.dropdown}
  365. </Dropdown>
  366. )
  367. } else {
  368. return (
  369. <OLTooltip
  370. id={`rail-tab-tooltip-${action.key}`}
  371. description={action.title}
  372. overlayProps={{ delay: 0, placement: 'right' }}
  373. >
  374. <OLIconButton
  375. onClick={onActionClick}
  376. className="ide-rail-tab-link ide-rail-tab-button"
  377. icon={action.icon}
  378. accessibilityLabel={action.title}
  379. unfilled
  380. />
  381. </OLTooltip>
  382. )
  383. }
  384. }
  385. const RailHelpDropdown = () => {
  386. const showSupport = getMeta('ol-showSupport')
  387. const { t } = useTranslation()
  388. const { setActiveModal } = useRailContext()
  389. const openKeyboardShortcutsModal = useCallback(() => {
  390. setActiveModal('keyboard-shortcuts')
  391. }, [setActiveModal])
  392. const openContactUsModal = useCallback(() => {
  393. setActiveModal('contact-us')
  394. }, [setActiveModal])
  395. return (
  396. <DropdownMenu>
  397. <DropdownItem onClick={openKeyboardShortcutsModal}>
  398. {t('keyboard_shortcuts')}
  399. </DropdownItem>
  400. <DropdownItem
  401. href="/learn"
  402. role="menuitem"
  403. target="_blank"
  404. rel="noopener noreferrer"
  405. >
  406. {t('documentation')}
  407. </DropdownItem>
  408. <DropdownDivider />
  409. {showSupport && (
  410. <DropdownItem onClick={openContactUsModal}>
  411. {t('contact_us')}
  412. </DropdownItem>
  413. )}
  414. <DropdownItem
  415. href="https://forms.gle/soyVStc5qDx9na1Z6"
  416. role="menuitem"
  417. target="_blank"
  418. rel="noopener noreferrer"
  419. >
  420. {t('give_feedback')}
  421. </DropdownItem>
  422. </DropdownMenu>
  423. )
  424. }