Browse Source

fix(app): allow adding projects from root

Adam 1 month ago
parent
commit
a8018dcc43
1 changed files with 85 additions and 33 deletions
  1. 85 33
      packages/app/src/components/dialog-select-directory.tsx

+ 85 - 33
packages/app/src/components/dialog-select-directory.tsx

@@ -3,6 +3,7 @@ import { Dialog } from "@opencode-ai/ui/dialog"
 import { FileIcon } from "@opencode-ai/ui/file-icon"
 import { List } from "@opencode-ai/ui/list"
 import { getDirectory, getFilename } from "@opencode-ai/util/path"
+import fuzzysort from "fuzzysort"
 import { createMemo } from "solid-js"
 import { useGlobalSDK } from "@/context/global-sdk"
 import { useGlobalSync } from "@/context/global-sync"
@@ -21,63 +22,114 @@ export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
   const language = useLanguage()
 
   const home = createMemo(() => sync.data.path.home)
-  const root = createMemo(() => sync.data.path.home || sync.data.path.directory)
+
+  const start = createMemo(() => sync.data.path.home || sync.data.path.directory)
+
+  function normalize(input: string) {
+    const v = input.replaceAll("\\", "/")
+    if (v.startsWith("//") && !v.startsWith("///")) return "//" + v.slice(2).replace(/\/+/g, "/")
+    return v.replace(/\/+/g, "/")
+  }
+
+  function normalizeDriveRoot(input: string) {
+    const v = normalize(input)
+    if (/^[A-Za-z]:$/.test(v)) return v + "/"
+    return v
+  }
+
+  function trimTrailing(input: string) {
+    const v = normalizeDriveRoot(input)
+    if (v === "/") return v
+    if (/^[A-Za-z]:\/$/.test(v)) return v
+    return v.replace(/\/+$/, "")
+  }
 
   function join(base: string | undefined, rel: string) {
-    const b = (base ?? "").replace(/[\\/]+$/, "")
-    const r = rel.replace(/^[\\/]+/, "").replace(/[\\/]+$/, "")
+    const b = trimTrailing(base ?? "")
+    const r = trimTrailing(rel).replace(/^\/+/, "")
     if (!b) return r
     if (!r) return b
+    if (b.endsWith("/")) return b + r
     return b + "/" + r
   }
 
-  function display(rel: string) {
-    const full = join(root(), rel)
+  function rootOf(input: string) {
+    const v = normalizeDriveRoot(input)
+    if (v.startsWith("//")) return "//"
+    if (v.startsWith("/")) return "/"
+    if (/^[A-Za-z]:\//.test(v)) return v.slice(0, 3)
+    return ""
+  }
+
+  function isRoot(input: string) {
+    const v = trimTrailing(input)
+    if (v === "/") return true
+    return /^[A-Za-z]:\/$/.test(v)
+  }
+
+  function display(path: string) {
+    const full = trimTrailing(path)
     const h = home()
     if (!h) return full
-    if (full === h) return "~"
-    if (full.startsWith(h + "/") || full.startsWith(h + "\\")) {
-      return "~" + full.slice(h.length)
-    }
+
+    const hn = trimTrailing(h)
+    const lc = full.toLowerCase()
+    const hc = hn.toLowerCase()
+    if (lc === hc) return "~"
+    if (lc.startsWith(hc + "/")) return "~" + full.slice(hn.length)
     return full
   }
 
-  function normalizeQuery(query: string) {
-    const h = home()
+  function parse(filter: string) {
+    const base = start()
+    if (!base) return
 
-    if (!query) return query
-    if (query.startsWith("~/")) return query.slice(2)
+    const raw = normalizeDriveRoot(filter.trim())
+    if (!raw) return { directory: trimTrailing(base), query: "" }
 
-    if (h) {
-      const lc = query.toLowerCase()
-      const hc = h.toLowerCase()
-      if (lc === hc || lc.startsWith(hc + "/") || lc.startsWith(hc + "\\")) {
-        return query.slice(h.length).replace(/^[\\/]+/, "")
-      }
-    }
+    const h = home()
+    const expanded = raw === "~" ? h : raw.startsWith("~/") ? (h ? join(h, raw.slice(2)) : raw.slice(2)) : raw
+    const absolute = rootOf(expanded) ? expanded : join(base, expanded)
+    const abs = normalizeDriveRoot(absolute)
+
+    if (abs.endsWith("/")) return { directory: trimTrailing(abs), query: "" }
+    const i = abs.lastIndexOf("/")
+    if (i === -1) return { directory: trimTrailing(base), query: abs }
 
-    return query
+    const dir = i === 0 ? "/" : /^[A-Za-z]:$/.test(abs.slice(0, i)) ? abs.slice(0, i) + "/" : abs.slice(0, i)
+    return { directory: trimTrailing(dir), query: abs.slice(i + 1) }
   }
 
-  async function fetchDirs(query: string) {
-    const directory = root()
-    if (!directory) return [] as string[]
+  async function fetchDirs(input: { directory: string; query: string }) {
+    if (isRoot(input.directory)) {
+      const nodes = await sdk.client.file
+        .list({ directory: input.directory, path: "" })
+        .then((x) => x.data ?? [])
+        .catch(() => [])
+
+      const dirs = nodes.filter((n) => n.type === "directory").map((n) => n.name)
+      const sorted = input.query
+        ? fuzzysort.go(input.query, dirs, { limit: 50 }).map((x) => x.target)
+        : dirs.slice().sort((a, b) => a.localeCompare(b))
+
+      return sorted.slice(0, 50).map((name) => join(input.directory, name))
+    }
 
     const results = await sdk.client.find
-      .files({ directory, query, type: "directory", limit: 50 })
+      .files({ directory: input.directory, query: input.query, type: "directory", limit: 50 })
       .then((x) => x.data ?? [])
       .catch(() => [])
 
-    return results.map((x) => x.replace(/[\\/]+$/, ""))
+    return results.map((rel) => join(input.directory, rel))
   }
 
   const directories = async (filter: string) => {
-    const query = normalizeQuery(filter.trim())
-    return fetchDirs(query)
+    const input = parse(filter)
+    if (!input) return [] as string[]
+    return fetchDirs(input)
   }
 
-  function resolve(rel: string) {
-    const absolute = join(root(), rel)
+  function resolve(absolute: string) {
     props.onSelect(props.multiple ? [absolute] : absolute)
     dialog.close()
   }
@@ -95,12 +147,12 @@ export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
           resolve(path)
         }}
       >
-        {(rel) => {
-          const path = display(rel)
+        {(absolute) => {
+          const path = display(absolute)
           return (
             <div class="w-full flex items-center justify-between rounded-md">
               <div class="flex items-center gap-x-3 grow min-w-0">
-                <FileIcon node={{ path: rel, type: "directory" }} class="shrink-0 size-4" />
+                <FileIcon node={{ path: absolute, type: "directory" }} class="shrink-0 size-4" />
                 <div class="flex items-center text-14-regular min-w-0">
                   <span class="text-text-weak whitespace-nowrap overflow-hidden overflow-ellipsis truncate min-w-0">
                     {getDirectory(path)}