|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useRef, useState, type FC } from 'react'; |
| 4 | + |
| 5 | +import PARTNERS from '#site/next.partners.constants'; |
| 6 | +import type { Partners } from '#site/types'; |
| 7 | + |
| 8 | +import PartnerIcon from '../PartnerIcon'; |
| 9 | +import style from './index.module.css'; |
| 10 | +import { randomPartnerList } from '../utils'; |
| 11 | + |
| 12 | +type PartnersIconListProps = { |
| 13 | + maxLength?: number; |
| 14 | +}; |
| 15 | + |
| 16 | +const PartnersIconList: FC<PartnersIconListProps> = ({ maxLength = 4 }) => { |
| 17 | + const initialRenderer = useRef(true); |
| 18 | + |
| 19 | + const [seedList, setSeedList] = useState<Array<Partners>>( |
| 20 | + PARTNERS.slice(0, maxLength) |
| 21 | + ); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + // We intentionally render the initial default "mock" list of sponsors |
| 25 | + // to have the Skeletons loading, and then we render the actual list |
| 26 | + // after an enough amount of time has passed to give a proper sense of Animation |
| 27 | + // We do this client-side effect, to ensure that a random-amount of sponsors is renderered |
| 28 | + // on every page load. Since our page is natively static, we need to ensure that |
| 29 | + // on the client-side we have a random amount of sponsors rendered. |
| 30 | + // Although whilst we are deployed on Vercel or other environment that supports ISR |
| 31 | + // (Incremental Static Generation) whose would invalidate the cache every 5 minutes |
| 32 | + // We want to ensure that this feature is compatible on a full-static environment |
| 33 | + const renderSponsorsAnimation = setTimeout(() => { |
| 34 | + initialRenderer.current = false; |
| 35 | + |
| 36 | + setSeedList(randomPartnerList(PARTNERS, maxLength, 1)); |
| 37 | + }, 0); |
| 38 | + |
| 39 | + return () => clearTimeout(renderSponsorsAnimation); |
| 40 | + // We only want this to run once on initial render |
| 41 | + // We don't really care if the props change as realistically they shouldn't ever |
| 42 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 43 | + }, []); |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className={style.partnersIconList}> |
| 47 | + {seedList.map((partner, index) => ( |
| 48 | + <PartnerIcon |
| 49 | + {...partner} |
| 50 | + key={index} |
| 51 | + loading={initialRenderer.current} |
| 52 | + /> |
| 53 | + ))} |
| 54 | + </div> |
| 55 | + ); |
| 56 | +}; |
| 57 | + |
| 58 | +export default PartnersIconList; |
0 commit comments