"use client"; import { useState, useEffect, Suspense } from "react"; import Link from "next/link"; import { useSearchParams } from "next/navigation"; interface House { id: string; title: string; description: string; price: number; district: string; address: string; phone: string; images: string[]; createdAt: string; } function HomeContent() { const searchParams = useSearchParams(); const [houses, setHouses] = useState([]); const [districts, setDistricts] = useState([]); const [loading, setLoading] = useState(true); const [district, setDistrict] = useState(searchParams.get("district") || "全部"); const [keyword, setKeyword] = useState(searchParams.get("keyword") || ""); useEffect(() => { fetchDistricts(); fetchHouses(); }, [district]); async function fetchDistricts() { try { const res = await fetch("/api/districts"); const data = await res.json(); setDistricts(["全部", ...(data.districts || [])]); } catch (error) { console.error("获取地区失败", error); setDistricts(["全部"]); } } async function fetchHouses() { setLoading(true); try { const params = new URLSearchParams(); if (district !== "全部") params.set("district", district); if (keyword) params.set("keyword", keyword); const res = await fetch(`/api/houses?${params}`); const data = await res.json(); setHouses(data.houses || []); } catch (error) { console.error("获取房屋失败", error); } finally { setLoading(false); } } function handleSearch(e: React.FormEvent) { e.preventDefault(); fetchHouses(); } function formatDate(dateStr: string) { const date = new Date(dateStr); const now = new Date(); const diff = Math.floor((now.getTime() - date.getTime()) / (1000 * 60 * 60 * 24)); if (diff === 0) return "今天"; if (diff === 1) return "昨天"; if (diff < 7) return `${diff}天前`; return `${Math.floor(diff / 7)}周前`; } return (

🏠 城中村租房

setKeyword(e.target.value)} placeholder="搜索区域、小区名称..." className="flex-1 px-3 py-2 rounded-lg border border-gray-200 text-sm" />
{loading ? (
{[1, 2, 3].map((i) => (
))}
) : houses.length === 0 ? (
🏠

暂无房源

看看其他区域吧

) : (
{houses.map((house) => (
{house.images && house.images.length > 0 ? ( {house.title} ) : (
🏠
)} {house.district}

{house.title}

{house.address}

¥{house.price}/月 {formatDate(house.createdAt)}
))}
)}
); } export default function Home() { return (
🏠

加载中...

}>
); }