main-layout.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { Panel, PanelGroup } from 'react-resizable-panels'
  2. import { FC } from 'react'
  3. import { HorizontalResizeHandle } from '../resize/horizontal-resize-handle'
  4. import classNames from 'classnames'
  5. import { useLayoutContext } from '@/shared/context/layout-context'
  6. import EditorNavigationToolbar from '@/features/ide-react/components/editor-navigation-toolbar'
  7. import ChatPane from '@/features/chat/components/chat-pane'
  8. import { HorizontalToggler } from '@/features/ide-react/components/resize/horizontal-toggler'
  9. import { HistorySidebar } from '@/features/ide-react/components/history-sidebar'
  10. import { HistoryProvider } from '@/features/history/context/history-context'
  11. import History from '@/features/ide-react/components/history'
  12. import EditorSidebar from '@/features/ide-react/components/editor-sidebar'
  13. import { EditorPane } from '@/features/ide-react/components/editor/editor-pane'
  14. import { useFileTree } from '@/features/ide-react/hooks/use-file-tree'
  15. import { useTranslation } from 'react-i18next'
  16. import { useSidebarPane } from '@/features/ide-react/hooks/use-sidebar-pane'
  17. import { useChatPane } from '@/features/ide-react/hooks/use-chat-pane'
  18. import { EditorAndPdf } from '@/features/ide-react/components/editor-and-pdf'
  19. export const MainLayout: FC = () => {
  20. const { view } = useLayoutContext()
  21. const {
  22. isOpen: sidebarIsOpen,
  23. setIsOpen: setSidebarIsOpen,
  24. fixedPanelRef: sidebarPanelRef,
  25. handleLayout: handleSidebarLayout,
  26. togglePane: toggleSidebar,
  27. handlePaneExpand: handleSidebarExpand,
  28. handlePaneCollapse: handleSidebarCollapse,
  29. resizing: sidebarResizing,
  30. setResizing: setSidebarResizing,
  31. } = useSidebarPane()
  32. const {
  33. isOpen: chatIsOpen,
  34. fixedPanelRef: chatPanelRef,
  35. handleLayout: handleChatLayout,
  36. togglePane: toggleChat,
  37. resizing: chatResizing,
  38. setResizing: setChatResizing,
  39. handlePaneCollapse: handleChatCollapse,
  40. handlePaneExpand: handleChatExpand,
  41. } = useChatPane()
  42. const {
  43. selectedEntityCount,
  44. openEntity,
  45. openDocId,
  46. handleFileTreeInit,
  47. handleFileTreeSelect,
  48. handleFileTreeDelete,
  49. } = useFileTree()
  50. const { t } = useTranslation()
  51. // keep the editor pane open
  52. const editorPane = openDocId ? (
  53. <EditorPane
  54. show={openEntity?.type === 'doc' && selectedEntityCount === 1}
  55. />
  56. ) : null
  57. return (
  58. <div className="ide-react-main">
  59. <EditorNavigationToolbar />
  60. <div className="ide-react-body">
  61. <PanelGroup
  62. autoSaveId="ide-outer-layout"
  63. direction="horizontal"
  64. onLayout={handleSidebarLayout}
  65. className={classNames({
  66. 'ide-panel-group-resizing': sidebarResizing || chatResizing,
  67. })}
  68. >
  69. {/* sidebar */}
  70. <Panel
  71. ref={sidebarPanelRef}
  72. id="panel-sidebar"
  73. order={1}
  74. defaultSizePixels={200}
  75. minSizePixels={150}
  76. collapsible
  77. onCollapse={handleSidebarCollapse}
  78. onExpand={handleSidebarExpand}
  79. >
  80. <EditorSidebar
  81. shouldShow={view !== 'history'}
  82. onFileTreeInit={handleFileTreeInit}
  83. onFileTreeSelect={handleFileTreeSelect}
  84. onFileTreeDelete={handleFileTreeDelete}
  85. />
  86. {view === 'history' && <HistorySidebar />}
  87. </Panel>
  88. <HorizontalResizeHandle
  89. onDoubleClick={toggleSidebar}
  90. resizable={sidebarIsOpen}
  91. onDragging={setSidebarResizing}
  92. >
  93. <HorizontalToggler
  94. id="panel-sidebar"
  95. togglerType="west"
  96. isOpen={sidebarIsOpen}
  97. setIsOpen={setSidebarIsOpen}
  98. tooltipWhenOpen={t('tooltip_hide_filetree')}
  99. tooltipWhenClosed={t('tooltip_show_filetree')}
  100. />
  101. </HorizontalResizeHandle>
  102. <Panel id="panel-outer-main" order={2}>
  103. <PanelGroup
  104. autoSaveId="ide-inner-layout"
  105. direction="horizontal"
  106. onLayout={handleChatLayout}
  107. >
  108. <Panel className="ide-react-panel" id="panel-main" order={1}>
  109. {view === 'history' ? (
  110. <HistoryProvider>
  111. <History />
  112. </HistoryProvider>
  113. ) : (
  114. <EditorAndPdf
  115. editorPane={editorPane}
  116. selectedEntityCount={selectedEntityCount}
  117. openEntity={openEntity}
  118. />
  119. )}
  120. </Panel>
  121. <HorizontalResizeHandle
  122. onDoubleClick={toggleChat}
  123. resizable={chatIsOpen}
  124. onDragging={setChatResizing}
  125. />
  126. {/* chat */}
  127. <Panel
  128. ref={chatPanelRef}
  129. id="panel-chat"
  130. order={2}
  131. defaultSizePixels={200}
  132. minSizePixels={150}
  133. collapsible
  134. onCollapse={handleChatCollapse}
  135. onExpand={handleChatExpand}
  136. >
  137. <ChatPane />
  138. </Panel>
  139. </PanelGroup>
  140. </Panel>
  141. </PanelGroup>
  142. </div>
  143. </div>
  144. )
  145. }