Keep a typed main kind from being ranked as the catalog other kind, and store that catalog kind on the finished Rule.

This commit is contained in:
adilallo committed 2026-11-23 11:07:50 -07:00
1 parent 4b90bce006
commit 3e0181e4de
7 files changed
+174 -48

No files matched your search

+56 -22
View File
@@ -6,30 +6,33 @@ import {
resolveScaleChipId,
resolveSizeChipId,
} from "./communityChipIds";
import { TYPED_ORG_TYPE_MARKER } from "./deckTypes";
const FACET_GROUPS = ["size", "orgType", "scale", "maturity"] as const;
type FacetGroupId = (typeof FACET_GROUPS)[number];
const CHIP_TO_VALUE_BY_GROUP: Record<FacetGroupId, Record<string, string>> =
(() => {
const out: Record<FacetGroupId, Record<string, string>> = {
size: {},
orgType: {},
scale: {},
maturity: {},
};
for (const group of FACET_GROUPS) {
const block = (facetGroups as Record<string, unknown>)[group];
if (block && typeof block === "object" && "values" in block) {
const values = (block as { values: Record<string, { chipId: string }> })
.values;
for (const [valueId, entry] of Object.entries(values)) {
out[group][entry.chipId] = valueId;
}
const CHIP_TO_VALUE_BY_GROUP: Record<
FacetGroupId,
Record<string, string>
> = (() => {
const out: Record<FacetGroupId, Record<string, string>> = {
size: {},
orgType: {},
scale: {},
maturity: {},
};
for (const group of FACET_GROUPS) {
const block = (facetGroups as Record<string, unknown>)[group];
if (block && typeof block === "object" && "values" in block) {
const values = (block as { values: Record<string, { chipId: string }> })
.values;
for (const [valueId, entry] of Object.entries(values)) {
out[group][entry.chipId] = valueId;
}
}
return out;
})();
}
return out;
})();
const STATE_KEY_BY_GROUP: Record<FacetGroupId, keyof CreateFlowState> = {
size: "selectedCommunitySizeIds",
@@ -45,14 +48,41 @@ const RESOLVE_BY_GROUP: Record<FacetGroupId, (id: string) => string> = {
maturity: resolveMaturityChipId,
};
function readChipIds(
state: CreateFlowState,
group: FacetGroupId,
): string[] {
function readChipIds(state: CreateFlowState, group: FacetGroupId): string[] {
const value = state[STATE_KEY_BY_GROUP[group]];
return Array.isArray(value) ? (value as string[]) : [];
}
function catalogOrgTypeId(chipId: string): string | null {
const resolved = resolveOrgChipId(chipId);
const valueId =
CHIP_TO_VALUE_BY_GROUP.orgType[resolved] ??
CHIP_TO_VALUE_BY_GROUP.orgType[chipId];
if (!valueId || valueId === "enterprise") return null;
return valueId;
}
/** Catalog kinds keep their order. A typed main kind is a leading marker. */
function appendOrgTypeFacets(params: URLSearchParams, state: CreateFlowState) {
const chipIds = readChipIds(state, "orgType");
const catalogIds: string[] = [];
const seen = new Set<string>();
for (const chipId of chipIds) {
const valueId = catalogOrgTypeId(chipId);
if (!valueId || seen.has(valueId)) continue;
seen.add(valueId);
catalogIds.push(valueId);
}
const firstIsCatalog =
chipIds.length > 0 && catalogOrgTypeId(chipIds[0]) !== null;
if (!firstIsCatalog && catalogIds.length > 0) {
params.append("facet.orgType", TYPED_ORG_TYPE_MARKER);
}
for (const valueId of catalogIds) {
params.append("facet.orgType", valueId);
}
}
/**
* Build `facet.size=…&facet.orgType=…` query string from Create Community
* chip selections. Shared by `/api/create-flow/methods` and
@@ -61,6 +91,10 @@ function readChipIds(
export function buildFacetQueryString(state: CreateFlowState): string {
const params = new URLSearchParams();
for (const group of FACET_GROUPS) {
if (group === "orgType") {
appendOrgTypeFacets(params, state);
continue;
}
const valuesById = CHIP_TO_VALUE_BY_GROUP[group];
const resolve = RESOLVE_BY_GROUP[group];
const seen = new Set<string>();
+13 -1
View File
@@ -27,6 +27,12 @@ export const KINDS = [
] as const;
export type Kind = (typeof KINDS)[number];
/**
* First `facet.orgType` value when the main kind was typed.
* Later catalog values stay other kinds instead of becoming the main kind.
*/
export const TYPED_ORG_TYPE_MARKER = "__typed__";
export const SCALES = ["local", "regional", "national", "global"] as const;
export type Scale = (typeof SCALES)[number];
@@ -48,7 +54,13 @@ export const TRAIT_KEYS = [
] as const;
export type TraitKey = (typeof TRAIT_KEYS)[number];
export const DISPLAY_TIERS = ["frame", "safety", "fitted", "floor", "none"] as const;
export const DISPLAY_TIERS = [
"frame",
"safety",
"fitted",
"floor",
"none",
] as const;
export type DisplayTier = (typeof DISPLAY_TIERS)[number];
export type RankTier = Exclude<DisplayTier, "none"> | "answers";
+4 -4
View File
@@ -15,13 +15,14 @@ function trimmedLocation(raw: unknown): string | undefined {
return trimmed.slice(0, LOCATION_MAX);
}
/** Place and main group type stored on a finished Rule for launch counts. */
/** Place and catalog group type stored on a finished Rule for launch counts. */
export function publishedRuleAnalyticsFromState(state: CreateFlowState): {
communityLocation?: string;
orgType?: Kind;
} {
const communityLocation = trimmedLocation(state.communityLocation);
const orgType = groupAnswersFromCreateFlowState(state).mainKind ?? undefined;
const answers = groupAnswersFromCreateFlowState(state);
const orgType = answers.mainKind ?? answers.otherKinds?.[0];
return {
...(communityLocation ? { communityLocation } : {}),
...(orgType ? { orgType } : {}),
@@ -37,8 +38,7 @@ export function publishedRuleAnalyticsFromDocument(document: unknown): {
}
const row = document as Record<string, unknown>;
const communityLocation = trimmedLocation(row.communityLocation) ?? null;
const orgTypeRaw =
typeof row.orgType === "string" ? row.orgType.trim() : "";
const orgTypeRaw = typeof row.orgType === "string" ? row.orgType.trim() : "";
const orgType = isKind(orgTypeRaw) ? orgTypeRaw : null;
return { communityLocation, orgType };
}