version 1
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
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
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useState } from "react";
|
||||
import { lookupDictionary } from "@/lib/api";
|
||||
import { useResource } from "@/hooks/useResource";
|
||||
import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage";
|
||||
import type { DictionaryResult, Entry, Fetched, Language } from "@/types/api";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Separator } from "@/components/ui/separator";
|
||||
import { ChevronDownIcon, ChevronUpIcon } from "lucide-react";
|
||||
import { Marker, MarkerContent, MarkerIcon } from "./ui/marker";
|
||||
import { Spinner } from "./ui/spinner";
|
||||
import { useTranslations } from "next-intl";
|
||||
|
||||
const INITIAL_SENSES_SHOWN = 3;
|
||||
const INITIAL_SYNONYMS_SHOWN = 12;
|
||||
|
||||
const GENDER_BADGE_CLASSES: Record<string, string> = {
|
||||
masculine: "text-white bg-sky-500 dark:bg-sky-800",
|
||||
feminine: "text-white bg-rose-500 dark:bg-rose-800",
|
||||
};
|
||||
|
||||
function EntrySection({ entry }: { entry: Entry }) {
|
||||
const [sensesExpanded, setSensesExpanded] = useState(false);
|
||||
const [synonymsExpanded, setSynonymsExpanded] = useState(false);
|
||||
|
||||
const hasMoreSenses = entry.senses.length > INITIAL_SENSES_SHOWN;
|
||||
const visibleSenses = sensesExpanded
|
||||
? entry.senses
|
||||
: entry.senses.slice(0, INITIAL_SENSES_SHOWN);
|
||||
|
||||
const synonymsCount = entry.synonyms?.length ?? 0;
|
||||
const hasMoreSynonyms = synonymsCount > INITIAL_SYNONYMS_SHOWN;
|
||||
const visibleSynonyms = synonymsExpanded
|
||||
? entry.synonyms
|
||||
: entry.synonyms?.slice(0, INITIAL_SYNONYMS_SHOWN);
|
||||
|
||||
const tc = useTranslations("Common");
|
||||
const td = useTranslations("Definition");
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
<div className="flex flex-row items-center gap-2 mb-2">
|
||||
<h4 className="font-semibold italic">{entry.partOfSpeech}</h4>
|
||||
{entry.ipa && (
|
||||
<h4 className="text-muted-foreground">/{entry.ipa.replace(/\//g, "")}/</h4>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex flex-col gap-4">
|
||||
{visibleSenses.map((sense, i) => (
|
||||
<div key={i} className="flex flex-row gap-2 items-start">
|
||||
<h5 className="text-muted-foreground shrink-0 tabular-nums">{i + 1}.</h5>
|
||||
<div className="flex flex-col gap-1">
|
||||
{sense.tags && sense.tags.length > 0 && (
|
||||
<div className="flex flex-row flex-wrap gap-1">
|
||||
{sense.tags.map((tag) => (
|
||||
<Badge
|
||||
key={tag}
|
||||
variant="secondary"
|
||||
className={GENDER_BADGE_CLASSES[tag.trim().toLowerCase()]}
|
||||
>
|
||||
{tag}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div className="flex flex-col">
|
||||
<p>{sense.definition}</p>
|
||||
{sense.examples?.[0] && (
|
||||
<p className="text-muted-foreground italic">
|
||||
"{sense.examples[0]}"
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{hasMoreSenses && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="w-max text-muted-foreground mt-2"
|
||||
onClick={() => setSensesExpanded((v) => !v)}
|
||||
>
|
||||
<div className="flex flex-row items-center gap-2">
|
||||
{sensesExpanded ? <ChevronUpIcon /> : <ChevronDownIcon />}
|
||||
<h5>
|
||||
{sensesExpanded
|
||||
? tc("showLess")
|
||||
: tc("showMore", { count: entry.senses.length - INITIAL_SENSES_SHOWN })}
|
||||
</h5>
|
||||
</div>
|
||||
</Button>
|
||||
)}
|
||||
|
||||
{(entry.synonyms || entry.antonyms) && (
|
||||
<div className="flex flex-col gap-4 my-4">
|
||||
{entry.synonyms && entry.synonyms.length > 0 && (
|
||||
<div className="flex flex-col gap-4">
|
||||
<h4 className="font-semibold">{td("synonyms")}</h4>
|
||||
|
||||
<div className="flex flex-row flex-wrap gap-2">
|
||||
{visibleSynonyms?.map((s, i) => (
|
||||
<Badge variant="outline" key={i} className="h-6">
|
||||
<h5>{s}</h5>
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{hasMoreSynonyms && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="w-max text-muted-foreground"
|
||||
onClick={() => setSynonymsExpanded((v) => !v)}
|
||||
>
|
||||
<div className="flex flex-row items-center gap-2">
|
||||
{synonymsExpanded ? <ChevronUpIcon /> : <ChevronDownIcon />}
|
||||
<h5>
|
||||
{synonymsExpanded
|
||||
? tc("showLess")
|
||||
: tc("showMore", { count: synonymsCount - INITIAL_SYNONYMS_SHOWN })}
|
||||
</h5>
|
||||
</div>
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{entry.antonyms && entry.antonyms.length > 0 && (
|
||||
<div className="flex flex-row flex-wrap gap-4">
|
||||
<h4 className="font-semibold">{td("antonyms")}</h4>
|
||||
|
||||
{entry.antonyms.map((s, i) => (
|
||||
<Badge variant="outline" key={i} className="h-6">
|
||||
<h5>{s}</h5>
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
interface WordDefinitionProps {
|
||||
word: string;
|
||||
lang: Language;
|
||||
onResult?: (result: Fetched<DictionaryResult>) => void;
|
||||
}
|
||||
|
||||
export function WordDefinition({ word, lang, onResult }: WordDefinitionProps) {
|
||||
const cacheKey = `dict:${lang}:${word.toLowerCase()}`;
|
||||
|
||||
const report = useCallback(
|
||||
(data: DictionaryResult) =>
|
||||
onResult?.({
|
||||
...data,
|
||||
fetched_at: new Date().toISOString(),
|
||||
}),
|
||||
[onResult]
|
||||
);
|
||||
|
||||
const state = useResource<DictionaryResult>(
|
||||
cacheKey,
|
||||
() => lookupDictionary(lang, word),
|
||||
report
|
||||
);
|
||||
|
||||
const t = useTranslations("Definition");
|
||||
const tApi = useTranslations("ApiErrors");
|
||||
|
||||
const errorMessage = resolveApiErrorMessage(
|
||||
state.error,
|
||||
(kind) => (kind === "not-found" ? t("notFound") : tApi(kind)),
|
||||
t("notFound")
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<h2 className="font-semibold">{word}</h2>
|
||||
{state.status === "loading" && (
|
||||
<Marker>
|
||||
<MarkerIcon>
|
||||
<Spinner />
|
||||
</MarkerIcon>
|
||||
<MarkerContent className="shimmer">{t("loading")}</MarkerContent>
|
||||
</Marker>
|
||||
)}
|
||||
{state.status === "error" && (
|
||||
<p className="text-muted-foreground">{errorMessage}</p>
|
||||
)}
|
||||
{state.status === "done" &&
|
||||
state.data?.entries.map((entry, i) => (
|
||||
<div key={i}>
|
||||
{i > 0 && <Separator className="mb-4" />}
|
||||
<EntrySection entry={entry} />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user