58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import Game from '@/components/Game';
|
|
|
|
async function getDailyPuzzle() {
|
|
try {
|
|
// In a real app, use absolute URL or internal API call
|
|
// Since we are server-side, we can call the DB directly or fetch the API
|
|
// Calling API requires full URL (http://localhost:3000...), which is tricky in some envs
|
|
// Better to call DB directly here or use a helper function shared with the API
|
|
// But for simplicity, let's fetch the API if we can, or just use Prisma directly.
|
|
// Using Prisma directly is better for Server Components.
|
|
|
|
const { PrismaClient } = await import('@prisma/client');
|
|
const prisma = new PrismaClient();
|
|
|
|
const today = new Date().toISOString().split('T')[0];
|
|
let dailyPuzzle = await prisma.dailyPuzzle.findUnique({
|
|
where: { date: today },
|
|
include: { song: true },
|
|
});
|
|
|
|
if (!dailyPuzzle) {
|
|
// Trigger generation logic (same as API)
|
|
// Duplicating logic is bad, but for now it's quickest.
|
|
// Ideally extract "getOrCreateDailyPuzzle" to a lib.
|
|
const songsCount = await prisma.song.count();
|
|
if (songsCount > 0) {
|
|
const skip = Math.floor(Math.random() * songsCount);
|
|
const randomSong = await prisma.song.findFirst({ skip });
|
|
if (randomSong) {
|
|
dailyPuzzle = await prisma.dailyPuzzle.create({
|
|
data: { date: today, songId: randomSong.id },
|
|
include: { song: true },
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!dailyPuzzle) return null;
|
|
|
|
return {
|
|
id: dailyPuzzle.id,
|
|
audioUrl: `/uploads/${dailyPuzzle.song.filename}`,
|
|
songId: dailyPuzzle.songId
|
|
};
|
|
} catch (e) {
|
|
console.error(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export default async function Home() {
|
|
const dailyPuzzle = await getDailyPuzzle();
|
|
|
|
return (
|
|
<Game dailyPuzzle={dailyPuzzle} />
|
|
);
|
|
}
|