import { useState, useCallback, useEffect } 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 } from "../hooks/useDirectives";
import { useAuth } from "../contexts/AuthContext";
import type {
DirectiveWithChains,
CreateDirectiveRequest,
} from "../lib/api";
export default function DirectivesPage() {
const { isAuthenticated, isAuthConfigured, isLoading: authLoading } = useAuth();
const navigate = useNavigate();
// Redirect to login if not authenticated (when auth is configured)
useEffect(() => {
if (!authLoading && isAuthConfigured && !isAuthenticated) {
navigate("/login");
}
}, [authLoading, isAuthConfigured, isAuthenticated, navigate]);
if (authLoading) {
return (
);
}
if (isAuthConfigured && !isAuthenticated) {
return null;
}
return ;
}
function DirectivesContent() {
const { id } = useParams<{ id?: string }>();
const navigate = useNavigate();
const {
directives,
loading,
error,
fetchDirective,
saveDirective,
removeDirective,
startDirective,
} = useDirectives();
const [selectedDirective, setSelectedDirective] =
useState(null);
const [detailLoading, setDetailLoading] = useState(false);
const [showCreateForm, setShowCreateForm] = useState(false);
const [createTitle, setCreateTitle] = useState("");
const [createGoal, setCreateGoal] = useState("");
const [createRepoUrl, setCreateRepoUrl] = useState("");
// Load directive when ID changes
useEffect(() => {
if (id) {
setDetailLoading(true);
fetchDirective(id).then((d) => {
setSelectedDirective(d);
setDetailLoading(false);
});
} else {
setSelectedDirective(null);
}
}, [id, fetchDirective]);
const handleSelect = useCallback(
(directiveId: string) => {
navigate(`/directives/${directiveId}`);
},
[navigate]
);
const handleBack = useCallback(() => {
navigate("/directives");
}, [navigate]);
const handleCreate = useCallback(async () => {
if (!createTitle.trim() || !createGoal.trim()) return;
const data: CreateDirectiveRequest = {
title: createTitle.trim(),
goal: createGoal.trim(),
};
if (createRepoUrl.trim()) {
data.repositoryUrl = createRepoUrl.trim();
}
const result = await saveDirective(data);
if (result) {
setShowCreateForm(false);
setCreateTitle("");
setCreateGoal("");
setCreateRepoUrl("");
}
}, [createTitle, createGoal, createRepoUrl, saveDirective]);
const handleDelete = useCallback(
async (directiveId: string) => {
const ok = await removeDirective(directiveId);
if (ok && id === directiveId) {
navigate("/directives");
}
},
[removeDirective, id, navigate]
);
const handleStart = useCallback(
async (directiveId: string) => {
const ok = await startDirective(directiveId);
if (ok) {
// Refresh the detail view
const updated = await fetchDirective(directiveId);
if (updated) {
setSelectedDirective(updated);
}
}
},
[startDirective, fetchDirective]
);
const handleRefresh = useCallback(
(updated: DirectiveWithChains) => {
setSelectedDirective(updated);
},
[]
);
return (
{error && (
{error}
)}
{/* Create directive form */}
{showCreateForm && (
)}
{/* Directive list */}
setShowCreateForm(true)}
onDelete={(d) => handleDelete(d.id)}
selectedId={id}
/>
{/* Directive detail or empty state */}
{detailLoading ? (
) : selectedDirective ? (
) : (
Select a directive or create a new one
)}
);
}