"use client"; import { useCallback, useEffect, useMemo, useState } from "react"; import dynamic from "next/dynamic"; import { useSearchParams } from "next/navigation"; import { useTranslations } from "next-intl"; import { ArrowLeftRightIcon } from "lucide-react"; import Main from "@/components/main"; import { PropertyDialog } from "@/components/propertyDialog"; import { TranslationCard } from "@/components/translationCard"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Marker, MarkerContent, MarkerIcon } from "@/components/ui/marker"; import { Spinner } from "@/components/ui/spinner"; import { LOOKUP_DEBOUNCE_MS, useDebouncedValue } from "@/hooks/useDebouncedValue"; import { TRANSLATION_DEBOUNCE_MS, useTranslation } from "@/hooks/useTranslation"; import { DEFAULT_SOURCE_LANG, DEFAULT_TARGET_LANG, LANGUAGES } from "@/lib/languages"; import { setLanguages } from "@/app/actions/setLanguages"; import { parseLanguage } from "@/types/api"; import type { CollocationsResult, DictionaryResult, ExamplesResult, Fetched, Language } from "@/types/api"; import type { UiLanguage } from "@/types/language"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import Header from "./header"; import { Item, ItemActions, ItemContent } from "./ui/item"; import { Skeleton } from "./ui/skeleton"; function CollectionsListLoading() { return ( ); } function WordDefinitionLoading() { const t = useTranslations("Home"); return ( {t("loadingDefinition")} ); } function CollocationsLoading() { const t = useTranslations("Home"); return (

{t("collocations")}

{t("generatingCollocations")}
); } function ExamplesLoading() { const t = useTranslations("Home"); return (

{t("usageExamples")}

{t("generatingExamples")}
); } const CollectionsListPanel = dynamic( () => import("@/components/collectionsList").then((mod) => mod.CollectionsList), { ssr: false, loading: () => , } ); const WordDefinitionPanel = dynamic( () => import("@/components/wordDefinition").then((mod) => mod.WordDefinition), { ssr: false, loading: () => , } ); const CollocationsPanel = dynamic( () => import("@/components/collocations").then((mod) => mod.Collocations), { ssr: false, loading: () => , } ); const ExamplesPanel = dynamic( () => import("@/components/examples").then((mod) => mod.Examples), { ssr: false, loading: () => , } ); interface Selection { word: string; lang: Language; srcText?: string; } const WORD_ONLY_PATTERN = /^[\p{L}'-]+$/u; function languageFromParam(value: string | null): UiLanguage | null { const id = value ? parseLanguage(value) : null; return id ? LANGUAGES.find((l) => l.id === id) ?? null : null; } function fallbackLanguage(id: Language): UiLanguage { return LANGUAGES.find((l) => l.id !== id) ?? LANGUAGES[0]; } import type { User } from "@/types/api"; interface HomeProps { user: User | null; rememberedSourceLang?: UiLanguage | null; rememberedTargetLang?: UiLanguage | null; } export default function Home({ user, rememberedSourceLang, rememberedTargetLang }: HomeProps) { const searchParams = useSearchParams(); const [sourceLang, setSourceLang] = useState( () => languageFromParam(searchParams.get("sourceLang")) ?? rememberedSourceLang ?? DEFAULT_SOURCE_LANG ); const [targetLang, setTargetLang] = useState(() => { const fromUrl = languageFromParam(searchParams.get("targetLang")); if (fromUrl && fromUrl.id !== sourceLang.id) return fromUrl; const stored = rememberedTargetLang && rememberedTargetLang.id !== sourceLang.id ? rememberedTargetLang : null; return stored ?? LANGUAGES.find((l) => l.id !== sourceLang.id) ?? DEFAULT_TARGET_LANG; }); const [sourceText, setSourceText] = useState(() => searchParams.get("text") ?? ""); const [selection, setSelection] = useState(null); const [activeTab, setActiveTab] = useState("definition"); const [definitionResult, setDefinitionResult] = useState | null>(null); const [collocationsResult, setCollocationsResult] = useState | null>(null); const [examplesResult, setExamplesResult] = useState | null>(null); const t = useTranslations("Home"); const { translatedText, result, isLoading, error } = useTranslation( sourceText, sourceLang.id, targetLang.id ); useEffect(() => { const timeoutId = setTimeout(() => { const params = new URLSearchParams(); if (sourceText.trim()) params.set("text", sourceText.trim()); params.set("sourceLang", sourceLang.id); params.set("targetLang", targetLang.id); window.history.replaceState(null, "", `/?${params.toString()}`); }, TRANSLATION_DEBOUNCE_MS); return () => clearTimeout(timeoutId); }, [sourceText, sourceLang.id, targetLang.id]); const isSingleWord = useMemo(() => { if (!sourceText.trim()) return false; const words = sourceText.trim().split(/\s+/); return words.length === 1 && WORD_ONLY_PATTERN.test(words[0]); }, [sourceText]); const definition: Selection | null = useMemo(() => { if (selection && selection.srcText === sourceText) return selection; if (isSingleWord) { return { word: sourceText.trim(), lang: sourceLang.id, }; } return null; }, [selection, sourceText, isSingleWord, sourceLang.id]); const debouncedDefinition = useDebouncedValue( definition, definition?.srcText ? 0 : LOOKUP_DEBOUNCE_MS ); const selectSourceWord = useCallback( (word: string) => setSelection({ word, lang: sourceLang.id, srcText: sourceText }), [sourceLang.id, sourceText] ); const selectTargetWord = useCallback( (word: string) => setSelection({ word, lang: targetLang.id, srcText: sourceText }), [targetLang.id, sourceText] ); const handleSourceChange = useCallback( (lang: UiLanguage) => { setSourceLang(lang); setSelection(null); if (lang.id === targetLang.id) setTargetLang(fallbackLanguage(lang.id)); void setLanguages(lang.id, lang.id === targetLang.id ? fallbackLanguage(lang.id).id : targetLang.id); }, [targetLang.id] ); const handleTargetChange = useCallback( (lang: UiLanguage) => { setTargetLang(lang); setSelection(null); if (lang.id === sourceLang.id) setSourceLang(fallbackLanguage(lang.id)); void setLanguages(lang.id === sourceLang.id ? fallbackLanguage(lang.id).id : sourceLang.id, lang.id); }, [sourceLang.id] ); const handleSwap = useCallback(() => { setSourceLang(targetLang); setTargetLang(sourceLang); setSourceText(translatedText); void setLanguages(targetLang.id, sourceLang.id); }, [sourceLang, targetLang, translatedText]); const handleTabChange = useCallback((value: unknown) => { if (typeof value === "string") setActiveTab(value); }, []); return (
) } />
{debouncedDefinition && } {debouncedDefinition && ( {t("definition")} {t("usageExamples")} {t("collocations")} )}
); }