From 9005cfa5ef8a17166b01564496b25c9638d8312a Mon Sep 17 00:00:00 2001 From: virtus Date: Mon, 4 May 2026 20:59:30 +0700 Subject: [PATCH] feat: Enhance AI suggestion functionality in ImportClient and improve compactLine handling --- app/mod/import/import-client.tsx | 11 ++++++++--- components/home-hot-carousel.tsx | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/mod/import/import-client.tsx b/app/mod/import/import-client.tsx index 1b520fd..ddd6629 100644 --- a/app/mod/import/import-client.tsx +++ b/app/mod/import/import-client.tsx @@ -56,6 +56,7 @@ export function ImportClient() { const [parseError, setParseError] = useState("") const [aiLoading, setAiLoading] = useState(false) + const [aiModel, setAiModel] = useState("") const [importing, setImporting] = useState(false) const [result, setResult] = useState | null>(null) @@ -237,16 +238,19 @@ export function ImportClient() { form.append("preview", "true") form.append("splitMode", splitMode) if (splitMode === "regex") form.append("chapterRegex", chapterStartPattern) - const res = await fetch("/api/mod/epub", { method: "POST", credentials: "include", body: form }) + form.append("title", title) + form.append("authorName", author) + const res = await fetch("/api/mod/epub/ai-suggest", { method: "POST", credentials: "include", body: form }) const data = await res.json() if (!res.ok) throw new Error(data?.detail || "AI suggest failed") - const suggestedGenres: string[] = (data?.novel?.detectedGenres || []).slice(0, 6) + const suggestedGenres: string[] = (data?.suggestedGenres || []).slice(0, 6) + setAiModel(String(data?.model || data?.source || "")) setGenreQuery("") if (suggestedGenres.length > 0) { const ensuredIds = await ensureGenreIdsByNames(suggestedGenres) setSelectedGenreIds(ensuredIds) } - if (!shortDescription) setShortDescription(data?.novel?.description || "") + setShortDescription(data?.shortDescription || "") toast.success("Đã áp dụng gợi ý AI") } catch (error) { toast.error(error instanceof Error ? error.message : "AI suggest lỗi") @@ -480,6 +484,7 @@ export function ImportClient() { + {aiModel &&

AI model: {aiModel}

} )} diff --git a/components/home-hot-carousel.tsx b/components/home-hot-carousel.tsx index 3571c39..dd4e5a6 100644 --- a/components/home-hot-carousel.tsx +++ b/components/home-hot-carousel.tsx @@ -32,8 +32,8 @@ function sourceClass(source: HotCarouselItem["hotSource"]) { return "border-primary/30 bg-primary/20 text-primary" } -function compactLine(text: string, max = 180) { - const normalized = text.replace(/\s+/g, " ").trim() +function compactLine(text: string | null | undefined, max = 180) { + const normalized = String(text || "").replace(/\s+/g, " ").trim() if (normalized.length <= max) return normalized return `${normalized.slice(0, max).trim()}...` }