Intelligence

Artifacts

Browse the repository, read documents, and manage the governance folders. Source, runtime, and infrastructure are read-only.

Repository
.gitignoreDockerfilenext-env.d.tsnext.config.mjspackage-lock.jsonpackage.jsonpostcss.config.mjsREADME.mdtailwind.config.tstsconfig.jsontsconfig.tsbuildinfo
README.md
CONSTITUTION_COMPLIANCE_AUDIT_V1.mdREADME.md
repositories/aaf-holdings/hq01/components/missions/mission-edit.tsx
5.1 KB
"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { Pencil, X, Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Card } from "@/components/ui/card";
import type { OwnerOption } from "./create-mission-form";
import type { Mission, MissionPriority } from "@/lib/missions/types";

const PRIORITIES: MissionPriority[] = ["P0", "P1", "P2", "P3"];

/**
 * Edit a mission's fields. Status is NOT edited here — it is governed by the
 * lifecycle state machine (see MissionStateActions). Reassigning the owner
 * records executive_assigned.
 */
export function MissionEdit({
  mission,
  owners,
}: {
  mission: Mission;
  owners: OwnerOption[];
}) {
  const router = useRouter();
  const [open, setOpen] = useState(false);
  const [busy, setBusy] = useState<string | null>(null);
  const [error, setError] = useState<string | null>(null);
  const [form, setForm] = useState({
    title: mission.title,
    description: mission.description,
    product: mission.product,
    repository: mission.repository,
    executive_owner: mission.executive_owner ?? "",
    priority: mission.priority,
  });

  async function save(e: React.FormEvent) {
    e.preventDefault();
    setBusy("save");
    setError(null);
    try {
      const res = await fetch(`/api/missions/${mission.id}`, {
        method: "PATCH",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(form),
      });
      const data = await res.json().catch(() => ({}));
      if (!res.ok) throw new Error(data.error ?? "Update failed.");
      setOpen(false);
      router.refresh();
    } catch (err) {
      setError(err instanceof Error ? err.message : "Update failed.");
    } finally {
      setBusy(null);
    }
  }

  return (
    <div className="flex flex-col items-end gap-2">
      <Button type="button" variant="outline" size="sm" onClick={() => setOpen((v) => !v)}>
        {open ? <X className="h-3.5 w-3.5" /> : <Pencil className="h-3.5 w-3.5" />}
        {open ? "Close" : "Edit"}
      </Button>

      {error && <span className="text-[11px] text-destructive">{error}</span>}

      {open && (
        <Card className="w-full max-w-md p-5 text-left">
          <form className="space-y-3" onSubmit={save}>
            <Labeled label="Title">
              <Input value={form.title} onChange={(e) => setForm({ ...form, title: e.target.value })} />
            </Labeled>
            <Labeled label="Description">
              <textarea
                value={form.description}
                onChange={(e) => setForm({ ...form, description: e.target.value })}
                rows={2}
                className="flex w-full rounded-md border border-input bg-background px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
              />
            </Labeled>
            <div className="grid grid-cols-2 gap-3">
              <Labeled label="Product">
                <Input value={form.product} onChange={(e) => setForm({ ...form, product: e.target.value })} />
              </Labeled>
              <Labeled label="Repository">
                <Input
                  value={form.repository}
                  onChange={(e) => setForm({ ...form, repository: e.target.value })}
                  className="font-mono text-xs"
                />
              </Labeled>
            </div>
            <div className="grid grid-cols-2 gap-3">
              <Labeled label="Owner">
                <select
                  value={form.executive_owner}
                  onChange={(e) => setForm({ ...form, executive_owner: e.target.value })}
                  className="flex h-10 w-full rounded-md border border-input bg-background px-2 text-sm"
                >
                  {owners.map((o) => (
                    <option key={o.id} value={o.id}>
                      {o.label}
                    </option>
                  ))}
                </select>
              </Labeled>
              <Labeled label="Priority">
                <select
                  value={form.priority}
                  onChange={(e) => setForm({ ...form, priority: e.target.value as MissionPriority })}
                  className="flex h-10 w-full rounded-md border border-input bg-background px-2 text-sm"
                >
                  {PRIORITIES.map((p) => (
                    <option key={p}>{p}</option>
                  ))}
                </select>
              </Labeled>
            </div>
            <div className="flex justify-end">
              <Button type="submit" size="sm" disabled={busy !== null}>
                {busy === "save" ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : "Save"}
              </Button>
            </div>
          </form>
        </Card>
      )}
    </div>
  );
}

function Labeled({ label, children }: { label: string; children: React.ReactNode }) {
  return (
    <label className="block">
      <span className="mb-1 block text-[11px] font-semibold uppercase tracking-[0.08em] text-muted-foreground">
        {label}
      </span>
      {children}
    </label>
  );
}

root · /srv/aaf