44 */
55
66import { TagType } from "../data/tags" ;
7+ import { RESOURCE_TYPE_PRIORITY } from "./buttonTextUtils" ;
78
89/**
9- * Maps CTA button text to preferred resource type tag priority
10- * This ensures the most relevant tag appears first based on the card's action
11- * Content hierarchy: Primary content type (video, workshop, training, etc.)
12- * comes before delivery format (blog)
13- * Example: Blog about video → video tag first, then blog tag
10+ * Sorts resource type tags so that the tag matching the card's CTA appears first,
11+ * followed by the remaining resource type tags in priority order.
12+ *
13+ * Since CTA is now determined purely by RESOURCE_TYPE_PRIORITY (highest-priority
14+ * tag wins), sorting by the same order keeps the displayed tag badge consistent
15+ * with the CTA button text.
16+ *
17+ * @param resourceTypeTags - Array of resource type tag objects to sort
18+ * @param _buttonText - Kept for API compatibility; no longer used internally
19+ * @returns Sorted array with the CTA-relevant tag first
1420 */
15- export const CTA_TO_RESOURCE_TYPE_PRIORITY : Record < string , TagType [ ] > = {
16- // Workshop CTAs - workshop comes first when both workshop and documentation are present
17- "Workshop" : [ "workshop" , "documentation" , "tutorial" , "concepts" , "how-to" , "solution-accelerator" , "video" , "training" , "blog" ] ,
18-
19- // Training CTAs - training comes first when both training and documentation are present
20- "Training" : [ "training" , "documentation" , "tutorial" , "concepts" , "how-to" , "solution-accelerator" , "video" , "workshop" , "blog" ] ,
21-
22- // Tutorial CTAs - documentation first, then tutorial content (as it's a doc sub-type)
23- "Tutorial" : [ "documentation" , "tutorial" , "concepts" , "how-to" , "solution-accelerator" , "video" , "workshop" , "training" , "blog" ] ,
24-
25- // Documentation CTAs - documentation and its sub-types take priority
26- "Documentation" : [ "documentation" , "tutorial" , "concepts" , "how-to" , "solution-accelerator" , "video" , "workshop" , "training" , "blog" ] ,
27- "Concepts" : [ "documentation" , "concepts" , "tutorial" , "how-to" , "solution-accelerator" , "video" , "workshop" , "training" , "blog" ] ,
28- "How-To Guide" : [ "documentation" , "how-to" , "tutorial" , "concepts" , "solution-accelerator" , "video" , "workshop" , "training" , "blog" ] ,
29-
30- // Video CTAs - documentation first, then doc sub-types, then video content
31- "Video" : [ "documentation" , "tutorial" , "concepts" , "how-to" , "video" , "solution-accelerator" , "workshop" , "training" , "blog" ] ,
32-
33- // Blog CTAs - if blog is about workshop/training, those come first before documentation
34- // Priority: workshop → training → documentation → tutorial/concepts/how-to → other content → blog
35- "Blog" : [ "workshop" , "training" , "documentation" , "tutorial" , "concepts" , "how-to" , "solution-accelerator" , "video" , "blog" ] ,
36-
37- // Sample CTAs - documentation first, then doc sub-types, then samples
38- "Sample" : [ "documentation" , "tutorial" , "concepts" , "how-to" , "solution-accelerator" , "video" , "workshop" , "training" , "blog" ] ,
39- "GitHub Repo" : [ "documentation" , "tutorial" , "concepts" , "how-to" , "solution-accelerator" , "video" , "workshop" , "training" , "blog" ] ,
40-
41- // Solution Accelerator CTAs - documentation first, then doc sub-types, then solution-accelerator
42- "Solution Accelerator" : [ "documentation" , "tutorial" , "concepts" , "how-to" , "solution-accelerator" , "video" , "workshop" , "training" , "blog" ] ,
43-
44- // Default fallback for generic CTAs - workshop and training before documentation when present
45- "More" : [ "workshop" , "training" , "documentation" , "tutorial" , "concepts" , "how-to" , "solution-accelerator" , "video" , "blog" ]
46- } ;
21+ export function sortResourceTypeTagsByCTA (
22+ resourceTypeTags : Array < { tag : TagType ; [ key : string ] : any } > ,
23+ _buttonText ?: string
24+ ) : Array < { tag : TagType ; [ key : string ] : any } > {
25+ return [ ...resourceTypeTags ] . sort ( ( a , b ) => {
26+ const aIdx = RESOURCE_TYPE_PRIORITY . indexOf ( a . tag ) ;
27+ const bIdx = RESOURCE_TYPE_PRIORITY . indexOf ( b . tag ) ;
4728
48- /**
49- * Default resource type priority order when no CTA match is found
50- */
51- export const DEFAULT_RESOURCE_TYPE_PRIORITY : TagType [ ] = [
52- "documentation" ,
53- "tutorial" ,
54- "concepts" ,
55- "how-to" ,
56- "solution-accelerator" ,
57- "video" ,
58- "workshop" ,
59- "training" ,
60- "blog"
61- ] ;
29+ // Both in priority list → sort by position
30+ if ( aIdx !== - 1 && bIdx !== - 1 ) return aIdx - bIdx ;
6231
63- /**
64- * Gets the resource type tag priority based on CTA button text
65- * @param buttonText - The CTA button text from getButtonText()
66- * @returns Array of resource type tags in priority order
67- */
68- export function getResourceTypePriorityByCTA ( buttonText : string ) : TagType [ ] {
69- return CTA_TO_RESOURCE_TYPE_PRIORITY [ buttonText ] || DEFAULT_RESOURCE_TYPE_PRIORITY ;
32+ // Only one is in the list → that one goes first
33+ if ( aIdx !== - 1 ) return - 1 ;
34+ if ( bIdx !== - 1 ) return 1 ;
35+
36+ // Neither is in the list → preserve original order
37+ return 0 ;
38+ } ) ;
7039}
7140
7241/**
73- * Sorts resource type tags based on CTA button text priority
74- * @param resourceTypeTags - Array of resource type tag objects
75- * @param buttonText - The CTA button text
76- * @returns Sorted array with CTA-relevant tags first
42+ * Returns the resource type tag priority order.
43+ * Exported for consumers that need to inspect or test the order directly.
7744 */
78- export function sortResourceTypeTagsByCTA (
79- resourceTypeTags : Array < { tag : TagType ; [ key : string ] : any } > ,
80- buttonText : string
81- ) : Array < { tag : TagType ; [ key : string ] : any } > {
82- const priorityOrder = getResourceTypePriorityByCTA ( buttonText ) ;
83-
84- return resourceTypeTags . sort ( ( firstTag , secondTag ) => {
85- const firstTagIndex = priorityOrder . indexOf ( firstTag . tag ) ;
86- const secondTagIndex = priorityOrder . indexOf ( secondTag . tag ) ;
87-
88- // If both tags are in priority list, sort by priority
89- if ( firstTagIndex !== - 1 && secondTagIndex !== - 1 ) {
90- return firstTagIndex - secondTagIndex ;
91- }
92-
93- // If only one tag is in priority list, prioritize it
94- if ( firstTagIndex !== - 1 ) return - 1 ;
95- if ( secondTagIndex !== - 1 ) return 1 ;
96-
97- // If neither tag is in priority list, maintain original order
98- return 0 ;
99- } ) ;
100- }
45+ export { RESOURCE_TYPE_PRIORITY } ;
0 commit comments