Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
117 lines
3.5 KiB
TypeScript
117 lines
3.5 KiB
TypeScript
"use client";
|
|
|
|
import type { ReactNode } from "react";
|
|
import { TriangleAlertIcon } from "lucide-react";
|
|
import { CopyButton } from "@/components/copyButton";
|
|
import { LanguageSelect } from "@/components/languageSelect";
|
|
import { TokenizedText } from "@/components/tokenizedText";
|
|
import { Card, CardContent, CardHeader } from "@/components/ui/card";
|
|
import { Marker, MarkerContent, MarkerIcon } from "@/components/ui/marker";
|
|
import { Separator } from "@/components/ui/separator";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import type { ApiError } from "@/lib/api";
|
|
import { cn } from "@/lib/utils";
|
|
import { useApiErrorMessage } from "@/lib/useApiErrorMessage";
|
|
import type { Language } from "@/types/api";
|
|
import type { UiLanguage } from "@/types/language";
|
|
import { useTranslations } from "next-intl";
|
|
|
|
interface TranslationCardProps {
|
|
variant: "source" | "target";
|
|
lang: UiLanguage;
|
|
onLangChange: (lang: UiLanguage) => void;
|
|
excludeLangId?: Language;
|
|
placeholder: string;
|
|
text: string;
|
|
onTextChange?: (text: string) => void;
|
|
isLoading?: boolean;
|
|
error?: ApiError | null;
|
|
onSelectWord: (word: string) => void;
|
|
footerExtra?: ReactNode;
|
|
}
|
|
|
|
export function TranslationCard({
|
|
variant,
|
|
lang,
|
|
onLangChange,
|
|
excludeLangId,
|
|
placeholder,
|
|
text,
|
|
onTextChange,
|
|
isLoading,
|
|
error,
|
|
onSelectWord,
|
|
footerExtra,
|
|
}: TranslationCardProps) {
|
|
const isSource = variant === "source";
|
|
|
|
const t = useTranslations("Home");
|
|
const apiErrorMessage = useApiErrorMessage(error?.kind);
|
|
|
|
return (
|
|
<Card className="w-full md:w-1/2 flex flex-col">
|
|
<CardHeader>
|
|
<LanguageSelect value={lang} onChange={onLangChange} excludeId={excludeLangId} />
|
|
</CardHeader>
|
|
<CardContent className="flex flex-col flex-1 justify-between gap-4">
|
|
{isSource ? (
|
|
<Textarea
|
|
placeholder={placeholder}
|
|
value={text}
|
|
onChange={(e) => onTextChange?.(e.target.value)}
|
|
className={cn(
|
|
"text-xl! resize-none flex-1 min-h-32",
|
|
"border-none p-0",
|
|
"dark:bg-transparent focus-visible:ring-0 focus-visible:ring-offset-0"
|
|
)}
|
|
/>
|
|
) : (
|
|
<h2
|
|
aria-busy={isLoading}
|
|
aria-live="polite"
|
|
className={cn(
|
|
"flex-1 min-h-32",
|
|
!text && "text-muted-foreground",
|
|
isLoading && "animate-pulse"
|
|
)}
|
|
>
|
|
{text ? (
|
|
<TokenizedText text={text} onSelect={onSelectWord} />
|
|
) : (
|
|
placeholder
|
|
)}
|
|
</h2>
|
|
)}
|
|
|
|
{!isSource && error && (
|
|
<Marker>
|
|
<MarkerIcon>
|
|
<TriangleAlertIcon />
|
|
</MarkerIcon>
|
|
<MarkerContent>{apiErrorMessage}</MarkerContent>
|
|
</Marker>
|
|
)}
|
|
|
|
{isSource && text.trim().split(/\s+/).length > 1 && (
|
|
<div className="flex flex-col gap-4">
|
|
<Separator />
|
|
<div className="flex flex-col gap-2">
|
|
<p>
|
|
<TokenizedText text={text} onSelect={onSelectWord} />
|
|
</p>
|
|
<h6 className="text-muted-foreground">{t("clickWordHint")}</h6>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex flex-row items-center justify-between">
|
|
<div className="flex flex-row items-center gap-2">
|
|
{text && <CopyButton text={text} />}
|
|
</div>
|
|
{footerExtra}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
);
|
|
}
|