Export is PDF, RULE.md, and Print on US Letter, with every shared term, no member names, and a takeaway that points at Ask an organizer. Finished Rules keep type and place so the team can read folded counts without a database, Umami can sit beside the app with no cookies, and the person who holds the pen gets working PDF and RULE.md links 48 hours later.
273 lines
9.9 KiB
TypeScript
273 lines
9.9 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useEffect } from "react";
|
|
import { useRouter, useSearchParams } from "next/navigation";
|
|
import CommunityRule from "../../../../components/type/CommunityRule";
|
|
import type { CommunityRuleSection } from "../../../../components/type/CommunityRule/CommunityRule.types";
|
|
import Link from "next/link";
|
|
import Alert from "../../../../components/modals/Alert";
|
|
import { useMessages } from "../../../../contexts/MessagesContext";
|
|
import { fetchPublishedRuleDetail } from "../../../../../lib/create/api";
|
|
import { parsePublishedDocumentForCommunityRuleDisplay } from "../../../../../lib/create/publishedDocumentToDisplaySections";
|
|
import {
|
|
finishedAtIso,
|
|
readLastPublishedRule,
|
|
writeLastPublishedRule,
|
|
} from "../../../../../lib/create/lastPublishedRule";
|
|
import { ASK_ORGANIZER_HASH } from "../../../../../lib/sharePaths";
|
|
import { useCreateFlowMdUp } from "../../hooks/useCreateFlowMdUp";
|
|
import { CreateFlowHeaderLockup } from "../../components/CreateFlowHeaderLockup";
|
|
import {
|
|
CREATE_FLOW_MD_UP_GRID_CELL_CLASS,
|
|
CREATE_FLOW_PAGE_GUTTER_CLASS,
|
|
CREATE_FLOW_SCROLL_REGION_CLASS,
|
|
CREATE_FLOW_TWO_COLUMN_MAX_WIDTH_CLASS,
|
|
} from "../../components/createFlowLayoutTokens";
|
|
import {
|
|
CREATE_FLOW_COMPLETED_CELEBRATE_QUERY,
|
|
CREATE_FLOW_COMPLETED_CELEBRATE_VALUE,
|
|
} from "../../utils/flowSteps";
|
|
import {
|
|
CREATE_FLOW_COPIED_QUERY,
|
|
CREATE_FLOW_COPIED_VALUE,
|
|
} from "../../../../../lib/create/invitePaths";
|
|
import { communityPossessive } from "../../../../../lib/create/ruleInviteCardMeta";
|
|
|
|
function emptyCompletedDocumentState(): {
|
|
headerTitle: string;
|
|
headerDescription: string | undefined;
|
|
documentSections: CommunityRuleSection[];
|
|
} {
|
|
return {
|
|
headerTitle: "",
|
|
headerDescription: undefined,
|
|
documentSections: [],
|
|
};
|
|
}
|
|
|
|
function initialCompletedUi(
|
|
ruleIdFromUrl: string | null,
|
|
): {
|
|
headerTitle: string;
|
|
headerDescription: string | undefined;
|
|
documentSections: CommunityRuleSection[];
|
|
} {
|
|
if (ruleIdFromUrl) {
|
|
return emptyCompletedDocumentState();
|
|
}
|
|
if (typeof sessionStorage === "undefined") {
|
|
return emptyCompletedDocumentState();
|
|
}
|
|
const stored = readLastPublishedRule();
|
|
if (!stored) {
|
|
return emptyCompletedDocumentState();
|
|
}
|
|
const parsed = parsePublishedDocumentForCommunityRuleDisplay(stored.document);
|
|
if (parsed.length === 0) {
|
|
return {
|
|
headerTitle: "",
|
|
headerDescription: undefined,
|
|
documentSections: [],
|
|
};
|
|
}
|
|
const sum =
|
|
typeof stored.summary === "string" ? stored.summary.trim() : "";
|
|
return {
|
|
headerTitle: stored.title,
|
|
headerDescription: sum.length > 0 ? sum : undefined,
|
|
documentSections: parsed,
|
|
};
|
|
}
|
|
|
|
export function CompletedScreen() {
|
|
const router = useRouter();
|
|
const searchParams = useSearchParams();
|
|
const ruleIdParam = searchParams.get("ruleId");
|
|
const celebrateInUrl =
|
|
searchParams.get(CREATE_FLOW_COMPLETED_CELEBRATE_QUERY) ===
|
|
CREATE_FLOW_COMPLETED_CELEBRATE_VALUE;
|
|
const copiedInUrl =
|
|
searchParams.get(CREATE_FLOW_COPIED_QUERY) === CREATE_FLOW_COPIED_VALUE;
|
|
const storedForCopy = ruleIdParam ? null : readLastPublishedRule();
|
|
const copiedFromTitle = storedForCopy?.copiedFromTitle;
|
|
const copiedNameHit = storedForCopy?.copiedNameHit ?? null;
|
|
const mdUp = useCreateFlowMdUp();
|
|
const m = useMessages();
|
|
const completed = m.create.reviewAndComplete.completed;
|
|
|
|
const initial = initialCompletedUi(ruleIdParam);
|
|
const [toastDismissed, setToastDismissed] = useState(false);
|
|
/** Latch: toast copy should survive `router.replace` after we strip `?celebrate=1`. */
|
|
const [showCelebrateToast] = useState(celebrateInUrl);
|
|
const [showCopiedReady] = useState(copiedInUrl);
|
|
const [headerTitle, setHeaderTitle] = useState(initial.headerTitle);
|
|
const [headerDescription, setHeaderDescription] = useState<
|
|
string | undefined
|
|
>(initial.headerDescription);
|
|
const [documentSections, setDocumentSections] =
|
|
useState<CommunityRuleSection[]>(initial.documentSections);
|
|
|
|
useEffect(() => {
|
|
if (!ruleIdParam) return;
|
|
let cancelled = false;
|
|
void (async () => {
|
|
const detail = await fetchPublishedRuleDetail(ruleIdParam);
|
|
if (cancelled) return;
|
|
if (
|
|
!detail ||
|
|
!detail.viewerIsOwner ||
|
|
detail.rule.document === null ||
|
|
typeof detail.rule.document !== "object" ||
|
|
Array.isArray(detail.rule.document)
|
|
) {
|
|
router.replace(`/rules/${encodeURIComponent(ruleIdParam)}`);
|
|
return;
|
|
}
|
|
const doc = detail.rule.document as Record<string, unknown>;
|
|
const finishedAt = finishedAtIso(detail.rule.createdAt);
|
|
writeLastPublishedRule({
|
|
id: detail.rule.id,
|
|
title: detail.rule.title,
|
|
summary: detail.rule.summary,
|
|
document: doc,
|
|
...(finishedAt ? { finishedAt } : {}),
|
|
});
|
|
const parsed = parsePublishedDocumentForCommunityRuleDisplay(doc);
|
|
if (parsed.length === 0) {
|
|
router.replace(`/rules/${encodeURIComponent(ruleIdParam)}`);
|
|
return;
|
|
}
|
|
queueMicrotask(() => {
|
|
setDocumentSections(parsed);
|
|
setHeaderTitle(detail.rule.title);
|
|
const sum =
|
|
typeof detail.rule.summary === "string"
|
|
? detail.rule.summary.trim()
|
|
: "";
|
|
setHeaderDescription(sum.length > 0 ? sum : undefined);
|
|
});
|
|
router.replace("/create/completed");
|
|
})();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [ruleIdParam, router]);
|
|
|
|
useEffect(() => {
|
|
if (!celebrateInUrl && !copiedInUrl) return;
|
|
router.replace("/create/completed");
|
|
}, [celebrateInUrl, copiedInUrl, router]);
|
|
|
|
const surfaceCopied = showCopiedReady || Boolean(copiedFromTitle);
|
|
const surfaceClass = surfaceCopied
|
|
? "bg-[var(--color-lavender-lavender150,#d1bdf3)]"
|
|
: "bg-[var(--color-teal-teal50,#c9fef9)]";
|
|
|
|
const toast =
|
|
!toastDismissed && (showCopiedReady || showCelebrateToast) ? (
|
|
<div
|
|
className="fixed bottom-0 left-0 right-0 z-10 w-full"
|
|
role="status"
|
|
aria-live="polite"
|
|
>
|
|
<Alert
|
|
type="toast"
|
|
status="default"
|
|
title={
|
|
showCopiedReady
|
|
? completed.copyReadyToastTitle
|
|
: completed.toastTitle
|
|
}
|
|
description={
|
|
showCopiedReady
|
|
? completed.copyReadyToastDescription.replace(
|
|
"{possessive}",
|
|
communityPossessive(copiedFromTitle ?? ""),
|
|
)
|
|
: completed.toastDescription
|
|
}
|
|
hasLeadingIcon
|
|
hasBodyText
|
|
onClose={() => setToastDismissed(true)}
|
|
className="w-full"
|
|
/>
|
|
</div>
|
|
) : null;
|
|
|
|
return (
|
|
<>
|
|
<div className={`flex min-h-0 w-full flex-1 flex-col overflow-hidden ${surfaceClass} md:h-full`}>
|
|
<div
|
|
className={`mx-auto grid min-h-0 w-full grid-cols-1 gap-4 ${CREATE_FLOW_PAGE_GUTTER_CLASS} max-md:h-full max-md:grid-rows-[auto_minmax(0,1fr)] max-md:overflow-hidden max-md:pt-[var(--space-800)] max-md:pb-8 md:h-full md:grid-cols-2 md:grid-rows-1 md:items-stretch md:justify-items-center md:gap-[var(--space-1200)] md:overflow-hidden md:py-0 ${CREATE_FLOW_TWO_COLUMN_MAX_WIDTH_CLASS}`}
|
|
>
|
|
<div
|
|
className={`flex shrink-0 flex-col justify-start min-h-0 overflow-hidden md:justify-center md:pb-8 ${CREATE_FLOW_MD_UP_GRID_CELL_CLASS}`}
|
|
>
|
|
<CreateFlowHeaderLockup
|
|
title={headerTitle}
|
|
description={headerDescription}
|
|
justification="left"
|
|
size="L"
|
|
palette="inverse"
|
|
/>
|
|
{headerTitle && !surfaceCopied ? (
|
|
<p className="mt-[var(--space-400)] max-w-[640px] text-small-paragraph text-[var(--color-content-invert-secondary)]">
|
|
{completed.askOrganizerLead ? (
|
|
<>
|
|
{completed.askOrganizerLead}{" "}
|
|
</>
|
|
) : null}
|
|
<Link
|
|
href={`/#${ASK_ORGANIZER_HASH}`}
|
|
className="text-small-paragraph text-[var(--color-content-invert-primary)] underline"
|
|
>
|
|
{completed.askOrganizerLink}
|
|
</Link>
|
|
</p>
|
|
) : null}
|
|
</div>
|
|
<div
|
|
className={`relative flex min-h-0 flex-col overflow-x-hidden overflow-y-auto md:max-h-full md:self-stretch ${CREATE_FLOW_SCROLL_REGION_CLASS} ${CREATE_FLOW_MD_UP_GRID_CELL_CLASS}`}
|
|
>
|
|
<div
|
|
className={`pointer-events-none sticky top-0 z-10 hidden h-5 shrink-0 bg-gradient-to-b from-0% via-50% to-transparent md:block ${
|
|
surfaceCopied
|
|
? "from-[var(--color-lavender-lavender150,#d1bdf3)]/55 via-[var(--color-lavender-lavender150,#d1bdf3)]/20"
|
|
: "from-[var(--color-teal-teal50,#c9fef9)]/55 via-[var(--color-teal-teal50,#c9fef9)]/20"
|
|
}`}
|
|
aria-hidden
|
|
/>
|
|
<div className="w-full min-w-0 py-0 md:pb-8">
|
|
{copiedNameHit && copiedFromTitle ? (
|
|
<div className="mb-[var(--space-400)]">
|
|
<Alert
|
|
type="banner"
|
|
status="warning"
|
|
title={completed.copyNameHitTitle.replace(
|
|
"{name}",
|
|
copiedFromTitle,
|
|
)}
|
|
description={completed.copyNameHitDescription
|
|
.replace("{entry}", copiedNameHit.entryTitle)
|
|
.replace("{category}", copiedNameHit.categoryName)}
|
|
hasLeadingIcon
|
|
hasBodyText
|
|
hasTrailingIcon={false}
|
|
className="w-full"
|
|
/>
|
|
</div>
|
|
) : null}
|
|
<CommunityRule
|
|
sections={documentSections}
|
|
useCardStyle={!mdUp}
|
|
className={mdUp ? "min-w-0" : "w-full min-w-0 p-4"}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{toast}
|
|
</>
|
|
);
|
|
}
|