Files
community-rule/app/(app)/create/components/TemplateCustomizeDialog.tsx
T
adilallo 6498d52541 Skip setup questions on the template path and collect name, photo, and description without recommendations.
Customize opens a dialog then walks the template decks; Use without changes lands on final review with Has Name Field. Template cards stay first, the engine is not called, and the catalog no longer shows a recommended tag.
2026-11-09 17:34:33 -07:00

257 lines
7.2 KiB
TypeScript

"use client";
/**
* Figma: "Modal / Customize (template)" (24991:40503).
* Name, optional logo, description, and optional location before the
* template decks.
*/
import { useEffect, useState } from "react";
import Button from "../../../components/buttons/Button";
import Icon from "../../../components/asset/icon";
import TextInput from "../../../components/controls/TextInput";
import TextArea from "../../../components/controls/TextArea";
import Create from "../../../components/modals/Create";
import HeaderLockup from "../../../components/type/HeaderLockup";
import InputLabel from "../../../components/type/InputLabel";
import { useMessages, useTranslation } from "../../../contexts/MessagesContext";
import { COMMUNITY_TITLE_FIELD_MAX_LENGTH } from "./FinalReviewTitleEditModal";
import { COMMUNITY_CONTEXT_FIELD_MAX_LENGTH } from "./FinalReviewCommunityContextEditModal";
import { useCommunityAvatarUpload } from "../hooks/useCommunityAvatarUpload";
import type { TemplateCustomizeFields } from "../hooks/useTemplateReviewActions";
const COMMUNITY_LOCATION_MAX_LENGTH = 200;
const COUNT_WORD_KEYS = [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
] as const;
function countWord(
count: number,
words: Record<string, string>,
): string {
const key = String(count);
if ((COUNT_WORD_KEYS as readonly string[]).includes(key)) {
const word = words[key];
if (typeof word === "string" && word.length > 0) return word;
}
return key;
}
function countPhrase(
count: number,
one: string,
many: string,
words: Record<string, string>,
): string {
if (count === 1) return one;
return many.replace("{word}", countWord(count, words));
}
export function TemplateCustomizeDialog({
isOpen,
templateTitle,
valuesCount,
cardsCount,
initialTitle,
initialDescription,
initialLocation,
onClose,
onContinue,
}: {
isOpen: boolean;
templateTitle: string;
valuesCount: number;
cardsCount: number;
initialTitle: string;
initialDescription: string;
initialLocation: string;
onClose: () => void;
onContinue: (_fields: TemplateCustomizeFields) => void;
}) {
const copy = useMessages().create.templateCustomize;
const tName = useTranslation("create.community.communityName");
const tContext = useTranslation("create.community.communityContext");
const avatar = useCommunityAvatarUpload();
const [title, setTitle] = useState(initialTitle);
const [description, setDescription] = useState(initialDescription);
const [location, setLocation] = useState(initialLocation);
const [attempted, setAttempted] = useState(false);
useEffect(() => {
if (!isOpen) {
setAttempted(false);
return;
}
setTitle(initialTitle);
setDescription(initialDescription);
setLocation(initialLocation);
}, [isOpen, initialTitle, initialDescription, initialLocation]);
const words = copy.countWords;
const valuesPhrase = countPhrase(
valuesCount,
copy.valuesOne,
copy.valuesMany,
words,
);
const cardsPhrase = countPhrase(
cardsCount,
copy.cardsOne,
copy.cardsMany,
words,
);
const heading = copy.title.replace("{template}", templateTitle);
const lockupDescription = copy.description
.replaceAll("{template}", templateTitle)
.replace("{valuesPhrase}", valuesPhrase)
.replace("{cardsPhrase}", cardsPhrase);
const trimmedTitle = title.trim();
const nameError = attempted && trimmedTitle.length === 0;
const canContinue = trimmedTitle.length > 0 && !avatar.busy;
const nameHint = nameError ? copy.nameRequired : copy.nameHint;
const handleContinue = () => {
setAttempted(true);
if (!canContinue) return;
onContinue({
title: trimmedTitle,
communityContext: description.trim(),
communityLocation: location.trim(),
});
};
return (
<Create
isOpen={isOpen}
onClose={onClose}
backdropVariant="default"
ariaLabel={heading}
headerContent={
<div className="px-[24px] pt-[8px]">
<HeaderLockup
title={heading}
description={lockupDescription}
justification="left"
size="M"
/>
</div>
}
showBackButton={false}
showCancelButton
showNextButton
cancelButtonText={copy.cancel}
nextButtonText={copy.continue}
nextButtonDisabled={!canContinue}
onCancel={onClose}
onNext={handleContinue}
>
<div className="flex flex-col gap-[16px]">
<TextInput
className="!transition-none"
type="text"
label={tName("title")}
placeholder={tName("placeholder")}
value={title}
onChange={(event) => setTitle(event.target.value)}
inputSize="medium"
formHeader
showHelpIcon={false}
textHint={nameHint}
error={nameError}
maxLength={COMMUNITY_TITLE_FIELD_MAX_LENGTH}
required
autoFocus
/>
<div className="flex flex-col gap-[8px]">
<InputLabel
label={copy.logoLabel}
helpIcon={false}
size="s"
palette="default"
/>
<div className="flex items-center gap-[12px]">
<input
ref={avatar.fileInputRef}
type="file"
className="sr-only"
tabIndex={-1}
accept={avatar.accept}
aria-label={copy.upload}
onChange={(event) => void avatar.handleFileChange(event)}
/>
<Button
type="button"
buttonType="outline"
palette="default"
size="xsmall"
hasIconLeading
disabled={avatar.busy}
onClick={avatar.handlePickFile}
>
<Icon name="image" size={14} />
{copy.upload}
</Button>
<p className="min-w-0 flex-1 text-x-small-label text-[color:var(--color-content-default-secondary)]">
{avatar.errorMessage ??
(avatar.fileName && avatar.displaySrc
? avatar.fileName
: copy.logoHint)}
</p>
</div>
</div>
<TextArea
className="!transition-none"
label={tContext("title")}
placeholder={copy.descriptionPlaceholder}
value={description}
onChange={(event) => setDescription(event.target.value)}
size="medium"
formHeader
showHelpIcon={false}
maxLength={COMMUNITY_CONTEXT_FIELD_MAX_LENGTH}
rows={2}
/>
<TextInput
className="!transition-none"
type="text"
label={tContext("locationLabel")}
placeholder={tContext("locationPlaceholder")}
value={location}
onChange={(event) => setLocation(event.target.value)}
inputSize="medium"
formHeader
showHelpIcon={false}
textHint={tContext("locationHint")}
maxLength={COMMUNITY_LOCATION_MAX_LENGTH}
/>
<p className="text-small-paragraph text-[color:var(--color-content-default-tertiary)]">
{copy.privacyNote}
</p>
</div>
</Create>
);
}