main-layout.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. resizing: chatResizing,
  37. setResizing: setChatResizing,
  38. } = useChatPane()
  39. const {
  40. selectedEntityCount,
  41. openEntity,
  42. openDocId,
  43. handleFileTreeInit,
  44. handleFileTreeSelect,
  45. handleFileTreeDelete,
  46. } = useFileTree()
  47. const { t } = useTranslation()
  48. // keep the editor pane open
  49. const editorPane = openDocId ? (
  50. <EditorPane
  51. show={openEntity?.type === 'doc' && selectedEntityCount === 1}
  52. />
  53. ) : null
  54. return (
  55. <div className="ide-react-main">
  56. <EditorNavigationToolbar />
  57. <div className="ide-react-body">
  58. <PanelGroup
  59. autoSaveId="ide-outer-layout"
  60. direction="horizontal"
  61. onLayout={handleSidebarLayout}
  62. className={classNames({
  63. 'ide-panel-group-resizing': sidebarResizing || chatResizing,
  64. })}
  65. >
  66. {/* sidebar */}
  67. <Panel
  68. ref={sidebarPanelRef}
  69. id="panel-sidebar"
  70. order={1}
  71. defaultSizePixels={200}
  72. minSizePixels={150}
  73. collapsible
  74. onCollapse={handleSidebarCollapse}
  75. onExpand={handleSidebarExpand}
  76. >
  77. <EditorSidebar
  78. shouldShow={view !== 'history'}
  79. onFileTreeInit={handleFileTreeInit}
  80. onFileTreeSelect={handleFileTreeSelect}
  81. onFileTreeDelete={handleFileTreeDelete}
  82. />
  83. {view === 'history' && <HistorySidebar />}
  84. </Panel>
  85. <HorizontalResizeHandle
  86. onDoubleClick={toggleSidebar}
  87. resizable={sidebarIsOpen}
  88. onDragging={setSidebarResizing}
  89. >
  90. <HorizontalToggler
  91. id="panel-sidebar"
  92. togglerType="west"
  93. isOpen={sidebarIsOpen}
  94. setIsOpen={setSidebarIsOpen}
  95. tooltipWhenOpen={t('tooltip_hide_filetree')}
  96. tooltipWhenClosed={t('tooltip_show_filetree')}
  97. />
  98. </HorizontalResizeHandle>
  99. <Panel id="panel-outer-main" order={2}>
  100. <PanelGroup
  101. autoSaveId="ide-inner-layout"
  102. direction="horizontal"
  103. onLayout={handleChatLayout}
  104. >
  105. <Panel className="ide-react-panel" id="panel-main" order={1}>
  106. {view === 'history' ? (
  107. <HistoryProvider>
  108. <History />
  109. </HistoryProvider>
  110. ) : (
  111. <EditorAndPdf
  112. editorPane={editorPane}
  113. selectedEntityCount={selectedEntityCount}
  114. openEntity={openEntity}
  115. />
  116. )}
  117. </Panel>
  118. {chatIsOpen && (
  119. <>
  120. <HorizontalResizeHandle onDragging={setChatResizing} />
  121. {/* chat */}
  122. <Panel
  123. ref={chatPanelRef}
  124. id="panel-chat"
  125. order={2}
  126. defaultSizePixels={200}
  127. minSizePixels={150}
  128. collapsible
  129. >
  130. <ChatPane />
  131. </Panel>
  132. </>
  133. )}
  134. </PanelGroup>
  135. </Panel>
  136. </PanelGroup>
  137. </div>
  138. </div>
  139. )
  140. }