2026-08-09 11:35:42 -07:00
|
|
|
import { useId, useRef, useState } from "react";
|
2026-02-27 04:29:51 +00:00
|
|
|
import { open } from "@tauri-apps/plugin-dialog";
|
|
|
|
|
import { useProjects } from "../../hooks/useProjects";
|
2026-02-28 21:18:33 +00:00
|
|
|
import type { ProjectPath } from "../../lib/types";
|
2026-08-09 11:35:42 -07:00
|
|
|
import Modal from "../ui/Modal";
|
|
|
|
|
import Button from "../ui/Button";
|
|
|
|
|
import { inputClass, monoInputClass } from "../ui/Field";
|
2026-02-27 04:29:51 +00:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-28 21:18:33 +00:00
|
|
|
interface PathEntry {
|
|
|
|
|
host_path: string;
|
|
|
|
|
mount_name: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function basenameFromPath(p: string): string {
|
|
|
|
|
return p.replace(/[/\\]$/, "").split(/[/\\]/).pop() || "";
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-27 04:29:51 +00:00
|
|
|
export default function AddProjectDialog({ onClose }: Props) {
|
|
|
|
|
const { add } = useProjects();
|
|
|
|
|
const [name, setName] = useState("");
|
2026-02-28 21:18:33 +00:00
|
|
|
const [pathEntries, setPathEntries] = useState<PathEntry[]>([
|
|
|
|
|
{ host_path: "", mount_name: "" },
|
|
|
|
|
]);
|
2026-02-27 04:29:51 +00:00
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
2026-02-28 20:42:40 +00:00
|
|
|
const nameInputRef = useRef<HTMLInputElement>(null);
|
2026-08-09 11:35:42 -07:00
|
|
|
const formId = useId();
|
2026-02-27 04:29:51 +00:00
|
|
|
|
2026-02-28 21:18:33 +00:00
|
|
|
const handleBrowse = async (index: number) => {
|
2026-02-27 04:29:51 +00:00
|
|
|
const selected = await open({ directory: true, multiple: false });
|
|
|
|
|
if (typeof selected === "string") {
|
2026-02-28 21:18:33 +00:00
|
|
|
const basename = basenameFromPath(selected);
|
|
|
|
|
const entries = [...pathEntries];
|
|
|
|
|
entries[index] = {
|
|
|
|
|
host_path: selected,
|
|
|
|
|
mount_name: entries[index].mount_name || basename,
|
|
|
|
|
};
|
|
|
|
|
setPathEntries(entries);
|
|
|
|
|
// Auto-fill project name from first folder
|
|
|
|
|
if (!name && index === 0) {
|
|
|
|
|
setName(basename);
|
2026-02-27 04:29:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-09 11:35:42 -07:00
|
|
|
const updateEntry = (index: number, field: keyof PathEntry, value: string) => {
|
2026-02-28 21:18:33 +00:00
|
|
|
const entries = [...pathEntries];
|
|
|
|
|
entries[index] = { ...entries[index], [field]: value };
|
|
|
|
|
setPathEntries(entries);
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-28 20:42:40 +00:00
|
|
|
const handleSubmit = async (e?: React.FormEvent) => {
|
|
|
|
|
if (e) e.preventDefault();
|
2026-02-28 21:18:33 +00:00
|
|
|
if (!name.trim()) {
|
|
|
|
|
setError("Project name is required");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const validPaths: ProjectPath[] = pathEntries
|
|
|
|
|
.filter((p) => p.host_path.trim())
|
|
|
|
|
.map((p) => ({
|
|
|
|
|
host_path: p.host_path.trim(),
|
|
|
|
|
mount_name: p.mount_name.trim() || basenameFromPath(p.host_path),
|
|
|
|
|
}));
|
|
|
|
|
if (validPaths.length === 0) {
|
|
|
|
|
setError("At least one folder path is required");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const mountNames = validPaths.map((p) => p.mount_name);
|
|
|
|
|
if (new Set(mountNames).size !== mountNames.length) {
|
|
|
|
|
setError("Mount names must be unique");
|
2026-02-27 04:29:51 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
try {
|
2026-02-28 21:18:33 +00:00
|
|
|
await add(name.trim(), validPaths);
|
2026-02-27 04:29:51 +00:00
|
|
|
onClose();
|
2026-02-28 20:42:40 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
setError(String(err));
|
2026-02-27 04:29:51 +00:00
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2026-08-09 11:35:42 -07:00
|
|
|
<Modal
|
|
|
|
|
title="Add Project"
|
|
|
|
|
onClose={onClose}
|
|
|
|
|
widthClassName="w-[30rem]"
|
|
|
|
|
initialFocusRef={nameInputRef}
|
|
|
|
|
footer={
|
|
|
|
|
<>
|
|
|
|
|
<Button size="md" variant="ghost" onClick={onClose}>
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
<Button size="md" variant="primary" type="submit" form={formId} disabled={loading}>
|
|
|
|
|
{loading ? "Adding…" : "Add Project"}
|
|
|
|
|
</Button>
|
|
|
|
|
</>
|
|
|
|
|
}
|
2026-02-28 20:42:40 +00:00
|
|
|
>
|
2026-08-09 11:35:42 -07:00
|
|
|
<form id={formId} onSubmit={handleSubmit} className="space-y-4">
|
|
|
|
|
<div>
|
|
|
|
|
<label
|
|
|
|
|
htmlFor={`${formId}-name`}
|
|
|
|
|
className="block text-[13px] font-medium mb-1"
|
|
|
|
|
>
|
|
|
|
|
Project name
|
2026-02-28 20:42:40 +00:00
|
|
|
</label>
|
2026-02-27 04:29:51 +00:00
|
|
|
<input
|
2026-08-09 11:35:42 -07:00
|
|
|
id={`${formId}-name`}
|
2026-02-28 20:42:40 +00:00
|
|
|
ref={nameInputRef}
|
|
|
|
|
value={name}
|
|
|
|
|
onChange={(e) => setName(e.target.value)}
|
|
|
|
|
placeholder="my-project"
|
2026-08-09 11:35:42 -07:00
|
|
|
className={inputClass}
|
2026-02-27 04:29:51 +00:00
|
|
|
/>
|
2026-08-09 11:35:42 -07:00
|
|
|
</div>
|
2026-02-27 04:29:51 +00:00
|
|
|
|
2026-08-09 11:35:42 -07:00
|
|
|
<div>
|
|
|
|
|
<span className="block text-[13px] font-medium mb-1">Folders</span>
|
|
|
|
|
<div className="space-y-2">
|
2026-02-28 21:18:33 +00:00
|
|
|
{pathEntries.map((entry, i) => (
|
2026-08-09 11:35:42 -07:00
|
|
|
<div
|
|
|
|
|
key={i}
|
|
|
|
|
className="space-y-1.5 p-2 bg-[var(--bg-primary)] rounded-[var(--radius-control)] border border-[var(--border-color)]"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex gap-1.5">
|
2026-02-28 21:18:33 +00:00
|
|
|
<input
|
|
|
|
|
value={entry.host_path}
|
|
|
|
|
onChange={(e) => updateEntry(i, "host_path", e.target.value)}
|
|
|
|
|
placeholder="/path/to/folder"
|
2026-08-09 11:35:42 -07:00
|
|
|
aria-label={`Folder ${i + 1} host path`}
|
|
|
|
|
className={inputClass}
|
2026-02-28 21:18:33 +00:00
|
|
|
/>
|
2026-08-09 11:35:42 -07:00
|
|
|
<Button size="md" onClick={() => handleBrowse(i)}>
|
2026-02-28 21:18:33 +00:00
|
|
|
Browse
|
2026-08-09 11:35:42 -07:00
|
|
|
</Button>
|
2026-02-28 21:18:33 +00:00
|
|
|
{pathEntries.length > 1 && (
|
2026-08-09 11:35:42 -07:00
|
|
|
<Button
|
|
|
|
|
size="md"
|
|
|
|
|
variant="danger"
|
|
|
|
|
aria-label={`Remove folder ${i + 1}`}
|
|
|
|
|
onClick={() =>
|
|
|
|
|
setPathEntries(pathEntries.filter((_, j) => j !== i))
|
|
|
|
|
}
|
2026-02-28 21:18:33 +00:00
|
|
|
>
|
2026-08-09 11:35:42 -07:00
|
|
|
Remove
|
|
|
|
|
</Button>
|
2026-02-28 21:18:33 +00:00
|
|
|
)}
|
|
|
|
|
</div>
|
2026-08-09 11:35:42 -07:00
|
|
|
<div className="flex items-center gap-1.5">
|
|
|
|
|
<span className="text-xs text-[var(--text-secondary)] flex-shrink-0 font-mono">
|
|
|
|
|
/workspace/
|
|
|
|
|
</span>
|
2026-02-28 21:18:33 +00:00
|
|
|
<input
|
|
|
|
|
value={entry.mount_name}
|
|
|
|
|
onChange={(e) => updateEntry(i, "mount_name", e.target.value)}
|
|
|
|
|
placeholder="mount-name"
|
2026-08-09 11:35:42 -07:00
|
|
|
aria-label={`Folder ${i + 1} mount name`}
|
|
|
|
|
className={monoInputClass}
|
2026-02-28 21:18:33 +00:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
2026-02-28 20:42:40 +00:00
|
|
|
</div>
|
2026-08-09 11:35:42 -07:00
|
|
|
<Button
|
|
|
|
|
className="mt-2"
|
|
|
|
|
onClick={() =>
|
|
|
|
|
setPathEntries([...pathEntries, { host_path: "", mount_name: "" }])
|
|
|
|
|
}
|
2026-02-28 21:18:33 +00:00
|
|
|
>
|
|
|
|
|
+ Add folder
|
2026-08-09 11:35:42 -07:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
2026-02-27 04:29:51 +00:00
|
|
|
|
2026-08-09 11:35:42 -07:00
|
|
|
{error && (
|
|
|
|
|
<div
|
|
|
|
|
role="alert"
|
|
|
|
|
className="px-2 py-1.5 text-xs text-[var(--error)] bg-[var(--error-muted)] border border-[var(--error)]/30 rounded-[var(--radius-control)]"
|
|
|
|
|
>
|
|
|
|
|
{error}
|
2026-02-28 20:42:40 +00:00
|
|
|
</div>
|
2026-08-09 11:35:42 -07:00
|
|
|
)}
|
|
|
|
|
</form>
|
|
|
|
|
</Modal>
|
2026-02-27 04:29:51 +00:00
|
|
|
);
|
|
|
|
|
}
|