diff options
| author | Pinapelz <yukais@pinapelz.com> | 2026-06-03 09:58:12 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2026-06-03 09:58:12 -0700 |
| commit | 88e2e3d62677f6af58b19d8e5d4d789b96d525db (patch) | |
| tree | 2934d599fa4ef8c830cddf452fbdf7e787b1ca12 | |
| parent | b1c92e09707a423b7b5b01ccb60ba59a91d9f055 (diff) | |
move songs.ts to be entirely server side
| -rw-r--r-- | server/data/songs.ts (renamed from src/data/songs.ts) | 0 | ||||
| -rw-r--r-- | server/index.ts | 2 | ||||
| -rw-r--r-- | src/components/Search/index.tsx | 20 | ||||
| -rw-r--r-- | src/constants/index.ts | 1 | ||||
| -rw-r--r-- | src/helpers/fetchSolution.ts | 2 | ||||
| -rw-r--r-- | src/helpers/fetchSongs.ts | 3 | ||||
| -rw-r--r-- | src/helpers/index.ts | 1 | ||||
| -rw-r--r-- | src/helpers/searchSong.ts | 6 | ||||
| -rw-r--r-- | src/helpers/todaysSolution.ts | 11 | ||||
| -rw-r--r-- | tsconfig.json | 2 |
10 files changed, 25 insertions, 23 deletions
diff --git a/src/data/songs.ts b/server/data/songs.ts index 08e9230..08e9230 100644 --- a/src/data/songs.ts +++ b/server/data/songs.ts diff --git a/server/index.ts b/server/index.ts index 677c736..4e9d1b5 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1,7 +1,7 @@ import express from 'express'; import path from 'path'; import crypto from 'crypto'; -import { songs } from '../src/data/songs'; +import { songs } from './data/songs'; import { startDate } from '../src/constants/startDate'; import cors from 'cors'; diff --git a/src/components/Search/index.tsx b/src/components/Search/index.tsx index c999f97..7f9d502 100644 --- a/src/components/Search/index.tsx +++ b/src/components/Search/index.tsx @@ -15,11 +15,23 @@ export function Search({ currentTry, setSelectedSong }: Props) { const [results, setResults] = React.useState<Song[]>([]); React.useEffect(() => { - if (value) { - setResults(searchSong(value)); - } else if (value === "") { - setResults([]); + let cancelled = false; + + async function runSearch() { + if (!value) { + setResults([]); + return; + } + const songs = await searchSong(value); + + if (!cancelled) { + setResults(songs); + } } + runSearch(); + return () => { + cancelled = true; + }; }, [value]); // clear value on selection diff --git a/src/constants/index.ts b/src/constants/index.ts index 9d6bd5f..fb5d7aa 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1,5 +1,4 @@ export { appName } from "./appName"; export { playTimes } from "./playTimes"; -export { songs } from "../data/songs"; export { startDate } from "./startDate"; export { theme } from "./theme"; diff --git a/src/helpers/fetchSolution.ts b/src/helpers/fetchSolution.ts index d271dd0..5f5e2f3 100644 --- a/src/helpers/fetchSolution.ts +++ b/src/helpers/fetchSolution.ts @@ -1,7 +1,7 @@ import { Song } from "../types/song"; const SALT = process.env.REACT_APP_HEARDLE_SALT ?? 'changeme'; -const API_URL = process.env.REACT_APP_HEARDLE_API_URL ?? 'http://23:432e3001'; +const API_URL = process.env.REACT_APP_HEARDLE_API_URL ?? 'https://127.0.0.1:3001'; function hexToBytes(hex: string): Uint8Array { const bytes = new Uint8Array(hex.length / 2); diff --git a/src/helpers/fetchSongs.ts b/src/helpers/fetchSongs.ts index 5ae0d83..b403a52 100644 --- a/src/helpers/fetchSongs.ts +++ b/src/helpers/fetchSongs.ts @@ -6,12 +6,13 @@ function fuzzyMatch(input: string): string { export async function fetchSongs(useCache=true): Promise<Song[]> { + const API_URL = process.env.REACT_APP_HEARDLE_API_URL || "http://localhost:3001"; if (useCache && cachedSongs) { return cachedSongs; } try { - const response = await fetch('/songs'); + const response = await fetch(`${API_URL}/songs`); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } diff --git a/src/helpers/index.ts b/src/helpers/index.ts index 5593bc5..f8513ac 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -1,3 +1,2 @@ export { scoreToEmoji } from "./scoreToEmoji"; export { searchSong } from "./searchSong"; -export { todaysSolution } from "./todaysSolution"; diff --git a/src/helpers/searchSong.ts b/src/helpers/searchSong.ts index 13fa794..9ce36cf 100644 --- a/src/helpers/searchSong.ts +++ b/src/helpers/searchSong.ts @@ -1,12 +1,14 @@ -import { songs } from "../constants"; +import { fetchSongs } from "./fetchSongs"; import { Song } from "../types/song"; -export function searchSong(searchTerm: string): Song[] { +export async function searchSong(searchTerm: string): Promise<Song[]> { function fuzzyMatch(input: string){ return input.toLowerCase().replace(/[^0-9a-z ]/gi, ''); } searchTerm = fuzzyMatch(searchTerm); + const songs = await fetchSongs(); + return songs .filter((song: Song) => { const songName = fuzzyMatch(song.name); diff --git a/src/helpers/todaysSolution.ts b/src/helpers/todaysSolution.ts deleted file mode 100644 index 32760cb..0000000 --- a/src/helpers/todaysSolution.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { songs, startDate } from "../constants"; - -// const epochMs = new Date(2022, 3, 10).valueOf(); -// const now = Date.now(); -// const index = Math.floor((now - epochMs) / msInDay); - -const msInDay = 86400000; -const todaysDate = new Date(); -const index = Math.floor((todaysDate.getTime() - startDate.getTime() )/msInDay) - -export const todaysSolution = songs[index % songs.length]; diff --git a/tsconfig.json b/tsconfig.json index a273b0c..67c0712 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,5 +22,5 @@ }, "include": [ "src" - ] +, "server/data", "server/songs.ts" ] } |
