Files
community-rule/app/(app)/create/hooks/useMethodCardDeckOrdering.ts
T
adilalloandCursor bcbc6f30d9 Import the remade decks on stable ids and recommend one card per job, so a saved draft still loads after a chip is renamed.
The real-group profiles stay as a test, so a change that moves a pick fails.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-04 14:22:54 -07:00

98 lines
2.5 KiB
TypeScript

"use client";
import { useMemo } from "react";
import { mergeCompactCardIdsWithPinnedSelected } from "../../../../lib/create/methodCardDisplayOrder";
import {
deriveCompactCards,
groupAnswersForRecommendations,
type RecommendationSection,
} from "./useFacetRecommendations";
import { useCreateFlow } from "../context/CreateFlowContext";
type MethodEntry = { id: string; label: string; supportText: string };
function orderByIds<T extends { id: string }>(
methods: readonly T[],
orderedIds: readonly string[],
): T[] {
const byId = new Map(methods.map((method) => [method.id, method]));
const ordered = orderedIds.flatMap((id) => {
const method = byId.get(id);
return method ? [method] : [];
});
const seen = new Set(ordered.map((method) => method.id));
return [...ordered, ...methods.filter((method) => !seen.has(method.id))];
}
/**
* Deck order and the compact stack come from the job rule. Current screens
* show five cards; a deck screen passes seven. Selected cards still pin
* into the compact stack.
*/
export function useMethodCardDeckOrdering(
section: RecommendationSection,
methods: readonly MethodEntry[],
selectedIds: readonly string[],
) {
const { state } = useCreateFlow();
const answers = useMemo(
() => groupAnswersForRecommendations(state),
[state],
);
const deck = useMemo(
() => deriveCompactCards(section, answers, 5),
[section, answers],
);
const rankedMethods = useMemo(
() => orderByIds(methods, deck.orderedIds),
[methods, deck.orderedIds],
);
const selectionShowcaseActive = selectedIds.length > 0;
const { recommendedIds } = deck;
const compactCardIds = useMemo(
() =>
mergeCompactCardIdsWithPinnedSelected(
rankedMethods.map((method) => method.id),
deck.compactCardIds,
selectedIds,
selectionShowcaseActive,
5,
),
[
rankedMethods,
deck.compactCardIds,
selectedIds,
selectionShowcaseActive,
],
);
const sampleCards = useMemo(
() =>
rankedMethods.map((entry) => ({
id: entry.id,
label: entry.label,
supportText: entry.supportText,
recommended: recommendedIds.has(entry.id),
})),
[rankedMethods, recommendedIds],
);
const methodById = useMemo(
() => new Map(rankedMethods.map((entry) => [entry.id, entry])),
[rankedMethods],
);
return {
rankedMethods,
displayMethods: rankedMethods,
compactCardIds,
recommendedIds,
sampleCards,
methodById,
recommendationsReady: true,
};
}