Skip to content

Commit

Permalink
refactor(ui): optimize the init function in code browser (#1904)
Browse files Browse the repository at this point in the history
* refactor(ui): optimize init function in code browser

* format
  • Loading branch information
liangfung authored Apr 21, 2024
1 parent 97557ea commit cff9290
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
7 changes: 2 additions & 5 deletions ee/tabby-ui/app/files/components/blob-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { toast } from 'sonner'
import { useEnableCodeBrowserQuickActionBar } from '@/lib/experiment-flags'
import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard'
import { useIsSticky } from '@/lib/hooks/use-is-sticky'
import { useIsChatEnabled } from '@/lib/hooks/use-server-info'
import { cn } from '@/lib/utils'
import { Button, buttonVariants } from '@/components/ui/button'
import { IconCheck, IconCopy, IconDownload } from '@/components/ui/icons'
Expand Down Expand Up @@ -42,10 +41,8 @@ export const BlobHeader: React.FC<BlobHeaderProps> = ({
children,
...props
}) => {
const isChatEnabled = useIsChatEnabled()
const { chatSideBarVisible, setChatSideBarVisible } = React.useContext(
SourceCodeBrowserContext
)
const { chatSideBarVisible, setChatSideBarVisible, isChatEnabled } =
React.useContext(SourceCodeBrowserContext)
const [enableCodeBrowserQuickActionBar] = useEnableCodeBrowserQuickActionBar()
const containerRef = React.useRef<HTMLDivElement>(null)
const { activePath } = React.useContext(SourceCodeBrowserContext)
Expand Down
4 changes: 2 additions & 2 deletions ee/tabby-ui/app/files/components/code-editor-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { drawSelection, EditorView } from '@codemirror/view'
import { useTheme } from 'next-themes'

import { EXP_enable_code_browser_quick_action_bar } from '@/lib/experiment-flags'
import { useIsChatEnabled } from '@/lib/hooks/use-server-info'
import { TCodeTag } from '@/lib/types'
import CodeEditor, {
CodeMirrorEditorRef
Expand All @@ -14,6 +13,7 @@ import { highlightTagExtension } from '@/components/codemirror/tag-range-highlig
import { codeTagHoverTooltip } from '@/components/codemirror/tooltip-extesion'

import { ActionBarWidgetExtension } from './action-bar-widget/action-bar-widget-extension'
import { SourceCodeBrowserContext } from './source-code-browser'

interface CodeEditorViewProps {
value: string
Expand All @@ -24,7 +24,7 @@ const CodeEditorView: React.FC<CodeEditorViewProps> = ({ value, language }) => {
const { theme } = useTheme()
const tags: TCodeTag[] = []
const editorRef = React.useRef<CodeMirrorEditorRef>(null)
const isChatEnabled = useIsChatEnabled()
const { isChatEnabled } = React.useContext(SourceCodeBrowserContext)

const extensions = React.useMemo(() => {
let result: Extension[] = [
Expand Down
11 changes: 10 additions & 1 deletion ee/tabby-ui/app/files/components/source-code-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SWRResponse } from 'swr'
import useSWRImmutable from 'swr/immutable'

import useRouterStuff from '@/lib/hooks/use-router-stuff'
import { useIsChatEnabled } from '@/lib/hooks/use-server-info'
import { filename2prism } from '@/lib/language-utils'
import fetcher from '@/lib/tabby/fetcher'
import type { ResolveEntriesResponse, TFile } from '@/lib/types'
Expand Down Expand Up @@ -74,6 +75,7 @@ type SourceCodeBrowserContextValue = {
setChatSideBarVisible: React.Dispatch<React.SetStateAction<boolean>>
pendingEvent: QuickActionEventPayload | undefined
setPendingEvent: (d: QuickActionEventPayload | undefined) => void
isChatEnabled: boolean | undefined
}

const SourceCodeBrowserContext =
Expand All @@ -88,6 +90,7 @@ const SourceCodeBrowserContextProvider: React.FC<PropsWithChildren> = ({
const activePath = React.useMemo(() => {
return searchParams.get('path')?.toString() ?? ''
}, [searchParams])
const isChatEnabled = useIsChatEnabled()

const setActivePath = (path: string | undefined, replace?: boolean) => {
if (!path) {
Expand Down Expand Up @@ -157,7 +160,8 @@ const SourceCodeBrowserContextProvider: React.FC<PropsWithChildren> = ({
chatSideBarVisible,
setChatSideBarVisible,
pendingEvent,
setPendingEvent
setPendingEvent,
isChatEnabled
}}
>
{children}
Expand Down Expand Up @@ -186,6 +190,7 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
setChatSideBarVisible,
setPendingEvent
} = React.useContext(SourceCodeBrowserContext)
const initializing = React.useRef(false)
const { setProgress } = useTopbarProgress()
const chatSideBarPanelRef = React.useRef<ImperativePanelHandle>(null)
const [chatSideBarPanelSize, setChatSideBarPanelSize] = React.useState(35)
Expand Down Expand Up @@ -278,13 +283,17 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({

React.useEffect(() => {
const init = async () => {
if (initializing.current) return

initializing.current = true
const { patchMap, expandedKeys, repos } = await getInitialFileData(
activePath
)

// By default, selecting the first repository if initialPath is empty
if (repos?.length && !activePath) {
setActivePath(repos?.[0]?.basename, true)
initializing.current = false
return
}

Expand Down

0 comments on commit cff9290

Please sign in to comment.