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.
258 lines
8.3 KiB
TypeScript
258 lines
8.3 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useState } from "react";
|
|
import { buildPublishPayload } from "../../../../lib/create/buildPublishPayload";
|
|
import { stateWithPendingCommunityAvatar } from "../../../../lib/create/flushPendingCommunityAvatar";
|
|
import { publishRule, updatePublishedRule } from "../../../../lib/create/api";
|
|
import { writeLastPublishedRule } from "../../../../lib/create/lastPublishedRule";
|
|
import { writePendingKeepRuleLogin } from "../../../../lib/create/pendingKeepRuleLogin";
|
|
import messages from "../../../../messages/en/index";
|
|
import type { CreateFlowState } from "../types";
|
|
import {
|
|
CREATE_FLOW_COMPLETED_CELEBRATE_QUERY,
|
|
CREATE_FLOW_COMPLETED_CELEBRATE_VALUE,
|
|
} from "../utils/flowSteps";
|
|
import { createFlowStepPath } from "../utils/createFlowPaths";
|
|
|
|
type AppRouterLike = { push: (_href: string) => void };
|
|
|
|
type OpenLogin = (args: {
|
|
variant: "default" | "saveProgress" | "keepRule" | "invite";
|
|
nextPath: string;
|
|
backdropVariant: "blurredYellow";
|
|
}) => void;
|
|
|
|
type SessionUser = { id: string; email: string };
|
|
|
|
export type FinalizeOptions = {
|
|
/** When true, finish without sending the current email list. */
|
|
skipInvites?: boolean;
|
|
};
|
|
|
|
export type UseCreateFlowFinalizeResult = {
|
|
publishBannerTitle: string | null;
|
|
publishBannerMessage: string | null;
|
|
setPublishBannerMessage: (_message: string | null) => void;
|
|
isPublishing: boolean;
|
|
finalize: (_opts?: FinalizeOptions) => Promise<void>;
|
|
};
|
|
|
|
/** Invite / Finish → publish: banner + `isPublishing`, consumed by `CreateFlowLayoutClient`. */
|
|
export function useCreateFlowFinalize({
|
|
state,
|
|
router,
|
|
openLogin,
|
|
updateState,
|
|
loginReturnPath,
|
|
sessionUser,
|
|
onGuestPublished,
|
|
}: {
|
|
state: CreateFlowState;
|
|
router: AppRouterLike;
|
|
openLogin: OpenLogin;
|
|
updateState: (_patch: Partial<CreateFlowState>) => void;
|
|
/** Session gate return path (`?syncDraft=1`) — differs for `/create/edit-rule` vs `/create/final-review`. */
|
|
loginReturnPath: string;
|
|
/**
|
|
* `undefined` while `/api/auth/session` is in flight — publish is a no-op
|
|
* until it resolves. `null` is a guest (anonymous publish).
|
|
*/
|
|
sessionUser: SessionUser | null | undefined;
|
|
/** Guest publish succeeded; `skippedInvites` when stakeholder emails were not sent. */
|
|
onGuestPublished?: (_info: { skippedInvites: boolean }) => void;
|
|
}): UseCreateFlowFinalizeResult {
|
|
const [publishBannerTitle, setPublishBannerTitle] = useState<string | null>(
|
|
null,
|
|
);
|
|
const [publishBannerMessage, setPublishBannerMessage] = useState<
|
|
string | null
|
|
>(null);
|
|
const [isPublishing, setIsPublishing] = useState(false);
|
|
|
|
const showFailedBanner = useCallback(
|
|
(message: string, sendingInvites: boolean) => {
|
|
const inviteCopy = messages.create.reviewAndComplete.confirmStakeholders;
|
|
if (sendingInvites) {
|
|
setPublishBannerTitle(inviteCopy.sendFailedTitle);
|
|
setPublishBannerMessage(inviteCopy.sendFailedDescription);
|
|
return;
|
|
}
|
|
setPublishBannerTitle(
|
|
messages.create.reviewAndComplete.publish.publishBannerTitle,
|
|
);
|
|
setPublishBannerMessage(message);
|
|
},
|
|
[],
|
|
);
|
|
|
|
const finalize = useCallback(
|
|
async (opts?: FinalizeOptions) => {
|
|
if (sessionUser === undefined) return;
|
|
const skipInvites = opts?.skipInvites === true;
|
|
|
|
setPublishBannerTitle(null);
|
|
setPublishBannerMessage(null);
|
|
setIsPublishing(true);
|
|
const prepared = await stateWithPendingCommunityAvatar(state);
|
|
if (prepared.ok === false) {
|
|
setIsPublishing(false);
|
|
showFailedBanner(messages.create.upload.errors.generic, false);
|
|
return;
|
|
}
|
|
if (prepared.state.communityAvatarUrl !== state.communityAvatarUrl) {
|
|
updateState({ communityAvatarUrl: prepared.state.communityAvatarUrl });
|
|
}
|
|
const payloadResult = buildPublishPayload(prepared.state);
|
|
if (payloadResult.ok === false) {
|
|
setIsPublishing(false);
|
|
showFailedBanner(
|
|
payloadResult.error === "missingCommunityName"
|
|
? messages.create.reviewAndComplete.publish.missingCommunityName
|
|
: payloadResult.error,
|
|
false,
|
|
);
|
|
return;
|
|
}
|
|
const { title, summary, document: ruleDocument } = payloadResult;
|
|
|
|
const editingId =
|
|
typeof state.editingPublishedRuleId === "string"
|
|
? state.editingPublishedRuleId.trim()
|
|
: "";
|
|
|
|
if (editingId.length > 0 && sessionUser === null) {
|
|
setIsPublishing(false);
|
|
writeLastPublishedRule({
|
|
id: editingId,
|
|
title,
|
|
summary: summary ?? null,
|
|
document: ruleDocument,
|
|
});
|
|
updateState({ editingPublishedRuleId: undefined });
|
|
writePendingKeepRuleLogin();
|
|
onGuestPublished?.({ skippedInvites: false });
|
|
router.push(createFlowStepPath("completed"));
|
|
return;
|
|
}
|
|
|
|
if (editingId.length > 0) {
|
|
const updateResult = await updatePublishedRule(editingId, {
|
|
title,
|
|
summary: summary ?? null,
|
|
document: ruleDocument,
|
|
});
|
|
setIsPublishing(false);
|
|
if (updateResult.ok === true) {
|
|
writeLastPublishedRule({
|
|
id: editingId,
|
|
title,
|
|
summary: summary ?? null,
|
|
document: ruleDocument,
|
|
});
|
|
updateState({ editingPublishedRuleId: undefined });
|
|
router.push(createFlowStepPath("completed"));
|
|
return;
|
|
}
|
|
if (updateResult.status === 401) {
|
|
openLogin({
|
|
variant: sessionUser === null ? "keepRule" : "default",
|
|
nextPath:
|
|
sessionUser === null
|
|
? createFlowStepPath("completed")
|
|
: loginReturnPath,
|
|
backdropVariant: "blurredYellow",
|
|
});
|
|
return;
|
|
}
|
|
showFailedBanner(
|
|
updateResult.error.trim() !== ""
|
|
? updateResult.error
|
|
: messages.create.reviewAndComplete.publish.genericPublishFailed,
|
|
false,
|
|
);
|
|
return;
|
|
}
|
|
|
|
const isGuest = sessionUser === null;
|
|
const rawStakeholderEmails = skipInvites
|
|
? []
|
|
: (state.stakeholderEmails ?? []).filter(
|
|
(e) => typeof e === "string" && e.trim() !== "",
|
|
);
|
|
const stakeholderEmails = isGuest ? [] : rawStakeholderEmails;
|
|
const skippedGuestInvites = isGuest && rawStakeholderEmails.length > 0;
|
|
const sendingInvites = stakeholderEmails.length > 0;
|
|
|
|
const publishResult = await publishRule({
|
|
title,
|
|
summary,
|
|
document: ruleDocument,
|
|
...(stakeholderEmails.length > 0 ? { stakeholderEmails } : {}),
|
|
});
|
|
setIsPublishing(false);
|
|
if (publishResult.ok === true) {
|
|
writeLastPublishedRule({
|
|
id: publishResult.id,
|
|
title,
|
|
summary: summary ?? null,
|
|
document: ruleDocument,
|
|
...(publishResult.createdAt
|
|
? { finishedAt: publishResult.createdAt }
|
|
: {}),
|
|
});
|
|
if (isGuest) {
|
|
writePendingKeepRuleLogin();
|
|
onGuestPublished?.({ skippedInvites: skippedGuestInvites });
|
|
}
|
|
router.push(
|
|
createFlowStepPath("completed", {
|
|
[CREATE_FLOW_COMPLETED_CELEBRATE_QUERY]:
|
|
CREATE_FLOW_COMPLETED_CELEBRATE_VALUE,
|
|
}),
|
|
);
|
|
return;
|
|
}
|
|
if (publishResult.status === 401) {
|
|
openLogin({
|
|
variant: sessionUser === null ? "keepRule" : "default",
|
|
nextPath:
|
|
sessionUser === null
|
|
? createFlowStepPath("completed")
|
|
: loginReturnPath,
|
|
backdropVariant: "blurredYellow",
|
|
});
|
|
return;
|
|
}
|
|
showFailedBanner(
|
|
publishResult.error.trim() !== ""
|
|
? publishResult.error
|
|
: messages.create.reviewAndComplete.publish.genericPublishFailed,
|
|
sendingInvites,
|
|
);
|
|
},
|
|
[
|
|
state,
|
|
router,
|
|
openLogin,
|
|
updateState,
|
|
loginReturnPath,
|
|
sessionUser,
|
|
onGuestPublished,
|
|
showFailedBanner,
|
|
],
|
|
);
|
|
|
|
const clearPublishBanner = useCallback((message: string | null) => {
|
|
if (message === null) setPublishBannerTitle(null);
|
|
setPublishBannerMessage(message);
|
|
}, []);
|
|
|
|
return {
|
|
publishBannerTitle,
|
|
publishBannerMessage,
|
|
setPublishBannerMessage: clearPublishBanner,
|
|
isPublishing,
|
|
finalize,
|
|
};
|
|
}
|