From 03ea19f0f264c3b22ef0f3d0155a03e49923c9d7 Mon Sep 17 00:00:00 2001 From: Shane Loeffler Date: Tue, 5 Dec 2023 14:08:40 -0600 Subject: [PATCH 1/3] build years dynamically --- components/main.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/components/main.js b/components/main.js index 533b63f..44fde82 100644 --- a/components/main.js +++ b/components/main.js @@ -10,10 +10,11 @@ import { import List from './list' -const initYear = { - 2021: true, - 2022: true, - 2023: true, +const currentYear = new Date().getFullYear() +const initYear = {} + +for (let year = 2021; year <= currentYear; year++) { + initYear[year] = true } const Settings = ({ setYear, year }) => { From 94f7f58c1c230d502aa1b8a90b9d77743999e542 Mon Sep 17 00:00:00 2001 From: Shane Loeffler Date: Tue, 5 Dec 2023 14:09:58 -0600 Subject: [PATCH 2/3] use plural 'years' for clarity --- components/list.js | 6 +++--- components/main.js | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/components/list.js b/components/list.js index 68225b7..3ed106f 100644 --- a/components/list.js +++ b/components/list.js @@ -2,10 +2,10 @@ import { useMemo } from 'react' import { Box } from 'theme-ui' import Entry from './entry' -const List = ({ posts, year }) => { +const List = ({ posts, years }) => { const filteredContents = useMemo(() => { - return posts.filter((d) => year[d.date.split('-')[2]]) - }, [posts, year]) + return posts.filter((d) => years[d.date.split('-')[2]]) + }, [posts, years]) return ( diff --git a/components/main.js b/components/main.js index 44fde82..24d820f 100644 --- a/components/main.js +++ b/components/main.js @@ -11,18 +11,18 @@ import { import List from './list' const currentYear = new Date().getFullYear() -const initYear = {} +const initYears = {} for (let year = 2021; year <= currentYear; year++) { - initYear[year] = true + initYears[year] = true } -const Settings = ({ setYear, year }) => { +const Settings = ({ setYears, years }) => { return ( @@ -31,9 +31,9 @@ const Settings = ({ setYear, year }) => { } const Main = ({ showMobileSettings, posts }) => { - const [year, setYear] = useState(initYear) + const [years, setYears] = useState(initYears) - const settings = + const settings = return ( <> @@ -62,7 +62,7 @@ const Main = ({ showMobileSettings, posts }) => { {settings} - + From 78ed6721b1b2f5d4c31e3f7611a4ef8596b8590b Mon Sep 17 00:00:00 2001 From: Shane Loeffler Date: Wed, 6 Dec 2023 08:57:49 -0600 Subject: [PATCH 3/3] don't eval current year at build time --- components/main.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/components/main.js b/components/main.js index 24d820f..e0cafe7 100644 --- a/components/main.js +++ b/components/main.js @@ -10,12 +10,7 @@ import { import List from './list' -const currentYear = new Date().getFullYear() -const initYears = {} - -for (let year = 2021; year <= currentYear; year++) { - initYears[year] = true -} +const getCurrentYear = () => new Date().getFullYear() const Settings = ({ setYears, years }) => { return ( @@ -31,7 +26,14 @@ const Settings = ({ setYears, years }) => { } const Main = ({ showMobileSettings, posts }) => { - const [years, setYears] = useState(initYears) + const [years, setYears] = useState(() => { + const currentYear = getCurrentYear() + const initYears = {} + for (let year = 2021; year <= currentYear; year++) { + initYears[year] = true + } + return initYears + }) const settings =