Files
community-rule/app/(app)/create/utils/createFlowFooterLabels.ts
T

69 lines
2.4 KiB
TypeScript

import type footerMessages from "../../../../messages/en/create/footer.json";
import type { CreateFlowStep } from "../types";
type FooterMessages = typeof footerMessages;
/**
* Per-step label override for the default "next-step" primary footer
* button (the catch-all branch in `CreateFlowLayoutClient`'s footer that
* fires `goToNextStep` for steps without a bespoke footer). Steps absent
* from this map fall back to `footer.next`.
*
* `final-review` is handled separately by the caller because its label
* also depends on the in-flight publish flag (`publishButtonPublishing`
* vs `publishCommunityRule`).
*/
const DEFAULT_FOOTER_LABEL_BY_STEP: ReadonlyMap<
CreateFlowStep,
keyof FooterMessages
> = new Map<CreateFlowStep, keyof FooterMessages>([
["confirm-stakeholders", "confirmStakeholders"],
["community-context", "confirmDescription"],
["community-structure", "confirmDetails"],
["community-size", "confirmSize"],
]);
function hasNonEmptyId(ids: readonly string[] | undefined): boolean {
return (ids ?? []).some((id) => id.length > 0);
}
export function isCommunityNameStepBlocked(
step: CreateFlowStep | null | undefined,
title: string | undefined,
): boolean {
if (step !== "community-name") return false;
return typeof title !== "string" || title.trim().length === 0;
}
export function isCommunityStructureStepBlocked(
step: CreateFlowStep | null | undefined,
selectedOrganizationTypeIds: readonly string[] | undefined,
selectedMaturityIds: readonly string[] | undefined,
): boolean {
if (step !== "community-structure") return false;
const mainKind = (selectedOrganizationTypeIds ?? []).find((id) => id.length > 0);
return !mainKind || !hasNonEmptyId(selectedMaturityIds);
}
export function isCommunitySizeStepBlocked(
step: CreateFlowStep | null | undefined,
selectedCommunitySizeIds: readonly string[] | undefined,
): boolean {
if (step !== "community-size") return false;
return !hasNonEmptyId(selectedCommunitySizeIds);
}
/**
* Resolve the localized label for the default "next-step" footer button.
* Returns the per-step override when one is registered, otherwise
* `footer.next`. Caller still owns the `final-review` special case.
*/
export function getDefaultFooterLabel(
step: CreateFlowStep | null | undefined,
footer: FooterMessages,
): string {
if (step == null) return footer.next;
const key = DEFAULT_FOOTER_LABEL_BY_STEP.get(step);
return key != null ? footer[key] : footer.next;
}