"use client"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { GoogleLogin } from "@react-oauth/google"; import { api } from "@/lib/api"; import type { Restaurant, Channel, Review } from "@/lib/api"; import { useAuth } from "@/lib/auth-context"; import MapView, { MapBounds, FlyTo } from "@/components/MapView"; import SearchBar from "@/components/SearchBar"; import RestaurantList from "@/components/RestaurantList"; import RestaurantDetail from "@/components/RestaurantDetail"; import MyReviewsList from "@/components/MyReviewsList"; import BottomSheet from "@/components/BottomSheet"; import { getCuisineIcon } from "@/lib/cuisine-icons"; const CUISINE_GROUPS: { label: string; prefix: string }[] = [ { label: "한식", prefix: "한식" }, { label: "일식", prefix: "일식" }, { label: "중식", prefix: "중식" }, { label: "양식", prefix: "양식" }, { label: "아시아", prefix: "아시아" }, { label: "기타", prefix: "기타" }, ]; function matchCuisineGroup(cuisineType: string | null, group: string): boolean { if (!cuisineType) return false; const g = CUISINE_GROUPS.find((g) => g.label === group); if (!g) return false; return cuisineType.startsWith(g.prefix); } const PRICE_GROUPS: { label: string; test: (p: string) => boolean }[] = [ { label: "저렴 (~1만원)", test: (p) => /저렴|가성비|착한|만원 이하|[3-9]천원|^\d[,.]?\d*천원/.test(p) || /^[1]만원대$/.test(p) || /^[5-9],?\d{3}원/.test(p), }, { label: "보통 (1~3만원)", test: (p) => /[1-2]만원대|1-[23]만|인당 [12]\d?,?\d*원|1[2-9],?\d{3}원|2[0-9],?\d{3}원/.test(p), }, { label: "고가 (3만원~)", test: (p) => /[3-9]만원|고가|높은|묵직|살벌|10만원|5만원|4만원|6만원/.test(p), }, ]; function matchPriceGroup(priceRange: string | null, group: string): boolean { if (!priceRange) return false; const g = PRICE_GROUPS.find((g) => g.label === group); if (!g) return false; return g.test(priceRange); } /** Parse pipe-delimited region "나라|시|구" into parts. */ function parseRegion(region: string | null): { country: string; city: string; district: string } | null { if (!region) return null; const parts = region.split("|"); return { country: parts[0] || "", city: parts[1] || "", district: parts[2] || "", }; } /** Build 3-level tree: country → city → district[] */ function buildRegionTree(restaurants: Restaurant[]) { const tree = new Map>>(); for (const r of restaurants) { const p = parseRegion(r.region); if (!p || !p.country) continue; if (!tree.has(p.country)) tree.set(p.country, new Map()); const cityMap = tree.get(p.country)!; if (p.city) { if (!cityMap.has(p.city)) cityMap.set(p.city, new Set()); if (p.district) cityMap.get(p.city)!.add(p.district); } } return tree; } /** Compute centroid + appropriate zoom from a set of restaurants. */ function computeFlyTo(rests: Restaurant[]): FlyTo | null { if (rests.length === 0) return null; const lat = rests.reduce((s, r) => s + r.latitude, 0) / rests.length; const lng = rests.reduce((s, r) => s + r.longitude, 0) / rests.length; // Pick zoom based on geographic spread const latSpread = Math.max(...rests.map((r) => r.latitude)) - Math.min(...rests.map((r) => r.latitude)); const lngSpread = Math.max(...rests.map((r) => r.longitude)) - Math.min(...rests.map((r) => r.longitude)); const spread = Math.max(latSpread, lngSpread); let zoom = 13; if (spread > 2) zoom = 8; else if (spread > 1) zoom = 9; else if (spread > 0.5) zoom = 10; else if (spread > 0.2) zoom = 11; else if (spread > 0.1) zoom = 12; else if (spread > 0.02) zoom = 14; else zoom = 15; return { lat, lng, zoom }; } /** Find best matching country + city from user's coordinates using restaurant data. */ function findRegionFromCoords( lat: number, lng: number, restaurants: Restaurant[], ): { country: string; city: string } | null { // Group restaurants by country|city and compute centroids const groups = new Map(); for (const r of restaurants) { const p = parseRegion(r.region); if (!p || !p.country || !p.city) continue; const key = `${p.country}|${p.city}`; if (!groups.has(key)) groups.set(key, { country: p.country, city: p.city, lats: [], lngs: [] }); const g = groups.get(key)!; g.lats.push(r.latitude); g.lngs.push(r.longitude); } let best: { country: string; city: string } | null = null; let bestDist = Infinity; for (const g of groups.values()) { const cLat = g.lats.reduce((a, b) => a + b, 0) / g.lats.length; const cLng = g.lngs.reduce((a, b) => a + b, 0) / g.lngs.length; const dist = (cLat - lat) ** 2 + (cLng - lng) ** 2; if (dist < bestDist) { bestDist = dist; best = { country: g.country, city: g.city }; } } return best; } export default function Home() { const { user, login, logout, isLoading: authLoading } = useAuth(); const [restaurants, setRestaurants] = useState([]); const [selected, setSelected] = useState(null); const [loading, setLoading] = useState(true); const [showDetail, setShowDetail] = useState(false); const [channels, setChannels] = useState([]); const [channelFilter, setChannelFilter] = useState(""); const [cuisineFilter, setCuisineFilter] = useState(""); const [priceFilter, setPriceFilter] = useState(""); const [viewMode, setViewMode] = useState<"map" | "list">("list"); const [showMobileFilters, setShowMobileFilters] = useState(false); const [mapBounds, setMapBounds] = useState(null); const [boundsFilterOn, setBoundsFilterOn] = useState(false); const [countryFilter, setCountryFilter] = useState(""); const [cityFilter, setCityFilter] = useState(""); const [districtFilter, setDistrictFilter] = useState(""); const [regionFlyTo, setRegionFlyTo] = useState(null); const [showFavorites, setShowFavorites] = useState(false); const [showMyReviews, setShowMyReviews] = useState(false); const [myReviews, setMyReviews] = useState<(Review & { restaurant_id: string; restaurant_name: string | null })[]>([]); const [visits, setVisits] = useState<{ today: number; total: number } | null>(null); const geoApplied = useRef(false); const regionTree = useMemo(() => buildRegionTree(restaurants), [restaurants]); const countries = useMemo(() => [...regionTree.keys()].sort(), [regionTree]); const cities = useMemo(() => { if (!countryFilter) return []; const cityMap = regionTree.get(countryFilter); return cityMap ? [...cityMap.keys()].sort() : []; }, [regionTree, countryFilter]); const districts = useMemo(() => { if (!countryFilter || !cityFilter) return []; const cityMap = regionTree.get(countryFilter); if (!cityMap) return []; const set = cityMap.get(cityFilter); return set ? [...set].sort() : []; }, [regionTree, countryFilter, cityFilter]); const filteredRestaurants = useMemo(() => { return restaurants.filter((r) => { if (channelFilter && !(r.channels || []).includes(channelFilter)) return false; if (cuisineFilter && !matchCuisineGroup(r.cuisine_type, cuisineFilter)) return false; if (priceFilter && !matchPriceGroup(r.price_range, priceFilter)) return false; if (countryFilter) { const parsed = parseRegion(r.region); if (!parsed || parsed.country !== countryFilter) return false; if (cityFilter && parsed.city !== cityFilter) return false; if (districtFilter && parsed.district !== districtFilter) return false; } if (boundsFilterOn && mapBounds) { if (r.latitude < mapBounds.south || r.latitude > mapBounds.north) return false; if (r.longitude < mapBounds.west || r.longitude > mapBounds.east) return false; } return true; }); }, [restaurants, channelFilter, cuisineFilter, priceFilter, countryFilter, cityFilter, districtFilter, boundsFilterOn, mapBounds]); // Set desktop default to map mode on mount useEffect(() => { if (window.innerWidth >= 768) setViewMode("map"); }, []); // Load channels + record visit on mount useEffect(() => { api.getChannels().then(setChannels).catch(console.error); api.recordVisit().then(() => api.getVisits().then(setVisits)).catch(console.error); }, []); // Load restaurants on mount and when channel filter changes useEffect(() => { setLoading(true); api .getRestaurants({ limit: 500, channel: channelFilter || undefined }) .then(setRestaurants) .catch(console.error) .finally(() => setLoading(false)); }, [channelFilter]); // Auto-select region from user's geolocation (once) useEffect(() => { if (geoApplied.current || restaurants.length === 0) return; if (!navigator.geolocation) return; navigator.geolocation.getCurrentPosition( (pos) => { if (geoApplied.current) return; geoApplied.current = true; const match = findRegionFromCoords(pos.coords.latitude, pos.coords.longitude, restaurants); if (match) { setCountryFilter(match.country); setCityFilter(match.city); const matched = restaurants.filter((r) => { const p = parseRegion(r.region); return p && p.country === match.country && p.city === match.city; }); setRegionFlyTo(computeFlyTo(matched)); } }, () => { /* user denied or error — do nothing */ }, { timeout: 5000 }, ); }, [restaurants]); const handleSearch = useCallback( async (query: string, mode: "keyword" | "semantic" | "hybrid") => { setLoading(true); try { const results = await api.search(query, mode); setRestaurants(results); setSelected(null); setShowDetail(false); } catch (e) { console.error("Search failed:", e); } finally { setLoading(false); } }, [] ); const handleSelectRestaurant = useCallback((r: Restaurant) => { setSelected(r); setShowDetail(true); }, []); const handleCloseDetail = useCallback(() => { setShowDetail(false); }, []); const handleBoundsChanged = useCallback((bounds: MapBounds) => { setMapBounds(bounds); }, []); const handleCountryChange = useCallback((country: string) => { setCountryFilter(country); setCityFilter(""); setDistrictFilter(""); if (!country) { setRegionFlyTo(null); return; } const matched = restaurants.filter((r) => { const p = parseRegion(r.region); return p && p.country === country; }); setRegionFlyTo(computeFlyTo(matched)); }, [restaurants]); const handleCityChange = useCallback((city: string) => { setCityFilter(city); setDistrictFilter(""); if (!city) { // Re-fly to country level const matched = restaurants.filter((r) => { const p = parseRegion(r.region); return p && p.country === countryFilter; }); setRegionFlyTo(computeFlyTo(matched)); return; } const matched = restaurants.filter((r) => { const p = parseRegion(r.region); return p && p.country === countryFilter && p.city === city; }); setRegionFlyTo(computeFlyTo(matched)); }, [restaurants, countryFilter]); const handleDistrictChange = useCallback((district: string) => { setDistrictFilter(district); if (!district) { const matched = restaurants.filter((r) => { const p = parseRegion(r.region); return p && p.country === countryFilter && p.city === cityFilter; }); setRegionFlyTo(computeFlyTo(matched)); return; } const matched = restaurants.filter((r) => { const p = parseRegion(r.region); return p && p.country === countryFilter && p.city === cityFilter && p.district === district; }); setRegionFlyTo(computeFlyTo(matched)); }, [restaurants, countryFilter, cityFilter]); const handleReset = useCallback(() => { setLoading(true); setChannelFilter(""); setCuisineFilter(""); setPriceFilter(""); setCountryFilter(""); setCityFilter(""); setDistrictFilter(""); setRegionFlyTo(null); setBoundsFilterOn(false); setShowFavorites(false); setShowMyReviews(false); api .getRestaurants({ limit: 500 }) .then((data) => { setRestaurants(data); setSelected(null); setShowDetail(false); }) .catch(console.error) .finally(() => setLoading(false)); }, []); const handleToggleFavorites = async () => { if (showFavorites) { setShowFavorites(false); const data = await api.getRestaurants({ limit: 500, channel: channelFilter || undefined }); setRestaurants(data); } else { try { const favs = await api.getMyFavorites(); setRestaurants(favs); setShowFavorites(true); setShowMyReviews(false); setMyReviews([]); setSelected(null); setShowDetail(false); } catch { /* ignore */ } } }; const handleToggleMyReviews = async () => { if (showMyReviews) { setShowMyReviews(false); setMyReviews([]); } else { try { const reviews = await api.getMyReviews(); setMyReviews(reviews); setShowMyReviews(true); setShowFavorites(false); setSelected(null); setShowDetail(false); } catch { /* ignore */ } } }; // Desktop sidebar: shows detail inline const sidebarContent = showMyReviews ? ( { setShowMyReviews(false); setMyReviews([]); }} onSelectRestaurant={async (restaurantId) => { try { const r = await api.getRestaurant(restaurantId); handleSelectRestaurant(r); setShowMyReviews(false); setMyReviews([]); } catch { /* ignore */ } }} /> ) : showDetail && selected ? ( ) : ( ); // Mobile list: always shows list (detail goes to bottom sheet) const mobileListContent = showMyReviews ? ( { setShowMyReviews(false); setMyReviews([]); }} onSelectRestaurant={async (restaurantId) => { try { const r = await api.getRestaurant(restaurantId); handleSelectRestaurant(r); setShowMyReviews(false); setMyReviews([]); } catch { /* ignore */ } }} /> ) : ( ); return (
{/* ── Header row 1: Logo + User ── */}
{/* Desktop: search + filters — two rows */}
{/* Row 1: Search + dropdown filters */}
{/* Row 2: Region filters + Toggle buttons + count */}
{countryFilter && cities.length > 0 && ( )} {cityFilter && districts.length > 0 && ( )}
{user && ( <> )} {filteredRestaurants.length}개
{/* User area */}
{authLoading ? null : user ? (
{user.avatar_url ? ( ) : (
{(user.nickname || user.email || "?").charAt(0).toUpperCase()}
)} {user.nickname || user.email}
) : ( { if (credentialResponse.credential) { login(credentialResponse.credential).catch(console.error); } }} onError={() => console.error("Google login failed")} size="small" /> )}
{/* ── Header row 2 (mobile only): search + toolbar ── */}
{/* Row 1: Search */} {/* Row 2: Toolbar */}
{user && ( <> )} {filteredRestaurants.length}개
{/* Collapsible filter panel */} {showMobileFilters && (
{/* Dropdown filters */}
{/* Region filters */}
{countryFilter && cities.length > 0 && ( )} {cityFilter && districts.length > 0 && ( )}
{/* Toggle buttons */}
)}
{/* ── Body: Desktop = side-by-side, Mobile = stacked ── */} {/* Desktop layout */}
{viewMode === "map" ? ( <>
{visits && (
오늘 {visits.today} · 전체 {visits.total.toLocaleString()}
)}
) : ( <>
{visits && (
오늘 {visits.today} · 전체 {visits.total.toLocaleString()}
)}
)}
{/* Mobile layout */}
{viewMode === "map" ? ( <>
{visits && (
오늘 {visits.today} · 전체 {visits.total.toLocaleString()}
)}
) : ( <> {/* List area — if selected, show single row; otherwise full list */} {selected ? (
{ setSelected(null); setShowDetail(false); }} > {getCuisineIcon(selected.cuisine_type)} {selected.name} {selected.rating && ( {selected.rating} )} {selected.cuisine_type && ( {selected.cuisine_type} )}
) : (
{mobileListContent}
)} {/* Map fills remaining space below the list */}
{visits && (
오늘 {visits.today} · 전체 {visits.total.toLocaleString()}
)}
)} {/* Mobile Bottom Sheet for restaurant detail */} {selected && ( )}
{/* Footer */}
); }