diff --git a/ee/tabby-ui/components/chat/chat-panel.tsx b/ee/tabby-ui/components/chat/chat-panel.tsx index 9afed6890119..c488e3727cd5 100644 --- a/ee/tabby-ui/components/chat/chat-panel.tsx +++ b/ee/tabby-ui/components/chat/chat-panel.tsx @@ -73,11 +73,10 @@ function ChatPanelRenderer( removeRelevantContext, activeSelection, onCopyContent, - indexedRepository, selectedRepoId, setSelectedRepoId, repos, - fetchingRepos + initialized } = React.useContext(ChatContext) const enableActiveSelection = useChatStore( state => state.enableActiveSelection @@ -240,8 +239,7 @@ function ChatPanelRenderer( value={selectedRepoId} onChange={onSelectRepo} repos={repos} - workspaceRepoId={indexedRepository?.sourceId} - isInitializing={fetchingRepos} + isInitializing={!initialized} /> {activeSelection ? ( void chatInputRef: RefObject supportsOnApplyInEditorV2: boolean - indexedRepository: ResolveGitUrlQuery['resolveGitUrl'] selectedRepoId: string | undefined setSelectedRepoId: React.Dispatch> repos: ContextInfo['sources'] - fetchingRepos: boolean + fetchingSources: boolean } export const ChatContext = React.createContext( @@ -151,8 +151,6 @@ function ChatRenderer( const [activeSelection, setActiveSelection] = React.useState( null ) - // gitUrl from workspace - const [gitUrl, setGitUrl] = React.useState() // sourceId const [selectedRepoId, setSelectedRepoId] = React.useState< string | undefined @@ -178,14 +176,6 @@ function ChatRenderer( ) }, [contextInfoData]) - const [{ data: resolvedGitUrl }] = useQuery({ - query: resolveGitUrlQuery, - variables: { - gitUrl: gitUrl as string - }, - pause: !gitUrl - }) - const { sendUserMessage, isLoading, @@ -198,22 +188,6 @@ function ChatRenderer( threadId }) - React.useEffect(() => { - const setDefaultRepoId = () => { - if (!resolvedGitUrl || !repos?.length) return - if (selectedRepoId) return - - const defaultRepo = repos.find( - o => o.sourceId === resolvedGitUrl.resolveGitUrl?.sourceId - ) - if (defaultRepo) { - setSelectedRepoId(defaultRepo.sourceId) - } - } - - setDefaultRepoId() - }, [resolvedGitUrl, repos]) - const onDeleteMessage = async (userMessageId: string) => { if (!threadId) return @@ -549,6 +523,45 @@ function ChatRenderer( debouncedUpdateActiveSelection.run(ctx) } + const fetchWorkspaceGitRepo = () => { + if (provideWorkspaceGitRepoInfo) { + return provideWorkspaceGitRepoInfo() + } else { + return [] + } + } + + const resolveGitUrl = async (gitUrl: string | undefined) => { + if (!gitUrl) return undefined + return client.query(resolveGitUrlQuery, { gitUrl }).toPromise() + } + + React.useEffect(() => { + const init = async () => { + const gitRepoInfo = await fetchWorkspaceGitRepo() + // get default repo + if (gitRepoInfo?.length) { + const defaultGitUrl = gitRepoInfo[0].gitUrl + const repo = await resolveGitUrl(defaultGitUrl) + if (repo?.data?.resolveGitUrl) { + setSelectedRepoId(repo.data.resolveGitUrl.sourceId) + } + } + + setInitialzed(true) + } + + if (!fetchingSources && !initialized) { + init() + } + }, [fetchingSources]) + + React.useEffect(() => { + if (initialized) { + onLoaded?.() + } + }, [initialized]) + React.useImperativeHandle( ref, () => { @@ -564,22 +577,6 @@ function ChatRenderer( [] ) - React.useEffect(() => { - const fetchWorkspaceGitRepo = () => { - if (provideWorkspaceGitRepoInfo) { - provideWorkspaceGitRepoInfo().then(repos => { - if (repos.length) { - setGitUrl(repos[0].gitUrl) - } - }) - } - } - - fetchWorkspaceGitRepo() - setInitialzed(true) - onLoaded?.() - }, []) - const chatMaxWidthClass = maxWidth ? `max-w-${maxWidth}` : 'max-w-2xl' if (!initialized) { return ( @@ -606,11 +603,11 @@ function ChatRenderer( chatInputRef, activeSelection, supportsOnApplyInEditorV2, - indexedRepository: gitUrl ? resolvedGitUrl?.resolveGitUrl : undefined, selectedRepoId, setSelectedRepoId, repos, - fetchingRepos: fetchingSources + fetchingSources, + initialized }} >
diff --git a/ee/tabby-ui/components/chat/repo-select.tsx b/ee/tabby-ui/components/chat/repo-select.tsx index e26a09db8a85..d2ee9229c8d2 100644 --- a/ee/tabby-ui/components/chat/repo-select.tsx +++ b/ee/tabby-ui/components/chat/repo-select.tsx @@ -1,5 +1,4 @@ import { useRef, useState } from 'react' -import { useWindowSize } from '@uidotdev/usehooks' import { ContextInfo } from '@/lib/gql/generates/graphql' import { cn } from '@/lib/utils' @@ -30,27 +29,21 @@ import LoadingWrapper from '@/components/loading-wrapper' import { SourceIcon } from '../source-icon' interface RepoSelectProps { - repos: ContextInfo['sources'] + repos: ContextInfo['sources'] | undefined value: string | undefined onChange: (v: string | undefined) => void isInitializing?: boolean - // sourceId - workspaceRepoId?: string } export function RepoSelect({ repos, value, onChange, - isInitializing, - workspaceRepoId + isInitializing }: RepoSelectProps) { const [open, setOpen] = useState(false) const commandListRef = useRef(null) - const { width } = useWindowSize() - const isExtraSmallScreen = typeof width === 'number' && width < 360 - const onSelectRepo = (v: string) => { onChange(v) } @@ -73,7 +66,7 @@ export function RepoSelect({ const selectedRepoName = selectedRepo?.sourceName // if there's no repo, hide the repo select - if (!isInitializing && !repos.length) return null + if (!isInitializing && !repos?.length) return null return ( No context found - {repos.map(repo => { + {repos?.map(repo => { const isSelected = repo.sourceId === value return (