rail.tsx 11 KB

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