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

This commit is contained in:
Ivan Rezchikov
2026-09-18 00:45:51 +03:00
commit 628dd6384b
149 changed files with 11478 additions and 0 deletions
@@ -0,0 +1,152 @@
"use client";
import { useState, useTransition } from "react";
import { useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { Button } from "@/components/ui/button";
import { InfoIcon, MenuIcon } from "lucide-react";
import {
Dialog,
DialogClose,
DialogContent,
DialogFooter,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import {
Field,
FieldGroup,
FieldLabel,
FieldSet,
} from "@/components/ui/field";
import { CollectionResult } from "@/types/api";
import { formatDate } from "@/lib/formatDate";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { deleteCollection, updateCollection } from "@/lib/api";
import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage";
import { Spinner } from "@/components/ui/spinner";
export function MenuDialog({ collection }: { collection: CollectionResult }) {
const router = useRouter();
const [open, setOpen] = useState(false);
const [name, setName] = useState(collection.name);
const [error, setError] = useState<string | null>(null);
const [isDeleting, startDelete] = useTransition();
const [isUpdating, startUpdate] = useTransition();
const tt = useTranslations("Time");
const t = useTranslations("CollectionMenu");
const tc = useTranslations("Common");
const tl = useTranslations("Languages");
const tApi = useTranslations("ApiErrors");
const isAuto = collection.type === "auto";
const isBusy = isDeleting || isUpdating;
const handleDelete = () => {
setError(null);
startDelete(async () => {
try {
await deleteCollection(collection.id);
router.push("/collections");
} catch (err) {
setError(resolveApiErrorMessage(err, (kind) => tApi(kind), t("deleteFailed")));
}
});
};
const handleUpdate = () => {
setError(null);
startUpdate(async () => {
try {
await updateCollection(collection.id, name);
router.refresh();
setOpen(false);
} catch (err) {
setError(resolveApiErrorMessage(err, (kind) => tApi(kind), t("updateFailed")));
}
});
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger
render={
<Button variant="outline" size="icon">
<MenuIcon />
</Button>
}
/>
<DialogContent>
<DialogTitle>{t("title")}</DialogTitle>
{isAuto && (
<Alert>
<InfoIcon />
<AlertTitle>{t("autoTitle")}</AlertTitle>
<AlertDescription>{t("autoDescription")}</AlertDescription>
</Alert>
)}
{error && (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<FieldSet className="w-full">
<FieldGroup>
<div className="flex flex-col gap-2">
<Field className="mb-1">
<FieldLabel htmlFor="name">{t("nameLabel")}</FieldLabel>
<Input
id="name"
type="text"
placeholder={t("namePlaceholder")}
value={name === "history" ? tc("history") : name}
onChange={(e) => setName(e.target.value)}
disabled={isAuto || isBusy}
/>
</Field>
<div className="flex flex-row items-center justify-between">
<h5 className="font-medium">{t("language")}</h5>
<h5>{tl(collection.lang)}</h5>
</div>
<div className="flex flex-row items-center justify-between">
<h5 className="font-medium">{t("wordCount")}</h5>
<h5>{collection.words.length}</h5>
</div>
<div className="flex flex-row items-center justify-between">
<h5 className="font-medium">{t("lastUpdate")}</h5>
<h5>{formatDate(collection.updated_at, tt)}</h5>
</div>
<div className="flex flex-row items-center justify-between">
<h5 className="font-medium">{t("created")}</h5>
<h5>{formatDate(collection.created_at, tt)}</h5>
</div>
</div>
</FieldGroup>
</FieldSet>
<DialogFooter>
<div className="flex flex-row items-start justify-between w-full">
<div className="flex flex-row gap-2 items-center">
<Button onClick={handleUpdate} disabled={isAuto || isBusy || name.trim() === collection.name}>
{isUpdating && <Spinner />}{tc("update")}
</Button>
<DialogClose
render={<Button variant="outline" disabled={isBusy} />}
>
{tc("cancel")}
</DialogClose>
</div>
<Button variant="destructive" onClick={handleDelete} disabled={isAuto || isBusy}>
{isUpdating && <Spinner />}{t("delete")}
</Button>
</div>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
@@ -0,0 +1,70 @@
import HeaderWithAuth from "@/components/headerWithAuth";
import Main from "@/components/main";
import { ArrowLeftIcon } from "lucide-react";
import Link from "next/link";
import { notFound } from "next/navigation";
import { isApiError } from "@/lib/api";
import { getCollection } from "@/lib/serverApi";
import { LANGUAGES } from "@/lib/languages";
import type { CollectionResult } from "@/types/api";
import { MenuDialog } from "./menuDialog";
import { getTranslations } from "next-intl/server";
import { WordsSearch } from "./wordsSearch";
export default async function CollectionPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const tCollections = await getTranslations("Collections");
const tc = await getTranslations("Common");
const tl = await getTranslations("Languages");
const tApi = await getTranslations("ApiErrors");
let collection: CollectionResult;
try {
collection = await getCollection(id);
} catch (err) {
if (isApiError(err) && err.kind === "not-found") notFound();
const message = isApiError(err) ? tApi(err.kind) : tc("somethingWrong");
return (
<Main>
<HeaderWithAuth />
<p className="text-destructive">{message}</p>
</Main>
);
}
const language = LANGUAGES.find((l) => l.id === collection.lang);
const words = [...collection.words].reverse();
return (
<Main>
<HeaderWithAuth />
<Link href="/collections" className="flex items-center gap-1.5 text-muted-foreground w-fit">
<ArrowLeftIcon size={16} /><p>{tCollections("title")}</p>
</Link>
<div className="flex flex-row items-start justify-between">
<div className="flex flex-col gap-2.5 justify-between">
<h2 className="font-semibold">{collection.name === "history" ? tc("history") : collection.name}</h2>
<div className="flex flex-row gap-8">
<h3 className="font-medium">{collection.words.length} {collection.words.length === 1 ? tc("word") : tc("words")}</h3>
{language && <div className="flex flex-row gap-2.5 items-center">
<h3 className="font-medium">{tl(language.id)}</h3>
</div>}
</div>
</div>
<MenuDialog collection={collection} />
</div>
<WordsSearch
words={words}
lang={collection.lang}
collectionId={collection.id}
isRemovable={collection.type === "user"}
placeholder={tc("search")}
noWordsLabel={tCollections("noWords")}
noResultsLabel={tCollections("noSearchResults")}
/>
</Main>
);
}
@@ -0,0 +1,27 @@
"use client";
import { useTransition } from "react";
import { XIcon } from "lucide-react";
import { useRouter } from "next/navigation";
import { deleteWordFromCollection } from "@/lib/api";
import { Spinner } from "@/components/ui/spinner";
export function RemoveWordButton({ collectionId, word }: { collectionId: string; word: string }) {
const router = useRouter();
const [isPending, startTransition] = useTransition();
const handleClick = () => {
startTransition(async () => {
try {
await deleteWordFromCollection(collectionId, word);
router.refresh();
} catch {
}
});
};
return (
isPending ? <Spinner className="text-muted-foreground" /> :
<XIcon size={18} onClick={handleClick} className="text-muted-foreground cursor-pointer" />
);
}
@@ -0,0 +1,74 @@
"use client";
import Link from "next/link";
import { useMemo } from "react";
import { ArrowRightIcon } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
import { lookupDictionary } from "@/lib/api";
import { useResource } from "@/hooks/useResource";
import { useTranslations } from "next-intl";
import type { DictionaryResult, Language } from "@/types/api";
import { RemoveWordButton } from "./removeWordButton";
export function WordCard({
word,
lang,
collectionId,
isRemovable,
}: {
word: string;
lang: Language;
collectionId: string;
isRemovable: boolean;
}) {
const td = useTranslations("Definition");
const key = `dict:${lang}:${word.toLowerCase()}`;
const state = useResource<DictionaryResult>(key, () => lookupDictionary(lang, word));
const definition = useMemo(() => {
if (state.status !== "done" || !state.data) return undefined;
const entry = state.data.entries[0];
if (!entry) return null;
return {
text: entry.senses[0]?.definition ?? null,
quantity: entry.senses.length,
};
}, [state]);
return (
<Card size="sm" className="h-full flex flex-col">
<CardHeader>
<div className="flex flex-row items-center justify-between">
<CardTitle><h4>{word}</h4></CardTitle>
<div className="flex flex-row items-center gap-2">
{isRemovable && <RemoveWordButton collectionId={collectionId} word={word} />}
<Link href={`/?text=${encodeURIComponent(word)}&sourceLang=${lang}`}>
<ArrowRightIcon size={18} className="text-muted-foreground" />
</Link>
</div>
</div>
</CardHeader>
<CardContent className="flex-1">
{state.status === "loading" ? (
<div className="flex flex-col gap-2">
<Skeleton className="w-full h-4" />
<Skeleton className="w-2/3 h-4" />
</div>
) : state.status === "error" || !definition?.text ? (
<h5 className="text-muted-foreground italic">{td("notFound")}</h5>
) : (
<div className="flex flex-col gap-2">
<h5 className="text-muted-foreground line-clamp-3">
{definition.text}
{definition.quantity > 1 && (
<span className="italic"> {td("otherDefinitions", { count: definition.quantity - 1 })}</span>
)}
</h5>
</div>
)}
</CardContent>
</Card>
);
}
@@ -0,0 +1,69 @@
"use client";
import { useMemo, useState } from "react";
import { InputGroup, InputGroupAddon, InputGroupInput } from "@/components/ui/input-group";
import { SearchIcon } from "lucide-react";
import type { Language } from "@/types/api";
import { WordCard } from "./wordCard";
interface WordsSearchProps {
words: string[];
lang: Language;
collectionId: string;
isRemovable: boolean;
placeholder: string;
noWordsLabel: string;
noResultsLabel: string;
}
export function WordsSearch({
words,
lang,
collectionId,
isRemovable,
placeholder,
noWordsLabel,
noResultsLabel,
}: WordsSearchProps) {
const [query, setQuery] = useState("");
const filtered = useMemo(() => {
const q = query.trim().toLowerCase();
if (!q) return words;
return words.filter((w) => w.toLowerCase().includes(q));
}, [words, query]);
return (
<div className="flex flex-col gap-4">
<InputGroup className="max-w-sm">
<InputGroupInput
placeholder={placeholder}
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
<InputGroupAddon>
<SearchIcon />
</InputGroupAddon>
</InputGroup>
{words.length === 0 ? (
<p className="text-muted-foreground mx-auto">{noWordsLabel}</p>
) : filtered.length === 0 ? (
<p className="text-muted-foreground mx-auto">{noResultsLabel}</p>
) : (
<div className="grid md:grid-cols-4 gap-4">
{filtered.map((word) => (
<div key={word}>
<WordCard
word={word}
lang={lang}
collectionId={collectionId}
isRemovable={isRemovable}
/>
</div>
))}
</div>
)}
</div>
);
}
@@ -0,0 +1,111 @@
"use client";
import { useState, useTransition } from "react";
import { useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { PlusIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogFooter,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Field, FieldGroup, FieldLabel, FieldSet } from "@/components/ui/field";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Spinner } from "@/components/ui/spinner";
import { LanguageSelect } from "@/components/languageSelect";
import { createCollection } from "@/lib/api";
import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage";
import { DEFAULT_SOURCE_LANG } from "@/lib/languages";
import type { UiLanguage } from "@/types/language";
export function CreateCollectionDialog() {
const router = useRouter();
const t = useTranslations("Collections");
const tc = useTranslations("Common");
const tApi = useTranslations("ApiErrors");
const [open, setOpen] = useState(false);
const [name, setName] = useState("");
const [lang, setLang] = useState<UiLanguage>(DEFAULT_SOURCE_LANG);
const [error, setError] = useState<string | null>(null);
const [isPending, startTransition] = useTransition();
const reset = () => {
setName("");
setLang(DEFAULT_SOURCE_LANG);
setError(null);
};
const handleCreate = () => {
const trimmed = name.trim();
if (!trimmed) return;
setError(null);
startTransition(async () => {
try {
const collection = await createCollection(trimmed, lang.id);
setOpen(false);
reset();
router.push(`/collections/${collection.id}`);
} catch (err) {
setError(resolveApiErrorMessage(err, (kind) => tApi(kind), t("createFailed")));
}
});
};
return (
<Dialog
open={open}
onOpenChange={(next) => {
setOpen(next);
if (!next) reset();
}}
>
<DialogTrigger
render={
<Button variant="outline" size="icon-sm">
<PlusIcon />
</Button>
}
/>
<DialogContent>
<DialogTitle>{t("createTitle")}</DialogTitle>
{error && (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<FieldSet className="w-full">
<FieldGroup>
<Field>
<FieldLabel htmlFor="collection-name">{t("nameLabel")}</FieldLabel>
<Input
id="collection-name"
type="text"
placeholder={t("namePlaceholder")}
value={name}
onChange={(e) => setName(e.target.value)}
disabled={isPending}
/>
</Field>
<Field>
<FieldLabel>{t("languageLabel")}</FieldLabel>
<LanguageSelect value={lang} onChange={setLang} />
</Field>
</FieldGroup>
</FieldSet>
<DialogFooter>
<Button onClick={handleCreate} disabled={isPending || !name.trim()}>
{isPending && <Spinner />}
{tc("create")}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
@@ -0,0 +1,81 @@
import HeaderWithAuth from "@/components/headerWithAuth";
import Main from "@/components/main";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import Link from "next/link";
import { isApiError } from "@/lib/api";
import { getCollections } from "@/lib/serverApi";
import type { CollectionResult } from "@/types/api";
import { formatDate, type RelativeTimeTranslator } from "@/lib/formatDate";
import { getTranslations } from "next-intl/server";
import { CreateCollectionDialog } from "./createCollectionDialog";
type Translator = (key: string, values?: Record<string, string | number | Date>) => string;
function CollectionCard({ collection, tc, tt, tl }: { collection: CollectionResult; tc: Translator; tt: RelativeTimeTranslator; tl: Translator }) {
return (
<Card size="sm" className="justify-between">
<CardHeader>
<div className="flex items-center justify-between">
<CardTitle><h4>{collection.name === "history" ? tc("history") : collection.name}</h4></CardTitle>
<h5 className="flex flex-row gap-0.5 items-center text-muted-foreground">
{tl(collection.lang)}
</h5>
</div>
<CardDescription>
<p>{collection.words.length} {collection.words.length === 1 ? tc("word") : tc("words")} &bull; {tc("updated")} {formatDate(collection.updated_at, tt)}</p>
</CardDescription>
</CardHeader>
{collection.words.length > 0 && (<CardContent>
<div className="flex flex-wrap gap-1.5 items-center">
{[...collection.words].reverse().slice(0, 3).map((w) => (
<Badge key={w} variant="outline">{w}</Badge>
))}
{collection.words.length > 3 && <h6 className="text-muted-foreground">{tc("andMore", { count: collection.words.length - 3 })}</h6>}
</div>
</CardContent>)}
<CardFooter className="flex gap-2">
<Link href={`/collections/${collection.id}`} className="w-full">
<Button variant="outline" size="sm" className="w-full">{tc("seeAll")}</Button>
</Link>
</CardFooter>
</Card>
);
}
export default async function Collections() {
const tt = await getTranslations("Time");
const tc = await getTranslations("Common");
const tl = await getTranslations("Languages");
const tCollections = await getTranslations("Collections");
const tApi = await getTranslations("ApiErrors");
let collections: CollectionResult[] = [];
let error: string | null = null;
try {
collections = await getCollections();
} catch (err) {
error = isApiError(err) ? tApi(err.kind) : tc("somethingWrong");
}
return (
<Main>
<HeaderWithAuth />
<div className="flex flex-row items-center gap-4">
<h2 className="font-semibold">{tCollections("title")}</h2>
<CreateCollectionDialog />
</div>
{error ? (
<p className="text-muted-foreground">{error}</p>
) : (
<div className="grid md:grid-cols-4 gap-4">
{collections.map((c) => (
<CollectionCard key={c.id} collection={c} tc={tc} tt={tt} tl={tl} />
))}
</div>
)}
</Main>
);
}