Skip to content

Commit

Permalink
update: code browser
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed May 28, 2024
1 parent 0912178 commit 2394644
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
3 changes: 2 additions & 1 deletion ee/tabby-ui/app/files/components/file-tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Skeleton } from '@/components/ui/skeleton'
import { SourceCodeBrowserContext, TFileMap } from './source-code-browser'
import {
encodeURIComponentIgnoringSlash,
getProviderVariantFromKind,
resolveFileNameFromPath,
resolveRepositoryInfoFromPath
} from './utils'
Expand Down Expand Up @@ -246,7 +247,7 @@ const DirectoryTreeNode: React.FC<DirectoryTreeNodeProps> = ({
const repoId = activeRepo?.id
if (!kind || !repoId) return ''

return `${kind.toLowerCase()}/${repoId}`
return `${getProviderVariantFromKind(kind)}/${repoId}`
}, [activeRepo])

const { repositorySpecifier } = resolveRepositoryInfoFromPath(activePath)
Expand Down
2 changes: 2 additions & 0 deletions ee/tabby-ui/app/files/components/repository-kind-icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export function RepositoryKindIcon({
case RepositoryKind.Git:
return <IconDirectorySolid style={{ color: 'rgb(84, 174, 255)' }} />
case RepositoryKind.Github:
case RepositoryKind.GithubSelfHosted:
return <IconGitHub />
case RepositoryKind.Gitlab:
case RepositoryKind.GitlabSelfHosted:
return <IconGitLab />
default:
return fallback ?? null
Expand Down
7 changes: 6 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 @@ -38,6 +38,7 @@ import {
encodeURIComponentIgnoringSlash,
fetchEntriesFromPath,
getDirectoriesFromBasename,
getProviderVariantFromKind,
repositoryList2Map,
resolveFileNameFromPath,
resolveRepositoryInfoFromPath,
Expand Down Expand Up @@ -227,6 +228,8 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
chatSideBarVisible,
setChatSideBarVisible,
setPendingEvent,
fileTreeData,
repoMap,
setRepoMap,
activeRepo
} = React.useContext(SourceCodeBrowserContext)
Expand All @@ -241,7 +244,7 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
const repoId = activeRepo?.id
const kind = activeRepo?.kind
if (!repoId || !kind) return ''
return `${activeRepo.kind?.toLowerCase()}/${repoId}`
return `${getProviderVariantFromKind(kind)}/${repoId}`
}, [activeRepo])

const activeBasename = React.useMemo(() => {
Expand Down Expand Up @@ -434,6 +437,8 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
}
}, [chatSideBarVisible])

console.log('repoMap', repoMap, fileTreeData)

return (
<ResizablePanelGroup
direction="horizontal"
Expand Down
42 changes: 32 additions & 10 deletions ee/tabby-ui/app/files/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
import fetcher from '@/lib/tabby/fetcher'
import { ResolveEntriesResponse, TFile } from '@/lib/types'

function getProviderVariantFromKind(kind: RepositoryKind) {
return kind.toLowerCase().replaceAll('_', '')
}

function resolveRepositoryInfoFromPath(path: string | undefined): {
repositoryKind?: RepositoryKind
repositoryName?: string
Expand All @@ -32,19 +36,36 @@ function resolveRepositoryInfoFromPath(path: string | undefined): {
basename: pathSegments.slice(2).join('/'),
repositorySpecifier: `git/${repositoryName}`
}
} else if (['github', 'gitlab'].includes(repositoryKindStr)) {
} else if (
['github', 'gitlab', 'githubselfhosted', 'gitlabselfhosted'].includes(
repositoryKindStr
)
) {
if (pathSegments.length < 3) return emptyResult
const kind =
repositoryKindStr === 'github'
? RepositoryKind.Github
: RepositoryKind.Gitlab
let kind: RepositoryKind = RepositoryKind.Github
switch (repositoryKindStr) {
case 'github':
kind = RepositoryKind.Github
break
case 'gitlab':
kind = RepositoryKind.Gitlab
break
case 'githubselfhosted':
kind = RepositoryKind.GithubSelfHosted
break
case 'gitlabselfhosted':
kind = RepositoryKind.GitlabSelfHosted
break
}
const repositoryName = [pathSegments[1], pathSegments[2]].join('/')

return {
repositoryKind: kind,
repositoryName,
basename: pathSegments.slice(3).join('/'),
repositorySpecifier: `${kind.toLowerCase()}/${repositoryName}`
repositorySpecifier: `${getProviderVariantFromKind(
kind
)}/${repositoryName}`
}
}
return emptyResult
Expand Down Expand Up @@ -87,7 +108,7 @@ async function fetchEntriesFromPath(
dir => () =>
fetcher(
encodeURIComponentIgnoringSlash(
`/repositories/${repository.kind.toLowerCase()}/${
`/repositories/${getProviderVariantFromKind(repository.kind)}/${
repository.id
}/resolve/${dir}`
)
Expand All @@ -109,14 +130,14 @@ function resolveRepoSpecifierFromRepoInfo(
| undefined
) {
if (repo?.kind && repo?.name) {
return `${repo.kind.toLowerCase()}/${repo.name}`
return `${getProviderVariantFromKind(repo.kind)}/${repo.name}`
}

return undefined
}

function repositoryList2Map(repos: RepositoryListQuery['repositoryList']) {
return keyBy(repos, o => `${o.kind.toLowerCase()}/${o.name}`)
return keyBy(repos, o => `${getProviderVariantFromKind(o.kind)}/${o.name}`)
}

function encodeURIComponentIgnoringSlash(str: string) {
Expand All @@ -133,5 +154,6 @@ export {
fetchEntriesFromPath,
resolveRepositoryInfoFromPath,
repositoryList2Map,
encodeURIComponentIgnoringSlash
encodeURIComponentIgnoringSlash,
getProviderVariantFromKind
}

0 comments on commit 2394644

Please sign in to comment.