1
0
Fork 0
polaris-task-force/src/components/frontend/journals/JournalFormDialog.tsx
Z8MB1E 8a7d79c369 feat(journals): member-authored journal entries with author-owned publishing
- New Journals collection (Chronicle group): title, Lexical body, status
  draft/published, optional in-world date and mission link; authors read,
  edit, and delete their own entries at any status while every logged-in
  user reads published ones; journals:* staff permissions bypass ownership
  and gate the admin page (registered in the permission registry)
- /journals list with compose dialog and /journals/[id] reader reusing the
  Chronicle typography; edit/delete controls for authors and staff; drafts
  carry the DraftBadge and stay invisible to everyone but their author
- Server actions create/update/delete with ownership checks, publish-time
  journal:created and journal:deleted feed events (journals added to the
  GameEventLogs target collection enum via migration)
- Sidebar entry under Chronicle, command palette destination, admin panel
  icon; excerpt extractor as a pure module (src/lib/journals/excerpt.ts)
- Access-control integration spec covers draft privacy, publish visibility,
  non-author write blocking, and staff override (5 tests)
2026-10-12 14:39:48 -04:00

203 lines
6.2 KiB
TypeScript

"use client";
import * as React from "react";
import { useRouter } from "next/navigation";
import { toast } from "sonner";
import { Loader2Icon } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import {
LexicalRichTextEditor,
type LexicalJson,
} from "@/components/frontend/intelligence/LexicalRichTextEditor";
import { createJournal, updateJournal } from "@/app/(frontend)/journals/actions";
export interface MissionOption {
id: number;
label: string;
}
export interface JournalDraft {
id?: number;
title: string;
status: "draft" | "published";
/** yyyy-MM-dd or "" */
occurredAt: string;
missionId: number | null;
body: LexicalJson | null;
}
export const EMPTY_JOURNAL_DRAFT: JournalDraft = {
title: "",
status: "draft",
occurredAt: "",
missionId: null,
body: null,
};
interface JournalFormDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
missions: MissionOption[];
draft: JournalDraft;
}
/** Compose/edit dialog shared by the /journals list and the entry reader. */
export function JournalFormDialog({
open,
onOpenChange,
missions,
draft,
}: JournalFormDialogProps) {
const router = useRouter();
const isEdit = typeof draft.id === "number";
const [title, setTitle] = React.useState(draft.title);
const [status, setStatus] = React.useState<"draft" | "published">(draft.status);
const [occurredAt, setOccurredAt] = React.useState(draft.occurredAt);
const [missionId, setMissionId] = React.useState<string>(
draft.missionId === null ? "none" : String(draft.missionId),
);
const [body, setBody] = React.useState<LexicalJson | null>(draft.body);
const [saving, setSaving] = React.useState(false);
// Re-seed the form whenever a different entry (or a fresh compose) opens.
React.useEffect(() => {
if (!open) return;
setTitle(draft.title);
setStatus(draft.status);
setOccurredAt(draft.occurredAt);
setMissionId(draft.missionId === null ? "none" : String(draft.missionId));
setBody(draft.body);
}, [open, draft]);
const handleSubmit = async () => {
setSaving(true);
const input = {
title,
status,
occurredAt: occurredAt ? new Date(occurredAt).toISOString() : null,
mission: missionId === "none" ? null : Number(missionId),
body,
};
const result = isEdit
? await updateJournal(draft.id as number, input)
: await createJournal(input);
setSaving(false);
if (!result.success) {
toast.error(result.error ?? "Failed to save entry");
return;
}
toast.success(isEdit ? "Entry updated" : "Entry saved");
onOpenChange(false);
router.refresh();
if (!isEdit && result.data) {
router.push(`/journals/${result.data}`);
}
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-h-[90vh] overflow-y-auto sm:max-w-2xl">
<DialogHeader>
<DialogTitle>{isEdit ? "Edit journal entry" : "Write a journal entry"}</DialogTitle>
<DialogDescription>
Drafts are private to you. Publishing makes the entry readable by every operator on
Journals.
</DialogDescription>
</DialogHeader>
<div className="flex flex-col gap-4">
<div className="flex flex-col gap-1.5">
<Label htmlFor="journal-title">Title</Label>
<Input
id="journal-title"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="After-action reflections, deployment diaries..."
maxLength={200}
/>
</div>
<div className="grid gap-4 sm:grid-cols-3">
<div className="flex flex-col gap-1.5">
<Label>Status</Label>
<Select value={status} onValueChange={(v) => setStatus(v as "draft" | "published")}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="draft">Draft (private)</SelectItem>
<SelectItem value="published">Published</SelectItem>
</SelectContent>
</Select>
</div>
<div className="flex flex-col gap-1.5">
<Label htmlFor="journal-date">In-world date</Label>
<Input
id="journal-date"
type="date"
value={occurredAt}
onChange={(e) => setOccurredAt(e.target.value)}
/>
</div>
<div className="flex flex-col gap-1.5">
<Label>Related mission</Label>
<Select value={missionId} onValueChange={setMissionId}>
<SelectTrigger>
<SelectValue placeholder="None" />
</SelectTrigger>
<SelectContent>
<SelectItem value="none">None</SelectItem>
{missions.map((m) => (
<SelectItem key={m.id} value={String(m.id)}>
{m.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
<div className="flex flex-col gap-1.5">
<Label>Entry</Label>
<LexicalRichTextEditor
key={`${draft.id ?? "new"}-${open ? "open" : "closed"}`}
value={body}
onChange={setBody}
placeholder="Write your entry here..."
/>
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => onOpenChange(false)} disabled={saving}>
Cancel
</Button>
<Button onClick={handleSubmit} disabled={saving || !title.trim()}>
{saving && <Loader2Icon className="mr-2 size-4 animate-spin" />}
{isEdit ? "Save changes" : "Save entry"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}