fix(app): allow adding projects from root
This commit is contained in:
@@ -3,6 +3,7 @@ import { Dialog } from "@opencode-ai/ui/dialog"
|
|||||||
import { FileIcon } from "@opencode-ai/ui/file-icon"
|
import { FileIcon } from "@opencode-ai/ui/file-icon"
|
||||||
import { List } from "@opencode-ai/ui/list"
|
import { List } from "@opencode-ai/ui/list"
|
||||||
import { getDirectory, getFilename } from "@opencode-ai/util/path"
|
import { getDirectory, getFilename } from "@opencode-ai/util/path"
|
||||||
|
import fuzzysort from "fuzzysort"
|
||||||
import { createMemo } from "solid-js"
|
import { createMemo } from "solid-js"
|
||||||
import { useGlobalSDK } from "@/context/global-sdk"
|
import { useGlobalSDK } from "@/context/global-sdk"
|
||||||
import { useGlobalSync } from "@/context/global-sync"
|
import { useGlobalSync } from "@/context/global-sync"
|
||||||
@@ -21,63 +22,114 @@ export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
|
|||||||
const language = useLanguage()
|
const language = useLanguage()
|
||||||
|
|
||||||
const home = createMemo(() => sync.data.path.home)
|
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) {
|
function join(base: string | undefined, rel: string) {
|
||||||
const b = (base ?? "").replace(/[\\/]+$/, "")
|
const b = trimTrailing(base ?? "")
|
||||||
const r = rel.replace(/^[\\/]+/, "").replace(/[\\/]+$/, "")
|
const r = trimTrailing(rel).replace(/^\/+/, "")
|
||||||
if (!b) return r
|
if (!b) return r
|
||||||
if (!r) return b
|
if (!r) return b
|
||||||
|
if (b.endsWith("/")) return b + r
|
||||||
return b + "/" + r
|
return b + "/" + r
|
||||||
}
|
}
|
||||||
|
|
||||||
function display(rel: string) {
|
function rootOf(input: string) {
|
||||||
const full = join(root(), rel)
|
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()
|
const h = home()
|
||||||
if (!h) return full
|
if (!h) return full
|
||||||
if (full === h) return "~"
|
|
||||||
if (full.startsWith(h + "/") || full.startsWith(h + "\\")) {
|
const hn = trimTrailing(h)
|
||||||
return "~" + full.slice(h.length)
|
const lc = full.toLowerCase()
|
||||||
}
|
const hc = hn.toLowerCase()
|
||||||
|
if (lc === hc) return "~"
|
||||||
|
if (lc.startsWith(hc + "/")) return "~" + full.slice(hn.length)
|
||||||
return full
|
return full
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeQuery(query: string) {
|
function parse(filter: string) {
|
||||||
|
const base = start()
|
||||||
|
if (!base) return
|
||||||
|
|
||||||
|
const raw = normalizeDriveRoot(filter.trim())
|
||||||
|
if (!raw) return { directory: trimTrailing(base), query: "" }
|
||||||
|
|
||||||
const h = home()
|
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 (!query) return query
|
if (abs.endsWith("/")) return { directory: trimTrailing(abs), query: "" }
|
||||||
if (query.startsWith("~/")) return query.slice(2)
|
const i = abs.lastIndexOf("/")
|
||||||
|
if (i === -1) return { directory: trimTrailing(base), query: abs }
|
||||||
|
|
||||||
if (h) {
|
const dir = i === 0 ? "/" : /^[A-Za-z]:$/.test(abs.slice(0, i)) ? abs.slice(0, i) + "/" : abs.slice(0, i)
|
||||||
const lc = query.toLowerCase()
|
return { directory: trimTrailing(dir), query: abs.slice(i + 1) }
|
||||||
const hc = h.toLowerCase()
|
|
||||||
if (lc === hc || lc.startsWith(hc + "/") || lc.startsWith(hc + "\\")) {
|
|
||||||
return query.slice(h.length).replace(/^[\\/]+/, "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return query
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchDirs(query: string) {
|
async function fetchDirs(input: { directory: string; query: string }) {
|
||||||
const directory = root()
|
if (isRoot(input.directory)) {
|
||||||
if (!directory) return [] as string[]
|
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
|
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 ?? [])
|
.then((x) => x.data ?? [])
|
||||||
.catch(() => [])
|
.catch(() => [])
|
||||||
|
|
||||||
return results.map((x) => x.replace(/[\\/]+$/, ""))
|
return results.map((rel) => join(input.directory, rel))
|
||||||
}
|
}
|
||||||
|
|
||||||
const directories = async (filter: string) => {
|
const directories = async (filter: string) => {
|
||||||
const query = normalizeQuery(filter.trim())
|
const input = parse(filter)
|
||||||
return fetchDirs(query)
|
if (!input) return [] as string[]
|
||||||
|
return fetchDirs(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolve(rel: string) {
|
function resolve(absolute: string) {
|
||||||
const absolute = join(root(), rel)
|
|
||||||
props.onSelect(props.multiple ? [absolute] : absolute)
|
props.onSelect(props.multiple ? [absolute] : absolute)
|
||||||
dialog.close()
|
dialog.close()
|
||||||
}
|
}
|
||||||
@@ -95,12 +147,12 @@ export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
|
|||||||
resolve(path)
|
resolve(path)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{(rel) => {
|
{(absolute) => {
|
||||||
const path = display(rel)
|
const path = display(absolute)
|
||||||
return (
|
return (
|
||||||
<div class="w-full flex items-center justify-between rounded-md">
|
<div class="w-full flex items-center justify-between rounded-md">
|
||||||
<div class="flex items-center gap-x-3 grow min-w-0">
|
<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">
|
<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">
|
<span class="text-text-weak whitespace-nowrap overflow-hidden overflow-ellipsis truncate min-w-0">
|
||||||
{getDirectory(path)}
|
{getDirectory(path)}
|
||||||
|
|||||||
Reference in New Issue
Block a user