Wire Import and Audit Log pages, add Create User action

- Import page: schedule (XLSX/CSV) and entity import with dry-run toggle
- Audit Log page: paginated, filterable by object type
- Nav items now clickable (removed stubs)
- StartOS Action: Create User with name/email/password/role inputs
- Default first-boot creds: jonathan@ten31.xyz
This commit is contained in:
Johnny 5
2026-06-07 22:26:14 +00:00
parent fce02fe696
commit ba9f026545
5 changed files with 566 additions and 20 deletions
+283
View File
@@ -0,0 +1,283 @@
import { useEffect, useRef, useState } from "react";
import { api, type Entity } from "../api";
import { useAuth } from "../context/AuthContext";
export default function Import() {
const { user } = useAuth();
const [entities, setEntities] = useState<Entity[]>([]);
const [entityId, setEntityId] = useState("");
const [asOf, setAsOf] = useState("");
const [mode, setMode] = useState<"entities" | "schedule">("schedule");
const [commit, setCommit] = useState(false);
const [file, setFile] = useState<File | null>(null);
const [result, setResult] = useState<any>(null);
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const fileRef = useRef<HTMLInputElement>(null);
const canImport = user && ["approver", "cfo"].includes(user.role);
useEffect(() => {
api.listEntities().then(setEntities);
}, []);
async function handleImport() {
if (!file) return;
setError("");
setResult(null);
setLoading(true);
try {
const form = new FormData();
form.append("file", file);
const params = new URLSearchParams();
params.set("commit", String(commit));
let url: string;
if (mode === "entities") {
url = `/api/import/entities?${params}`;
} else {
if (!entityId || !asOf) {
setError("Select an entity and quarter-end date.");
setLoading(false);
return;
}
params.set("entity_id", entityId);
params.set("as_of", asOf);
url = `/api/import/schedule?${params}`;
}
const res = await fetch(url, { method: "POST", body: form });
const data = await res.json();
if (!res.ok) {
setError(data.detail || "Import failed");
} else {
setResult(data);
}
} catch (err: any) {
setError(err.message);
} finally {
setLoading(false);
}
}
if (!canImport) {
return (
<div className="text-gray-500 text-sm">
Import requires approver or CFO role.
</div>
);
}
return (
<div className="max-w-2xl">
<h1 className="text-2xl font-semibold text-gray-900 mb-6">Import</h1>
{/* Mode selector */}
<div className="flex gap-4 mb-6">
<button
onClick={() => setMode("schedule")}
className={`px-4 py-2 text-sm rounded ${
mode === "schedule"
? "bg-gray-900 text-white"
: "bg-gray-100 text-gray-700 hover:bg-gray-200"
}`}
>
Schedule of Investments
</button>
<button
onClick={() => setMode("entities")}
className={`px-4 py-2 text-sm rounded ${
mode === "entities"
? "bg-gray-900 text-white"
: "bg-gray-100 text-gray-700 hover:bg-gray-200"
}`}
>
Entities
</button>
</div>
<div className="bg-white border border-gray-200 rounded-lg p-6 space-y-4">
{mode === "schedule" && (
<>
<div>
<label className="block text-sm text-gray-700 mb-1">Entity</label>
<select
value={entityId}
onChange={(e) => setEntityId(e.target.value)}
className="w-full px-3 py-2 border border-gray-300 rounded text-sm"
>
<option value="">Select entity...</option>
{entities.map((e) => (
<option key={e.id} value={e.id}>
{e.name}
</option>
))}
</select>
</div>
<div>
<label className="block text-sm text-gray-700 mb-1">
Quarter-end date (seed round)
</label>
<input
type="date"
value={asOf}
onChange={(e) => setAsOf(e.target.value)}
className="w-full px-3 py-2 border border-gray-300 rounded text-sm"
/>
</div>
</>
)}
<div>
<label className="block text-sm text-gray-700 mb-1">
{mode === "schedule" ? "Carta XLSX or CSV file" : "Entities CSV file"}
</label>
<input
ref={fileRef}
type="file"
accept={mode === "schedule" ? ".xlsx,.xls,.csv" : ".csv"}
onChange={(e) => setFile(e.target.files?.[0] || null)}
className="w-full text-sm text-gray-600"
/>
</div>
<div className="flex items-center gap-2">
<input
type="checkbox"
id="commit"
checked={commit}
onChange={(e) => setCommit(e.target.checked)}
className="rounded border-gray-300"
/>
<label htmlFor="commit" className="text-sm text-gray-700">
Commit (uncheck for dry-run preview)
</label>
</div>
{error && (
<div className="p-3 bg-red-50 border border-red-200 rounded text-sm text-red-700">
{error}
</div>
)}
<button
onClick={handleImport}
disabled={loading || !file}
className="px-4 py-2 bg-gray-900 text-white text-sm rounded hover:bg-gray-800 disabled:opacity-50"
>
{loading ? "Importing..." : commit ? "Import" : "Preview (dry run)"}
</button>
</div>
{/* Results */}
{result && (
<div className="mt-6 bg-white border border-gray-200 rounded-lg p-6">
<h3 className="text-sm font-medium text-gray-900 mb-3">
{result.committed ? "Import Complete" : "Dry Run Preview"}
</h3>
{result.source_entity_name && (
<p className="text-sm text-gray-600 mb-2">
Source: {result.source_entity_name}
</p>
)}
{result.summary && (
<div className="text-sm text-gray-700 mb-3">
Created: {result.summary.created} | Updated: {result.summary.updated} | Errors: {result.summary.error_rows}
</div>
)}
{result.holdings_count != null && (
<div className="text-sm text-gray-700 mb-3">
Holdings: {result.holdings_count} | Positions: {result.positions_count}
{result.seed_round_id && ` | Seed round #${result.seed_round_id}`}
</div>
)}
{result.holdings && result.holdings.length > 0 && (
<div className="mb-3">
<h4 className="text-xs font-medium text-gray-500 uppercase mb-1">Holdings</h4>
<ul className="text-sm text-gray-700 space-y-0.5">
{result.holdings.map((h: any, i: number) => (
<li key={i}>{h.company_name}</li>
))}
</ul>
</div>
)}
{result.positions && result.positions.length > 0 && (
<div className="mb-3">
<h4 className="text-xs font-medium text-gray-500 uppercase mb-1">
Positions ({result.positions.length})
</h4>
<div className="overflow-x-auto">
<table className="text-xs w-full">
<thead>
<tr className="text-left text-gray-500">
<th className="pr-3 py-1">Company</th>
<th className="pr-3 py-1">Security</th>
<th className="pr-3 py-1">Date</th>
<th className="pr-3 py-1 text-right">Cost</th>
<th className="pr-3 py-1 text-right">Value</th>
</tr>
</thead>
<tbody>
{result.positions.map((p: any, i: number) => (
<tr key={i} className="text-gray-700">
<td className="pr-3 py-0.5">{p.company_name}</td>
<td className="pr-3 py-0.5">{p.security_name}</td>
<td className="pr-3 py-0.5">{p.investment_date || "—"}</td>
<td className="pr-3 py-0.5 text-right">
{p.cost_cents != null ? `$${(p.cost_cents / 100).toLocaleString()}` : "—"}
</td>
<td className="pr-3 py-0.5 text-right">
{p.value_cents != null ? `$${(p.value_cents / 100).toLocaleString()}` : "—"}
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
)}
{result.errors && result.errors.length > 0 && (
<div>
<h4 className="text-xs font-medium text-red-500 uppercase mb-1">
Errors ({result.errors.length})
</h4>
<ul className="text-xs text-red-600 space-y-1">
{result.errors.map((e: any, i: number) => (
<li key={i}>
Row {e.row}: {e.errors?.join(", ")}
</li>
))}
</ul>
</div>
)}
{result.preview && result.preview.length > 0 && (
<div>
<h4 className="text-xs font-medium text-gray-500 uppercase mb-1">
Entities ({result.preview.length})
</h4>
<ul className="text-sm text-gray-700 space-y-0.5">
{result.preview.map((p: any, i: number) => (
<li key={i}>
<span className={`text-xs px-1 rounded ${p.action === "create" ? "bg-green-100 text-green-700" : "bg-yellow-100 text-yellow-700"}`}>
{p.action}
</span>{" "}
{p.name}
</li>
))}
</ul>
</div>
)}
</div>
)}
</div>
);
}