From 01bdbf76024f51bbc1072e33ceffb0c59ae45032 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Wed, 3 Jun 2026 10:08:41 -0700 Subject: move startDate to backend --- server/data/startDate.ts | 1 + server/index.ts | 6 ++- src/components/Result/index.tsx | 88 ++++++++++++++++++++++++++--------------- src/constants/index.ts | 1 - src/constants/startDate.ts | 1 - src/helpers/fetchInfo.ts | 9 +++++ src/helpers/scoreToEmoji.ts | 7 +++- src/types/info.ts | 3 ++ tsconfig.server.json | 4 +- 9 files changed, 82 insertions(+), 38 deletions(-) create mode 100644 server/data/startDate.ts delete mode 100644 src/constants/startDate.ts create mode 100644 src/helpers/fetchInfo.ts create mode 100644 src/types/info.ts diff --git a/server/data/startDate.ts b/server/data/startDate.ts new file mode 100644 index 0000000..bb49bfe --- /dev/null +++ b/server/data/startDate.ts @@ -0,0 +1 @@ +export const startDate = new Date('6/3/2026'); diff --git a/server/index.ts b/server/index.ts index 4e9d1b5..d347e8f 100644 --- a/server/index.ts +++ b/server/index.ts @@ -2,7 +2,7 @@ import express from 'express'; import path from 'path'; import crypto from 'crypto'; import { songs } from './data/songs'; -import { startDate } from '../src/constants/startDate'; +import { startDate } from './data/startDate'; import cors from 'cors'; @@ -41,6 +41,10 @@ app.get('/songs', (_req, res) => { res.json(songs.map(({ artist, name }) => ({ artist, name }))); }); +app.get('/info', (_req, res) => { + res.json({ startDate: startDate.toISOString() }); +}); + if (process.env.NODE_ENV === 'production') { app.use(express.static(path.join(__dirname, '../build'))); app.get('*', (_req, res) => { diff --git a/src/components/Result/index.tsx b/src/components/Result/index.tsx index 259525b..706ddca 100644 --- a/src/components/Result/index.tsx +++ b/src/components/Result/index.tsx @@ -26,11 +26,13 @@ function Solution({ Today's song is {todaysSolution.artist} - {todaysSolution.name} - {didGuess && + + {didGuess && ( You guessed it in {currentTry} {currentTry === 1 ? 'try' : 'tries'}. - } + )} + ); @@ -41,33 +43,42 @@ interface ShareButtonProps { variant?: keyof typeof theme; } -function ShareButton({ - guesses, - variant -}:ShareButtonProps) { - const result = scoreToEmoji(guesses); +function ShareButton({ guesses, variant }: ShareButtonProps) { const [buttonText, setButtonText] = useState('Share Results'); - const handleClick = React.useCallback(() => { - // The Windows share sheet is dumb and doesn't have a copy function. + const [result, setResult] = useState(''); + + React.useEffect(() => { + let cancelled = false; + + scoreToEmoji(guesses).then((text) => { + if (!cancelled) setResult(text); + }); + + return () => { + cancelled = true; + }; + }, [guesses]); + + const handleClick = React.useCallback(async () => { const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE']; - const onWindows = windowsPlatforms.indexOf(window.navigator.platform) !== -1; + const onWindows = + windowsPlatforms.indexOf(window.navigator.platform) !== -1; + if (navigator.share !== undefined && !onWindows) { - navigator.share({text: result}) + await navigator.share({ text: result }); } else if (navigator.clipboard !== undefined) { - navigator.clipboard.writeText(result) + await navigator.clipboard.writeText(result); setButtonText('Copied!'); } else { setButtonText('Failed to open share menu or copy'); } - }, [guesses]); + }, [result]); return ( - <> - - - ) + + ); } interface Props { @@ -93,26 +104,41 @@ export function Result({ if (didGuess) { const textForTry = ["Perfect!", "Wow!", "Super!", "Congrats!", "Nice!"]; + return ( <> {textForTry[currentTry - 1]} - + + + + Remember to come back in {hoursToNextDay} hours! ); - } else { - return ( - <> - Unfortunately, thats wrong. - - - - Try again in {hoursToNextDay} hours. - - - ); } + + return ( + <> + Unfortunately, thats wrong. + + + + + + + Try again in {hoursToNextDay} hours. + + + ); } diff --git a/src/constants/index.ts b/src/constants/index.ts index fb5d7aa..75c49ec 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1,4 +1,3 @@ export { appName } from "./appName"; export { playTimes } from "./playTimes"; -export { startDate } from "./startDate"; export { theme } from "./theme"; diff --git a/src/constants/startDate.ts b/src/constants/startDate.ts deleted file mode 100644 index bb49bfe..0000000 --- a/src/constants/startDate.ts +++ /dev/null @@ -1 +0,0 @@ -export const startDate = new Date('6/3/2026'); diff --git a/src/helpers/fetchInfo.ts b/src/helpers/fetchInfo.ts new file mode 100644 index 0000000..248cd98 --- /dev/null +++ b/src/helpers/fetchInfo.ts @@ -0,0 +1,9 @@ + +import { Info } from "../types/info"; + +export const fetchInfo = async (): Promise => { + const API_URL = process.env.REACT_APP_API_URL || "https://localhost:3000"; + const response = await fetch(`${API_URL}/info`); + const data = await response.json(); + return data as Info; +}; diff --git a/src/helpers/scoreToEmoji.ts b/src/helpers/scoreToEmoji.ts index cb525a8..5af1026 100644 --- a/src/helpers/scoreToEmoji.ts +++ b/src/helpers/scoreToEmoji.ts @@ -1,9 +1,12 @@ import { GuessType, GuessState } from "../types/guess"; -import { appName, startDate } from "../constants"; +import { appName } from "../constants"; +import { fetchInfo } from "./fetchInfo"; -export function scoreToEmoji(guesses: GuessType[]): string { +export async function scoreToEmoji(guesses: GuessType[]): Promise { const msInDay = 86400000; const todaysDate = new Date(); + const info = await fetchInfo(); + const startDate = new Date(info.startDate); const index = Math.floor((todaysDate.getTime() - startDate.getTime() )/msInDay) + 1 const emojis = { incorrect: "🟥", diff --git a/src/types/info.ts b/src/types/info.ts new file mode 100644 index 0000000..e570325 --- /dev/null +++ b/src/types/info.ts @@ -0,0 +1,3 @@ +export interface Info { + startDate: string; +} diff --git a/tsconfig.server.json b/tsconfig.server.json index bc2f225..5d8c93b 100644 --- a/tsconfig.server.json +++ b/tsconfig.server.json @@ -5,7 +5,7 @@ "outDir": "dist/server", "rootDir": "server", "esModuleInterop": true, - "strict": true + "strict": true, }, - "include": ["server", "src/constants"] + "include": ["server"] } -- cgit v1.2.3