Skip to content

Commit

Permalink
refactor(ui): selecting the first repository by default
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Apr 9, 2024
1 parent abeaa18 commit 70a4b2f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
6 changes: 5 additions & 1 deletion ee/tabby-ui/app/files/components/file-tree-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ const FileTreeHeader: React.FC<FileTreeHeaderProps> = ({
<div className={cn(className)} {...props}>
<div className="py-4 font-bold leading-8">Files</div>
<div className="space-y-3">
<Select onValueChange={onSelectRepo} value={curerntRepoName}>
<Select
disabled={!initialized}
onValueChange={onSelectRepo}
value={curerntRepoName}
>
<SelectTrigger>
<SelectValue>
<div className="flex items-center gap-2">
Expand Down
68 changes: 35 additions & 33 deletions ee/tabby-ui/app/files/components/source-code-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
}) => {
const {
activePath,
setActivePath,
updateFileMap,
fileMap,
initialized,
Expand Down Expand Up @@ -286,18 +287,25 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({

React.useEffect(() => {
const init = async () => {
const { patchMap, expandedKeys } = await getInitialFileData(activePath)
if (patchMap) {
updateFileMap(patchMap)
}
if (expandedKeys?.length) {
setExpandedKeys(new Set(expandedKeys))
const { patchMap, expandedKeys, repos } = await getInitialFileData(
activePath
)

// By default, selecting the first repository if initialPath is empty
if (repos?.length && !activePath) {
setActivePath(repos?.[0]?.basename)
return
}

if (patchMap) updateFileMap(patchMap)
if (expandedKeys?.length) setExpandedKeys(new Set(expandedKeys))
setInitialized(true)
}

init()
}, [])
if (!initialized) {
init()
}
}, [activePath])

React.useEffect(() => {
const onFetchSubTree = () => {
Expand Down Expand Up @@ -432,13 +440,15 @@ const SourceCodeBrowser: React.FC<SourceCodeBrowserProps> = props => {
}

async function getInitialFileData(path?: string) {
const initialRepositoryName = resolveRepoNameFromPath(path)
const initialBasename = resolveBasenameFromPath(path)

try {
const initialRepositoryName = resolveRepoNameFromPath(path)
const initialBasename = resolveBasenameFromPath(path)
const repos = await fetchAllRepositories()
const initialEntries = await getInitialEntries(repos)
const initialExpandedDirs = getDirectoriesFromBasename(initialBasename)

const initialEntries = path ? await fetchInitialEntries(repos, path) : []
const initialExpandedDirs = path
? getDirectoriesFromBasename(initialBasename)
: []

const patchMap: TFileMap = {}
for (const repo of repos) {
Expand All @@ -462,7 +472,7 @@ async function getInitialFileData(path?: string) {
[initialRepositoryName, dir].filter(Boolean).join('/')
)

return { patchMap, expandedKeys }
return { patchMap, expandedKeys, repos }
} catch (e) {
console.error(e)
return {}
Expand All @@ -479,31 +489,23 @@ async function getInitialFileData(path?: string) {
}
}

async function fetchInitialEntries(data?: TFile[]) {
try {
if (!initialRepositoryName) return undefined
// match default repository
const repositoryIdx = findIndex(
data,
entry => entry.basename === initialRepositoryName
)
if (repositoryIdx < 0) return undefined

return fetchEntriesFromPath(path)
} catch (e) {
console.error(e)
}
}

async function getInitialEntries(data?: TFile[]) {
async function fetchInitialEntries(repos: TFile[] | undefined, path: string) {
let result: TFile[] = []
try {
if (initialRepositoryName && data?.length) {
const defaultEntries = await fetchInitialEntries(data)
if (repos?.length && path) {
const repoName = resolveRepoNameFromPath(path)
const repositoryIdx = findIndex(
repos,
entry => entry.basename === repoName
)
if (repositoryIdx < 0) return result

const defaultEntries = await fetchEntriesFromPath(path)
result = defaultEntries ?? []
}
} catch (e) {
console.error(e)
return result
}
return result
}
Expand Down
3 changes: 2 additions & 1 deletion ee/tabby-ui/lib/tabby/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ const client = new Client({
cacheExchange({
keys: {
CompletionStats: () => null,
ServerInfo: () => null
ServerInfo: () => null,
RepositorySearch: () => null
},
resolvers: {
Query: {
Expand Down

0 comments on commit 70a4b2f

Please sign in to comment.