celo-poll/app/api/og/route.tsx
2025-12-08 16:31:30 +03:00

366 lines
9.5 KiB
TypeScript

import { ImageResponse } from '@vercel/og'
import { NextRequest } from 'next/server'
import { getPollById } from '@/lib/db/polls'
import { formatCUSD, formatNumber } from '@/lib/format'
import { getCurrentNetwork } from '@/lib/celo'
export const runtime = 'edge'
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url)
const pollId = searchParams.get('pollId')
const mode = searchParams.get('mode') || 'poll' // 'poll', 'home', 'success'
if (!pollId && mode === 'poll') {
return new Response('Missing pollId', { status: 400 })
}
const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
const network = getCurrentNetwork()
if (mode === 'home') {
return new ImageResponse(
<HomePageOG />,
{ width: 1200, height: 630 }
)
}
const poll = pollId ? await getPollById(pollId) : null
if (!poll) {
return new ImageResponse(
<ErrorPageOG />,
{ width: 1200, height: 630 }
)
}
return new ImageResponse(
<PollOG poll={poll} appUrl={appUrl} network={network.name} />,
{ width: 1200, height: 630 }
)
} catch (error) {
console.error('Failed to generate OG image:', error)
return new Response('Failed to generate image', { status: 500 })
}
}
// Home Page OG Image
function HomePageOG() {
return (
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
background: 'linear-gradient(135deg, #35D07F 0%, #FBCC5C 100%)',
padding: '40px',
}}
>
{/* Header */}
<div
style={{
display: 'flex',
alignItems: 'center',
marginBottom: '40px',
}}
>
<div style={{ fontSize: 100, marginRight: '30px' }}>📊</div>
<div>
<div
style={{
fontSize: 72,
fontWeight: 'bold',
color: 'white',
marginBottom: '10px',
}}
>
PollCelo
</div>
<div style={{ fontSize: 32, color: 'white/90' }}>
Daily Prediction Markets
</div>
</div>
</div>
{/* Main Content */}
<div
style={{
background: 'white',
borderRadius: '32px',
padding: '60px',
width: '90%',
maxWidth: '1000px',
boxShadow: '0 25px 80px rgba(0, 0, 0, 0.4)',
textAlign: 'center',
}}
>
<div style={{ fontSize: 48, fontWeight: 'bold', color: '#2E3338', marginBottom: '30px' }}>
Vote with cUSD & Win Rewards!
</div>
<div style={{ fontSize: 24, color: '#666', marginBottom: '40px' }}>
🏆 95% of pot goes to winners • 💰 Vote with 0.01-0.5 cUSD • ⚡ Almost zero fees
</div>
<div style={{ fontSize: 28, fontWeight: 'bold', color: '#35D07F' }}>
Powered by Celo Network
</div>
</div>
</div>
)
}
// Poll OG Image Component
function PollOG({ poll, appUrl, network }: any) {
const totalVotes = poll.options.reduce((sum: number, opt: any) => sum + opt.votes, 0)
const timeUntilClose = poll.endTime - Date.now()
const hoursUntilClose = Math.max(0, Math.floor(timeUntilClose / (1000 * 60 * 60)))
const statusText = poll.status === 'active'
? `🟢 Live - ${hoursUntilClose}h left`
: '🔴 Closed'
return (
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
background: 'linear-gradient(135deg, #35D07F 0%, #FBCC5C 100%)',
padding: '40px',
}}
>
{/* Header */}
<div
style={{
display: 'flex',
alignItems: 'center',
marginBottom: '30px',
}}
>
<div style={{ fontSize: 80, marginRight: '20px' }}>📊</div>
<div>
<div
style={{
fontSize: 56,
fontWeight: 'bold',
color: 'white',
}}
>
PollCelo
</div>
<div style={{ fontSize: 24, color: 'white/80' }}>
{network}
</div>
</div>
</div>
{/* Poll Card */}
<div
style={{
background: 'white',
borderRadius: '24px',
padding: '40px',
width: '90%',
maxWidth: '900px',
boxShadow: '0 20px 60px rgba(0, 0, 0, 0.3)',
}}
>
{/* Poll Header */}
<div
style={{
display: 'flex',
alignItems: 'center',
marginBottom: '30px',
}}
>
<div style={{ fontSize: 64, marginRight: '20px' }}>{poll.emoji}</div>
<div style={{ flex: 1 }}>
<div
style={{
fontSize: 32,
fontWeight: 'bold',
color: '#2E3338',
lineHeight: 1.2,
marginBottom: '8px',
}}
>
{poll.question}
</div>
<div
style={{
fontSize: 18,
color: '#666',
lineHeight: 1.3,
}}
>
{poll.description}
</div>
</div>
</div>
{/* Stats Row */}
<div
style={{
display: 'grid',
gridTemplateColumns: '1fr 1fr 1fr',
gap: '20px',
marginBottom: '30px',
padding: '20px',
background: '#f8f9fa',
borderRadius: '16px',
}}
>
<StatBox
label="Total Pot"
value={formatCUSD(poll.totalPot)}
color="#35D07F"
/>
<StatBox
label="Votes"
value={formatNumber(totalVotes)}
color="#2E3338"
/>
<StatBox
label="Status"
value={statusText}
color={poll.status === 'active' ? '#35D07F' : '#dc3545'}
/>
</div>
{/* Top Options */}
<div style={{ display: 'flex', flexDirection: 'column', gap: '12px' }}>
{poll.options.slice(0, 3).map((option: any, index: number) => (
<OptionPreview key={option.id} option={option} index={index} />
))}
</div>
{/* CTA */}
<div
style={{
marginTop: '30px',
padding: '20px',
background: 'linear-gradient(135deg, #35D07F, #FBCC5C)',
borderRadius: '16px',
textAlign: 'center',
}}
>
<div
style={{
fontSize: 24,
fontWeight: 'bold',
color: 'white',
}}
>
{poll.status === 'active' ? '🗳️ Vote Now & Win!' : '📊 View Results'}
</div>
<div style={{ fontSize: 16, color: 'white/90', marginTop: '8px' }}>
{poll.status === 'active'
? '95% of pot goes to winners'
: `Winner: ${getWinningOption(poll)?.text || 'TBD'}`
}
</div>
</div>
</div>
</div>
)
}
// Stat Box Component
function StatBox({ label, value, color }: any) {
return (
<div style={{ textAlign: 'center' }}>
<div style={{ fontSize: 16, color: '#666', marginBottom: '4px' }}>
{label}
</div>
<div style={{ fontSize: 28, fontWeight: 'bold', color }}>
{value}
</div>
</div>
)
}
// Option Preview Component
function OptionPreview({ option, index }: any) {
return (
<div
style={{
display: 'flex',
alignItems: 'center',
padding: '16px 20px',
background: '#f9f9f9',
borderRadius: '12px',
border: '1px solid #e9ecef',
}}
>
<div style={{ fontSize: 32, marginRight: '15px' }}>{option.emoji}</div>
<div style={{ flex: 1 }}>
<div style={{ fontSize: 20, fontWeight: '600', color: '#2E3338' }}>
{option.text}
</div>
<div style={{ fontSize: 14, color: '#666' }}>
{option.votes} votes • {formatCUSD(option.totalAmount)}
</div>
</div>
{index === 0 && (
<div style={{
fontSize: 16,
color: '#35D07F',
fontWeight: 'bold',
padding: '4px 8px',
background: '#d4edda',
borderRadius: '8px'
}}>
Leader
</div>
)}
</div>
)
}
// Error Page OG Image
function ErrorPageOG() {
return (
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
background: 'linear-gradient(135deg, #dc3545 0%, #fd7e14 100%)',
padding: '40px',
}}
>
<div style={{ fontSize: 100, marginBottom: '30px' }}>😕</div>
<div
style={{
fontSize: 48,
fontWeight: 'bold',
color: 'white',
textAlign: 'center',
marginBottom: '20px',
}}
>
Poll Not Found
</div>
<div style={{ fontSize: 24, color: 'white/90', textAlign: 'center' }}>
The poll you&apos;re looking for doesn&apos;t exist or has been removed.
</div>
</div>
)
}
// Helper function
function getWinningOption(poll: any) {
if (!poll.correctOptionId) return null
return poll.options.find((opt: any) => opt.id === poll.correctOptionId)
}