rail.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import { FC, ReactElement, useCallback, useMemo } from 'react'
  2. import { Nav, NavLink, Tab, TabContainer } from 'react-bootstrap-5'
  3. import MaterialIcon, {
  4. AvailableUnfilledIcon,
  5. } from '@/shared/components/material-icon'
  6. import { Panel } from 'react-resizable-panels'
  7. import { useLayoutContext } from '@/shared/context/layout-context'
  8. import { ErrorIndicator, ErrorPane } from './errors'
  9. import {
  10. RailModalKey,
  11. RailTabKey,
  12. useRailContext,
  13. } from '../contexts/rail-context'
  14. import FileTreeOutlinePanel from './file-tree-outline-panel'
  15. import { ChatIndicator, ChatPane } from './chat/chat'
  16. import getMeta from '@/utils/meta'
  17. import { HorizontalResizeHandle } from '@/features/ide-react/components/resize/horizontal-resize-handle'
  18. import { HorizontalToggler } from '@/features/ide-react/components/resize/horizontal-toggler'
  19. import { useTranslation } from 'react-i18next'
  20. import classNames from 'classnames'
  21. import IntegrationsPanel from './integrations-panel/integrations-panel'
  22. import OLButton from '@/features/ui/components/ol/ol-button'
  23. import {
  24. Dropdown,
  25. DropdownDivider,
  26. DropdownItem,
  27. DropdownMenu,
  28. DropdownToggle,
  29. } from '@/features/ui/components/bootstrap-5/dropdown-menu'
  30. import { RailHelpShowHotkeysModal } from './help/keyboard-shortcuts'
  31. import { RailHelpContactUsModal } from './help/contact-us'
  32. import { HistorySidebar } from '@/features/ide-react/components/history-sidebar'
  33. import DictionarySettingsModal from './settings/editor-settings/dictionary-settings-modal'
  34. import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
  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. setSelectedTab,
  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. setSelectedTab((key ?? 'file-tree') as RailTabKey)
  157. setIsOpen(true)
  158. }
  159. },
  160. [setSelectedTab, selectedTab, setIsOpen, togglePane, railTabs]
  161. )
  162. const isReviewPanelOpen = selectedTab === 'review-panel'
  163. return (
  164. <TabContainer
  165. mountOnEnter // Only render when necessary (so that we can lazy load tab content)
  166. unmountOnExit={false} // TODO: Should we unmount the tabs when they're not used?
  167. transition={false}
  168. activeKey={selectedTab}
  169. onSelect={onTabSelect}
  170. id="ide-rail-tabs"
  171. >
  172. <div className={classNames('ide-rail', { hidden: isHistoryView })}>
  173. <Nav activeKey={selectedTab} className="ide-rail-tabs-nav">
  174. {railTabs
  175. .filter(({ hide }) => !hide)
  176. .map(({ icon, key, indicator, title }) => (
  177. <RailTab
  178. open={isOpen && selectedTab === key}
  179. key={key}
  180. eventKey={key}
  181. icon={icon}
  182. indicator={indicator}
  183. title={title}
  184. />
  185. ))}
  186. <div className="flex-grow-1" />
  187. {railActions?.map(action => (
  188. <RailActionElement key={action.key} action={action} />
  189. ))}
  190. </Nav>
  191. </div>
  192. <Panel
  193. id="ide-redesign-sidebar-panel"
  194. className={classNames({ hidden: isReviewPanelOpen })}
  195. order={1}
  196. defaultSize={15}
  197. minSize={5}
  198. maxSize={80}
  199. ref={panelRef}
  200. collapsible
  201. onCollapse={handlePaneCollapse}
  202. onExpand={handlePaneExpand}
  203. >
  204. {isHistoryView && <HistorySidebar />}
  205. <div
  206. className={classNames('ide-rail-content', {
  207. hidden: isHistoryView,
  208. })}
  209. >
  210. <Tab.Content>
  211. {railTabs
  212. .filter(({ hide }) => !hide)
  213. .map(({ key, component }) => (
  214. <Tab.Pane eventKey={key} key={key}>
  215. {component}
  216. </Tab.Pane>
  217. ))}
  218. </Tab.Content>
  219. </div>
  220. </Panel>
  221. <HorizontalResizeHandle
  222. className={classNames({ hidden: isReviewPanelOpen })}
  223. resizable
  224. hitAreaMargins={{ coarse: 0, fine: 0 }}
  225. onDoubleClick={togglePane}
  226. onDragging={setResizing}
  227. >
  228. <HorizontalToggler
  229. id="ide-redesign-sidebar-panel"
  230. togglerType="west"
  231. isOpen={isOpen}
  232. setIsOpen={setIsOpen}
  233. tooltipWhenOpen={t('tooltip_hide_panel')}
  234. tooltipWhenClosed={t('tooltip_show_panel')}
  235. />
  236. </HorizontalResizeHandle>
  237. {RAIL_MODALS.map(({ key, modalComponentFunction: Component }) => (
  238. <Component key={key} show={activeModal === key} />
  239. ))}
  240. </TabContainer>
  241. )
  242. }
  243. const RailTab = ({
  244. icon,
  245. eventKey,
  246. open,
  247. indicator,
  248. title,
  249. }: {
  250. icon: AvailableUnfilledIcon
  251. eventKey: string
  252. open: boolean
  253. indicator?: ReactElement
  254. title: string
  255. }) => {
  256. return (
  257. <OLTooltip
  258. id={`rail-tab-tooltip-${eventKey}`}
  259. description={title}
  260. overlayProps={{ delay: 0, placement: 'right' }}
  261. >
  262. <NavLink
  263. eventKey={eventKey}
  264. className={classNames('ide-rail-tab-link', {
  265. 'open-rail': open,
  266. })}
  267. >
  268. {open ? (
  269. <MaterialIcon className="ide-rail-tab-link-icon" type={icon} />
  270. ) : (
  271. <MaterialIcon
  272. className="ide-rail-tab-link-icon"
  273. type={icon}
  274. unfilled
  275. />
  276. )}
  277. {indicator}
  278. </NavLink>
  279. </OLTooltip>
  280. )
  281. }
  282. const RailActionElement = ({ action }: { action: RailAction }) => {
  283. const icon = (
  284. <MaterialIcon
  285. className="ide-rail-tab-link-icon"
  286. type={action.icon}
  287. unfilled
  288. />
  289. )
  290. const onActionClick = useCallback(() => {
  291. if ('action' in action) {
  292. action.action()
  293. }
  294. }, [action])
  295. if ('dropdown' in action) {
  296. return (
  297. <Dropdown align="end" drop="end">
  298. <OLTooltip
  299. id={`rail-dropdown-tooltip-${action.key}`}
  300. description={action.title}
  301. overlayProps={{ delay: 0, placement: 'right' }}
  302. >
  303. <span>
  304. <DropdownToggle
  305. id="rail-help-dropdown-btn"
  306. className="ide-rail-tab-link ide-rail-tab-button ide-rail-tab-dropdown"
  307. as="button"
  308. >
  309. {icon}
  310. </DropdownToggle>
  311. </span>
  312. </OLTooltip>
  313. {action.dropdown}
  314. </Dropdown>
  315. )
  316. } else {
  317. return (
  318. <OLTooltip
  319. id={`rail-tab-tooltip-${action.key}`}
  320. description={action.title}
  321. overlayProps={{ delay: 0, placement: 'right' }}
  322. >
  323. <button
  324. onClick={onActionClick}
  325. className="ide-rail-tab-link ide-rail-tab-button"
  326. type="button"
  327. >
  328. {icon}
  329. </button>
  330. </OLTooltip>
  331. )
  332. }
  333. }
  334. export const RailPanelHeader: FC<{ title: string }> = ({ title }) => {
  335. const { handlePaneCollapse } = useRailContext()
  336. return (
  337. <header className="rail-panel-header">
  338. <h4 className="rail-panel-title">{title}</h4>
  339. <OLButton
  340. onClick={handlePaneCollapse}
  341. className="rail-panel-header-button-subdued"
  342. size="sm"
  343. >
  344. <MaterialIcon type="close" />
  345. </OLButton>
  346. </header>
  347. )
  348. }
  349. const RailHelpDropdown = () => {
  350. const showSupport = getMeta('ol-showSupport')
  351. const { t } = useTranslation()
  352. const { setActiveModal } = useRailContext()
  353. const openKeyboardShortcutsModal = useCallback(() => {
  354. setActiveModal('keyboard-shortcuts')
  355. }, [setActiveModal])
  356. const openContactUsModal = useCallback(() => {
  357. setActiveModal('contact-us')
  358. }, [setActiveModal])
  359. return (
  360. <DropdownMenu>
  361. <DropdownItem onClick={openKeyboardShortcutsModal}>
  362. {t('keyboard_shortcuts')}
  363. </DropdownItem>
  364. <DropdownItem
  365. href="/learn"
  366. role="menuitem"
  367. target="_blank"
  368. rel="noopener noreferrer"
  369. >
  370. {t('documentation')}
  371. </DropdownItem>
  372. <DropdownDivider />
  373. {showSupport && (
  374. <DropdownItem onClick={openContactUsModal}>
  375. {t('contact_us')}
  376. </DropdownItem>
  377. )}
  378. <DropdownItem
  379. href="https://forms.gle/soyVStc5qDx9na1Z6"
  380. role="menuitem"
  381. target="_blank"
  382. rel="noopener noreferrer"
  383. >
  384. {t('give_feedback')}
  385. </DropdownItem>
  386. </DropdownMenu>
  387. )
  388. }