-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathusePartnersList.ts
More file actions
61 lines (51 loc) · 2.07 KB
/
usePartnersList.ts
File metadata and controls
61 lines (51 loc) · 2.07 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
'use client';
import { useEffect, useRef, useState } from 'react';
import { randomPartnerList } from '#site/components/Common/Partners/utils';
import type { PartnerCategory, Partners } from '#site/types/partners';
const usePartnersList = ({
logos,
maxLength,
sort,
categories,
}: {
logos: Array<Partners>;
maxLength: number;
sort?: 'name' | 'weight';
categories?: PartnerCategory;
}) => {
const initialRenderer = useRef(true);
const [seedList, setSeedList] = useState<Array<Partners>>(() => {
const filteredLogos = logos.filter(
partner => !categories || partner.categories.includes(categories)
);
return filteredLogos.slice(0, maxLength || filteredLogos.length);
});
useEffect(() => {
// We intentionally render the initial default "mock" list of sponsors
// to have the Skeletons loading, and then we render the actual list
// after an enough amount of time has passed to give a proper sense of Animation
// We do this client-side effect, to ensure that a random-amount of sponsors is renderered
// on every page load. Since our page is natively static, we need to ensure that
// on the client-side we have a random amount of sponsors rendered.
// Although whilst we are deployed on Vercel or other environment that supports ISR
// (Incremental Static Generation) whose would invalidate the cache every 5 minutes
// We want to ensure that this feature is compatible on a full-static environment
const renderSponsorsAnimation = setTimeout(async () => {
initialRenderer.current = false;
setSeedList(
await randomPartnerList(logos, {
pick: maxLength,
dateSeed: 1,
category: categories,
sort,
})
);
}, 0);
return () => clearTimeout(renderSponsorsAnimation);
// We only want this to run once on initial render
// We don't really care if the props change as realistically they shouldn't ever
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return { seedList, initialRenderer };
};
export default usePartnersList;