Skip to content

Commit

Permalink
navigate
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Jun 7, 2024
1 parent 1dce13c commit ebba780
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 75 deletions.
76 changes: 11 additions & 65 deletions ee/tabby-ui/app/files/components/chat-side-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ import { useClient } from 'tabby-chat-panel/react'

import { useLatest } from '@/lib/hooks/use-latest'
import { useMe } from '@/lib/hooks/use-me'
import useRouterStuff from '@/lib/hooks/use-router-stuff'
import { useStore } from '@/lib/hooks/use-store'
import { useChatStore } from '@/lib/stores/chat-store'
import { cn } from '@/lib/utils'
import { Button } from '@/components/ui/button'
import { IconClose } from '@/components/ui/icons'
import { useTopbarProgress } from '@/components/topbar-progress-indicator'

import { QuickActionEventPayload } from '../lib/event-emitter'
import { SourceCodeBrowserContext, TFileMap } from './source-code-browser'
import {
fetchEntriesFromPath,
getDirectoriesFromBasename,
resolveFileNameFromPath,
resolveRepoSpecifierFromRepoInfo
} from './utils'
import { SourceCodeBrowserContext } from './source-code-browser'
import { resolveRepoSpecifierFromRepoInfo } from './utils'

interface ChatSideBarProps
extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> {}
Expand All @@ -29,20 +22,18 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({
className,
...props
}) => {
const { setProgress } = useTopbarProgress()
const { updateSearchParams, updatePathnameAndSearch } = useRouterStuff()
const [{ data }] = useMe()
const {
pendingEvent,
setPendingEvent,
repoMap,
setExpandedKeys,
updateFileMap,
activeRepoRef
activeRepoRef,
updateActivePath
} = React.useContext(SourceCodeBrowserContext)
const activeChatId = useStore(useChatStore, state => state.activeChatId)
const iframeRef = React.useRef<HTMLIFrameElement>(null)
const repoMapRef = useLatest(repoMap)
const latestRepoRef = useLatest(activeRepoRef)
const onNavigate = async (context: Context) => {
if (context?.filepath && context?.git_url) {
const repoMap = repoMapRef.current
Expand All @@ -53,61 +44,16 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({
if (matchedRepositoryKey) {
const repository = repoMap[matchedRepositoryKey]
const repositorySpecifier = resolveRepoSpecifierFromRepoInfo(repository)
const rev = activeRepoRef?.name
const rev = latestRepoRef?.current?.name ?? 'main'

const fullPath = `${repositorySpecifier}/${rev}/${context.filepath}`
if (!fullPath) return
try {
setProgress(true)
const entries = await fetchEntriesFromPath(
fullPath,
repositorySpecifier
? repoMap?.[`${repositorySpecifier}/${rev}`]
: undefined
)
const initialExpandedDirs = getDirectoriesFromBasename(
context.filepath
)

const patchMap: TFileMap = {}
// fetch dirs
for (const entry of entries) {
const path = `${repositorySpecifier}/${entry.basename}`
patchMap[path] = {
file: entry,
name: resolveFileNameFromPath(path),
fullPath: path,
treeExpanded: initialExpandedDirs.includes(entry.basename)
}
}
const expandedKeys = initialExpandedDirs.map(dir =>
[repositorySpecifier, dir].filter(Boolean).join('/')
)
if (patchMap) {
updateFileMap(patchMap)
updateActivePath(fullPath, {
shouldFetchAllEntries: true,
params: {
line: String(context.range.start)
}
if (expandedKeys?.length) {
setExpandedKeys(prevKeys => {
const newSet = new Set(prevKeys)
for (const k of expandedKeys) {
newSet.add(k)
}
return newSet
})
}
} catch (e) {
} finally {
updatePathnameAndSearch(
`${repositorySpecifier ?? ''}/${context.filepath}`,
{
set: {
line: String(context.range.start ?? '')
},
del: 'plain'
}
)
setProgress(false)
}
})
}
}
}
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 @@ -46,10 +46,10 @@ const CodeEditorView: React.FC<CodeEditorViewProps> = ({ value, language }) => {
const { isChatEnabled, activePath, fileMap } = React.useContext(
SourceCodeBrowserContext
)
const { repositorySpecifier, basename } =
const { repositorySpecifier, rev, basename } =
resolveRepositoryInfoFromPath(activePath)
const gitUrl = repositorySpecifier
? fileMap[repositorySpecifier]?.repository?.gitUrl ?? ''
? fileMap[`${repositorySpecifier}/${rev}`]?.repository?.gitUrl ?? ''
: ''

const extensions = React.useMemo(() => {
Expand Down
18 changes: 15 additions & 3 deletions ee/tabby-ui/app/files/components/file-tree-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ type SearchOption = {
type RepositoryRefKind = 'branch' | 'tag'

const repositorySearch = graphql(/* GraphQL */ `
query RepositorySearch($kind: RepositoryKind!, $id: ID!, $rev: String, $pattern: String!) {
query RepositorySearch(
$kind: RepositoryKind!
$id: ID!
$rev: String
$pattern: String!
) {
repositorySearch(kind: $kind, id: $id, rev: $rev, pattern: $pattern) {
type
path
Expand Down Expand Up @@ -115,7 +120,9 @@ const FileTreeHeader: React.FC<FileTreeHeaderProps> = ({
kind: repositoryKind as RepositoryKind,
id: repoId as string,
pattern: repositorySearchPattern ?? '',
rev: activeRepoRef?.name ? decodeURIComponent(activeRepoRef.name) : undefined
rev: activeRepoRef?.name
? decodeURIComponent(activeRepoRef.name)
: undefined
},
pause: !repoId || !repositoryKind || !repositorySearchPattern
})
Expand Down Expand Up @@ -457,7 +464,12 @@ const HighlightMatches = ({
<p className="text-muted-foreground">
{text.split('').map((char, index) => {
return indicesSet.has(index) ? (
<span className="font-semibold text-foreground" key={`${char}_${index}`}>{char}</span>
<span
className="font-semibold text-foreground"
key={`${char}_${index}`}
>
{char}
</span>
) : (
char
)
Expand Down
4 changes: 1 addition & 3 deletions ee/tabby-ui/app/files/components/file-tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ const DirectoryTreeNode: React.FC<DirectoryTreeNodeProps> = ({
level,
root
}) => {
const { activeRepo, activeRepoRef } = React.useContext(
SourceCodeBrowserContext
)
const { activeRepo } = React.useContext(SourceCodeBrowserContext)
const {
fileMap,
updateFileMap,
Expand Down
2 changes: 0 additions & 2 deletions ee/tabby-ui/app/files/components/source-code-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
const redirect_git_url = searchParams.get('redirect_git_url')
const line = searchParams.get('line')

// todo need ref info
if (repos?.length && redirect_filepath && redirect_git_url) {
// get repo from repos and redirect_git_url
const targetRepo = repos.find(repo => repo.gitUrl === redirect_git_url)
Expand All @@ -488,7 +487,6 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
}
}

// todo if there's no rev, set default rev, should make a function to do it
// By default, selecting the first repository if initialPath is empty
if (repos?.length && !activePath) {
const targetRepo = repos?.[0]
Expand Down

0 comments on commit ebba780

Please sign in to comment.