data.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { cache } from "react";
  2. import { ref, getDownloadURL } from "firebase/storage";
  3. import { storage } from "@/firebase/firebase";
  4. import type { Locale } from "@/lib/i18n";
  5. import { getBlogPosts } from "@/lib/notion";
  6. export type HomeData = {
  7. photos: string[];
  8. avatarUrl?: string;
  9. /** The flip side of the avatar. Kept as `dogUrl` for callers; it now points at Yilu, the cat. */
  10. dogUrl?: string;
  11. webagentUrl?: string;
  12. chatbotUrl?: string;
  13. paperUrl?: string;
  14. };
  15. /** Reject if `promise` doesn't settle within `ms` — used so a blocked/expired
  16. * external host can't hang the whole page render forever. */
  17. function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
  18. return new Promise<T>((resolve, reject) => {
  19. const timer = setTimeout(
  20. () => reject(new Error(`Timed out after ${ms}ms`)),
  21. ms,
  22. );
  23. promise.then(
  24. (value) => {
  25. clearTimeout(timer);
  26. resolve(value);
  27. },
  28. (error) => {
  29. clearTimeout(timer);
  30. reject(error);
  31. },
  32. );
  33. });
  34. }
  35. // Firebase can take a very long time to fail (or never fail) when its host is
  36. // unreachable, so cap every call. Locally this also keeps the page usable when
  37. // Firebase credentials aren't configured.
  38. const FIREBASE_TIMEOUT_MS = 4000;
  39. // The owner's photo and the flip side (Yilu, a cat) ship with the site rather
  40. // than living in Firebase, so the avatar works offline and in local dev.
  41. const AVATAR_URL = "/avatar/me.jpg";
  42. const COMPANION_URL = "/avatar/yilu.jpg";
  43. // The photo deck ships with the site too: the owner's own shots, cropped to the
  44. // card's 16:9. Firebase now only serves the project screenshots.
  45. const LOCAL_PHOTOS = [
  46. "/photos/city.webp",
  47. "/photos/cat.webp",
  48. "/photos/game.webp",
  49. "/photos/temple.webp",
  50. "/photos/road.webp",
  51. ];
  52. const EMPTY_HOME: HomeData = {
  53. photos: LOCAL_PHOTOS,
  54. avatarUrl: AVATAR_URL,
  55. dogUrl: COMPANION_URL,
  56. webagentUrl: undefined,
  57. chatbotUrl: undefined,
  58. paperUrl: undefined,
  59. };
  60. // Use React cache to dedupe requests within a single render pass
  61. export const getHomeData = cache(async (): Promise<HomeData> => {
  62. // No Firebase credentials (e.g. local dev without a .env.local): skip the
  63. // network entirely so the page renders instead of hanging on a blocked host.
  64. if (!process.env.NEXT_PUBLIC_FIREBASE_API_KEY) {
  65. return EMPTY_HOME;
  66. }
  67. const allPaths = [
  68. "projects/webagent.jpg",
  69. "projects/chatbot.jpg",
  70. "projects/paper.jpg",
  71. ];
  72. try {
  73. // Parallel data fetching - initiate all requests at once
  74. const urlResults = await Promise.all(
  75. allPaths.map((p) =>
  76. withTimeout(getDownloadURL(ref(storage, p)), FIREBASE_TIMEOUT_MS),
  77. ),
  78. );
  79. return {
  80. photos: LOCAL_PHOTOS,
  81. avatarUrl: AVATAR_URL,
  82. dogUrl: COMPANION_URL,
  83. webagentUrl: urlResults[0],
  84. chatbotUrl: urlResults[1],
  85. paperUrl: urlResults[2],
  86. };
  87. } catch (error) {
  88. console.error("Failed to load home images from Firebase", error);
  89. return EMPTY_HOME;
  90. }
  91. });
  92. /** Latest posts for the home page; the page still renders if Notion is down. */
  93. export const getLatestPosts = cache(async (lang: Locale) => {
  94. try {
  95. const posts = await withTimeout(getBlogPosts(lang), FIREBASE_TIMEOUT_MS);
  96. return posts.map((p) => ({
  97. id: p.id,
  98. title: p.title,
  99. createdAt: p.createdAt ? p.createdAt.toISOString() : null,
  100. }));
  101. } catch (error) {
  102. console.error("Failed to load blog posts", error);
  103. return [];
  104. }
  105. });