search, db integration
This commit is contained in:
44
apps/web/src/lib/api/home.ts
Normal file
44
apps/web/src/lib/api/home.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Homepage data — fetches featured plants from the Turso DB.
|
||||
* Uses React's cache() to ensure one fetch per render pass.
|
||||
* Backed by the async fetch for SSR but stays sync in exported interface
|
||||
* via a module-level cache pattern.
|
||||
*/
|
||||
|
||||
import { unstable_cache } from "next/cache";
|
||||
|
||||
// Re-export the type for convenience
|
||||
export type { PlantCardData } from "@/components/PlantCard";
|
||||
|
||||
/**
|
||||
* Get featured plants for the homepage.
|
||||
* Cached via next/cache to avoid repeated DB calls.
|
||||
*/
|
||||
export const getFeaturedPlants = unstable_cache(
|
||||
async () => {
|
||||
const { getBrowsePlants } = await import("./browse");
|
||||
const all = await getBrowsePlants();
|
||||
const FEATURED_IDS = [
|
||||
"tomato",
|
||||
"basil",
|
||||
"rose",
|
||||
"monstera",
|
||||
"snake-plant",
|
||||
"pepper",
|
||||
"apple",
|
||||
"corn",
|
||||
"wheat",
|
||||
"strawberry",
|
||||
"blueberry",
|
||||
"lettuce",
|
||||
];
|
||||
const featured = all.filter((p) => FEATURED_IDS.includes(p.id));
|
||||
if (featured.length < 6) {
|
||||
const rest = all.filter((p) => !FEATURED_IDS.includes(p.id));
|
||||
return [...featured, ...rest].slice(0, 12);
|
||||
}
|
||||
return featured.slice(0, 12);
|
||||
},
|
||||
["featured-plants"],
|
||||
{ revalidate: 3600 },
|
||||
);
|
||||
Reference in New Issue
Block a user