rail.tsx 13 KB

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