| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 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<T>(promise: Promise<T>, ms: number): Promise<T> {
- return new Promise<T>((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<HomeData> => {
- // 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 [];
- }
- });
|