From 979e9a2fa96376c54993e9ca2837b3314ae2a519 Mon Sep 17 00:00:00 2001 From: Caleb Denio Date: Mon, 25 Apr 2022 20:19:09 -0400 Subject: [PATCH] Use Jia API for counttoamillion thing (#435) * Use Jia API for counttoamillion thing * COMMAS --- lib/fetcher.js | 2 +- pages/api/channels/count-to-a-million.js | 17 - pages/slack/index.js | 674 ++++++++++++----------- 3 files changed, 342 insertions(+), 351 deletions(-) delete mode 100644 pages/api/channels/count-to-a-million.js diff --git a/lib/fetcher.js b/lib/fetcher.js index 3ce5db62..b3ccdcb4 100644 --- a/lib/fetcher.js +++ b/lib/fetcher.js @@ -5,5 +5,5 @@ export default async function fetcher(...args) { const res = await fetch(...args) - return res.json() + return await res.json() } diff --git a/pages/api/channels/count-to-a-million.js b/pages/api/channels/count-to-a-million.js deleted file mode 100644 index 36cadb7e..00000000 --- a/pages/api/channels/count-to-a-million.js +++ /dev/null @@ -1,17 +0,0 @@ -const withCommas = x => x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') - -// fetches the latest message in #counttoamillion and returns the current count -export const getCount = async (req, res) => { - const history = await fetch( - `https://slack.com/api/conversations.history?token=${process.env.SLACK_LEGACY_TOKEN}&channel=CDJMS683D&limit=1&inclusive=true` - ) - .then(r => r.json()) - .catch(err => console.err(`Error: ${err}`)) - const str = history.messages?.[0].text.split(' ')?.[0].replace(/[^0-9]/g, '') - if (!str) return 'error' - return withCommas(Number(str)) -} - -const getCountEndpoint = (req, res) => getCount().then(v => res.send(v)) - -export default getCountEndpoint diff --git a/pages/slack/index.js b/pages/slack/index.js index 177e01f0..d7b2f3d7 100644 --- a/pages/slack/index.js +++ b/pages/slack/index.js @@ -10,380 +10,388 @@ import Stat from '../../components/stat' import Footer from '../../components/footer' import Header from '../../components/slack/header' import SlackEvents from '../../components/slack/slack-events' -import { getCount } from '../../pages/api/channels/count-to-a-million' import { formatted, thousands } from '../../lib/members' +import useSWR from 'swr' +import fetcher from '../../lib/fetcher' const zoomSlide = keyframes({ from: { backgroundPosition: '-32px bottom' }, to: { backgroundPosition: '32px bottom' } }) -export async function getStaticProps() { - const millionCount = (await getCount()) || null - return { props: { millionCount }, revalidate: 1 } -} +const withCommas = x => x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') -const SlackPage = ({ millionCount }) => ( - <> - - -