import { useState, useEffect, useCallback } from "react"; import { useParams, useNavigate } from "react-router"; import { Masthead } from "../components/Masthead"; import { DirectiveList } from "../components/directives/DirectiveList"; import { DirectiveDetail } from "../components/directives/DirectiveDetail"; import { useDirectives, useDirective } from "../hooks/useDirectives"; import { useAuth } from "../contexts/AuthContext"; import { getRepositorySuggestions, type RepositoryHistoryEntry } from "../lib/api"; export default function DirectivesPage() { const { isAuthenticated, isAuthConfigured, isLoading: authLoading } = useAuth(); const navigate = useNavigate(); const { id: selectedId } = useParams<{ id: string }>(); const { directives, loading: listLoading, create, remove } = useDirectives(); const { directive, refresh: refreshDetail, update, start, pause, advance, completeStep, failStep, skipStep, updateGoal, cleanup, pickUpOrders, createPR } = useDirective(selectedId); const [showCreate, setShowCreate] = useState(false); const [newTitle, setNewTitle] = useState(""); const [newGoal, setNewGoal] = useState(""); const [newRepoUrl, setNewRepoUrl] = useState(""); const [repoSuggestions, setRepoSuggestions] = useState([]); const [showRepoSuggestions, setShowRepoSuggestions] = useState(false); // Fetch repository suggestions when create form opens useEffect(() => { if (showCreate) { getRepositorySuggestions("remote", undefined, 10) .then((res) => { setRepoSuggestions(res.entries); setShowRepoSuggestions(res.entries.length > 0); }) .catch(() => { setRepoSuggestions([]); setShowRepoSuggestions(false); }); } else { setRepoSuggestions([]); setShowRepoSuggestions(false); } }, [showCreate]); const applyRepoSuggestion = useCallback((suggestion: RepositoryHistoryEntry) => { if (suggestion.repositoryUrl) { setNewRepoUrl(suggestion.repositoryUrl); } if (!newTitle.trim() && suggestion.name) { setNewTitle(suggestion.name); } setShowRepoSuggestions(false); }, [newTitle]); useEffect(() => { if (!authLoading && isAuthConfigured && !isAuthenticated) { navigate("/login"); } }, [authLoading, isAuthConfigured, isAuthenticated, navigate]); if (authLoading) { return (

Loading...

); } const handleCreate = async () => { if (!newTitle.trim() || !newGoal.trim()) return; try { const d = await create({ title: newTitle.trim(), goal: newGoal.trim(), repositoryUrl: newRepoUrl.trim() || undefined, }); setShowCreate(false); setNewTitle(""); setNewGoal(""); setNewRepoUrl(""); navigate(`/directives/${d.id}`); } catch (e) { console.error("Failed to create directive:", e); } }; const handleDelete = async () => { if (!selectedId) return; if (!window.confirm("Delete this directive?")) return; try { await remove(selectedId); navigate("/directives"); } catch (e) { console.error("Failed to delete:", e); } }; return (
{/* Left: List */}
navigate(`/directives/${id}`)} onCreate={() => setShowCreate(true)} />
{/* Right: Detail or Create */}
{showCreate ? (

New Directive

setNewTitle(e.target.value)} placeholder="Project title..." className="w-full bg-[#0a1628] border border-[rgba(117,170,252,0.2)] rounded px-2 py-1.5 text-[12px] font-mono text-white" />