import { cache } from "react"; import { ref, getDownloadURL } from "firebase/storage"; import { storage } from "@/firebase/firebase"; import type { Locale } from "@/lib/i18n"; import { getBlogPosts } from "@/lib/notion"; export type HomeData = { photos: string[]; avatarUrl?: string; /** The flip side of the avatar. Kept as `dogUrl` for callers; it now points at Yilu, the cat. */ dogUrl?: string; webagentUrl?: string; chatbotUrl?: string; paperUrl?: string; }; /** Reject if `promise` doesn't settle within `ms` — used so a blocked/expired * external host can't hang the whole page render forever. */ function withTimeout(promise: Promise, ms: number): Promise { return new Promise((resolve, reject) => { const timer = setTimeout( () => reject(new Error(`Timed out after ${ms}ms`)), ms, ); promise.then( (value) => { clearTimeout(timer); resolve(value); }, (error) => { clearTimeout(timer); reject(error); }, ); }); } // Firebase can take a very long time to fail (or never fail) when its host is // unreachable, so cap every call. Locally this also keeps the page usable when // Firebase credentials aren't configured. const FIREBASE_TIMEOUT_MS = 4000; // The owner's photo and the flip side (Yilu, a cat) ship with the site rather // than living in Firebase, so the avatar works offline and in local dev. const AVATAR_URL = "/avatar/me.jpg"; const COMPANION_URL = "/avatar/yilu.jpg"; // The photo deck ships with the site too: the owner's own shots, cropped to the // card's 16:9. Firebase now only serves the project screenshots. const LOCAL_PHOTOS = [ "/photos/city.webp", "/photos/cat.webp", "/photos/game.webp", "/photos/temple.webp", "/photos/road.webp", ]; const EMPTY_HOME: HomeData = { photos: LOCAL_PHOTOS, avatarUrl: AVATAR_URL, dogUrl: COMPANION_URL, webagentUrl: undefined, chatbotUrl: undefined, paperUrl: undefined, }; // Use React cache to dedupe requests within a single render pass export const getHomeData = cache(async (): Promise => { // No Firebase credentials (e.g. local dev without a .env.local): skip the // network entirely so the page renders instead of hanging on a blocked host. if (!process.env.NEXT_PUBLIC_FIREBASE_API_KEY) { return EMPTY_HOME; } const allPaths = [ "projects/webagent.jpg", "projects/chatbot.jpg", "projects/paper.jpg", ]; try { // Parallel data fetching - initiate all requests at once const urlResults = await Promise.all( allPaths.map((p) => withTimeout(getDownloadURL(ref(storage, p)), FIREBASE_TIMEOUT_MS), ), ); return { photos: LOCAL_PHOTOS, avatarUrl: AVATAR_URL, dogUrl: COMPANION_URL, webagentUrl: urlResults[0], chatbotUrl: urlResults[1], paperUrl: urlResults[2], }; } catch (error) { console.error("Failed to load home images from Firebase", error); return EMPTY_HOME; } }); /** Latest posts for the home page; the page still renders if Notion is down. */ export const getLatestPosts = cache(async (lang: Locale) => { try { const posts = await withTimeout(getBlogPosts(lang), FIREBASE_TIMEOUT_MS); return posts.map((p) => ({ id: p.id, title: p.title, createdAt: p.createdAt ? p.createdAt.toISOString() : null, })); } catch (error) { console.error("Failed to load blog posts", error); return []; } });