From cff92902b22ebd77c9c003d5628049aa14c6d305 Mon Sep 17 00:00:00 2001 From: aliang <1098486429@qq.com> Date: Mon, 22 Apr 2024 03:22:56 +0800 Subject: [PATCH] refactor(ui): optimize the init function in code browser (#1904) * refactor(ui): optimize init function in code browser * format --- ee/tabby-ui/app/files/components/blob-header.tsx | 7 ++----- ee/tabby-ui/app/files/components/code-editor-view.tsx | 4 ++-- .../app/files/components/source-code-browser.tsx | 11 ++++++++++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ee/tabby-ui/app/files/components/blob-header.tsx b/ee/tabby-ui/app/files/components/blob-header.tsx index e0076ebbbede..c81d60c520ad 100644 --- a/ee/tabby-ui/app/files/components/blob-header.tsx +++ b/ee/tabby-ui/app/files/components/blob-header.tsx @@ -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' @@ -42,10 +41,8 @@ export const BlobHeader: React.FC = ({ 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(null) const { activePath } = React.useContext(SourceCodeBrowserContext) diff --git a/ee/tabby-ui/app/files/components/code-editor-view.tsx b/ee/tabby-ui/app/files/components/code-editor-view.tsx index 78c881506254..deffea48a0e7 100644 --- a/ee/tabby-ui/app/files/components/code-editor-view.tsx +++ b/ee/tabby-ui/app/files/components/code-editor-view.tsx @@ -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 @@ -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 @@ -24,7 +24,7 @@ const CodeEditorView: React.FC = ({ value, language }) => { const { theme } = useTheme() const tags: TCodeTag[] = [] const editorRef = React.useRef(null) - const isChatEnabled = useIsChatEnabled() + const { isChatEnabled } = React.useContext(SourceCodeBrowserContext) const extensions = React.useMemo(() => { let result: Extension[] = [ diff --git a/ee/tabby-ui/app/files/components/source-code-browser.tsx b/ee/tabby-ui/app/files/components/source-code-browser.tsx index 7af9757bc9d5..f2545872d817 100644 --- a/ee/tabby-ui/app/files/components/source-code-browser.tsx +++ b/ee/tabby-ui/app/files/components/source-code-browser.tsx @@ -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' @@ -74,6 +75,7 @@ type SourceCodeBrowserContextValue = { setChatSideBarVisible: React.Dispatch> pendingEvent: QuickActionEventPayload | undefined setPendingEvent: (d: QuickActionEventPayload | undefined) => void + isChatEnabled: boolean | undefined } const SourceCodeBrowserContext = @@ -88,6 +90,7 @@ const SourceCodeBrowserContextProvider: React.FC = ({ const activePath = React.useMemo(() => { return searchParams.get('path')?.toString() ?? '' }, [searchParams]) + const isChatEnabled = useIsChatEnabled() const setActivePath = (path: string | undefined, replace?: boolean) => { if (!path) { @@ -157,7 +160,8 @@ const SourceCodeBrowserContextProvider: React.FC = ({ chatSideBarVisible, setChatSideBarVisible, pendingEvent, - setPendingEvent + setPendingEvent, + isChatEnabled }} > {children} @@ -186,6 +190,7 @@ const SourceCodeBrowserRenderer: React.FC = ({ setChatSideBarVisible, setPendingEvent } = React.useContext(SourceCodeBrowserContext) + const initializing = React.useRef(false) const { setProgress } = useTopbarProgress() const chatSideBarPanelRef = React.useRef(null) const [chatSideBarPanelSize, setChatSideBarPanelSize] = React.useState(35) @@ -278,6 +283,9 @@ const SourceCodeBrowserRenderer: React.FC = ({ React.useEffect(() => { const init = async () => { + if (initializing.current) return + + initializing.current = true const { patchMap, expandedKeys, repos } = await getInitialFileData( activePath ) @@ -285,6 +293,7 @@ const SourceCodeBrowserRenderer: React.FC = ({ // By default, selecting the first repository if initialPath is empty if (repos?.length && !activePath) { setActivePath(repos?.[0]?.basename, true) + initializing.current = false return }