Template recommendation implemented

This commit is contained in:
adilallo
2026-04-29 19:24:50 -06:00
parent c4c74ecdb4
commit a4f0c4bf27
20 changed files with 899 additions and 82 deletions
+60
View File
@@ -0,0 +1,60 @@
import facetGroups from "../../data/create/customRule/_facetGroups.json";
import type { CreateFlowState } from "../../app/(app)/create/types";
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;
}
}
}
return out;
})();
const STATE_KEY_BY_GROUP: Record<FacetGroupId, keyof CreateFlowState> = {
size: "selectedCommunitySizeIds",
orgType: "selectedOrganizationTypeIds",
scale: "selectedScaleIds",
maturity: "selectedMaturityIds",
};
function readChipIds(
state: CreateFlowState,
group: FacetGroupId,
): string[] {
const value = state[STATE_KEY_BY_GROUP[group]];
return Array.isArray(value) ? (value as string[]) : [];
}
/**
* Build `facet.size=…&facet.orgType=…` query string from Create Community
* chip selections. Shared by `/api/create-flow/methods` and
* `GET /api/templates` ranking (CR-88).
*/
export function buildFacetQueryString(state: CreateFlowState): string {
const params = new URLSearchParams();
for (const group of FACET_GROUPS) {
const valuesById = CHIP_TO_VALUE_BY_GROUP[group];
for (const chipId of readChipIds(state, group)) {
const valueId = valuesById[chipId];
if (valueId) {
params.append(`facet.${group}`, valueId);
}
}
}
return params.toString();
}
+55 -1
View File
@@ -13,7 +13,21 @@ export type RuleTemplateDto = {
featured: boolean;
};
type TemplatesResponse = { templates?: RuleTemplateDto[] };
type TemplatesResponse = {
templates?: RuleTemplateDto[];
scores?: Record<string, TemplateFacetScoreDto>;
};
/** Matches `listRankedRuleTemplatesFromDb` / GET `/api/templates` with facet params. */
export type TemplateFacetScoreDto = {
score: number;
matchedFacets: string[];
};
export type RankedTemplatesFetchResult = {
templates: RuleTemplateDto[];
scores: Record<string, TemplateFacetScoreDto>;
};
export type FetchTemplatesOptions = {
signal?: AbortSignal;
@@ -57,6 +71,46 @@ export async function fetchTemplates(
}
}
/**
* Facet-ranked list + per-template scores (CR-88). Query must be non-empty
* `facet.size=…&…` from {@link buildFacetQueryString}.
*/
export async function fetchRankedTemplatesByFacets(options: {
facetQuery: string;
signal?: AbortSignal;
}): Promise<RankedTemplatesFetchResult | { error: string }> {
if (options.facetQuery.length === 0) {
return { error: "Could not load templates" };
}
try {
const res = await fetch(`/api/templates?${options.facetQuery}`, {
credentials: "include",
signal: options.signal,
});
const data = (await res.json()) as TemplatesResponse & { error?: string };
if (!res.ok) {
return {
error:
typeof data.error === "string"
? data.error
: "Could not load templates",
};
}
const templates = Array.isArray(data.templates) ? data.templates : [];
const raw = data.scores;
const scores: Record<string, TemplateFacetScoreDto> =
raw && typeof raw === "object" && !Array.isArray(raw)
? (raw as Record<string, TemplateFacetScoreDto>)
: {};
return { templates, scores };
} catch (e) {
if (isAbortError(e)) {
throw e;
}
return { error: "Could not load templates" };
}
}
export async function fetchTemplateBySlug(
slug: string,
options?: FetchTemplatesOptions,