Skip to content

Commit

Permalink
chore: Cache contributors w/ use-resource (#1191)
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian authored Nov 7, 2024
1 parent a3f203e commit 8fdc912
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/components/footer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useState, useEffect, useCallback } from 'preact/hooks';
import { useRoute } from 'preact-iso';
import style from './style.module.css';
import { useLanguage } from '../../lib/i18n';
import { useResource } from '../../lib/use-resource';

/*
* To update the list, run:
Expand All @@ -25,19 +26,20 @@ import { useLanguage } from '../../lib/i18n';
* @param {any[]} deps
*/
function useContributors(deps) {
const [contributors, setContributors] = useState([]);
const contributors = useResource(() =>
fetch('/contributors.json').then(r => r.json()),
['/contributors.json']
);

const [value, setValue] = useState(
contributors ? contributors[new Date().getMonth()] : undefined
);
useEffect(() => {
fetch('/contributors.json')
.then(r => r.json())
.then(d => setContributors(d));
}, []);

useEffect(() => {
if (contributors)
setValue(contributors[(Math.random() * (contributors.length - 1)) | 0]);
}, [...deps, contributors]);
}, [...deps]);

return value;
}

Expand Down

0 comments on commit 8fdc912

Please sign in to comment.