2026-02-28 20:42:40 +00:00
|
|
|
import { useState, useEffect, useRef, useCallback } from "react";
|
2026-02-27 04:29:51 +00:00
|
|
|
import { open } from "@tauri-apps/plugin-dialog";
|
|
|
|
|
import { useProjects } from "../../hooks/useProjects";
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function AddProjectDialog({ onClose }: Props) {
|
|
|
|
|
const { add } = useProjects();
|
|
|
|
|
const [name, setName] = useState("");
|
|
|
|
|
const [path, setPath] = useState("");
|
|
|
|
|
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);
|
|
|
|
|
const overlayRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
|
|
// Auto-focus the first input when the dialog opens
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
nameInputRef.current?.focus();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
// Close on Escape key
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
|
|
|
if (e.key === "Escape") {
|
|
|
|
|
onClose();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
document.addEventListener("keydown", handleKeyDown);
|
|
|
|
|
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
|
|
|
}, [onClose]);
|
|
|
|
|
|
|
|
|
|
// Close on click outside (click on overlay backdrop)
|
|
|
|
|
const handleOverlayClick = useCallback(
|
|
|
|
|
(e: React.MouseEvent<HTMLDivElement>) => {
|
|
|
|
|
if (e.target === overlayRef.current) {
|
|
|
|
|
onClose();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[onClose],
|
|
|
|
|
);
|
2026-02-27 04:29:51 +00:00
|
|
|
|
|
|
|
|
const handleBrowse = async () => {
|
|
|
|
|
const selected = await open({ directory: true, multiple: false });
|
|
|
|
|
if (typeof selected === "string") {
|
|
|
|
|
setPath(selected);
|
|
|
|
|
if (!name) {
|
|
|
|
|
const parts = selected.replace(/[/\\]$/, "").split(/[/\\]/);
|
|
|
|
|
setName(parts[parts.length - 1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-28 20:42:40 +00:00
|
|
|
const handleSubmit = async (e?: React.FormEvent) => {
|
|
|
|
|
if (e) e.preventDefault();
|
2026-02-27 04:29:51 +00:00
|
|
|
if (!name.trim() || !path.trim()) {
|
|
|
|
|
setError("Name and path are required");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
try {
|
|
|
|
|
await add(name.trim(), path.trim());
|
|
|
|
|
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-02-28 20:42:40 +00:00
|
|
|
<div
|
|
|
|
|
ref={overlayRef}
|
|
|
|
|
onClick={handleOverlayClick}
|
|
|
|
|
className="fixed inset-0 bg-black/50 flex items-center justify-center z-50"
|
|
|
|
|
>
|
2026-02-27 04:29:51 +00:00
|
|
|
<div className="bg-[var(--bg-secondary)] border border-[var(--border-color)] rounded-lg p-6 w-96 shadow-xl">
|
|
|
|
|
<h2 className="text-lg font-semibold mb-4">Add Project</h2>
|
|
|
|
|
|
2026-02-28 20:42:40 +00:00
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
|
<label className="block text-sm text-[var(--text-secondary)] mb-1">
|
|
|
|
|
Project Name
|
|
|
|
|
</label>
|
2026-02-27 04:29:51 +00:00
|
|
|
<input
|
2026-02-28 20:42:40 +00:00
|
|
|
ref={nameInputRef}
|
|
|
|
|
value={name}
|
|
|
|
|
onChange={(e) => setName(e.target.value)}
|
|
|
|
|
placeholder="my-project"
|
|
|
|
|
className="w-full px-3 py-2 mb-3 bg-[var(--bg-primary)] border border-[var(--border-color)] rounded text-sm text-[var(--text-primary)] focus:outline-none focus:border-[var(--accent)]"
|
2026-02-27 04:29:51 +00:00
|
|
|
/>
|
|
|
|
|
|
2026-02-28 20:42:40 +00:00
|
|
|
<label className="block text-sm text-[var(--text-secondary)] mb-1">
|
|
|
|
|
Project Path
|
|
|
|
|
</label>
|
|
|
|
|
<div className="flex gap-2 mb-4">
|
|
|
|
|
<input
|
|
|
|
|
value={path}
|
|
|
|
|
onChange={(e) => setPath(e.target.value)}
|
|
|
|
|
placeholder="/path/to/project"
|
|
|
|
|
className="flex-1 px-3 py-2 bg-[var(--bg-primary)] border border-[var(--border-color)] rounded text-sm text-[var(--text-primary)] focus:outline-none focus:border-[var(--accent)]"
|
|
|
|
|
/>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={handleBrowse}
|
|
|
|
|
className="px-3 py-2 bg-[var(--bg-tertiary)] border border-[var(--border-color)] rounded text-sm hover:bg-[var(--border-color)] transition-colors"
|
|
|
|
|
>
|
|
|
|
|
Browse
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{error && (
|
|
|
|
|
<div className="text-xs text-[var(--error)] mb-3">{error}</div>
|
|
|
|
|
)}
|
2026-02-27 04:29:51 +00:00
|
|
|
|
2026-02-28 20:42:40 +00:00
|
|
|
<div className="flex justify-end gap-2">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={onClose}
|
|
|
|
|
className="px-4 py-2 text-sm text-[var(--text-secondary)] hover:text-[var(--text-primary)] transition-colors"
|
|
|
|
|
>
|
|
|
|
|
Cancel
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={loading}
|
|
|
|
|
className="px-4 py-2 text-sm bg-[var(--accent)] text-white rounded hover:bg-[var(--accent-hover)] disabled:opacity-50 transition-colors"
|
|
|
|
|
>
|
|
|
|
|
{loading ? "Adding..." : "Add Project"}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
2026-02-27 04:29:51 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|