dialog-select-directory.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { useDialog } from "@opencode-ai/ui/context/dialog"
  2. import { Dialog } from "@opencode-ai/ui/dialog"
  3. import { FileIcon } from "@opencode-ai/ui/file-icon"
  4. import { List } from "@opencode-ai/ui/list"
  5. import { getDirectory, getFilename } from "@opencode-ai/util/path"
  6. import { createMemo } from "solid-js"
  7. import { useGlobalSDK } from "@/context/global-sdk"
  8. import { useGlobalSync } from "@/context/global-sync"
  9. interface DialogSelectDirectoryProps {
  10. title?: string
  11. multiple?: boolean
  12. onSelect: (result: string | string[] | null) => void
  13. }
  14. export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
  15. const sync = useGlobalSync()
  16. const sdk = useGlobalSDK()
  17. const dialog = useDialog()
  18. const home = createMemo(() => sync.data.path.home)
  19. const root = createMemo(() => sync.data.path.home || sync.data.path.directory)
  20. function join(base: string | undefined, rel: string) {
  21. const b = (base ?? "").replace(/[\\/]+$/, "")
  22. const r = rel.replace(/^[\\/]+/, "").replace(/[\\/]+$/, "")
  23. if (!b) return r
  24. if (!r) return b
  25. return b + "/" + r
  26. }
  27. function display(rel: string) {
  28. const full = join(root(), rel)
  29. const h = home()
  30. if (!h) return full
  31. if (full === h) return "~"
  32. if (full.startsWith(h + "/") || full.startsWith(h + "\\")) {
  33. return "~" + full.slice(h.length)
  34. }
  35. return full
  36. }
  37. function normalizeQuery(query: string) {
  38. const h = home()
  39. if (!query) return query
  40. if (query.startsWith("~/")) return query.slice(2)
  41. if (h) {
  42. const lc = query.toLowerCase()
  43. const hc = h.toLowerCase()
  44. if (lc === hc || lc.startsWith(hc + "/") || lc.startsWith(hc + "\\")) {
  45. return query.slice(h.length).replace(/^[\\/]+/, "")
  46. }
  47. }
  48. return query
  49. }
  50. async function fetchDirs(query: string) {
  51. const directory = root()
  52. if (!directory) return [] as string[]
  53. const results = await sdk.client.find
  54. .files({ directory, query, type: "directory", limit: 50 })
  55. .then((x) => x.data ?? [])
  56. .catch(() => [])
  57. return results.map((x) => x.replace(/[\\/]+$/, ""))
  58. }
  59. const directories = async (filter: string) => {
  60. const query = normalizeQuery(filter.trim())
  61. return fetchDirs(query)
  62. }
  63. function resolve(rel: string) {
  64. const absolute = join(root(), rel)
  65. props.onSelect(props.multiple ? [absolute] : absolute)
  66. dialog.close()
  67. }
  68. return (
  69. <Dialog title={props.title ?? "Open project"}>
  70. <List
  71. search={{ placeholder: "Search folders", autofocus: true }}
  72. emptyMessage="No folders found"
  73. items={directories}
  74. key={(x) => x}
  75. onSelect={(path) => {
  76. if (!path) return
  77. resolve(path)
  78. }}
  79. >
  80. {(rel) => {
  81. const path = display(rel)
  82. return (
  83. <div class="w-full flex items-center justify-between rounded-md">
  84. <div class="flex items-center gap-x-3 grow min-w-0">
  85. <FileIcon node={{ path: rel, type: "directory" }} class="shrink-0 size-4" />
  86. <div class="flex items-center text-14-regular min-w-0">
  87. <span class="text-text-weak whitespace-nowrap overflow-hidden overflow-ellipsis truncate min-w-0">
  88. {getDirectory(path)}
  89. </span>
  90. <span class="text-text-strong whitespace-nowrap">{getFilename(path)}</span>
  91. </div>
  92. </div>
  93. </div>
  94. )
  95. }}
  96. </List>
  97. </Dialog>
  98. )
  99. }