From fd411afc5a63052d031f8e860183d242a9f939a3 Mon Sep 17 00:00:00 2001 From: liangfung Date: Mon, 30 Dec 2024 13:50:17 +0800 Subject: [PATCH 1/4] chore(ui): add readWorksapceRepositories to chat sidebar in code browser --- ee/tabby-ui/app/chat/page.tsx | 2 +- .../app/files/components/chat-side-bar.tsx | 17 +++++++++++++++-- .../files/components/source-code-browser.tsx | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/ee/tabby-ui/app/chat/page.tsx b/ee/tabby-ui/app/chat/page.tsx index bb1a1d44b60e..5bae0628ef85 100644 --- a/ee/tabby-ui/app/chat/page.tsx +++ b/ee/tabby-ui/app/chat/page.tsx @@ -418,7 +418,7 @@ export default function ChatPage() { openInEditor={openInEditor} openExternal={openExternal} readWorkspaceGitRepositories={ - isInEditor && supportsProvideWorkspaceGitRepoInfo + supportsProvideWorkspaceGitRepoInfo ? server?.readWorkspaceGitRepositories : undefined } diff --git a/ee/tabby-ui/app/files/components/chat-side-bar.tsx b/ee/tabby-ui/app/files/components/chat-side-bar.tsx index 5ca4f947fea6..1bc568c2ad68 100644 --- a/ee/tabby-ui/app/files/components/chat-side-bar.tsx +++ b/ee/tabby-ui/app/files/components/chat-side-bar.tsx @@ -1,8 +1,9 @@ import React from 'react' import { find } from 'lodash-es' -import type { FileLocation } from 'tabby-chat-panel' +import type { FileLocation, GitRepository } from 'tabby-chat-panel' import { useClient } from 'tabby-chat-panel/react' +import { RepositoryListQuery } from '@/lib/gql/generates/graphql' import { useLatest } from '@/lib/hooks/use-latest' import { useMe } from '@/lib/hooks/use-me' import { filename2prism } from '@/lib/language-utils' @@ -16,9 +17,12 @@ import { SourceCodeBrowserContext } from './source-code-browser' import { generateEntryPath, getDefaultRepoRef, resolveRepoRef } from './utils' interface ChatSideBarProps - extends Omit, 'children'> {} + extends Omit, 'children'> { + activeRepo: RepositoryListQuery['repositoryList'][0] | undefined +} export const ChatSideBar: React.FC = ({ + activeRepo, className, ...props }) => { @@ -60,6 +64,12 @@ export const ChatSideBar: React.FC = ({ return false } + const readWorkspaceGitRepositories = useLatest(() => { + if (!activeRepo) return [] + const list: GitRepository[] = [{ url: activeRepo.gitUrl }] + return list + }) + const client = useClient(iframeRef, { refresh: async () => { window.location.reload() @@ -78,6 +88,9 @@ export const ChatSideBar: React.FC = ({ }, openExternal: async (url: string) => { window.open(url, '_blank') + }, + readWorkspaceGitRepositories: async () => { + return readWorkspaceGitRepositories.current() } }) 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 79fe2ac360e4..a95f960512e4 100644 --- a/ee/tabby-ui/app/files/components/source-code-browser.tsx +++ b/ee/tabby-ui/app/files/components/source-code-browser.tsx @@ -736,7 +736,7 @@ const SourceCodeBrowserRenderer: React.FC = ({ ref={chatSideBarPanelRef} onCollapse={() => setChatSideBarVisible(false)} > - + From d15499e8a47863d755884dd6aed61c0c641bc119 Mon Sep 17 00:00:00 2001 From: liangfung Date: Mon, 30 Dec 2024 14:11:08 +0800 Subject: [PATCH 2/4] update: init --- .../files/components/source-code-browser.tsx | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) 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 a95f960512e4..baa7fd8abcfb 100644 --- a/ee/tabby-ui/app/files/components/source-code-browser.tsx +++ b/ee/tabby-ui/app/files/components/source-code-browser.tsx @@ -379,6 +379,8 @@ const SourceCodeBrowserRenderer: React.FC = ({ const { progress, setProgress } = useTopbarProgress() const chatSideBarPanelRef = React.useRef(null) const [chatSideBarPanelSize, setChatSideBarPanelSize] = React.useState(35) + const [chatSidebarInitialized, setChatSidebarInitialized] = useState(false) + const searchQuery = searchParams.get('q')?.toString() const parsedEntryInfo = React.useMemo(() => { @@ -619,12 +621,23 @@ const SourceCodeBrowserRenderer: React.FC = ({ }, [fetchingRawFile, fetchingTreeEntries]) React.useEffect(() => { - if (chatSideBarVisible) { - chatSideBarPanelRef.current?.expand() - chatSideBarPanelRef.current?.resize(chatSideBarPanelSize) - } else { - chatSideBarPanelRef.current?.collapse() + const initChatSidebar = () => { + if (chatSideBarVisible && !chatSidebarInitialized) { + setChatSidebarInitialized(true) + } + } + + const toggleChatSidebarPanel = () => { + if (chatSideBarVisible) { + chatSideBarPanelRef.current?.expand() + chatSideBarPanelRef.current?.resize(chatSideBarPanelSize) + } else { + chatSideBarPanelRef.current?.collapse() + } } + + initChatSidebar() + toggleChatSidebarPanel() }, [chatSideBarVisible]) React.useEffect(() => { @@ -736,7 +749,7 @@ const SourceCodeBrowserRenderer: React.FC = ({ ref={chatSideBarPanelRef} onCollapse={() => setChatSideBarVisible(false)} > - + {chatSidebarInitialized && } From 8a54e9250eacc9e8ee9ba1ca8d41018ee928ccb3 Mon Sep 17 00:00:00 2001 From: liangfung Date: Mon, 30 Dec 2024 17:57:06 +0800 Subject: [PATCH 3/4] update --- ee/tabby-ui/app/chat/page.tsx | 4 ++- .../app/files/components/chat-side-bar.tsx | 25 +++++++++++-------- .../files/components/source-code-browser.tsx | 4 ++- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/ee/tabby-ui/app/chat/page.tsx b/ee/tabby-ui/app/chat/page.tsx index 5bae0628ef85..d7864b99000a 100644 --- a/ee/tabby-ui/app/chat/page.tsx +++ b/ee/tabby-ui/app/chat/page.tsx @@ -279,7 +279,9 @@ export default function ChatPage() { currentChatRef.addRelevantContext(context) }) - currentChatRef.updateActiveSelection(pendingActiveSelection) + if (pendingActiveSelection) { + currentChatRef.updateActiveSelection(pendingActiveSelection) + } if (pendingCommand) { // FIXME: this delay is a workaround for waiting for the active selection to be updated diff --git a/ee/tabby-ui/app/files/components/chat-side-bar.tsx b/ee/tabby-ui/app/files/components/chat-side-bar.tsx index 1bc568c2ad68..8674951b0bcb 100644 --- a/ee/tabby-ui/app/files/components/chat-side-bar.tsx +++ b/ee/tabby-ui/app/files/components/chat-side-bar.tsx @@ -1,4 +1,4 @@ -import React from 'react' +import React, { useRef, useState } from 'react' import { find } from 'lodash-es' import type { FileLocation, GitRepository } from 'tabby-chat-panel' import { useClient } from 'tabby-chat-panel/react' @@ -27,10 +27,12 @@ export const ChatSideBar: React.FC = ({ ...props }) => { const [{ data }] = useMe() + const [initialized, setInitialized] = useState(false) const { pendingEvent, setPendingEvent, repoMap, updateActivePath } = React.useContext(SourceCodeBrowserContext) const activeChatId = useChatStore(state => state.activeChatId) const iframeRef = React.useRef(null) + const executedCommand = useRef(false) const repoMapRef = useLatest(repoMap) const openInCodeBrowser = async (fileLocation: FileLocation) => { const { filepath, location } = fileLocation @@ -80,7 +82,9 @@ export const ChatSideBar: React.FC = ({ }) }, onApplyInEditor(_content) {}, - onLoaded() {}, + onLoaded() { + setInitialized(true) + }, onCopy(_content) {}, onKeyboardEvent() {}, openInEditor: async (fileLocation: FileLocation) => { @@ -90,7 +94,7 @@ export const ChatSideBar: React.FC = ({ window.open(url, '_blank') }, readWorkspaceGitRepositories: async () => { - return readWorkspaceGitRepositories.current() + return readWorkspaceGitRepositories.current?.() } }) @@ -113,10 +117,10 @@ export const ChatSideBar: React.FC = ({ } }) } - }, [iframeRef?.current, client, data]) + }, [iframeRef?.current, client?.init, data]) React.useEffect(() => { - if (pendingEvent && client) { + if (pendingEvent && client && initialized) { const execute = async () => { const { lineFrom, lineTo, code, path, gitUrl } = pendingEvent client.updateActiveSelection({ @@ -132,17 +136,18 @@ export const ChatSideBar: React.FC = ({ gitUrl } }) + const command = getCommand(pendingEvent) // FIXME: this delay is a workaround for waiting for the active selection to be updated setTimeout(() => { - client.executeCommand(getCommand(pendingEvent)) - }, 500) + client.executeCommand(command) + }, 800) + setPendingEvent(undefined) } + execute() } - setPendingEvent(undefined) - }, [pendingEvent, client]) + }, [initialized, pendingEvent]) - if (!data?.me) return <> return (
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 baa7fd8abcfb..ef501e77a9b3 100644 --- a/ee/tabby-ui/app/files/components/source-code-browser.tsx +++ b/ee/tabby-ui/app/files/components/source-code-browser.tsx @@ -749,7 +749,9 @@ const SourceCodeBrowserRenderer: React.FC = ({ ref={chatSideBarPanelRef} onCollapse={() => setChatSideBarVisible(false)} > - {chatSidebarInitialized && } + {chatSidebarInitialized ? ( + + ) : null} From dfb5ed521318d5779a177814d613670c746a57bd Mon Sep 17 00:00:00 2001 From: liangfung Date: Mon, 30 Dec 2024 18:07:53 +0800 Subject: [PATCH 4/4] revert --- ee/tabby-ui/app/files/components/chat-side-bar.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ee/tabby-ui/app/files/components/chat-side-bar.tsx b/ee/tabby-ui/app/files/components/chat-side-bar.tsx index 8674951b0bcb..52d1a1156586 100644 --- a/ee/tabby-ui/app/files/components/chat-side-bar.tsx +++ b/ee/tabby-ui/app/files/components/chat-side-bar.tsx @@ -140,7 +140,7 @@ export const ChatSideBar: React.FC = ({ // FIXME: this delay is a workaround for waiting for the active selection to be updated setTimeout(() => { client.executeCommand(command) - }, 800) + }, 500) setPendingEvent(undefined) }