Custom add and create flow polish

This commit is contained in:
adilallo
2026-05-08 20:32:24 -06:00
parent 26bcd61ea3
commit 026a1e6d71
68 changed files with 6208 additions and 527 deletions
+92
View File
@@ -0,0 +1,92 @@
import type {
CommunityStructureChipSnapshotRow,
CreateFlowState,
} from "../../app/(app)/create/types";
import {
duplicateMethodCardTitle,
omitIdFromStringRecord,
} from "./duplicateMethodCardModalDraft";
import { moveFacetSelectionIdToFront } from "./methodCardSelectionOrder";
export const MAX_SELECTED_CORE_VALUES = 5;
/** Remove a chip from snapshot, selection ids, and per-chip detail overrides. */
export function removeCoreValueChipFromDraft(
state: CreateFlowState,
chipId: string,
): Partial<CreateFlowState> {
const snap = state.coreValuesChipsSnapshot ?? [];
const nextSnap = snap.filter((r) => r.id !== chipId);
const sel = [...(state.selectedCoreValueIds ?? [])].filter((id) => id !== chipId);
const hadDetail =
Boolean(state.coreValueDetailsByChipId) &&
Object.prototype.hasOwnProperty.call(state.coreValueDetailsByChipId, chipId);
const nextDetails = hadDetail
? omitIdFromStringRecord(state.coreValueDetailsByChipId, chipId)
: undefined;
const out: Partial<CreateFlowState> = {
coreValuesChipsSnapshot: nextSnap,
selectedCoreValueIds: sel,
};
if (hadDetail) {
out.coreValueDetailsByChipId = nextDetails;
}
return out;
}
/** Clone a core value chip with a suffixed label; returns null when at capacity. */
export function duplicateCoreValueChipInDraft(
state: CreateFlowState,
chipId: string,
duplicateTitleSuffix: string,
): {
patch: Partial<CreateFlowState>;
newId: string;
newLabel: string;
} | null {
const sel = [...(state.selectedCoreValueIds ?? [])];
if (sel.length >= MAX_SELECTED_CORE_VALUES) {
return null;
}
const snap = state.coreValuesChipsSnapshot ?? [];
const row = snap.find((r) => r.id === chipId);
if (!row) {
return null;
}
const rawLabel =
typeof row.label === "string" && row.label.trim().length > 0
? row.label.trim()
: chipId;
const newId = crypto.randomUUID();
const newLabel = duplicateMethodCardTitle(rawLabel, duplicateTitleSuffix);
const newRow: CommunityStructureChipSnapshotRow = {
id: newId,
label: newLabel,
state: "selected",
};
const nextSnap = [...snap, newRow];
const inherited = state.coreValueDetailsByChipId?.[chipId];
const nextDetails =
inherited !== undefined
? {
...(state.coreValueDetailsByChipId ?? {}),
[newId]: structuredClone(inherited),
}
: { ...(state.coreValueDetailsByChipId ?? {}) };
return {
newId,
newLabel,
patch: {
coreValuesChipsSnapshot: nextSnap,
selectedCoreValueIds: moveFacetSelectionIdToFront(sel, newId),
...(Object.keys(nextDetails).length > 0
? { coreValueDetailsByChipId: nextDetails }
: {}),
},
};
}