-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhooks.ts
More file actions
297 lines (275 loc) Β· 7.65 KB
/
Copy pathhooks.ts
File metadata and controls
297 lines (275 loc) Β· 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* Content Impact Score (CIS) - Custom Hooks
* Provides hooks for using CIS calculations in React components
*/
'use client';
import { useMemo } from 'react';
import { CISScoringService } from './scoring-service';
import type {
EducatorRawMetrics,
EducatorScore,
QuarterlyCISAllocation,
CISConfig,
} from './types';
/**
* Hook to calculate CIS scores from raw metrics
*
* @param rawMetrics - Array of raw educator metrics
* @param previousScores - Optional map of previous quarter scores for smoothing
* @param config - Optional CIS configuration overrides
* @returns Calculated educator scores
*/
export function useCISScores(
rawMetrics: EducatorRawMetrics[],
previousScores?: Map<string, number>,
config?: Partial<CISConfig>
): EducatorScore[] {
return useMemo(() => {
const service = new CISScoringService(config);
return service.calculateScores(rawMetrics, previousScores);
}, [rawMetrics, previousScores, config]);
}
/**
* Hook to calculate CIS allocation with revenue distribution and tiers
*
* @param rawMetrics - Array of raw educator metrics
* @param totalPoolUsd - Total funding pool in USD
* @param period - Quarter period (e.g., "2025-Q1")
* @param previousScores - Optional previous quarter scores
* @param config - Optional CIS configuration
* @returns Complete quarterly CIS allocation
*/
export function useCISAllocation(
rawMetrics: EducatorRawMetrics[],
totalPoolUsd: number,
period: string,
previousScores?: Map<string, number>,
config?: Partial<CISConfig>
): QuarterlyCISAllocation {
return useMemo(() => {
const service = new CISScoringService(config);
return service.generateQuarterlyAllocation(
rawMetrics,
totalPoolUsd,
period,
previousScores
);
}, [rawMetrics, totalPoolUsd, period, previousScores, config]);
}
/**
* Hook to get a single educator's score from the cohort
*
* @param educatorId - ID of the educator to find
* @param scores - Array of all educator scores
* @returns Single educator score or undefined
*/
export function useEducatorScore(
educatorId: string,
scores: EducatorScore[]
): EducatorScore | undefined {
return useMemo(() => {
return scores.find((s) => s.educatorId === educatorId);
}, [educatorId, scores]);
}
/**
* Hook to get educator tier badge info
*
* @param tier - Educator's tier
* @returns Tier badge configuration
*/
export function useEducatorTier(tier: EducatorScore['tier']): {
label: string;
color: string;
icon: string;
description: string;
} {
return useMemo(() => {
switch (tier) {
case 'platinum':
return {
label: 'Platinum Educator',
color: 'bg-gradient-to-r from-cyan-400 to-blue-400',
icon: 'π',
description: 'Top 5% - Elite React educator with exceptional impact',
};
case 'gold':
return {
label: 'Gold Educator',
color: 'bg-gradient-to-r from-yellow-400 to-orange-400',
icon: 'π',
description: 'Top 15% - Outstanding React educator',
};
case 'silver':
return {
label: 'Silver Educator',
color: 'bg-gradient-to-r from-gray-300 to-gray-400',
icon: 'π₯',
description: 'Top 30% - Excellent React educator',
};
case 'bronze':
return {
label: 'Bronze Educator',
color: 'bg-gradient-to-r from-orange-300 to-orange-400',
icon: 'π₯',
description: 'Top 50% - Valued React educator',
};
case 'none':
default:
return {
label: 'Emerging Educator',
color: 'bg-muted',
icon: 'π',
description: 'Growing impact in the React community',
};
}
}, [tier]);
}
/**
* Hook to sort and rank educators by CIS score
*
* @param scores - Array of educator scores
* @param descending - Sort descending (default: true)
* @returns Sorted scores with rankings
*/
export function useCISRankings(
scores: EducatorScore[],
descending: boolean = true
): Array<EducatorScore & { rank: number; percentile: number }> {
return useMemo(() => {
const sorted = [...scores].sort((a, b) =>
descending ? b.cis - a.cis : a.cis - b.cis
);
return sorted.map((score, idx) => ({
...score,
rank: idx + 1,
percentile: 1 - idx / sorted.length, // Top = 1.0, bottom = ~0.0
}));
}, [scores, descending]);
}
/**
* Hook to filter educators by tier
*
* @param scores - Array of educator scores
* @param tier - Tier to filter by
* @returns Educators in the specified tier
*/
export function useEducatorsByTier(
scores: EducatorScore[],
tier: EducatorScore['tier']
): EducatorScore[] {
return useMemo(() => {
return scores.filter((s) => s.tier === tier);
}, [scores, tier]);
}
/**
* Hook to calculate CIS statistics across the cohort
*
* @param scores - Array of educator scores
* @returns Statistical summary
*/
export function useCISStatistics(scores: EducatorScore[]): {
mean: number;
median: number;
min: number;
max: number;
stdDev: number;
totalAllocation: number;
tierCounts: Record<EducatorScore['tier'], number>;
} {
return useMemo(() => {
if (scores.length === 0) {
return {
mean: 0,
median: 0,
min: 0,
max: 0,
stdDev: 0,
totalAllocation: 0,
tierCounts: { platinum: 0, gold: 0, silver: 0, bronze: 0, none: 0 },
};
}
const cisValues = scores.map((s) => s.cis);
const sorted = [...cisValues].sort((a, b) => a - b);
// Mean
const mean = cisValues.reduce((sum, v) => sum + v, 0) / cisValues.length;
// Median
const mid = Math.floor(sorted.length / 2);
const median =
sorted.length % 2 === 0
? (sorted[mid - 1] + sorted[mid]) / 2
: sorted[mid];
// Min/Max
const min = sorted[0];
const max = sorted[sorted.length - 1];
// Standard Deviation
const variance =
cisValues.reduce((sum, v) => sum + Math.pow(v - mean, 2), 0) /
cisValues.length;
const stdDev = Math.sqrt(variance);
// Total Allocation
const totalAllocation = scores.reduce(
(sum, s) => sum + s.allocation_usd,
0
);
// Tier Counts
const tierCounts = scores.reduce(
(counts, s) => {
counts[s.tier] = (counts[s.tier] || 0) + 1;
return counts;
},
{ platinum: 0, gold: 0, silver: 0, bronze: 0, none: 0 } as Record<
EducatorScore['tier'],
number
>
);
return {
mean,
median,
min,
max,
stdDev,
totalAllocation,
tierCounts,
};
}, [scores]);
}
/**
* Hook to compare two educators
*
* @param educatorId1 - First educator ID
* @param educatorId2 - Second educator ID
* @param scores - Array of all educator scores
* @returns Comparison data
*/
export function useEducatorComparison(
educatorId1: string,
educatorId2: string,
scores: EducatorScore[]
): {
educator1: EducatorScore | undefined;
educator2: EducatorScore | undefined;
cisDiff: number;
rankDiff: number;
} {
return useMemo(() => {
const educator1 = scores.find((s) => s.educatorId === educatorId1);
const educator2 = scores.find((s) => s.educatorId === educatorId2);
if (!educator1 || !educator2) {
return {
educator1,
educator2,
cisDiff: 0,
rankDiff: 0,
};
}
const ranked = useCISRankings(scores);
const rank1 = ranked.find((r) => r.educatorId === educatorId1)?.rank || 0;
const rank2 = ranked.find((r) => r.educatorId === educatorId2)?.rank || 0;
return {
educator1,
educator2,
cisDiff: educator1.cis - educator2.cis,
rankDiff: rank1 - rank2,
};
}, [educatorId1, educatorId2, scores]);
}