"use client" import { useState, useEffect } from "react" import Link from "next/link" import { searchHanzi } from "@/actions/hanzi" type HanziResult = { id: string simplified: string traditional: string | null pinyin: string | null meaning: string | null hskLevels: string[] radical: string | null frequency: number | null } const HSK_LEVELS = ["new-1", "new-2", "new-3", "new-4", "new-5", "new-6", "old-1", "old-2", "old-3", "old-4", "old-5", "old-6"] export default function HanziSearchPage() { const [query, setQuery] = useState("") const [hskLevel, setHskLevel] = useState("") const [results, setResults] = useState([]) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [total, setTotal] = useState(0) const [hasMore, setHasMore] = useState(false) const [offset, setOffset] = useState(0) const limit = 20 const handleSearch = async (newOffset: number = 0) => { if (!query.trim()) { setError("Please enter a search query") return } setLoading(true) setError(null) try { const result = await searchHanzi( query, hskLevel || undefined, limit, newOffset ) if (result.success && result.data) { setResults(result.data.hanzi) setTotal(result.data.total) setHasMore(result.data.hasMore) setOffset(newOffset) } else { setError(result.message || "Failed to search hanzi") } } catch (err) { setError(err instanceof Error ? err.message : "An error occurred") } finally { setLoading(false) } } const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === "Enter") { handleSearch(0) } } const handleNextPage = () => { handleSearch(offset + limit) } const handlePrevPage = () => { handleSearch(Math.max(0, offset - limit)) } const currentPage = Math.floor(offset / limit) + 1 const totalPages = Math.ceil(total / limit) return (

Search Hanzi

Search by character, pinyin, or meaning

{/* Search Form */}
setQuery(e.target.value)} onKeyPress={handleKeyPress} placeholder="Enter character, pinyin, or meaning..." className="block w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white" disabled={loading} />
{/* Error Message */} {error && (
{error}
)} {/* Results */} {results.length > 0 && (

Found {total} result{total !== 1 ? "s" : ""} {hskLevel && ` for HSK ${hskLevel.toUpperCase()}`}

Page {currentPage} of {totalPages}

{results.map((hanzi) => (
{hanzi.simplified}
{hanzi.hskLevels.length > 0 && (
{hanzi.hskLevels.map((level) => ( {level.toUpperCase()} ))}
)}
{hanzi.traditional && hanzi.traditional !== hanzi.simplified && (

Traditional: {hanzi.traditional}

)} {hanzi.pinyin && (

{hanzi.pinyin}

)} {hanzi.meaning && (

{hanzi.meaning}

)} {hanzi.radical && (

Radical: {hanzi.radical}

)} ))}
{/* Pagination */} {totalPages > 1 && (
Page {currentPage} of {totalPages}
)}
)} {/* Empty State */} {!loading && results.length === 0 && query && (

No hanzi found matching "{query}" {hskLevel && ` in HSK ${hskLevel.toUpperCase()}`}

Try a different search term or remove the HSK filter

)} {/* Initial State */} {!loading && results.length === 0 && !query && (

Enter a search term to find hanzi

Search by simplified character, traditional character, pinyin, or English meaning

)}
) }