Files
fancy/fancy-frontend/components/propertyDialog.tsx
T
Ivan Rezchikov 628dd6384b
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
version 1
2026-09-18 00:45:51 +03:00

253 lines
8.8 KiB
TypeScript

"use client";
import { useEffect, useState, type ReactNode } from "react";
import {
ArrowDownToLineIcon,
BookOpenIcon,
ClockIcon,
CpuIcon,
GaugeIcon,
InfoIcon,
LandmarkIcon,
LanguagesIcon,
SparklesIcon,
SquareArrowRightEnterIcon,
} from "lucide-react";
import { Dialog, DialogContent, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Progress, ProgressLabel } from "@/components/ui/progress";
import { Separator } from "@/components/ui/separator";
import { getUsage } from "@/lib/api";
import { cn, formatNumber, formatTimestamp } from "@/lib/utils";
import type {
CollocationsResult,
DictionaryResult,
ExamplesResult,
Fetched,
Source,
TranslationResult,
UsageResult,
} from "@/types/api";
import { useLocale, useTranslations } from "next-intl";
interface PropertyDialogProps {
response: Fetched<TranslationResult> | null;
definition?: Fetched<DictionaryResult> | null;
examples?: Fetched<ExamplesResult> | null;
collocations?: Fetched<CollocationsResult> | null;
}
function PropertyRow({ icon, label, value }: { icon: ReactNode; label: string; value?: string | null }) {
const tc = useTranslations("Common");
return (
<div className="flex flex-row items-center gap-4">
<div className="flex flex-row items-center gap-2 text-muted-foreground">
{icon}
<p>{label}</p>
</div>
<p className="font-medium">{value || tc("dash")}</p>
</div>
);
}
function PropertySection({ icon, title, children }: { icon: ReactNode; title: string; children: ReactNode }) {
return (
<div className="flex flex-col gap-2">
<div className="flex flex-row items-center gap-2">
{icon}<p className="font-medium">{title}</p>
</div>
<div className="flex flex-col gap-1">{children}</div>
</div>
);
}
function UsageBar({ label, used, limit }: { label: string; used: number; limit: number }) {
const locale = useLocale();
const percent = limit > 0 ? Math.min(100, (used / limit) * 100) : 0;
return (
<Progress
value={percent}
className={cn("gap-1", percent >= 90 && "**:data-[slot=progress-indicator]:bg-destructive")}
>
<ProgressLabel>{label}</ProgressLabel>
<h5 className="ml-auto text-muted-foreground tabular-nums">{formatNumber(used, locale)} / {formatNumber(limit, locale)}</h5>
</Progress>
);
}
function UsageSection() {
const t = useTranslations("Properties");
const [usage, setUsage] = useState<UsageResult | null>(null);
const [failed, setFailed] = useState(false);
useEffect(() => {
let cancelled = false;
getUsage()
.then((data) => {
if (!cancelled) setUsage(data);
})
.catch((err) => {
console.error("Usage lookup failed:", err);
if (!cancelled) setFailed(true);
});
return () => {
cancelled = true;
};
}, []);
return (
<div className="flex flex-col gap-4">
<div className="flex flex-row items-center gap-2">
<GaugeIcon size={18} /><p className="font-medium">{t("usage")}</p>
</div>
{failed && <p className="text-muted-foreground">{t("usageUnavailable")}</p>}
{!failed && !usage && (
<p className="text-muted-foreground shimmer">{t("loadingUsage")}</p>
)}
{usage && (
<div className="flex flex-col gap-4">
{usage.deepl ? (
<UsageBar
label={t("deeplCharacters")}
used={usage.deepl.character_count}
limit={usage.deepl.character_limit}
/>
) : (
<p className="text-muted-foreground">{t("deeplUnavailable")}</p>
)}
<div className="flex flex-col gap-1">
<UsageBar label={t("aiTokens")} used={usage.ai.tokens} limit={usage.ai.token_limit} />
<h6 className="text-muted-foreground">
{t("requestsThisMonth", { count: usage.ai.requests })}
</h6>
</div>
</div>
)}
</div>
);
}
export function PropertyDialog({ response, definition, examples, collocations }: PropertyDialogProps) {
const t = useTranslations("Properties");
const tc = useTranslations("Common");
const locale = useLocale();
const sourceValue = (source: Source) =>
source === "provider" ? tc("provider") : tc("cache");
return (
<Dialog>
<DialogTrigger
render={
<Button variant="outline" size="icon" aria-label={t("details")}>
<InfoIcon />
</Button>
}
/>
<DialogContent className="max-h-[85vh] overflow-y-auto">
<DialogTitle>{t("title")}</DialogTitle>
<div className="flex flex-col gap-4 text-sm">
{response && (
<PropertySection icon={<LanguagesIcon size={18} />} title={t("translate")}>
<PropertyRow
icon={<SquareArrowRightEnterIcon size={18} />}
label={tc("source")}
value={sourceValue(response.source)}
/>
<PropertyRow icon={<LandmarkIcon size={18} />} label={tc("provider")} value={response.provider} />
{response.model && (
<PropertyRow icon={<CpuIcon size={18} />} label={tc("model")} value={response.model} />
)}
<PropertyRow
icon={<ClockIcon size={18} />}
label={tc("generated")}
value={formatTimestamp(response.generated_at, locale)}
/>
<PropertyRow
icon={<ArrowDownToLineIcon size={18} />}
label={tc("fetched")}
value={formatTimestamp(response.fetched_at, locale)}
/>
</PropertySection>
)}
{definition && (
<PropertySection icon={<BookOpenIcon size={18} />} title={t("definition")}>
<PropertyRow
icon={<SquareArrowRightEnterIcon size={18} />}
label={tc("source")}
value={sourceValue(definition.source)}
/>
<PropertyRow icon={<LandmarkIcon size={18} />} label={tc("provider")} value={definition.provider} />
{definition.model && (
<PropertyRow icon={<CpuIcon size={18} />} label={tc("model")} value={definition.model} />
)}
<PropertyRow
icon={<ClockIcon size={18} />}
label={tc("generated")}
value={formatTimestamp(definition.generated_at, locale)}
/>
<PropertyRow
icon={<ArrowDownToLineIcon size={18} />}
label={tc("fetched")}
value={formatTimestamp(definition.fetched_at, locale)}
/>
</PropertySection>
)}
{examples && (
<PropertySection icon={<SparklesIcon size={18} />} title={t("examples")}>
<PropertyRow
icon={<SquareArrowRightEnterIcon size={18} />}
label={tc("source")}
value={sourceValue(examples.source)}
/>
<PropertyRow icon={<LandmarkIcon size={18} />} label={tc("provider")} value={examples.provider} />
{examples.model && (
<PropertyRow icon={<CpuIcon size={18} />} label={tc("model")} value={examples.model} />
)}
<PropertyRow
icon={<ClockIcon size={18} />}
label={tc("generated")}
value={formatTimestamp(examples.generated_at, locale)}
/>
<PropertyRow
icon={<ArrowDownToLineIcon size={18} />}
label={tc("fetched")}
value={formatTimestamp(examples.fetched_at, locale)}
/>
</PropertySection>
)}
{collocations && (
<PropertySection icon={<SparklesIcon size={18} />} title={t("collocations")}>
<PropertyRow
icon={<SquareArrowRightEnterIcon size={18} />}
label={tc("source")}
value={sourceValue(collocations.source)}
/>
<PropertyRow icon={<LandmarkIcon size={18} />} label={tc("provider")} value={collocations.provider} />
{collocations.model && (
<PropertyRow icon={<CpuIcon size={18} />} label={tc("model")} value={collocations.model} />
)}
<PropertyRow
icon={<ClockIcon size={18} />}
label={tc("generated")}
value={formatTimestamp(collocations.generated_at, locale)}
/>
<PropertyRow
icon={<ArrowDownToLineIcon size={18} />}
label={tc("fetched")}
value={formatTimestamp(collocations.fetched_at, locale)}
/>
</PropertySection>
)}
<Separator />
<UsageSection />
</div>
</DialogContent>
</Dialog>
);
}