Skip to content

Commit 2ea728f

Browse files
authored
fix(app): populate project picker from home (#41158)
1 parent 284214c commit 2ea728f

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

packages/app/src/components/directory-picker-domain.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ test("resolves directory autocomplete from the current browser root", async () =
139139
directories.push(input.location?.directory ?? "")
140140
return Promise.resolve({ data: [] })
141141
},
142+
list: () => Promise.resolve({ data: [] }),
142143
},
143144
},
144145
} as unknown as Parameters<typeof createDirectorySearch>[0]["sdk"]
@@ -152,6 +153,70 @@ test("resolves directory autocomplete from the current browser root", async () =
152153
expect(directories).toEqual(["/repo", "/repo/src"])
153154
})
154155

156+
test("keeps indexed directory results for servers that support empty search", async () => {
157+
const sdk = {
158+
api: {
159+
file: {
160+
find: () => Promise.resolve({ data: [{ path: "projects/", type: "directory" }] }),
161+
list: () => Promise.reject(new Error("listing should not run when search returns results")),
162+
},
163+
},
164+
} as unknown as Parameters<typeof createDirectorySearch>[0]["sdk"]
165+
const search = createDirectorySearch({ sdk, home: () => "/home/luke", base: () => "/home/luke" })
166+
167+
expect(await search("")).toEqual(["/home/luke/projects"])
168+
})
169+
170+
test("lists the default directory when empty search is unsupported", async () => {
171+
const calls: string[] = []
172+
const directories = Array.from({ length: 60 }, (_, index) => ({
173+
path: `project-${index}/`,
174+
type: "directory" as const,
175+
}))
176+
const sdk = {
177+
api: {
178+
file: {
179+
find: () => Promise.resolve({ data: [] }),
180+
list: (input: { location?: { directory?: string } }) => {
181+
calls.push(input.location?.directory ?? "")
182+
return Promise.resolve({
183+
data: [
184+
...directories,
185+
{ path: "README.md", type: "file" },
186+
],
187+
})
188+
},
189+
},
190+
},
191+
} as unknown as Parameters<typeof createDirectorySearch>[0]["sdk"]
192+
const search = createDirectorySearch({ sdk, home: () => "/home/luke", base: () => "/home/luke" })
193+
194+
const results = await search("")
195+
expect(results).toHaveLength(60)
196+
expect(results.at(-1)).toBe("/home/luke/project-59")
197+
expect(calls).toEqual(["/home/luke"])
198+
})
199+
200+
test("matches the default directory listing when typed search is unsupported", async () => {
201+
const sdk = {
202+
api: {
203+
file: {
204+
find: () => Promise.resolve({ data: [] }),
205+
list: () =>
206+
Promise.resolve({
207+
data: [
208+
{ path: "Documents/", type: "directory" },
209+
{ path: "Downloads/", type: "directory" },
210+
],
211+
}),
212+
},
213+
},
214+
} as unknown as Parameters<typeof createDirectorySearch>[0]["sdk"]
215+
const search = createDirectorySearch({ sdk, home: () => "/home/luke", base: () => "/home/luke" })
216+
217+
expect(await search("documents")).toEqual(["/home/luke/Documents"])
218+
})
219+
155220
test("searches from an absolute root without a default base", async () => {
156221
const directories: string[] = []
157222
const sdk = {

packages/app/src/components/directory-picker-domain.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,14 @@ export function createDirectorySearch(args: { sdk: ServerSDK; base: () => string
379379
.then((result) => result.data.map((entry) => entry.path))
380380
.catch(() => [])
381381
if (!active()) return []
382-
return results.map((path) => joinPickerPath(input.directory, path)).slice(0, 50)
382+
if (results.length) {
383+
return results.map((path) => joinPickerPath(input.directory, path)).slice(0, 50)
384+
}
385+
const fallback = query
386+
? await match(input.directory, query, 50)
387+
: (await directories(input.directory)).map((item) => item.absolute)
388+
if (!active()) return []
389+
return fallback
383390
}
384391
const segments = query.replace(/^\/+/, "").split("/")
385392
const head = segments.slice(0, -1).filter((part) => part && part !== ".")

0 commit comments

Comments
 (0)