"use client"; import { useState, useMemo, useEffect, type Dispatch, type SetStateAction, } from "react"; import MultiSelect from "../../../components/controls/MultiSelect"; import type { ChipOption } from "../../../components/controls/MultiSelect/MultiSelect.types"; import { useMessages } from "../../../contexts/MessagesContext"; import { useCreateFlow } from "../../context/CreateFlowContext"; import { CreateFlowHeaderLockup } from "../../components/CreateFlowHeaderLockup"; import { CreateFlowStepShell } from "../../components/CreateFlowStepShell"; import { CREATE_FLOW_MD_UP_COLUMN_MAX_CLASS } from "../../components/createFlowLayoutTokens"; function createListCustomHandlers( setList: Dispatch>, confirmState: "Unselected" | "Selected", onInteraction?: () => void, ) { const touch = () => onInteraction?.(); return { onAddClick: () => { touch(); setList((prev) => [ ...prev, { id: crypto.randomUUID(), label: "", state: "Custom" }, ]); }, onCustomChipConfirm: (chipId: string, value: string) => { touch(); setList((prev) => prev.map((opt) => opt.id === chipId ? { ...opt, label: value, state: confirmState } : opt, ), ); }, onCustomChipClose: (chipId: string) => { touch(); setList((prev) => prev.filter((o) => o.id !== chipId)); }, }; } function chipRowsFromLabels( rows: readonly { label: string }[], ): ChipOption[] { return rows.map((row, i) => ({ id: String(i + 1), label: row.label, state: "Unselected" as const, })); } function applySavedSelection( options: ChipOption[], saved: string[] | undefined, ): ChipOption[] { const selected = new Set(saved ?? []); return options.map((opt) => opt.state === "Custom" ? opt : { ...opt, state: selected.has(opt.id) ? ("Selected" as const) : ("Unselected" as const), }, ); } function selectedIdsFromOptions(options: ChipOption[]): string[] { return options .filter((o) => o.state === "Selected") .map((o) => o.id); } /** Create Community step 3 — Figma `20094:18244` (responsive grid + column caps via `createFlowLayoutTokens`). */ export function CommunityStructureSelectScreen() { const m = useMessages(); const cs = m.create.communityStructure; const { markCreateFlowInteraction, updateState, state } = useCreateFlow(); const [organizationTypeOptions, setOrganizationTypeOptions] = useState< ChipOption[] >(() => applySavedSelection( chipRowsFromLabels(cs.organizationTypes), state.selectedOrganizationTypeIds, ), ); const [scaleOptions, setScaleOptions] = useState(() => applySavedSelection( chipRowsFromLabels(cs.scaleOptions), state.selectedScaleIds, ), ); const [maturityOptions, setMaturityOptions] = useState(() => applySavedSelection( chipRowsFromLabels(cs.maturityOptions), state.selectedMaturityIds, ), ); useEffect(() => { setOrganizationTypeOptions((prev) => applySavedSelection(prev, state.selectedOrganizationTypeIds), ); }, [state.selectedOrganizationTypeIds]); useEffect(() => { setScaleOptions((prev) => applySavedSelection(prev, state.selectedScaleIds)); }, [state.selectedScaleIds]); useEffect(() => { setMaturityOptions((prev) => applySavedSelection(prev, state.selectedMaturityIds), ); }, [state.selectedMaturityIds]); const organizationCustomHandlers = useMemo( () => createListCustomHandlers( setOrganizationTypeOptions, "Unselected", markCreateFlowInteraction, ), [markCreateFlowInteraction], ); const scaleCustomHandlers = useMemo( () => createListCustomHandlers( setScaleOptions, "Unselected", markCreateFlowInteraction, ), [markCreateFlowInteraction], ); const maturityCustomHandlers = useMemo( () => createListCustomHandlers( setMaturityOptions, "Unselected", markCreateFlowInteraction, ), [markCreateFlowInteraction], ); const persistOrg = (next: ChipOption[]) => { markCreateFlowInteraction(); setOrganizationTypeOptions(next); updateState({ selectedOrganizationTypeIds: selectedIdsFromOptions(next) }); }; const persistScale = (next: ChipOption[]) => { markCreateFlowInteraction(); setScaleOptions(next); updateState({ selectedScaleIds: selectedIdsFromOptions(next) }); }; const persistMaturity = (next: ChipOption[]) => { markCreateFlowInteraction(); setMaturityOptions(next); updateState({ selectedMaturityIds: selectedIdsFromOptions(next) }); }; const handleOrganizationTypeClick = (chipId: string) => { persistOrg( organizationTypeOptions.map((opt) => opt.id === chipId ? { ...opt, state: opt.state === "Selected" ? ("Unselected" as const) : ("Selected" as const), } : opt, ), ); }; const handleScaleClick = (chipId: string) => { persistScale( scaleOptions.map((opt) => opt.id === chipId ? { ...opt, state: opt.state === "Selected" ? ("Unselected" as const) : ("Selected" as const), } : opt, ), ); }; const handleMaturityClick = (chipId: string) => { persistMaturity( maturityOptions.map((opt) => opt.id === chipId ? { ...opt, state: opt.state === "Selected" ? ("Unselected" as const) : ("Selected" as const), } : opt, ), ); }; const multiSelectBlock = ( <> ); return (
{multiSelectBlock}
); }