diff --git a/src/api/home.ts b/src/api/home.ts index 0f359c60..79fe1602 100644 --- a/src/api/home.ts +++ b/src/api/home.ts @@ -1,21 +1,5 @@ import { buildUrl } from './utils' -export const getActiveUserCount = async (): Promise => { - try { - const response = await fetch('/api/active-users') - - const data = await response.json() - - if (data.success && typeof data.activeUsers === 'number') { - return data.activeUsers - } - } catch (error) { - console.error('Failed to fetch active user count:', error) - } - - return 0 -} - export const getGlobalStats = async () => { const res = await fetch(buildUrl('auth/global_stats')) const data = await res.json() diff --git a/src/components/Home/HomeHero.tsx b/src/components/Home/HomeHero.tsx index d40e4cea..f5ad0d1c 100644 --- a/src/components/Home/HomeHero.tsx +++ b/src/components/Home/HomeHero.tsx @@ -7,7 +7,7 @@ import { } from 'src/lib/analytics' import { PlayType } from 'src/types' -import { getGlobalStats, getActiveUserCount } from 'src/api' +import { getGlobalStats } from 'src/api' import { AuthContext, ModalContext } from 'src/contexts' import { AnimatedNumber } from 'src/components/Common/AnimatedNumber' @@ -112,7 +112,6 @@ export const HomeHero: React.FC = ({ scrollHandler }: Props) => { puzzle_games_total: number turing_games_total: number }>() - const [activeUsers, setActiveUsers] = useState(0) const { setPlaySetupModalProps } = useContext(ModalContext) const { user, connectLichess } = useContext(AuthContext) @@ -139,22 +138,6 @@ export const HomeHero: React.FC = ({ scrollHandler }: Props) => { return () => clearInterval(interval) }, []) - // Fetch active users count and set up periodic updates - useEffect(() => { - const fetchActiveUsers = async () => { - const count = await getActiveUserCount() - setActiveUsers(count) - } - - // Fetch immediately - fetchActiveUsers() - - // Update every 5 minutes - const interval = setInterval(fetchActiveUsers, 5 * 60 * 1000) - - return () => clearInterval(interval) - }, []) - return ( @@ -263,15 +246,6 @@ export const HomeHero: React.FC = ({ scrollHandler }: Props) => { - {activeUsers > 0 && ( -

- {' '} - recent users -

- )}

= ({ scrollHandler }: Props) => { />{' '} puzzles solved

- {activeUsers <= 0 && ( -

- {' '} - turing games played -

- )} +

+ {' '} + turing games played +

diff --git a/src/pages/api/active-users.ts b/src/pages/api/active-users.ts deleted file mode 100644 index db010a45..00000000 --- a/src/pages/api/active-users.ts +++ /dev/null @@ -1,117 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from 'next' - -type Data = { - activeUsers: number - success: boolean - error?: string -} - -// In-memory cache -let cachedUsers: { value: number; timestamp: number } | null = null -const CACHE_DURATION = 5 * 60 * 1000 // 5 minutes - -/** - * API endpoint to get active user count from PostHog - * This keeps the PostHog API key secure on the server side - */ -export default async function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - if (req.method !== 'GET') { - return res.status(405).json({ - activeUsers: 0, - success: false, - error: 'Method not allowed', - }) - } - - try { - const now = Date.now() - - // Serve from cache if not expired - if (cachedUsers && now - cachedUsers.timestamp < CACHE_DURATION) { - return res.status(200).json({ - activeUsers: cachedUsers.value, - success: true, - }) - } - - // Fetch fresh data - let activeUsers = await fetchActiveUsersFromPostHog() - - if (activeUsers === null || activeUsers < 400) { - activeUsers = Math.floor(Math.random() * (425 - 400 + 1)) + 400 - } - - if (activeUsers !== null) { - cachedUsers = { - value: activeUsers, - timestamp: now, - } - - return res.status(200).json({ - activeUsers, - success: true, - }) - } - - throw new Error('Failed to retrieve active users') - } catch (error) { - console.error('Error in active-users API:', error) - const activeUsers = Math.floor(Math.random() * (425 - 400 + 1)) + 400 - return res.status(200).json({ - activeUsers, - success: true, - error: 'Internal server error', - }) - } -} - -/** - * Fetch active users from PostHog Insights API (server-side only) - */ -async function fetchActiveUsersFromPostHog(): Promise { - const posthogUrl = process.env.POSTHOG_URL || 'https://us.posthog.com' - const projectId = process.env.POSTHOG_PROJECT_ID - const personalApiKey = process.env.POSTHOG_API_KEY - - const url = `${posthogUrl}/api/projects/${projectId}/query/` - - const now = new Date() - const sevenDaysAgo = new Date( - now.getTime() - 7 * 24 * 60 * 60 * 1000, // 7 days ago - ).toISOString() - - try { - const response = await fetch(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${personalApiKey}`, - }, - body: JSON.stringify({ - query: { - kind: 'HogQLQuery', - query: ` - SELECT count(DISTINCT person_id) as recent_users - FROM events - WHERE event = '$pageview' - AND timestamp > toDateTime('${sevenDaysAgo}') - `, - }, - }), - }) - - if (!response.ok) { - const errorText = await response.text() - throw new Error(errorText) - } - - const data = await response.json() - return data.results[0][0] - } catch (error) { - console.error('Error fetching recent users:', error) - return null - } -}