feat(events,commitments): add event archiving and commitment inline editing

Events: archive button (hover) moves event to archived section;
archived events collapsible at bottom with count; PATCH /api/events/[id]
for status change; getArchivedEvents() query; page fetches both in parallel.

Commitments: inline edit mode per row (pencil icon on hover); PATCH
/api/commitments/[id] for description/dueDate/financialImpact; extracted
CommitmentItem client component, CommitmentsCard stays server component.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Sonnet 4.6 committed 2026-04-24 18:16:43 +02:00
1 parent 9b6da430e2
commit 9e524cc998
7 files changed
+286 -52

No files matched your search

+26
View File
@@ -5,6 +5,32 @@ import { eq, and } from "drizzle-orm";
import { DEFAULT_USER_ID } from "@/lib/constants";
import { isValidUuid } from "@/lib/utils";
export async function PATCH(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const { id } = await params;
if (!isValidUuid(id)) return NextResponse.json({ error: "Invalid ID" }, { status: 400 });
const body = await req.json() as { description?: string; dueDate?: string | null; financialImpact?: string | null };
if (body.description !== undefined && !body.description.trim()) {
return NextResponse.json({ error: "Description cannot be empty" }, { status: 400 });
}
const patch: Record<string, unknown> = { updatedAt: new Date() };
if (body.description !== undefined) patch.description = body.description.trim();
if ("dueDate" in body) patch.dueDate = body.dueDate ? new Date(body.dueDate) : null;
if ("financialImpact" in body) patch.financialImpact = body.financialImpact?.trim() || null;
const [updated] = await db
.update(commitments)
.set(patch)
.where(and(eq(commitments.id, id), eq(commitments.userId, DEFAULT_USER_ID)))
.returning();
return NextResponse.json({ ok: true, commitment: updated });
}
export async function DELETE(
_req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
+23
View File
@@ -3,8 +3,31 @@ import { db } from "@/db";
import { events } from "@/db/schema";
import { eq, and } from "drizzle-orm";
import { DEFAULT_USER_ID } from "@/lib/constants";
import { EVENT_STATUS } from "@/lib/constants/statuses";
import { isValidUuid } from "@/lib/utils";
export async function PATCH(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const { id } = await params;
if (!isValidUuid(id)) return NextResponse.json({ error: "Invalid ID" }, { status: 400 });
const body = await req.json() as { status?: string };
const validStatuses = Object.values(EVENT_STATUS) as string[];
if (!body.status || !validStatuses.includes(body.status)) {
return NextResponse.json({ error: "Invalid status" }, { status: 400 });
}
const [updated] = await db
.update(events)
.set({ status: body.status, updatedAt: new Date() })
.where(and(eq(events.id, id), eq(events.userId, DEFAULT_USER_ID)))
.returning();
return NextResponse.json({ ok: true, event: updated });
}
export async function DELETE(
_req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
+3 -3
View File
@@ -1,15 +1,15 @@
import { PageLayout } from "@/components/ui/page-layout";
import { getEvents } from "@/db/queries/events";
import { getEvents, getArchivedEvents } from "@/db/queries/events";
import { EventsGrid } from "@/components/events/EventsGrid";
export default async function EventsPage() {
const items = await getEvents();
const [items, archived] = await Promise.all([getEvents(), getArchivedEvents()]);
return (
<PageLayout
title="Events"
subtitle="Opportunities, deadlines, and what's coming up"
>
<EventsGrid initialEvents={items} />
<EventsGrid initialEvents={items} initialArchived={archived} />
</PageLayout>
);
}
+71 -17
View File
@@ -1,7 +1,7 @@
"use client";
import { useState } from "react";
import { Search, ExternalLink, Plus, X, Loader2, Calendar } from "lucide-react";
import { Search, ExternalLink, Plus, X, Loader2, Calendar, Archive, ChevronDown, ChevronRight } from "lucide-react";
import { isPast, format, formatDistanceToNow } from "date-fns";
import { Card } from "@/components/ui/card";
import { DeleteButton } from "@/components/ui/delete-button";
@@ -12,15 +12,30 @@ import type { EventRow } from "@/db/queries/events";
function EventCard({
event,
onDelete,
onArchive,
dimmed = false,
}: {
event: EventRow;
onDelete: (id: string) => void;
onArchive?: (id: string) => void;
dimmed?: boolean;
}) {
const [archiving, setArchiving] = useState(false);
const deadline = event.deadline ? new Date(event.deadline) : null;
const overdue = deadline ? isPast(deadline) : false;
const handleArchive = async () => {
setArchiving(true);
await fetch(`/api/events/${event.id}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ status: "archived" }),
});
onArchive?.(event.id);
};
return (
<div className="group flex items-start gap-3 py-3 border-b border-white/[0.05] last:border-0">
<div className={`group flex items-start gap-3 py-3 border-b border-white/[0.05] last:border-0 ${dimmed ? "opacity-50" : ""}`}>
{/* Type + category badges */}
<div className="flex-1 min-w-0 space-y-1">
<div className="flex flex-wrap items-center gap-1.5">
@@ -60,8 +75,18 @@ function EventCard({
)}
</div>
{/* Delete */}
<div className="shrink-0 flex items-center gap-1 sm:opacity-0 sm:group-hover:opacity-100 transition-opacity">
{/* Actions */}
<div className="shrink-0 flex items-center gap-0.5 sm:opacity-0 sm:group-hover:opacity-100 transition-opacity">
{onArchive && (
<button
onClick={handleArchive}
disabled={archiving}
title="Archive event"
className="p-1 rounded text-white/20 hover:text-amber-400 hover:bg-white/[0.06] transition-colors disabled:opacity-40"
>
{archiving ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <Archive className="h-3.5 w-3.5" />}
</button>
)}
<DeleteButton
onDelete={async () => {
await fetch(`/api/events/${event.id}`, { method: "DELETE" });
@@ -199,12 +224,20 @@ function AddEventForm({ onCreated }: { onCreated: (event: EventRow) => void }) {
// ─── EventsGrid ───────────────────────────────────────────────────────────────
export function EventsGrid({ initialEvents }: { initialEvents: EventRow[] }) {
export function EventsGrid({
initialEvents,
initialArchived = [],
}: {
initialEvents: EventRow[];
initialArchived?: EventRow[];
}) {
const [items, setItems] = useState(initialEvents);
const [archived, setArchived] = useState(initialArchived);
const [showArchived, setShowArchived] = useState(false);
const [query, setQuery] = useState("");
const [typeFilter, setTypeFilter] = useState<string | null>(null);
// Unique types derived from data — SSOT, no hardcoding
// Unique types derived from active events — SSOT, no hardcoding
const types = [...new Set(items.map((e) => e.type).filter(Boolean))].sort() as string[];
const q = query.trim().toLowerCase();
@@ -221,13 +254,16 @@ export function EventsGrid({ initialEvents }: { initialEvents: EventRow[] }) {
const withoutDeadline = filtered.filter((e) => !e.deadline);
const handleDelete = (id: string) => setItems((prev) => prev.filter((e) => e.id !== id));
const handleDeleteArchived = (id: string) => setArchived((prev) => prev.filter((e) => e.id !== id));
const handleArchive = (id: string) => {
const event = items.find((e) => e.id === id);
setItems((prev) => prev.filter((e) => e.id !== id));
if (event) setArchived((prev) => [{ ...event, status: "archived" }, ...prev]);
};
const handleCreated = (event: EventRow) => {
// Insert with deadline at front of deadline group, else at end
setItems((prev) => {
if (event.deadline) return [event, ...prev];
return [...prev, event];
});
setItems((prev) => event.deadline ? [event, ...prev] : [...prev, event]);
};
return (
@@ -276,7 +312,7 @@ export function EventsGrid({ initialEvents }: { initialEvents: EventRow[] }) {
</div>
)}
{/* Events list */}
{/* Active events */}
<Card>
{filtered.length === 0 ? (
<div className="flex flex-col items-center gap-2 py-8 text-white/25">
@@ -287,23 +323,20 @@ export function EventsGrid({ initialEvents }: { initialEvents: EventRow[] }) {
</div>
) : (
<div>
{/* With deadline — ordered by deadline */}
{withDeadline.length > 0 && (
<div>
{withDeadline.map((event) => (
<EventCard key={event.id} event={event} onDelete={handleDelete} />
<EventCard key={event.id} event={event} onDelete={handleDelete} onArchive={handleArchive} />
))}
</div>
)}
{/* Without deadline */}
{withoutDeadline.length > 0 && (
<div className={withDeadline.length > 0 ? "mt-2 pt-2 border-t border-white/[0.05]" : ""}>
{withDeadline.length > 0 && (
<div className="text-[10px] uppercase tracking-wider text-white/20 mb-2">No deadline</div>
)}
{withoutDeadline.map((event) => (
<EventCard key={event.id} event={event} onDelete={handleDelete} />
<EventCard key={event.id} event={event} onDelete={handleDelete} onArchive={handleArchive} />
))}
</div>
)}
@@ -312,6 +345,27 @@ export function EventsGrid({ initialEvents }: { initialEvents: EventRow[] }) {
<AddEventForm onCreated={handleCreated} />
</Card>
{/* Archived events — collapsible */}
{archived.length > 0 && (
<div>
<button
onClick={() => setShowArchived((v) => !v)}
className="flex items-center gap-1.5 text-xs text-white/25 hover:text-white/50 transition-colors"
>
{showArchived ? <ChevronDown className="h-3.5 w-3.5" /> : <ChevronRight className="h-3.5 w-3.5" />}
<Archive className="h-3 w-3" />
{archived.length} archived
</button>
{showArchived && (
<Card className="mt-2">
{archived.map((event) => (
<EventCard key={event.id} event={event} onDelete={handleDeleteArchived} dimmed />
))}
</Card>
)}
</div>
)}
</>
);
}
+140
View File
@@ -0,0 +1,140 @@
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { AlertCircle, Pencil, X, Check, Loader2 } from "lucide-react";
import { formatDistanceToNow, format } from "date-fns";
import { FulfillCommitmentButton } from "./FulfillCommitmentButton";
import { DeleteCommitmentButton } from "./DeleteCommitmentButton";
type CommitmentItemProps = {
id: string;
description: string;
dueDate: Date | null;
financialImpact: string | null;
};
export function CommitmentItem({ id, description, dueDate, financialImpact }: CommitmentItemProps) {
const router = useRouter();
const [editing, setEditing] = useState(false);
const [desc, setDesc] = useState(description);
const [date, setDate] = useState(dueDate ? format(new Date(dueDate), "yyyy-MM-dd") : "");
const [impact, setImpact] = useState(financialImpact ?? "");
const [saving, setSaving] = useState(false);
const [error, setError] = useState("");
const isOverdue = dueDate && new Date(dueDate) < new Date();
const save = async () => {
if (!desc.trim()) { setError("Description required"); return; }
setSaving(true);
setError("");
try {
const res = await fetch(`/api/commitments/${id}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
description: desc,
dueDate: date || null,
financialImpact: impact || null,
}),
});
if (!res.ok) {
const data = await res.json() as { error?: string };
setError(data.error ?? "Failed to save");
return;
}
setEditing(false);
router.refresh();
} finally {
setSaving(false);
}
};
const cancel = () => {
setDesc(description);
setDate(dueDate ? format(new Date(dueDate), "yyyy-MM-dd") : "");
setImpact(financialImpact ?? "");
setError("");
setEditing(false);
};
if (editing) {
return (
<div className="flex gap-3 items-start">
<div className="h-4 w-4 shrink-0 mt-0.5" />
<div className="flex-1 space-y-1.5 min-w-0">
<input
value={desc}
onChange={(e) => setDesc(e.target.value)}
autoFocus
onKeyDown={(e) => { if (e.key === "Escape") cancel(); }}
className="w-full bg-white/[0.04] border border-white/15 rounded px-2 py-1 text-sm text-white/85 placeholder:text-white/20 focus:outline-none focus:border-white/30"
/>
<div className="flex gap-1.5">
<input
type="date"
value={date}
onChange={(e) => setDate(e.target.value)}
className="bg-white/[0.04] border border-white/10 rounded px-2 py-1 text-xs text-white/70 focus:outline-none focus:border-white/25"
/>
<input
value={impact}
onChange={(e) => setImpact(e.target.value)}
placeholder="Financial impact"
onKeyDown={(e) => { if (e.key === "Enter") save(); if (e.key === "Escape") cancel(); }}
className="flex-1 bg-white/[0.04] border border-white/10 rounded px-2 py-1 text-xs text-white/70 placeholder:text-white/20 focus:outline-none focus:border-white/25"
/>
</div>
{error && <p className="text-xs text-red-400">{error}</p>}
<div className="flex gap-1.5">
<button
onClick={save}
disabled={saving || !desc.trim()}
className="flex items-center gap-1 px-2.5 py-1 rounded bg-emerald-600/70 hover:bg-emerald-600 disabled:opacity-30 text-white text-xs font-medium transition-colors"
>
{saving ? <Loader2 className="h-3 w-3 animate-spin" /> : <Check className="h-3 w-3" />}
Save
</button>
<button onClick={cancel} className="flex items-center gap-1 text-xs text-white/30 hover:text-white/60">
<X className="h-3 w-3" /> Cancel
</button>
</div>
</div>
</div>
);
}
return (
<div className="group flex gap-3 items-start">
{isOverdue ? (
<AlertCircle className="h-4 w-4 text-red-400 shrink-0 mt-0.5" />
) : (
<div className="h-4 w-4 rounded-full border border-white/20 shrink-0 mt-0.5" />
)}
<div className="min-w-0 flex-1">
<div className="text-sm md:text-base truncate">{description}</div>
{dueDate && (
<div className={`text-xs md:text-sm ${isOverdue ? "text-red-400" : "text-white/40"}`}>
{isOverdue ? "Overdue" : "Due"}{" "}
{formatDistanceToNow(new Date(dueDate), { addSuffix: true })}
</div>
)}
{financialImpact && (
<div className="text-xs md:text-sm text-amber-400/70">{financialImpact}</div>
)}
</div>
<div className="flex gap-0.5 shrink-0 sm:opacity-0 sm:group-hover:opacity-100 transition-opacity">
<button
onClick={() => setEditing(true)}
title="Edit commitment"
className="p-1 rounded text-white/20 hover:text-white/60 hover:bg-white/[0.06] transition-colors"
>
<Pencil className="h-3.5 w-3.5" />
</button>
<FulfillCommitmentButton commitmentId={id} />
<DeleteCommitmentButton commitmentId={id} />
</div>
</div>
);
}
+11 -32
View File
@@ -1,9 +1,7 @@
import { CheckCircle, AlertCircle } from "lucide-react";
import { CheckCircle } from "lucide-react";
import { Card, CardHeader } from "@/components/ui/card";
import { getActiveCommitments } from "@/db/queries/today";
import { formatDistanceToNow } from "date-fns";
import { FulfillCommitmentButton } from "./FulfillCommitmentButton";
import { DeleteCommitmentButton } from "./DeleteCommitmentButton";
import { CommitmentItem } from "./CommitmentItem";
import { AddCommitmentButton } from "./AddCommitmentButton";
export async function CommitmentsCard() {
@@ -20,34 +18,15 @@ export async function CommitmentsCard() {
<div className="text-sm md:text-base text-white/30">No active commitments</div>
) : (
<div className="space-y-3">
{items.map((item) => {
const isOverdue = item.dueDate && new Date(item.dueDate) < new Date();
return (
<div key={item.id} className="flex gap-3 items-start">
{isOverdue ? (
<AlertCircle className="h-4 w-4 text-red-400 shrink-0 mt-0.5" />
) : (
<div className="h-4 w-4 rounded-full border border-white/20 shrink-0 mt-0.5" />
)}
<div className="min-w-0 flex-1">
<div className="text-sm md:text-base truncate">{item.description}</div>
{item.dueDate && (
<div className={`text-xs md:text-sm ${isOverdue ? "text-red-400" : "text-white/40"}`}>
{isOverdue ? "Overdue" : "Due"}{" "}
{formatDistanceToNow(new Date(item.dueDate), { addSuffix: true })}
</div>
)}
{item.financialImpact && (
<div className="text-xs md:text-sm text-amber-400/70">{item.financialImpact}</div>
)}
</div>
<div className="flex gap-0.5 shrink-0">
<FulfillCommitmentButton commitmentId={item.id} />
<DeleteCommitmentButton commitmentId={item.id} />
</div>
</div>
);
})}
{items.map((item) => (
<CommitmentItem
key={item.id}
id={item.id}
description={item.description}
dueDate={item.dueDate}
financialImpact={item.financialImpact}
/>
))}
</div>
)}
<AddCommitmentButton />
+12
View File
@@ -36,3 +36,15 @@ export async function getEvents(): Promise<EventRow[]> {
asc(events.name),
);
}
export async function getArchivedEvents(): Promise<EventRow[]> {
return db
.select()
.from(events)
.where(and(eq(events.userId, DEFAULT_USER_ID), eq(events.status, EVENT_STATUS.ARCHIVED)))
.orderBy(
sql`CASE WHEN ${events.deadline} IS NOT NULL THEN 0 ELSE 1 END`,
asc(events.deadline),
asc(events.name),
);
}