rail.tsx 13 KB

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