|
@@ -2,7 +2,7 @@ import { For, Show, createEffect, createUniqueId, on, onCleanup } from "solid-js
|
|
|
import { createStore } from "solid-js/store"
|
|
import { createStore } from "solid-js/store"
|
|
|
import { Icon } from "@opencode-ai/ui/icon"
|
|
import { Icon } from "@opencode-ai/ui/icon"
|
|
|
import { usePlatform } from "@/context/platform"
|
|
import { usePlatform } from "@/context/platform"
|
|
|
-import type { PaasProject } from "./projects"
|
|
|
|
|
|
|
+import { projectFolders, folderName, type PaasProject } from "./projects"
|
|
|
|
|
|
|
|
export function ProjectSwitcher(props: {
|
|
export function ProjectSwitcher(props: {
|
|
|
projects: PaasProject[]
|
|
projects: PaasProject[]
|
|
@@ -10,11 +10,21 @@ export function ProjectSwitcher(props: {
|
|
|
scope: string
|
|
scope: string
|
|
|
disabled?: boolean
|
|
disabled?: boolean
|
|
|
onSelect: (id: string) => void
|
|
onSelect: (id: string) => void
|
|
|
- onCreate: (name: string) => void
|
|
|
|
|
|
|
+ onCreate: (name: string, folder?: string) => void
|
|
|
|
|
+ onRename: (id: string, name: string) => void
|
|
|
|
|
+ onFolders: (id: string, folders: string[]) => void
|
|
|
onBind: (projectID: string, workspace: string) => void
|
|
onBind: (projectID: string, workspace: string) => void
|
|
|
}) {
|
|
}) {
|
|
|
const platform = usePlatform()
|
|
const platform = usePlatform()
|
|
|
- const [state, setState] = createStore({ creating: false, name: "", binding: false, error: "" })
|
|
|
|
|
|
|
+ const [state, setState] = createStore({
|
|
|
|
|
+ creating: false,
|
|
|
|
|
+ name: "",
|
|
|
|
|
+ binding: false,
|
|
|
|
|
+ error: "",
|
|
|
|
|
+ editing: false,
|
|
|
|
|
+ folder: "",
|
|
|
|
|
+ expanded: false,
|
|
|
|
|
+ })
|
|
|
const formID = createUniqueId()
|
|
const formID = createUniqueId()
|
|
|
let toggle: HTMLButtonElement | undefined
|
|
let toggle: HTMLButtonElement | undefined
|
|
|
let selector: HTMLSelectElement | undefined
|
|
let selector: HTMLSelectElement | undefined
|
|
@@ -26,13 +36,13 @@ export function ProjectSwitcher(props: {
|
|
|
createEffect(
|
|
createEffect(
|
|
|
on(
|
|
on(
|
|
|
() => props.scope,
|
|
() => props.scope,
|
|
|
- () => setState({ creating: false, name: "", error: "" }),
|
|
|
|
|
|
|
+ () => setState({ creating: false, name: "", error: "", editing: false, folder: "", expanded: false }),
|
|
|
),
|
|
),
|
|
|
)
|
|
)
|
|
|
createEffect(
|
|
createEffect(
|
|
|
on(
|
|
on(
|
|
|
() => props.activeID,
|
|
() => props.activeID,
|
|
|
- () => setState("error", ""),
|
|
|
|
|
|
|
+ () => setState({ error: "", creating: false, editing: false, name: "", folder: "" }),
|
|
|
),
|
|
),
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -41,15 +51,18 @@ export function ProjectSwitcher(props: {
|
|
|
toggle?.focus()
|
|
toggle?.focus()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const bind = async () => {
|
|
|
|
|
|
|
+ const bind = async (creating = false) => {
|
|
|
if (props.disabled || state.binding || platform.platform !== "desktop") return
|
|
if (props.disabled || state.binding || platform.platform !== "desktop") return
|
|
|
const projectID = props.activeID
|
|
const projectID = props.activeID
|
|
|
const scope = props.scope
|
|
const scope = props.scope
|
|
|
setState({ binding: true, error: "" })
|
|
setState({ binding: true, error: "" })
|
|
|
try {
|
|
try {
|
|
|
const directory = await platform.openDirectoryPickerDialog({ title: "选择项目工作文件夹" })
|
|
const directory = await platform.openDirectoryPickerDialog({ title: "选择项目工作文件夹" })
|
|
|
- if (!life.active || props.scope !== scope) return
|
|
|
|
|
- if (typeof directory === "string" && directory.trim()) props.onBind(projectID, directory)
|
|
|
|
|
|
|
+ if (!life.active || props.scope !== scope || props.activeID !== projectID || (creating && !state.creating)) return
|
|
|
|
|
+ if (typeof directory === "string" && directory.trim()) {
|
|
|
|
|
+ if (creating) setState({ folder: directory, name: state.name || folderName(directory) })
|
|
|
|
|
+ else props.onFolders(projectID, [...projectFolders(props.projects.find((p) => p.id === projectID)), directory])
|
|
|
|
|
+ }
|
|
|
} catch {
|
|
} catch {
|
|
|
if (life.active && props.scope === scope && props.activeID === projectID)
|
|
if (life.active && props.scope === scope && props.activeID === projectID)
|
|
|
setState("error", "未能打开文件夹选择器,请重试。")
|
|
setState("error", "未能打开文件夹选择器,请重试。")
|
|
@@ -60,8 +73,10 @@ export function ProjectSwitcher(props: {
|
|
|
|
|
|
|
|
const create = () => {
|
|
const create = () => {
|
|
|
if (props.disabled || !state.name.trim()) return
|
|
if (props.disabled || !state.name.trim()) return
|
|
|
- props.onCreate(state.name.trim())
|
|
|
|
|
- setState({ name: "", creating: false })
|
|
|
|
|
|
|
+ if (state.editing) props.onRename(props.activeID, state.name.trim())
|
|
|
|
|
+ else
|
|
|
|
|
+ props.onCreate(state.name.trim() === folderName(state.folder) ? "" : state.name.trim(), state.folder || undefined)
|
|
|
|
|
+ setState({ name: "", creating: false, editing: false, folder: "" })
|
|
|
selector?.focus()
|
|
selector?.focus()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -86,7 +101,9 @@ export function ProjectSwitcher(props: {
|
|
|
disabled={props.disabled}
|
|
disabled={props.disabled}
|
|
|
aria-expanded={state.creating}
|
|
aria-expanded={state.creating}
|
|
|
aria-controls={formID}
|
|
aria-controls={formID}
|
|
|
- onClick={() => (state.creating ? cancel() : setState("creating", true))}
|
|
|
|
|
|
|
+ onClick={() =>
|
|
|
|
|
+ state.creating ? cancel() : setState({ creating: true, editing: false, name: "", folder: "" })
|
|
|
|
|
+ }
|
|
|
>
|
|
>
|
|
|
新建项目
|
|
新建项目
|
|
|
</button>
|
|
</button>
|
|
@@ -124,27 +141,85 @@ export function ProjectSwitcher(props: {
|
|
|
value={state.name}
|
|
value={state.name}
|
|
|
onInput={(event) => setState("name", event.currentTarget.value)}
|
|
onInput={(event) => setState("name", event.currentTarget.value)}
|
|
|
/>
|
|
/>
|
|
|
|
|
+ <Show when={!state.editing && platform.platform === "desktop"}>
|
|
|
|
|
+ <button type="button" disabled={state.binding} onClick={() => void bind(true)}>
|
|
|
|
|
+ {state.folder ? folderName(state.folder) : "选择主文件夹"}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </Show>
|
|
|
<div class="paas-project-create-actions">
|
|
<div class="paas-project-create-actions">
|
|
|
<button type="button" onClick={cancel}>
|
|
<button type="button" onClick={cancel}>
|
|
|
取消
|
|
取消
|
|
|
</button>
|
|
</button>
|
|
|
<button type="submit" class="paas-primary" disabled={props.disabled || !state.name.trim()}>
|
|
<button type="submit" class="paas-primary" disabled={props.disabled || !state.name.trim()}>
|
|
|
- 创建项目
|
|
|
|
|
|
|
+ {state.editing ? "保存名称" : "创建项目"}
|
|
|
</button>
|
|
</button>
|
|
|
</div>
|
|
</div>
|
|
|
</form>
|
|
</form>
|
|
|
</Show>
|
|
</Show>
|
|
|
- <div class="paas-project-workspace">
|
|
|
|
|
- <Show when={active()?.workspace} fallback={<span>尚未绑定工作文件夹</span>}>
|
|
|
|
|
- {(workspace) => <span title={workspace()}>{workspace()}</span>}
|
|
|
|
|
- </Show>
|
|
|
|
|
- <Show when={platform.platform === "desktop"}>
|
|
|
|
|
- <button type="button" disabled={props.disabled || state.binding} onClick={() => void bind()}>
|
|
|
|
|
- <Icon name="folder" size="small" />
|
|
|
|
|
- <span>{state.binding ? "正在选择文件夹…" : active()?.workspace ? "更换工作文件夹" : "绑定工作文件夹"}</span>
|
|
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ class="paas-section-toggle"
|
|
|
|
|
+ aria-expanded={state.expanded}
|
|
|
|
|
+ onClick={() => setState("expanded", !state.expanded)}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Icon name={state.expanded ? "chevron-down" : "chevron-right"} size="small" />
|
|
|
|
|
+ <span>工作文件夹 · {projectFolders(active()).length}</span>
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <Show when={state.expanded}>
|
|
|
|
|
+ <div class="paas-project-workspace">
|
|
|
|
|
+ <small>工作区任务可使用全部绑定目录,默认从主目录开始;移除绑定不会删除文件。</small>
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ disabled={props.disabled}
|
|
|
|
|
+ onClick={() => setState({ creating: true, editing: true, name: active()?.name || "" })}
|
|
|
|
|
+ >
|
|
|
|
|
+ 重命名项目
|
|
|
</button>
|
|
</button>
|
|
|
- </Show>
|
|
|
|
|
- </div>
|
|
|
|
|
|
|
+ <For each={projectFolders(active())}>
|
|
|
|
|
+ {(folder, index) => (
|
|
|
|
|
+ <div class="paas-folder-row">
|
|
|
|
|
+ <span title={folder}>
|
|
|
|
|
+ {index() === 0 ? "主目录 · " : ""}
|
|
|
|
|
+ {folderName(folder)}
|
|
|
|
|
+ </span>
|
|
|
|
|
+ <Show when={index() > 0}>
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ disabled={props.disabled}
|
|
|
|
|
+ onClick={() =>
|
|
|
|
|
+ props.onFolders(props.activeID, [folder, ...projectFolders(active()).filter((p) => p !== folder)])
|
|
|
|
|
+ }
|
|
|
|
|
+ >
|
|
|
|
|
+ 设为主目录
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ disabled={props.disabled}
|
|
|
|
|
+ aria-label={`移除文件夹 ${folderName(folder)}`}
|
|
|
|
|
+ onClick={() =>
|
|
|
|
|
+ props.onFolders(
|
|
|
|
|
+ props.activeID,
|
|
|
|
|
+ projectFolders(active()).filter((p) => p !== folder),
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ >
|
|
|
|
|
+ 移除
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </For>
|
|
|
|
|
+ <Show when={active()?.workspace} fallback={<span>尚未绑定工作文件夹</span>}>
|
|
|
|
|
+ {(workspace) => <span title={workspace()}>{workspace()}</span>}
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ <Show when={platform.platform === "desktop"}>
|
|
|
|
|
+ <button type="button" disabled={props.disabled || state.binding} onClick={() => void bind()}>
|
|
|
|
|
+ <Icon name="folder" size="small" />
|
|
|
|
|
+ <span>{state.binding ? "正在选择文件夹…" : "添加工作文件夹"}</span>
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </Show>
|
|
|
<Show when={state.error}>
|
|
<Show when={state.error}>
|
|
|
<p class="paas-project-error" role="status">
|
|
<p class="paas-project-error" role="status">
|
|
|
{state.error}
|
|
{state.error}
|