Skip to content

Commit a244dd9

Browse files
committed
fix: Open project dialog shows directories properly in web UI
- Make query parameter optional in /find/file endpoint (default to empty string) - Add directory parameter support to search from specified path - Fix useFilteredList to properly handle async items functions with null safety - Add null-safety for filter parameter in dialog-select-directory Fixes anomalyco#8325
1 parent ff669d4 commit a244dd9

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎packages/app/src/components/dialog-select-directory.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
7070
}
7171

7272
const directories = async (filter: string) => {
73-
const query = normalizeQuery(filter.trim())
73+
const query = normalizeQuery((filter ?? "").trim())
7474
return fetchDirs(query)
7575
}
7676

‎packages/opencode/src/server/server.ts‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,20 +1962,56 @@ export namespace Server {
19621962
validator(
19631963
"query",
19641964
z.object({
1965-
query: z.string(),
1965+
directory: z.string().optional(),
1966+
query: z.string().optional().default(""),
19661967
dirs: z.enum(["true", "false"]).optional(),
19671968
type: z.enum(["file", "directory"]).optional(),
19681969
limit: z.coerce.number().int().min(1).max(200).optional(),
19691970
}),
19701971
),
19711972
async (c) => {
1972-
const query = c.req.valid("query").query
1973+
const directory = c.req.valid("query").directory
1974+
const query = c.req.valid("query").query ?? ""
19731975
const dirs = c.req.valid("query").dirs
19741976
const type = c.req.valid("query").type
1975-
const limit = c.req.valid("query").limit
1977+
const limit = c.req.valid("query").limit ?? 10
1978+
1979+
if (directory && type === "directory") {
1980+
const fs = await import("fs")
1981+
const path = await import("path")
1982+
const fuzzysort = await import("fuzzysort")
1983+
1984+
const scanDirs = async (base: string, depth: number = 0): Promise<string[]> => {
1985+
if (depth > 2) return []
1986+
const results: string[] = []
1987+
try {
1988+
const entries = await fs.promises.readdir(base, { withFileTypes: true })
1989+
for (const entry of entries) {
1990+
if (!entry.isDirectory()) continue
1991+
if (entry.name.startsWith(".")) continue
1992+
if (["node_modules", "dist", "build", ".git"].includes(entry.name)) continue
1993+
const rel = path.relative(directory, path.join(base, entry.name))
1994+
results.push(rel + "/")
1995+
if (depth < 1) {
1996+
const children = await scanDirs(path.join(base, entry.name), depth + 1)
1997+
results.push(...children)
1998+
}
1999+
}
2000+
} catch {}
2001+
return results
2002+
}
2003+
2004+
const allDirs = await scanDirs(directory)
2005+
if (!query) {
2006+
return c.json(allDirs.slice(0, limit))
2007+
}
2008+
const sorted = fuzzysort.go(query, allDirs, { limit }).map((r) => r.target)
2009+
return c.json(sorted)
2010+
}
2011+
19762012
const results = await File.search({
19772013
query,
1978-
limit: limit ?? 10,
2014+
limit,
19792015
dirs: dirs !== "false",
19802016
type,
19812017
})

‎packages/ui/src/hooks/use-filtered-list.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ export function useFilteredList<T>(props: FilteredListProps<T>) {
3333
}),
3434
async ({ filter, items }) => {
3535
const needle = filter?.toLowerCase()
36-
const all = (items ?? (await (props.items as (filter: string) => T[] | Promise<T[]>)(needle))) || []
36+
let all: T[]
37+
if (items !== undefined) {
38+
all = Array.isArray(items) ? items : []
39+
} else if (typeof props.items === "function") {
40+
all = (await props.items(needle ?? "")) || []
41+
} else {
42+
all = []
43+
}
3744
const result = pipe(
3845
all,
3946
(x) => {

0 commit comments

Comments
 (0)