Skip to content

Commit 4e45d2f

Browse files
authored
feat: refactor partner retrieval logic and add providePartners function for weighted shuffling
1 parent 1d8c034 commit 4e45d2f

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

apps/site/components/Common/Partners/index.tsx

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import Tooltip from '@node-core/ui-components/Common/Tooltip';
44
import * as PartnerLogos from '@node-core/ui-components/Icons/PartnerLogos';
55

6-
import { partners } from '#site/next.json.mjs';
7-
import { shuffle } from '#site/util/array';
6+
import { providePartners } from '#site/next-data/providers/partners';
7+
import { partners as allPartners } from '#site/next.json.mjs';
88

99
import type { Partner, PartnerCategory } from '#site/types';
1010
import type { FC } from 'react';
@@ -25,33 +25,19 @@ const getPartners = async (
2525
category?: PartnerCategory,
2626
sort?: 'name' | 'weight'
2727
) => {
28-
let result: Array<Partner> = [];
29-
30-
const filteredPartners = category
31-
? partners.filter(partner => partner.categories.includes(category))
32-
: partners;
28+
let result: Array<Partner>;
3329

3430
if (sort === 'name') {
35-
filteredPartners.sort((a, b) => a.name.localeCompare(b.name));
36-
result = [...filteredPartners];
37-
}
38-
39-
if (sort === 'weight') {
40-
const weightedPartners = filteredPartners.flatMap(partner => {
41-
const weight = partner.weight ?? 0;
42-
return Array(weight > 0 ? weight : 1).fill(partner);
43-
});
44-
31+
// For name sorting, just filter and sort directly - no need for weighted shuffling
32+
const filtered = category
33+
? allPartners.filter(p => p.categories.includes(category))
34+
: allPartners;
35+
result = filtered.toSorted((a, b) => a.name.localeCompare(b.name));
36+
} else {
37+
// For weight sorting, use cached weighted partners
4538
const minutes = 300; // Change every 5 minutes
4639
const seed = Math.floor(Date.now() / (minutes * 1000));
47-
48-
// Create a copy of the array to avoid modifying the original
49-
const shuffled = await shuffle(weightedPartners, seed);
50-
51-
// Remove duplicates while preserving order
52-
const unique = Array.from(new Set(shuffled));
53-
54-
result = [...unique];
40+
result = await providePartners(seed, category);
5541
}
5642

5743
return length ? result.slice(0, length) : result;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use cache';
2+
3+
import { partners } from '#site/next.json.mjs';
4+
import { shuffle } from '#site/util/array';
5+
6+
import type { Partner, PartnerCategory } from '#site/types';
7+
8+
/**
9+
* Provides the weighted and shuffled partners array.
10+
* Results are cached using React's cache directive.
11+
* @param seed - The seed for deterministic shuffling
12+
* @param category - Optional category to filter partners
13+
*/
14+
export const providePartners = async (
15+
seed: number,
16+
category?: PartnerCategory
17+
): Promise<Array<Partner>> => {
18+
// Create weighted array (duplicates based on weight)
19+
const weightedPartners = partners.flatMap(partner => {
20+
const weight = partner.weight ?? 0;
21+
return Array(weight > 0 ? weight : 1).fill(partner);
22+
});
23+
24+
// Shuffle and remove duplicates
25+
const shuffled = await shuffle(weightedPartners, seed);
26+
const unique = Array.from(new Set(shuffled));
27+
28+
return category
29+
? unique.filter(p => p.categories.includes(category))
30+
: unique;
31+
};

0 commit comments

Comments
 (0)