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,21 @@
|
||||
import { memo } from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
interface ClickableWordProps {
|
||||
word: string;
|
||||
onSelect?: (word: string) => void;
|
||||
}
|
||||
|
||||
export const ClickableWord = memo(function ClickableWord({ word, onSelect }: ClickableWordProps) {
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"-mx-1 px-1 py-0.5 cursor-pointer rounded-sm transition-colors",
|
||||
"hover:bg-accent hover:text-accent-foreground"
|
||||
)}
|
||||
onClick={() => onSelect?.(word)}
|
||||
>
|
||||
{word}
|
||||
</span>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,114 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { getCollectionsByWord } from "@/lib/api";
|
||||
import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage";
|
||||
import { CollectionResult, Fetched, Language } from "@/types/api";
|
||||
import { Item, ItemActions, ItemContent } from "./ui/item";
|
||||
import { Skeleton } from "./ui/skeleton";
|
||||
import { CollectionsListDialog } from "./collectionsListDialog";
|
||||
|
||||
interface CollectionsListProps {
|
||||
word: string;
|
||||
lang: Language;
|
||||
onResult?: (result: Fetched<CollectionResult[]>) => void;
|
||||
}
|
||||
|
||||
type ListState =
|
||||
| { status: "loading" }
|
||||
| { status: "error"; error: unknown }
|
||||
| { status: "done"; collections: CollectionResult[] };
|
||||
|
||||
export function CollectionsList({ word, lang, onResult }: CollectionsListProps) {
|
||||
const [byKey, setByKey] = useState<Record<string, ListState>>({});
|
||||
const key = `${lang}:${word}`;
|
||||
const state = byKey[key] ?? { status: "loading" };
|
||||
|
||||
const onResultRef = useRef(onResult);
|
||||
useEffect(() => {
|
||||
onResultRef.current = onResult;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
||||
getCollectionsByWord(lang, word)
|
||||
.then((data) => {
|
||||
if (cancelled) return;
|
||||
setByKey((prev) => ({ ...prev, [key]: { status: "done", collections: data } }));
|
||||
onResultRef.current?.(
|
||||
Object.assign(data, { fetched_at: new Date().toISOString() })
|
||||
);
|
||||
})
|
||||
.catch((error) => {
|
||||
if (cancelled) return;
|
||||
setByKey((prev) => ({ ...prev, [key]: { status: "error", error } }));
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [lang, word, key]);
|
||||
|
||||
const t = useTranslations("CollectionsList");
|
||||
const tApi = useTranslations("ApiErrors");
|
||||
|
||||
const errorMessage =
|
||||
state.status === "error"
|
||||
? resolveApiErrorMessage(
|
||||
state.error,
|
||||
(kind) => (kind === "not-found" ? t("notFound") : tApi(kind)),
|
||||
t("notFound")
|
||||
)
|
||||
: null;
|
||||
|
||||
const applyOptimistic = (collection: CollectionResult, adding: boolean) => {
|
||||
if (state.status !== "done") return () => {};
|
||||
const previous = state.collections;
|
||||
|
||||
setByKey((prev) => ({
|
||||
...prev,
|
||||
[key]: {
|
||||
status: "done",
|
||||
collections: adding
|
||||
? [...previous, collection]
|
||||
: previous.filter((c) => c.id !== collection.id),
|
||||
},
|
||||
}));
|
||||
|
||||
return () =>
|
||||
setByKey((prev) => ({ ...prev, [key]: { status: "done", collections: previous } }));
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
{state.status === "loading" && (
|
||||
<Item variant="outline" className="w-full">
|
||||
<ItemContent>
|
||||
<Skeleton className="w-4/5 h-4" />
|
||||
<Skeleton className="w-full h-4" />
|
||||
</ItemContent>
|
||||
<ItemActions>
|
||||
<Skeleton className="rounded-lg w-24 h-8" />
|
||||
</ItemActions>
|
||||
</Item>
|
||||
)}
|
||||
|
||||
{state.status === "error" && (
|
||||
<p className="text-muted-foreground">{errorMessage}</p>
|
||||
)}
|
||||
|
||||
{state.status === "done" && (
|
||||
<div className="flex flex-col md:w-1/5 gap-2">
|
||||
<CollectionsListDialog
|
||||
lang={lang}
|
||||
word={word}
|
||||
selectedCollections={state.collections}
|
||||
onOptimisticToggle={applyOptimistic}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ChevronRightIcon, PlusIcon, XIcon } from "lucide-react";
|
||||
import { Dialog, DialogContent, DialogFooter, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
|
||||
import { Alert, AlertDescription } from "@/components/ui/alert";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import { CollectionResult, Language } from "@/types/api";
|
||||
import { addWordsToCollection, deleteWordFromCollection, getCollections } from "@/lib/api";
|
||||
import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage";
|
||||
import { Item, ItemActions, ItemContent, ItemMedia, ItemTitle } from "./ui/item";
|
||||
|
||||
interface CollectionsListDialogProps {
|
||||
lang: Language;
|
||||
word: string;
|
||||
selectedCollections: CollectionResult[];
|
||||
onOptimisticToggle: (collection: CollectionResult, adding: boolean) => () => void;
|
||||
}
|
||||
|
||||
type AllCollectionsState =
|
||||
| { status: "loading" }
|
||||
| { status: "error"; error: unknown }
|
||||
| { status: "done"; collections: CollectionResult[] };
|
||||
|
||||
export function CollectionsListDialog({
|
||||
lang,
|
||||
word,
|
||||
selectedCollections,
|
||||
onOptimisticToggle,
|
||||
}: CollectionsListDialogProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [state, setState] = useState<AllCollectionsState>({ status: "loading" });
|
||||
const [rowErrors, setRowErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const t = useTranslations("CollectionsList");
|
||||
const tc = useTranslations("Common");
|
||||
const tApi = useTranslations("ApiErrors");
|
||||
|
||||
const handleOpenChange = (next: boolean) => {
|
||||
setOpen(next);
|
||||
if (next) setState({ status: "loading" });
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
|
||||
let cancelled = false;
|
||||
|
||||
getCollections()
|
||||
.then((all) => {
|
||||
if (cancelled) return;
|
||||
setState({
|
||||
status: "done",
|
||||
collections: all.filter((c) => c.lang === lang && c.type === "user"),
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
if (!cancelled) setState({ status: "error", error });
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [open, lang]);
|
||||
|
||||
const selectedIds = new Set(selectedCollections.map((c) => c.id));
|
||||
|
||||
const toggle = (collection: CollectionResult) => {
|
||||
const alreadyIn = selectedIds.has(collection.id);
|
||||
setRowErrors((prev) => ({ ...prev, [collection.id]: "" }));
|
||||
|
||||
const rollback = onOptimisticToggle(collection, !alreadyIn);
|
||||
|
||||
const request = alreadyIn
|
||||
? deleteWordFromCollection(collection.id, word)
|
||||
: addWordsToCollection(collection.id, [word]);
|
||||
|
||||
request.catch((error) => {
|
||||
rollback();
|
||||
setRowErrors((prev) => ({
|
||||
...prev,
|
||||
[collection.id]: resolveApiErrorMessage(error, (kind) => tApi(kind), t("toggleFailed")),
|
||||
}));
|
||||
});
|
||||
};
|
||||
|
||||
const errorMessage =
|
||||
state.status === "error"
|
||||
? resolveApiErrorMessage(state.error, (kind) => tApi(kind), t("loadFailed"))
|
||||
: null;
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleOpenChange}>
|
||||
<DialogTrigger>
|
||||
<Item variant="muted" size="sm" className="cursor-pointer">
|
||||
<ItemMedia>
|
||||
<PlusIcon className="size-5" />
|
||||
</ItemMedia>
|
||||
<ItemContent>
|
||||
<ItemTitle>{t("trigger")}</ItemTitle>
|
||||
</ItemContent>
|
||||
<ItemActions>
|
||||
<ChevronRightIcon className="size-4" />
|
||||
</ItemActions>
|
||||
</Item>
|
||||
</DialogTrigger>
|
||||
<DialogContent>
|
||||
<DialogTitle>{t("dialogTitle", { word })}</DialogTitle>
|
||||
|
||||
{state.status === "loading" && (
|
||||
<div className="flex flex-col gap-2">
|
||||
<Item variant="muted">
|
||||
<ItemContent className="flex flex-row items-center justify-between">
|
||||
<Skeleton className="w-1/2 h-4" />
|
||||
<Skeleton className="w-1/4 h-8" />
|
||||
</ItemContent>
|
||||
</Item>
|
||||
<Item variant="muted">
|
||||
<ItemContent className="flex flex-row items-center justify-between">
|
||||
<Skeleton className="w-1/2 h-4" />
|
||||
<Skeleton className="w-1/4 h-8" />
|
||||
</ItemContent>
|
||||
</Item>
|
||||
<Item variant="muted">
|
||||
<ItemContent className="flex flex-row items-center justify-between">
|
||||
<Skeleton className="w-1/2 h-4" />
|
||||
<Skeleton className="w-1/4 h-8" />
|
||||
</ItemContent>
|
||||
</Item>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{state.status === "error" && (
|
||||
<Alert variant="destructive">
|
||||
<AlertDescription>{errorMessage}</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
{state.status === "done" && (
|
||||
<div className="flex flex-col gap-2">
|
||||
{state.collections.length === 0 && (
|
||||
<p className="text-muted-foreground text-sm">{t("empty")}</p>
|
||||
)}
|
||||
{state.collections.map((collection) => {
|
||||
const checked = selectedIds.has(collection.id);
|
||||
const rowError = rowErrors[collection.id];
|
||||
return (
|
||||
<div key={collection.id} className="flex flex-col gap-2">
|
||||
<Item variant="muted">
|
||||
<ItemContent>
|
||||
<ItemTitle>{collection.name}</ItemTitle>
|
||||
</ItemContent>
|
||||
<ItemActions>
|
||||
<Button
|
||||
variant={checked ? "outline" : "default"}
|
||||
size="sm"
|
||||
onClick={() => toggle(collection)}
|
||||
>
|
||||
{checked ? (
|
||||
<>
|
||||
<XIcon className="size-4" /> {t("removeAction")}
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<PlusIcon className="size-4" /> {t("addAction")}
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</ItemActions>
|
||||
</Item>
|
||||
{rowError && <h5 className="text-muted-foreground">{rowError}</h5>}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setOpen(false)}>
|
||||
{tc("close")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback } from "react";
|
||||
import { getCollocations } from "@/lib/api";
|
||||
import { useResource } from "@/hooks/useResource";
|
||||
import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage";
|
||||
import type { CollocationsResult, Fetched, Language } from "@/types/api";
|
||||
import { Spinner } from "./ui/spinner";
|
||||
import { Marker, MarkerContent, MarkerIcon } from "./ui/marker";
|
||||
import { useTranslations } from "next-intl";
|
||||
|
||||
function HighlightWord({ text, word }: { text: string; word: string }) {
|
||||
const escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const regex = new RegExp(`(${escapedWord})`, "gi");
|
||||
|
||||
return (
|
||||
<>
|
||||
{text.split(regex).map((part, i) =>
|
||||
part.toLowerCase() === word.toLowerCase() ? (
|
||||
<span key={i} className="px-1 py-0.5 rounded-sm bg-accent text-accent-foreground">
|
||||
{part}
|
||||
</span>
|
||||
) : (
|
||||
<span key={i}>{part}</span>
|
||||
),
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
interface CollocationsProps {
|
||||
word: string;
|
||||
lang: Language;
|
||||
onResult?: (result: Fetched<CollocationsResult>) => void;
|
||||
}
|
||||
|
||||
export function Collocations({ word, lang, onResult }: CollocationsProps) {
|
||||
const cacheKey = `col:${lang}:${word.toLowerCase()}`;
|
||||
|
||||
const report = useCallback(
|
||||
(data: CollocationsResult) =>
|
||||
onResult?.({
|
||||
...data,
|
||||
fetched_at: new Date().toISOString(),
|
||||
}),
|
||||
[onResult]
|
||||
);
|
||||
|
||||
const state = useResource<CollocationsResult>(
|
||||
cacheKey,
|
||||
() => getCollocations(lang, word),
|
||||
report
|
||||
);
|
||||
|
||||
const t = useTranslations("Collocations");
|
||||
const tApi = useTranslations("ApiErrors");
|
||||
|
||||
const errorMessage = resolveApiErrorMessage(
|
||||
state.error,
|
||||
(kind) => (kind === "not-found" ? t("notFound") : tApi(kind)),
|
||||
t("failed")
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<h3 className="font-semibold">{t("title")}</h3>
|
||||
{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" && (
|
||||
<div className="flex flex-col gap-2">
|
||||
{state.data?.collocations.map((collocation, 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">
|
||||
<p><HighlightWord text={collocation} word={word} /></p>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { CheckIcon, CopyIcon } from "lucide-react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
const COPIED_RESET_MS = 2000;
|
||||
|
||||
interface CopyButtonProps {
|
||||
text: string;
|
||||
}
|
||||
|
||||
export function CopyButton({ text }: CopyButtonProps) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
const t = useTranslations("Common");
|
||||
|
||||
useEffect(() => {
|
||||
if (!copied) return;
|
||||
|
||||
const timeoutId = setTimeout(() => setCopied(false), COPIED_RESET_MS);
|
||||
return () => clearTimeout(timeoutId);
|
||||
}, [copied]);
|
||||
|
||||
const handleCopy = async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
setCopied(true);
|
||||
} catch (err) {
|
||||
console.error("Copy failed:", err);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={handleCopy}
|
||||
disabled={!text}
|
||||
aria-label={t(copied ? "copied" : "copy")}
|
||||
title={t(copied ? "copied" : "copy")}
|
||||
>
|
||||
{copied ? <CheckIcon /> : <CopyIcon />}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback } from "react";
|
||||
import { getExamples } from "@/lib/api";
|
||||
import { useResource } from "@/hooks/useResource";
|
||||
import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage";
|
||||
import type { ExamplesResult, Fetched, Language } from "@/types/api";
|
||||
import { Spinner } from "./ui/spinner";
|
||||
import { Marker, MarkerContent, MarkerIcon } from "./ui/marker";
|
||||
import { useTranslations } from "next-intl";
|
||||
|
||||
function HighlightWord({ text, word }: { text: string; word: string }) {
|
||||
const escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const regex = new RegExp(`(${escapedWord})`, "gi");
|
||||
|
||||
return (
|
||||
<>
|
||||
{text.split(regex).map((part, i) =>
|
||||
part.toLowerCase() === word.toLowerCase() ? (
|
||||
<span key={i} className="px-1 py-0.5 rounded-sm bg-accent text-accent-foreground">
|
||||
{part}
|
||||
</span>
|
||||
) : (
|
||||
<span key={i}>{part}</span>
|
||||
),
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
interface ExamplesProps {
|
||||
word: string;
|
||||
lang: Language;
|
||||
onResult?: (result: Fetched<ExamplesResult>) => void;
|
||||
}
|
||||
|
||||
export function Examples({ word, lang, onResult }: ExamplesProps) {
|
||||
const cacheKey = `example:${lang}:${word.toLowerCase()}`;
|
||||
|
||||
const report = useCallback(
|
||||
(data: ExamplesResult) =>
|
||||
onResult?.({
|
||||
...data,
|
||||
fetched_at: new Date().toISOString(),
|
||||
}),
|
||||
[onResult]
|
||||
);
|
||||
|
||||
const state = useResource<ExamplesResult>(
|
||||
cacheKey,
|
||||
() => getExamples(lang, word),
|
||||
report
|
||||
);
|
||||
|
||||
const t = useTranslations("Examples");
|
||||
const tApi = useTranslations("ApiErrors");
|
||||
|
||||
const errorMessage = resolveApiErrorMessage(
|
||||
state.error,
|
||||
(kind) => (kind === "not-found" ? t("notFound") : tApi(kind)),
|
||||
t("failed")
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<h3 className="font-semibold">{t("title")}</h3>
|
||||
{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" && (
|
||||
<div className="flex flex-col gap-2">
|
||||
{state.data?.examples.map((example, 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">
|
||||
<p><HighlightWord text={example} word={word} /></p>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
"use client";
|
||||
|
||||
import { KeyIcon, ListIcon, LogOutIcon } from "lucide-react";
|
||||
import Logo from "./logo";
|
||||
import { ThemeToggler } from "./themeToggler";
|
||||
import Link from "next/link";
|
||||
import { Button } from "./ui/button";
|
||||
import { LocaleSwitcher } from "./localeSwitcher";
|
||||
import { logout } from "@/app/actions/logout";
|
||||
import { useTranslations } from "next-intl";
|
||||
import type { User } from "@/types/api";
|
||||
|
||||
interface HeaderProps {
|
||||
user: User | null;
|
||||
}
|
||||
|
||||
export default function Header({ user }: HeaderProps) {
|
||||
const t = useTranslations("Header");
|
||||
|
||||
const handleLogout = async () => {
|
||||
await logout();
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex flex-row items-center justify-between">
|
||||
<Logo />
|
||||
<div className="flex flex-row gap-2">
|
||||
{user ? (
|
||||
<>
|
||||
<Button
|
||||
nativeButton={false}
|
||||
render={<Link href="/collections" />}
|
||||
variant="outline"
|
||||
size="icon"
|
||||
aria-label={t("collections")}
|
||||
>
|
||||
<ListIcon />
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={handleLogout}
|
||||
aria-label={t("logout")}
|
||||
>
|
||||
<LogOutIcon />
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<Button
|
||||
nativeButton={false}
|
||||
render={<Link href="/oauth/login" />}
|
||||
size="icon"
|
||||
aria-label={t("login")}
|
||||
>
|
||||
<KeyIcon />
|
||||
</Button>
|
||||
)}
|
||||
<ThemeToggler />
|
||||
<LocaleSwitcher />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
"use client";
|
||||
|
||||
import { useUser } from "@/providers/userProvider";
|
||||
import Header from "./header";
|
||||
|
||||
export default function HeaderWithAuth() {
|
||||
const user = useUser();
|
||||
return <Header user={user} />;
|
||||
}
|
||||
@@ -0,0 +1,337 @@
|
||||
"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 (
|
||||
<Item variant="muted" className="md:w-1/5">
|
||||
<Skeleton className="rounded-lg w-5 h-5" />
|
||||
<ItemContent>
|
||||
<Skeleton className="w-4/5 h-5" />
|
||||
</ItemContent>
|
||||
<ItemActions>
|
||||
<Skeleton className="rounded-lg w-5 h-5" />
|
||||
</ItemActions>
|
||||
</Item>
|
||||
);
|
||||
}
|
||||
|
||||
function WordDefinitionLoading() {
|
||||
const t = useTranslations("Home");
|
||||
|
||||
return (
|
||||
<Marker>
|
||||
<MarkerIcon>
|
||||
<Spinner />
|
||||
</MarkerIcon>
|
||||
<MarkerContent>{t("loadingDefinition")}</MarkerContent>
|
||||
</Marker>
|
||||
);
|
||||
}
|
||||
|
||||
function CollocationsLoading() {
|
||||
const t = useTranslations("Home");
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<h3 className="font-semibold">{t("collocations")}</h3>
|
||||
<Marker>
|
||||
<MarkerIcon>
|
||||
<Spinner />
|
||||
</MarkerIcon>
|
||||
<MarkerContent>{t("generatingCollocations")}</MarkerContent>
|
||||
</Marker>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ExamplesLoading() {
|
||||
const t = useTranslations("Home");
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<h3 className="font-semibold">{t("usageExamples")}</h3>
|
||||
<Marker>
|
||||
<MarkerIcon>
|
||||
<Spinner />
|
||||
</MarkerIcon>
|
||||
<MarkerContent>{t("generatingExamples")}</MarkerContent>
|
||||
</Marker>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const CollectionsListPanel = dynamic(
|
||||
() => import("@/components/collectionsList").then((mod) => mod.CollectionsList),
|
||||
{
|
||||
ssr: false,
|
||||
loading: () => <CollectionsListLoading />,
|
||||
}
|
||||
);
|
||||
|
||||
const WordDefinitionPanel = dynamic(
|
||||
() => import("@/components/wordDefinition").then((mod) => mod.WordDefinition),
|
||||
{
|
||||
ssr: false,
|
||||
loading: () => <WordDefinitionLoading />,
|
||||
}
|
||||
);
|
||||
|
||||
const CollocationsPanel = dynamic(
|
||||
() => import("@/components/collocations").then((mod) => mod.Collocations),
|
||||
{
|
||||
ssr: false,
|
||||
loading: () => <CollocationsLoading />,
|
||||
}
|
||||
);
|
||||
|
||||
const ExamplesPanel = dynamic(
|
||||
() => import("@/components/examples").then((mod) => mod.Examples),
|
||||
{
|
||||
ssr: false,
|
||||
loading: () => <ExamplesLoading />,
|
||||
}
|
||||
);
|
||||
|
||||
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<UiLanguage>(
|
||||
() => languageFromParam(searchParams.get("sourceLang")) ?? rememberedSourceLang ?? DEFAULT_SOURCE_LANG
|
||||
);
|
||||
const [targetLang, setTargetLang] = useState<UiLanguage>(() => {
|
||||
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<Selection | null>(null);
|
||||
const [activeTab, setActiveTab] = useState("definition");
|
||||
|
||||
const [definitionResult, setDefinitionResult] = useState<Fetched<DictionaryResult> | null>(null);
|
||||
const [collocationsResult, setCollocationsResult] = useState<Fetched<CollocationsResult> | null>(null);
|
||||
const [examplesResult, setExamplesResult] = useState<Fetched<ExamplesResult> | 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 (
|
||||
<Main>
|
||||
<Header user={user} />
|
||||
<div className="flex flex-col md:flex-row gap-2 w-full items-stretch">
|
||||
<TranslationCard
|
||||
variant="source"
|
||||
lang={sourceLang}
|
||||
onLangChange={handleSourceChange}
|
||||
excludeLangId={targetLang.id}
|
||||
placeholder={t("sourcePlaceholder")}
|
||||
text={sourceText}
|
||||
onTextChange={setSourceText}
|
||||
onSelectWord={selectSourceWord}
|
||||
/>
|
||||
<Button variant="outline" size="icon-lg" className="self-center shrink-0" onClick={handleSwap}>
|
||||
<ArrowLeftRightIcon />
|
||||
</Button>
|
||||
<TranslationCard
|
||||
variant="target"
|
||||
lang={targetLang}
|
||||
onLangChange={handleTargetChange}
|
||||
excludeLangId={sourceLang.id}
|
||||
placeholder={t("translatedPlaceholder")}
|
||||
text={translatedText}
|
||||
isLoading={isLoading}
|
||||
error={error}
|
||||
onSelectWord={selectTargetWord}
|
||||
footerExtra={
|
||||
result && (
|
||||
<PropertyDialog
|
||||
response={result}
|
||||
definition={debouncedDefinition ? definitionResult : null}
|
||||
examples={debouncedDefinition ? examplesResult : null}
|
||||
collocations={debouncedDefinition ? collocationsResult : null}
|
||||
/>
|
||||
)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
{debouncedDefinition && <CollectionsListPanel lang={debouncedDefinition.lang} word={debouncedDefinition.word} />}
|
||||
{debouncedDefinition && (
|
||||
<Tabs value={activeTab} onValueChange={handleTabChange}>
|
||||
<TabsList>
|
||||
<TabsTrigger value="definition">{t("definition")}</TabsTrigger>
|
||||
<TabsTrigger value="examples">{t("usageExamples")}</TabsTrigger>
|
||||
<TabsTrigger value="collocations">{t("collocations")}</TabsTrigger>
|
||||
</TabsList>
|
||||
<TabsContent value="definition">
|
||||
<Card>
|
||||
<CardContent className="flex flex-col gap-4">
|
||||
<WordDefinitionPanel
|
||||
key={`${debouncedDefinition.lang}:${debouncedDefinition.word}:definition`}
|
||||
word={debouncedDefinition.word}
|
||||
lang={debouncedDefinition.lang}
|
||||
onResult={setDefinitionResult}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
<TabsContent value="collocations">
|
||||
<Card>
|
||||
<CardContent className="flex flex-col gap-4">
|
||||
<CollocationsPanel
|
||||
key={`${debouncedDefinition.lang}:${debouncedDefinition.word}:collocations`}
|
||||
word={debouncedDefinition.word}
|
||||
lang={debouncedDefinition.lang}
|
||||
onResult={setCollocationsResult}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
<TabsContent value="examples">
|
||||
<Card>
|
||||
<CardContent className="flex flex-col gap-4">
|
||||
<ExamplesPanel
|
||||
key={`${debouncedDefinition.lang}:${debouncedDefinition.word}:examples`}
|
||||
word={debouncedDefinition.word}
|
||||
lang={debouncedDefinition.lang}
|
||||
onResult={setExamplesResult}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
)}
|
||||
</Main>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import { Card, CardContent, CardHeader } from "@/components/ui/card";
|
||||
|
||||
export function HomeSkeleton() {
|
||||
return (
|
||||
<div className="flex flex-col gap-8 px-6 py-8">
|
||||
<div className="flex flex-row items-center justify-between">
|
||||
<Skeleton className="w-24 h-8" />
|
||||
<div className="flex flex-row gap-2">
|
||||
<Skeleton className="size-8 rounded-lg" />
|
||||
<Skeleton className="size-8 rounded-lg" />
|
||||
<Skeleton className="size-8 rounded-lg" />
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col md:flex-row gap-2 w-full items-stretch">
|
||||
<Card className="w-full md:w-1/2">
|
||||
<CardHeader>
|
||||
<Skeleton className="w-24 h-6" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Skeleton className="w-full h-32" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Skeleton className="self-center size-9 rounded-lg shrink-0" />
|
||||
<Card className="w-full md:w-1/2">
|
||||
<CardHeader>
|
||||
<Skeleton className="w-24 h-6" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Skeleton className="w-full h-32" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
<Card className="w-full">
|
||||
<CardContent>
|
||||
<Skeleton className="w-1/2 h-6" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
"use client";
|
||||
|
||||
import Image from "next/image";
|
||||
import { LANGUAGES } from "@/lib/languages";
|
||||
import type { UiLanguage } from "@/types/language";
|
||||
import { useTranslations } from "next-intl";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectGroup,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
|
||||
interface LanguageSelectProps {
|
||||
value: UiLanguage;
|
||||
onChange: (lang: UiLanguage) => void;
|
||||
excludeId?: UiLanguage["id"];
|
||||
}
|
||||
|
||||
export function LanguageSelect({ value, onChange, excludeId }: LanguageSelectProps) {
|
||||
const t = useTranslations("Languages");
|
||||
const options = LANGUAGES.filter((l) => l.id !== excludeId);
|
||||
|
||||
return (
|
||||
<Select
|
||||
value={value.id}
|
||||
onValueChange={(id) =>
|
||||
onChange(options.find((l) => l.id === id) ?? options[0])
|
||||
}
|
||||
>
|
||||
<SelectTrigger className="w-fit border-none shadow-none px-0 dark:bg-transparent dark:hover:bg-transparent">
|
||||
<SelectValue>
|
||||
<div className="flex flex-row gap-2.5 items-center">
|
||||
<Image src={value.flag} alt={value.autonym} width={32} height={16} className="rounded-xs" />
|
||||
<h3 className="font-medium">{t(value.id)}</h3>
|
||||
</div>
|
||||
</SelectValue>
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
{options.map((l) => (
|
||||
<SelectItem key={l.id} value={l.id}>
|
||||
<div className="flex flex-row items-center gap-2.5">
|
||||
<Image src={l.flag} alt={l.autonym} width={20} height={12} className="rounded-xs" />
|
||||
<div className="flex flex-col gap-0">
|
||||
<p className="font-medium">{t(l.id)}</p>
|
||||
<h5>{l.autonym}</h5>
|
||||
</div>
|
||||
</div>
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useLocale, useTranslations } from "next-intl";
|
||||
import { LanguagesIcon } from "lucide-react";
|
||||
import { setLocale } from "@/app/actions/setLocale";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import { LANGUAGE_CODES } from "@/types/api";
|
||||
|
||||
export function LocaleSwitcher() {
|
||||
const locale = useLocale();
|
||||
const t = useTranslations("Locale");
|
||||
const router = useRouter();
|
||||
|
||||
const handleChange = async (next: string) => {
|
||||
if (next === locale) return;
|
||||
await setLocale(next);
|
||||
router.refresh();
|
||||
};
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger
|
||||
render={
|
||||
<Button variant="outline" size="icon" aria-label={t("switch")}>
|
||||
<LanguagesIcon />
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
<DropdownMenuContent align="end">
|
||||
{LANGUAGE_CODES.map((code) => (
|
||||
<DropdownMenuItem key={code} onClick={() => handleChange(code)}>
|
||||
{t(code)}
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Merriweather } from "next/font/google";
|
||||
import Link from "next/link";
|
||||
|
||||
const merriweather = Merriweather({
|
||||
weight: "700",
|
||||
subsets: ["latin"]
|
||||
});
|
||||
|
||||
export default function Logo() {
|
||||
return (
|
||||
<Link href="/">
|
||||
<h1 className={merriweather.className}>Fancy</h1>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export default function Main({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<div className="flex flex-col gap-8 px-6 py-8">
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
"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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import { ThemeProvider as NextThemesProvider } from "next-themes"
|
||||
|
||||
export function ThemeProvider({
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<typeof NextThemesProvider>) {
|
||||
return <NextThemesProvider {...props}>{children}</NextThemesProvider>
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
"use client"
|
||||
|
||||
import { MoonIcon, SunIcon } from "lucide-react";
|
||||
import { useTheme } from "next-themes";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import { useTranslations } from "next-intl";
|
||||
|
||||
export function ThemeToggler() {
|
||||
const { setTheme } = useTheme();
|
||||
const t = useTranslations("Theme");
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger
|
||||
render={
|
||||
<Button variant="outline" size="icon" className="relative" aria-label={t("toggle")}>
|
||||
<SunIcon className="h-[1.2rem] w-[1.2rem] scale-100 rotate-0 transition-all dark:scale-0 dark:-rotate-90" />
|
||||
<MoonIcon className="absolute h-[1.2rem] w-[1.2rem] scale-0 rotate-90 transition-all dark:scale-100 dark:rotate-0" />
|
||||
<span className="sr-only">{t("toggle")}</span>
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={() => setTheme("light")}>
|
||||
{t("light")}
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setTheme("dark")}>
|
||||
{t("dark")}
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setTheme("system")}>
|
||||
{t("system")}
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { memo, useMemo } from "react";
|
||||
import { ClickableWord } from "@/components/clickableWord";
|
||||
|
||||
const TOKEN_PATTERN = /[\p{L}\p{M}\p{N}]+(?:['’-][\p{L}\p{M}\p{N}]+)*|[^\p{L}\p{M}\p{N}\s]|\s+/gu;
|
||||
const WORD_PATTERN = /^[\p{L}\p{M}\p{N}]+(?:['’-][\p{L}\p{M}\p{N}]+)*$/u;
|
||||
|
||||
interface TokenizedTextProps {
|
||||
text: string;
|
||||
onSelect?: (word: string) => void;
|
||||
}
|
||||
|
||||
export const TokenizedText = memo(function TokenizedText({ text, onSelect }: TokenizedTextProps) {
|
||||
const tokens = useMemo(() => text.match(TOKEN_PATTERN) ?? [], [text]);
|
||||
|
||||
return (
|
||||
<>
|
||||
{tokens.map((token, i) =>
|
||||
WORD_PATTERN.test(token) ? (
|
||||
<ClickableWord key={i} word={token} onSelect={onSelect} />
|
||||
) : (
|
||||
<span key={i}>{token}</span>
|
||||
)
|
||||
)}
|
||||
</>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,116 @@
|
||||
"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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import * as React from "react"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const alertVariants = cva(
|
||||
"group/alert relative grid w-full gap-0.5 rounded-lg border px-2.5 py-2 text-left text-sm has-data-[slot=alert-action]:relative has-data-[slot=alert-action]:pr-18 has-[>svg]:grid-cols-[auto_1fr] has-[>svg]:gap-x-2 *:[svg]:row-span-2 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-card text-card-foreground",
|
||||
destructive:
|
||||
"bg-card text-destructive *:data-[slot=alert-description]:text-destructive/90 *:[svg]:text-current",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function Alert({
|
||||
className,
|
||||
variant,
|
||||
...props
|
||||
}: React.ComponentProps<"div"> & VariantProps<typeof alertVariants>) {
|
||||
return (
|
||||
<div
|
||||
data-slot="alert"
|
||||
role="alert"
|
||||
className={cn(alertVariants({ variant }), className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="alert-title"
|
||||
className={cn(
|
||||
"font-medium group-has-[>svg]/alert:col-start-2 [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function AlertDescription({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="alert-description"
|
||||
className={cn(
|
||||
"text-sm text-balance text-muted-foreground md:text-pretty [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p:not(:last-child)]:mb-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function AlertAction({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="alert-action"
|
||||
className={cn("absolute top-2 right-2", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Alert, AlertTitle, AlertDescription, AlertAction }
|
||||
@@ -0,0 +1,52 @@
|
||||
import { mergeProps } from "@base-ui/react/merge-props"
|
||||
import { useRender } from "@base-ui/react/use-render"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const badgeVariants = cva(
|
||||
"group/badge inline-flex h-5 w-fit shrink-0 items-center justify-center gap-1 overflow-hidden rounded-4xl border border-transparent px-2 py-0.5 text-xs font-medium whitespace-nowrap transition-all focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 [&>svg]:pointer-events-none [&>svg]:size-3!",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-primary text-primary-foreground [a]:hover:bg-primary/80",
|
||||
secondary:
|
||||
"bg-secondary text-secondary-foreground [a]:hover:bg-secondary/80",
|
||||
destructive:
|
||||
"bg-destructive/10 text-destructive focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:focus-visible:ring-destructive/40 [a]:hover:bg-destructive/20",
|
||||
outline:
|
||||
"border-border text-foreground [a]:hover:bg-muted [a]:hover:text-muted-foreground",
|
||||
ghost:
|
||||
"hover:bg-muted hover:text-muted-foreground dark:hover:bg-muted/50",
|
||||
link: "text-primary underline-offset-4 hover:underline",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function Badge({
|
||||
className,
|
||||
variant = "default",
|
||||
render,
|
||||
...props
|
||||
}: useRender.ComponentProps<"span"> & VariantProps<typeof badgeVariants>) {
|
||||
return useRender({
|
||||
defaultTagName: "span",
|
||||
props: mergeProps<"span">(
|
||||
{
|
||||
className: cn(badgeVariants({ variant }), className),
|
||||
},
|
||||
props
|
||||
),
|
||||
render,
|
||||
state: {
|
||||
slot: "badge",
|
||||
variant,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export { Badge, badgeVariants }
|
||||
@@ -0,0 +1,58 @@
|
||||
import { Button as ButtonPrimitive } from "@base-ui/react/button"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const buttonVariants = cva(
|
||||
"group/button inline-flex shrink-0 items-center justify-center rounded-lg border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-primary text-primary-foreground hover:bg-primary/80",
|
||||
outline:
|
||||
"border-border bg-background hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
|
||||
secondary:
|
||||
"bg-secondary text-secondary-foreground hover:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)] aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
|
||||
ghost:
|
||||
"hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
|
||||
destructive:
|
||||
"bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
|
||||
link: "text-primary underline-offset-4 hover:underline",
|
||||
},
|
||||
size: {
|
||||
default:
|
||||
"h-8 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
||||
xs: "h-6 gap-1 rounded-[min(var(--radius-md),10px)] px-2 text-xs in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
|
||||
sm: "h-7 gap-1 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
|
||||
lg: "h-9 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
||||
icon: "size-8",
|
||||
"icon-xs":
|
||||
"size-6 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-lg [&_svg:not([class*='size-'])]:size-3",
|
||||
"icon-sm":
|
||||
"size-7 rounded-[min(var(--radius-md),12px)] in-data-[slot=button-group]:rounded-lg [&_svg:not([class*='size-'])]:size-3.5",
|
||||
"icon-lg": "size-9",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "default",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function Button({
|
||||
className,
|
||||
variant = "default",
|
||||
size = "default",
|
||||
...props
|
||||
}: ButtonPrimitive.Props & VariantProps<typeof buttonVariants>) {
|
||||
return (
|
||||
<ButtonPrimitive
|
||||
data-slot="button"
|
||||
className={cn(buttonVariants({ variant, size, className }))}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Button, buttonVariants }
|
||||
@@ -0,0 +1,103 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Card({
|
||||
className,
|
||||
size = "default",
|
||||
...props
|
||||
}: React.ComponentProps<"div"> & { size?: "default" | "sm" }) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card"
|
||||
data-size={size}
|
||||
className={cn(
|
||||
"group/card flex flex-col gap-(--card-spacing) overflow-hidden rounded-xl bg-card py-(--card-spacing) text-sm text-card-foreground ring-1 ring-foreground/10 [--card-spacing:--spacing(4)] has-data-[slot=card-footer]:pb-0 has-[>img:first-child]:pt-0 data-[size=sm]:[--card-spacing:--spacing(3)] data-[size=sm]:has-data-[slot=card-footer]:pb-0 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardHeader({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-header"
|
||||
className={cn(
|
||||
"group/card-header @container/card-header grid auto-rows-min items-start gap-1 rounded-t-xl px-(--card-spacing) has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto] [.border-b]:pb-(--card-spacing)",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardTitle({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-title"
|
||||
className={cn(
|
||||
"font-heading text-base leading-snug font-medium group-data-[size=sm]/card:text-sm",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardDescription({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-description"
|
||||
className={cn("text-sm text-muted-foreground", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardAction({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-action"
|
||||
className={cn(
|
||||
"col-start-2 row-span-2 row-start-1 self-start justify-self-end",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardContent({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-content"
|
||||
className={cn("px-(--card-spacing)", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function CardFooter({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="card-footer"
|
||||
className={cn(
|
||||
"flex items-center rounded-b-xl border-t bg-muted/50 p-(--card-spacing)",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Card,
|
||||
CardHeader,
|
||||
CardFooter,
|
||||
CardTitle,
|
||||
CardAction,
|
||||
CardDescription,
|
||||
CardContent,
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import { Dialog as DialogPrimitive } from "@base-ui/react/dialog"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { XIcon } from "lucide-react"
|
||||
import { useTranslations } from "next-intl"
|
||||
|
||||
function Dialog({ ...props }: DialogPrimitive.Root.Props) {
|
||||
return <DialogPrimitive.Root data-slot="dialog" {...props} />
|
||||
}
|
||||
|
||||
function DialogTrigger({ ...props }: DialogPrimitive.Trigger.Props) {
|
||||
return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />
|
||||
}
|
||||
|
||||
function DialogPortal({ ...props }: DialogPrimitive.Portal.Props) {
|
||||
return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />
|
||||
}
|
||||
|
||||
function DialogClose({ ...props }: DialogPrimitive.Close.Props) {
|
||||
return <DialogPrimitive.Close data-slot="dialog-close" {...props} />
|
||||
}
|
||||
|
||||
function DialogOverlay({
|
||||
className,
|
||||
...props
|
||||
}: DialogPrimitive.Backdrop.Props) {
|
||||
return (
|
||||
<DialogPrimitive.Backdrop
|
||||
data-slot="dialog-overlay"
|
||||
className={cn(
|
||||
"fixed inset-0 isolate z-50 bg-black/10 duration-100 supports-backdrop-filter:backdrop-blur-xs data-open:animate-in data-open:fade-in-0 data-closed:animate-out data-closed:fade-out-0",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogContent({
|
||||
className,
|
||||
children,
|
||||
showCloseButton = true,
|
||||
...props
|
||||
}: DialogPrimitive.Popup.Props & {
|
||||
showCloseButton?: boolean
|
||||
}) {
|
||||
const t = useTranslations("Common")
|
||||
|
||||
return (
|
||||
<DialogPortal>
|
||||
<DialogOverlay />
|
||||
<DialogPrimitive.Popup
|
||||
data-slot="dialog-content"
|
||||
className={cn(
|
||||
"fixed top-1/2 left-1/2 z-50 grid w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 gap-4 rounded-xl bg-popover p-4 text-sm text-popover-foreground ring-1 ring-foreground/10 duration-100 outline-none sm:max-w-sm data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
{showCloseButton && (
|
||||
<DialogPrimitive.Close
|
||||
data-slot="dialog-close"
|
||||
render={
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="absolute top-2 right-2"
|
||||
size="icon-sm"
|
||||
/>
|
||||
}
|
||||
>
|
||||
<XIcon
|
||||
/>
|
||||
<span className="sr-only">{t("close")}</span>
|
||||
</DialogPrimitive.Close>
|
||||
)}
|
||||
</DialogPrimitive.Popup>
|
||||
</DialogPortal>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="dialog-header"
|
||||
className={cn("flex flex-col gap-2", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogFooter({
|
||||
className,
|
||||
showCloseButton = false,
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<"div"> & {
|
||||
showCloseButton?: boolean
|
||||
}) {
|
||||
const t = useTranslations("Common")
|
||||
|
||||
return (
|
||||
<div
|
||||
data-slot="dialog-footer"
|
||||
className={cn(
|
||||
"-mx-4 -mb-4 flex flex-col-reverse gap-2 rounded-b-xl border-t bg-muted/50 p-4 sm:flex-row sm:justify-end",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
{showCloseButton && (
|
||||
<DialogPrimitive.Close render={<Button variant="outline" />}>
|
||||
{t("close")}
|
||||
</DialogPrimitive.Close>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogTitle({ className, ...props }: DialogPrimitive.Title.Props) {
|
||||
return (
|
||||
<DialogPrimitive.Title
|
||||
data-slot="dialog-title"
|
||||
className={cn(
|
||||
"font-heading text-base leading-none font-medium",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DialogDescription({
|
||||
className,
|
||||
...props
|
||||
}: DialogPrimitive.Description.Props) {
|
||||
return (
|
||||
<DialogPrimitive.Description
|
||||
data-slot="dialog-description"
|
||||
className={cn(
|
||||
"text-sm text-muted-foreground *:[a]:underline *:[a]:underline-offset-3 *:[a]:hover:text-foreground",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogOverlay,
|
||||
DialogPortal,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import { Menu as MenuPrimitive } from "@base-ui/react/menu"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
import { ChevronRightIcon, CheckIcon } from "lucide-react"
|
||||
|
||||
function DropdownMenu({ ...props }: MenuPrimitive.Root.Props) {
|
||||
return <MenuPrimitive.Root data-slot="dropdown-menu" {...props} />
|
||||
}
|
||||
|
||||
function DropdownMenuPortal({ ...props }: MenuPrimitive.Portal.Props) {
|
||||
return <MenuPrimitive.Portal data-slot="dropdown-menu-portal" {...props} />
|
||||
}
|
||||
|
||||
function DropdownMenuTrigger({ ...props }: MenuPrimitive.Trigger.Props) {
|
||||
return <MenuPrimitive.Trigger data-slot="dropdown-menu-trigger" {...props} />
|
||||
}
|
||||
|
||||
function DropdownMenuContent({
|
||||
align = "start",
|
||||
alignOffset = 0,
|
||||
side = "bottom",
|
||||
sideOffset = 4,
|
||||
className,
|
||||
...props
|
||||
}: MenuPrimitive.Popup.Props &
|
||||
Pick<
|
||||
MenuPrimitive.Positioner.Props,
|
||||
"align" | "alignOffset" | "side" | "sideOffset"
|
||||
>) {
|
||||
return (
|
||||
<MenuPrimitive.Portal>
|
||||
<MenuPrimitive.Positioner
|
||||
className="isolate z-50 outline-none"
|
||||
align={align}
|
||||
alignOffset={alignOffset}
|
||||
side={side}
|
||||
sideOffset={sideOffset}
|
||||
>
|
||||
<MenuPrimitive.Popup
|
||||
data-slot="dropdown-menu-content"
|
||||
className={cn("z-50 max-h-(--available-height) w-(--anchor-width) min-w-32 origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-lg bg-popover p-1 text-popover-foreground shadow-md ring-1 ring-foreground/10 duration-100 outline-none data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:overflow-hidden data-closed:fade-out-0 data-closed:zoom-out-95", className )}
|
||||
{...props}
|
||||
/>
|
||||
</MenuPrimitive.Positioner>
|
||||
</MenuPrimitive.Portal>
|
||||
)
|
||||
}
|
||||
|
||||
function DropdownMenuGroup({ ...props }: MenuPrimitive.Group.Props) {
|
||||
return <MenuPrimitive.Group data-slot="dropdown-menu-group" {...props} />
|
||||
}
|
||||
|
||||
function DropdownMenuLabel({
|
||||
className,
|
||||
inset,
|
||||
...props
|
||||
}: MenuPrimitive.GroupLabel.Props & {
|
||||
inset?: boolean
|
||||
}) {
|
||||
return (
|
||||
<MenuPrimitive.GroupLabel
|
||||
data-slot="dropdown-menu-label"
|
||||
data-inset={inset}
|
||||
className={cn(
|
||||
"px-1.5 py-1 text-xs font-medium text-muted-foreground data-inset:pl-7",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DropdownMenuItem({
|
||||
className,
|
||||
inset,
|
||||
variant = "default",
|
||||
...props
|
||||
}: MenuPrimitive.Item.Props & {
|
||||
inset?: boolean
|
||||
variant?: "default" | "destructive"
|
||||
}) {
|
||||
return (
|
||||
<MenuPrimitive.Item
|
||||
data-slot="dropdown-menu-item"
|
||||
data-inset={inset}
|
||||
data-variant={variant}
|
||||
className={cn(
|
||||
"group/dropdown-menu-item relative flex cursor-default items-center gap-1.5 rounded-md px-1.5 py-1 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-inset:pl-7 data-[variant=destructive]:text-destructive data-[variant=destructive]:focus:bg-destructive/10 data-[variant=destructive]:focus:text-destructive dark:data-[variant=destructive]:focus:bg-destructive/20 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 data-[variant=destructive]:*:[svg]:text-destructive",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DropdownMenuSub({ ...props }: MenuPrimitive.SubmenuRoot.Props) {
|
||||
return <MenuPrimitive.SubmenuRoot data-slot="dropdown-menu-sub" {...props} />
|
||||
}
|
||||
|
||||
function DropdownMenuSubTrigger({
|
||||
className,
|
||||
inset,
|
||||
children,
|
||||
...props
|
||||
}: MenuPrimitive.SubmenuTrigger.Props & {
|
||||
inset?: boolean
|
||||
}) {
|
||||
return (
|
||||
<MenuPrimitive.SubmenuTrigger
|
||||
data-slot="dropdown-menu-sub-trigger"
|
||||
data-inset={inset}
|
||||
className={cn(
|
||||
"flex cursor-default items-center gap-1.5 rounded-md px-1.5 py-1 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-inset:pl-7 data-popup-open:bg-accent data-popup-open:text-accent-foreground data-open:bg-accent data-open:text-accent-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<ChevronRightIcon className="ml-auto" />
|
||||
</MenuPrimitive.SubmenuTrigger>
|
||||
)
|
||||
}
|
||||
|
||||
function DropdownMenuSubContent({
|
||||
align = "start",
|
||||
alignOffset = -3,
|
||||
side = "right",
|
||||
sideOffset = 0,
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof DropdownMenuContent>) {
|
||||
return (
|
||||
<DropdownMenuContent
|
||||
data-slot="dropdown-menu-sub-content"
|
||||
className={cn("w-auto min-w-[96px] rounded-lg bg-popover p-1 text-popover-foreground shadow-lg ring-1 ring-foreground/10 duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95", className )}
|
||||
align={align}
|
||||
alignOffset={alignOffset}
|
||||
side={side}
|
||||
sideOffset={sideOffset}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DropdownMenuCheckboxItem({
|
||||
className,
|
||||
children,
|
||||
checked,
|
||||
inset,
|
||||
...props
|
||||
}: MenuPrimitive.CheckboxItem.Props & {
|
||||
inset?: boolean
|
||||
}) {
|
||||
return (
|
||||
<MenuPrimitive.CheckboxItem
|
||||
data-slot="dropdown-menu-checkbox-item"
|
||||
data-inset={inset}
|
||||
className={cn(
|
||||
"relative flex cursor-default items-center gap-1.5 rounded-md py-1 pr-8 pl-1.5 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground focus:**:text-accent-foreground data-inset:pl-7 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
checked={checked}
|
||||
{...props}
|
||||
>
|
||||
<span
|
||||
className="pointer-events-none absolute right-2 flex items-center justify-center"
|
||||
data-slot="dropdown-menu-checkbox-item-indicator"
|
||||
>
|
||||
<MenuPrimitive.CheckboxItemIndicator>
|
||||
<CheckIcon
|
||||
/>
|
||||
</MenuPrimitive.CheckboxItemIndicator>
|
||||
</span>
|
||||
{children}
|
||||
</MenuPrimitive.CheckboxItem>
|
||||
)
|
||||
}
|
||||
|
||||
function DropdownMenuRadioGroup({ ...props }: MenuPrimitive.RadioGroup.Props) {
|
||||
return (
|
||||
<MenuPrimitive.RadioGroup
|
||||
data-slot="dropdown-menu-radio-group"
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DropdownMenuRadioItem({
|
||||
className,
|
||||
children,
|
||||
inset,
|
||||
...props
|
||||
}: MenuPrimitive.RadioItem.Props & {
|
||||
inset?: boolean
|
||||
}) {
|
||||
return (
|
||||
<MenuPrimitive.RadioItem
|
||||
data-slot="dropdown-menu-radio-item"
|
||||
data-inset={inset}
|
||||
className={cn(
|
||||
"relative flex cursor-default items-center gap-1.5 rounded-md py-1 pr-8 pl-1.5 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground focus:**:text-accent-foreground data-inset:pl-7 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<span
|
||||
className="pointer-events-none absolute right-2 flex items-center justify-center"
|
||||
data-slot="dropdown-menu-radio-item-indicator"
|
||||
>
|
||||
<MenuPrimitive.RadioItemIndicator>
|
||||
<CheckIcon
|
||||
/>
|
||||
</MenuPrimitive.RadioItemIndicator>
|
||||
</span>
|
||||
{children}
|
||||
</MenuPrimitive.RadioItem>
|
||||
)
|
||||
}
|
||||
|
||||
function DropdownMenuSeparator({
|
||||
className,
|
||||
...props
|
||||
}: MenuPrimitive.Separator.Props) {
|
||||
return (
|
||||
<MenuPrimitive.Separator
|
||||
data-slot="dropdown-menu-separator"
|
||||
className={cn("-mx-1 my-1 h-px bg-border", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function DropdownMenuShortcut({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<"span">) {
|
||||
return (
|
||||
<span
|
||||
data-slot="dropdown-menu-shortcut"
|
||||
className={cn(
|
||||
"ml-auto text-xs tracking-widest text-muted-foreground group-focus/dropdown-menu-item:text-accent-foreground",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
DropdownMenu,
|
||||
DropdownMenuPortal,
|
||||
DropdownMenuTrigger,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuGroup,
|
||||
DropdownMenuLabel,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuCheckboxItem,
|
||||
DropdownMenuRadioGroup,
|
||||
DropdownMenuRadioItem,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuShortcut,
|
||||
DropdownMenuSub,
|
||||
DropdownMenuSubTrigger,
|
||||
DropdownMenuSubContent,
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
"use client"
|
||||
|
||||
import { useMemo } from "react"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
import { Label } from "@/components/ui/label"
|
||||
import { Separator } from "@/components/ui/separator"
|
||||
|
||||
function FieldSet({ className, ...props }: React.ComponentProps<"fieldset">) {
|
||||
return (
|
||||
<fieldset
|
||||
data-slot="field-set"
|
||||
className={cn(
|
||||
"flex flex-col gap-4 has-[>[data-slot=checkbox-group]]:gap-3 has-[>[data-slot=radio-group]]:gap-3",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function FieldLegend({
|
||||
className,
|
||||
variant = "legend",
|
||||
...props
|
||||
}: React.ComponentProps<"legend"> & { variant?: "legend" | "label" }) {
|
||||
return (
|
||||
<legend
|
||||
data-slot="field-legend"
|
||||
data-variant={variant}
|
||||
className={cn(
|
||||
"mb-1.5 font-medium data-[variant=label]:text-sm data-[variant=legend]:text-base",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function FieldGroup({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="field-group"
|
||||
className={cn(
|
||||
"group/field-group @container/field-group flex w-full flex-col gap-5 data-[slot=checkbox-group]:gap-3 *:data-[slot=field-group]:gap-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const fieldVariants = cva(
|
||||
"group/field flex w-full gap-2 data-[invalid=true]:text-destructive",
|
||||
{
|
||||
variants: {
|
||||
orientation: {
|
||||
vertical: "flex-col *:w-full [&>.sr-only]:w-auto",
|
||||
horizontal:
|
||||
"flex-row items-center has-[>[data-slot=field-content]]:items-start *:data-[slot=field-label]:flex-auto has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
|
||||
responsive:
|
||||
"flex-col *:w-full @md/field-group:flex-row @md/field-group:items-center @md/field-group:*:w-auto @md/field-group:has-[>[data-slot=field-content]]:items-start @md/field-group:*:data-[slot=field-label]:flex-auto [&>.sr-only]:w-auto @md/field-group:has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
orientation: "vertical",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function Field({
|
||||
className,
|
||||
orientation = "vertical",
|
||||
...props
|
||||
}: React.ComponentProps<"div"> & VariantProps<typeof fieldVariants>) {
|
||||
return (
|
||||
<div
|
||||
role="group"
|
||||
data-slot="field"
|
||||
data-orientation={orientation}
|
||||
className={cn(fieldVariants({ orientation }), className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function FieldContent({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="field-content"
|
||||
className={cn(
|
||||
"group/field-content flex flex-1 flex-col gap-0.5 leading-snug",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function FieldLabel({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof Label>) {
|
||||
return (
|
||||
<Label
|
||||
data-slot="field-label"
|
||||
className={cn(
|
||||
"group/field-label peer/field-label flex w-fit gap-2 leading-snug group-data-[disabled=true]/field:opacity-50 has-data-checked:border-primary/30 has-data-checked:bg-primary/5 has-[>[data-slot=field]]:rounded-lg has-[>[data-slot=field]]:border has-[>[data-slot=field]]:not-has-[:disabled,[data-disabled]]:hover:bg-muted/50 has-[>[data-slot=field]]:has-[:focus-visible]:border-ring has-[>[data-slot=field]]:has-[:focus-visible]:ring-3 has-[>[data-slot=field]]:has-[:focus-visible]:ring-ring/50 *:data-[slot=field]:p-2.5 dark:has-data-checked:border-primary/20 dark:has-data-checked:bg-primary/10",
|
||||
"has-[>[data-slot=field]]:w-full has-[>[data-slot=field]]:flex-col",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function FieldTitle({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="field-label"
|
||||
className={cn(
|
||||
"flex w-fit items-center gap-2 text-sm font-medium group-data-[disabled=true]/field:opacity-50",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function FieldDescription({ className, ...props }: React.ComponentProps<"p">) {
|
||||
return (
|
||||
<p
|
||||
data-slot="field-description"
|
||||
className={cn(
|
||||
"text-left text-sm leading-normal font-normal text-muted-foreground group-has-data-horizontal/field:text-balance [[data-variant=legend]+&]:-mt-1.5",
|
||||
"last:mt-0 nth-last-2:-mt-1",
|
||||
"[&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function FieldSeparator({
|
||||
children,
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<"div"> & {
|
||||
children?: React.ReactNode
|
||||
}) {
|
||||
return (
|
||||
<div
|
||||
data-slot="field-separator"
|
||||
data-content={!!children}
|
||||
className={cn(
|
||||
"relative -my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<Separator className="absolute inset-0 top-1/2" />
|
||||
{children && (
|
||||
<span
|
||||
className="relative mx-auto block w-fit bg-background px-2 text-muted-foreground"
|
||||
data-slot="field-separator-content"
|
||||
>
|
||||
{children}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function FieldError({
|
||||
className,
|
||||
children,
|
||||
errors,
|
||||
...props
|
||||
}: React.ComponentProps<"div"> & {
|
||||
errors?: Array<{ message?: string } | undefined>
|
||||
}) {
|
||||
const content = useMemo(() => {
|
||||
if (children) {
|
||||
return children
|
||||
}
|
||||
|
||||
if (!errors?.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
const uniqueErrors = [
|
||||
...new Map(errors.map((error) => [error?.message, error])).values(),
|
||||
]
|
||||
|
||||
if (uniqueErrors?.length === 1) {
|
||||
return uniqueErrors[0]?.message
|
||||
}
|
||||
|
||||
return (
|
||||
<ul className="ml-4 flex list-disc flex-col gap-1">
|
||||
{uniqueErrors.map(
|
||||
(error, index) =>
|
||||
error?.message && <li key={index}>{error.message}</li>
|
||||
)}
|
||||
</ul>
|
||||
)
|
||||
}, [children, errors])
|
||||
|
||||
if (!content) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
role="alert"
|
||||
data-slot="field-error"
|
||||
className={cn("text-sm font-normal text-destructive", className)}
|
||||
{...props}
|
||||
>
|
||||
{content}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Field,
|
||||
FieldLabel,
|
||||
FieldDescription,
|
||||
FieldError,
|
||||
FieldGroup,
|
||||
FieldLegend,
|
||||
FieldSeparator,
|
||||
FieldSet,
|
||||
FieldContent,
|
||||
FieldTitle,
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
|
||||
function InputGroup({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="input-group"
|
||||
role="group"
|
||||
className={cn(
|
||||
"group/input-group relative flex h-8 w-full min-w-0 items-center rounded-lg border border-input transition-colors outline-none in-data-[slot=combobox-content]:focus-within:border-inherit in-data-[slot=combobox-content]:focus-within:ring-0 has-disabled:bg-input/50 has-disabled:opacity-50 has-[[data-slot=input-group-control]:focus-visible]:border-ring has-[[data-slot=input-group-control]:focus-visible]:ring-3 has-[[data-slot=input-group-control]:focus-visible]:ring-ring/50 has-[[data-slot][aria-invalid=true]]:border-destructive has-[[data-slot][aria-invalid=true]]:ring-3 has-[[data-slot][aria-invalid=true]]:ring-destructive/20 has-[>[data-align=block-end]]:h-auto has-[>[data-align=block-end]]:flex-col has-[>[data-align=block-start]]:h-auto has-[>[data-align=block-start]]:flex-col has-[>textarea]:h-auto dark:bg-input/30 dark:has-disabled:bg-input/80 dark:has-[[data-slot][aria-invalid=true]]:ring-destructive/40 has-[>[data-align=block-end]]:[&>input]:pt-3 has-[>[data-align=block-start]]:[&>input]:pb-3 has-[>[data-align=inline-end]]:[&>input]:pr-1.5 has-[>[data-align=inline-start]]:[&>input]:pl-1.5",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const inputGroupAddonVariants = cva(
|
||||
"flex h-auto cursor-text items-center justify-center gap-2 py-1.5 text-sm font-medium text-muted-foreground select-none group-data-[disabled=true]/input-group:opacity-50 [&>kbd]:rounded-[calc(var(--radius)-5px)] [&>svg:not([class*='size-'])]:size-4",
|
||||
{
|
||||
variants: {
|
||||
align: {
|
||||
"inline-start":
|
||||
"order-first pl-2 has-[>button]:ml-[-0.3rem] has-[>kbd]:ml-[-0.15rem]",
|
||||
"inline-end":
|
||||
"order-last pr-2 has-[>button]:mr-[-0.3rem] has-[>kbd]:mr-[-0.15rem]",
|
||||
"block-start":
|
||||
"order-first w-full justify-start px-2.5 pt-2 group-has-[>input]/input-group:pt-2 [.border-b]:pb-2",
|
||||
"block-end":
|
||||
"order-last w-full justify-start px-2.5 pb-2 group-has-[>input]/input-group:pb-2 [.border-t]:pt-2",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
align: "inline-start",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function InputGroupAddon({
|
||||
className,
|
||||
align = "inline-start",
|
||||
...props
|
||||
}: React.ComponentProps<"div"> & VariantProps<typeof inputGroupAddonVariants>) {
|
||||
return (
|
||||
<div
|
||||
role="group"
|
||||
data-slot="input-group-addon"
|
||||
data-align={align}
|
||||
className={cn(inputGroupAddonVariants({ align }), className)}
|
||||
onClick={(e) => {
|
||||
if ((e.target as HTMLElement).closest("button")) {
|
||||
return
|
||||
}
|
||||
e.currentTarget.parentElement?.querySelector("input")?.focus()
|
||||
}}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const inputGroupButtonVariants = cva(
|
||||
"flex items-center gap-2 text-sm shadow-none",
|
||||
{
|
||||
variants: {
|
||||
size: {
|
||||
xs: "h-6 gap-1 rounded-[calc(var(--radius)-3px)] px-1.5 [&>svg:not([class*='size-'])]:size-3.5",
|
||||
sm: "",
|
||||
"icon-xs":
|
||||
"size-6 rounded-[calc(var(--radius)-3px)] p-0 has-[>svg]:p-0",
|
||||
"icon-sm": "size-8 p-0 has-[>svg]:p-0",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
size: "xs",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function InputGroupButton({
|
||||
className,
|
||||
type = "button",
|
||||
variant = "ghost",
|
||||
size = "xs",
|
||||
...props
|
||||
}: Omit<React.ComponentProps<typeof Button>, "size" | "type"> &
|
||||
VariantProps<typeof inputGroupButtonVariants> & {
|
||||
type?: "button" | "submit" | "reset"
|
||||
}) {
|
||||
return (
|
||||
<Button
|
||||
type={type}
|
||||
data-size={size}
|
||||
variant={variant}
|
||||
className={cn(inputGroupButtonVariants({ size }), className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function InputGroupText({ className, ...props }: React.ComponentProps<"span">) {
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"flex items-center gap-2 text-sm text-muted-foreground [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function InputGroupInput({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<"input">) {
|
||||
return (
|
||||
<Input
|
||||
data-slot="input-group-control"
|
||||
className={cn(
|
||||
"flex-1 rounded-none border-0 bg-transparent shadow-none ring-0 focus-visible:ring-0 disabled:bg-transparent aria-invalid:ring-0 dark:bg-transparent dark:disabled:bg-transparent",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function InputGroupTextarea({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<"textarea">) {
|
||||
return (
|
||||
<Textarea
|
||||
data-slot="input-group-control"
|
||||
className={cn(
|
||||
"flex-1 resize-none rounded-none border-0 bg-transparent py-2 shadow-none ring-0 focus-visible:ring-0 disabled:bg-transparent aria-invalid:ring-0 dark:bg-transparent dark:disabled:bg-transparent",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
InputGroup,
|
||||
InputGroupAddon,
|
||||
InputGroupButton,
|
||||
InputGroupText,
|
||||
InputGroupInput,
|
||||
InputGroupTextarea,
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import * as React from "react"
|
||||
import { Input as InputPrimitive } from "@base-ui/react/input"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
||||
return (
|
||||
<InputPrimitive
|
||||
type={type}
|
||||
data-slot="input"
|
||||
className={cn(
|
||||
"h-8 w-full min-w-0 rounded-lg border border-input bg-transparent px-2.5 py-1 text-base transition-colors outline-none file:inline-flex file:h-6 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-input/50 disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Input }
|
||||
@@ -0,0 +1,201 @@
|
||||
import * as React from "react"
|
||||
import { mergeProps } from "@base-ui/react/merge-props"
|
||||
import { useRender } from "@base-ui/react/use-render"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
import { cn } from "cn"
|
||||
|
||||
import { Separator } from "@/components/ui/separator"
|
||||
|
||||
function ItemGroup({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
role="list"
|
||||
data-slot="item-group"
|
||||
className={cn(
|
||||
"group/item-group flex w-full flex-col gap-4 has-data-[size=sm]:gap-2.5 has-data-[size=xs]:gap-2",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ItemSeparator({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof Separator>) {
|
||||
return (
|
||||
<Separator
|
||||
data-slot="item-separator"
|
||||
orientation="horizontal"
|
||||
className={cn("my-2", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const itemVariants = cva(
|
||||
"group/item flex w-full flex-wrap items-center rounded-lg border text-sm transition-colors duration-100 outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 [a]:transition-colors [a]:hover:bg-muted",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "border-transparent",
|
||||
outline: "border-border",
|
||||
muted: "border-transparent bg-muted/50",
|
||||
},
|
||||
size: {
|
||||
default: "gap-2.5 px-3 py-2.5",
|
||||
sm: "gap-2.5 px-3 py-2.5",
|
||||
xs: "gap-2 px-2.5 py-2 in-data-[slot=dropdown-menu-content]:p-0",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "default",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function Item({
|
||||
className,
|
||||
variant = "default",
|
||||
size = "default",
|
||||
render,
|
||||
...props
|
||||
}: useRender.ComponentProps<"div"> & VariantProps<typeof itemVariants>) {
|
||||
return useRender({
|
||||
defaultTagName: "div",
|
||||
props: mergeProps<"div">(
|
||||
{
|
||||
className: cn(itemVariants({ variant, size, className })),
|
||||
},
|
||||
props
|
||||
),
|
||||
render,
|
||||
state: {
|
||||
slot: "item",
|
||||
variant,
|
||||
size,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const itemMediaVariants = cva(
|
||||
"flex shrink-0 items-center justify-center gap-2 group-has-data-[slot=item-description]/item:translate-y-0.5 group-has-data-[slot=item-description]/item:self-start [&_svg]:pointer-events-none",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-transparent",
|
||||
icon: "[&_svg:not([class*='size-'])]:size-4",
|
||||
image:
|
||||
"size-10 overflow-hidden rounded-sm group-data-[size=sm]/item:size-8 group-data-[size=xs]/item:size-6 [&_img]:size-full [&_img]:object-cover",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function ItemMedia({
|
||||
className,
|
||||
variant = "default",
|
||||
...props
|
||||
}: React.ComponentProps<"div"> & VariantProps<typeof itemMediaVariants>) {
|
||||
return (
|
||||
<div
|
||||
data-slot="item-media"
|
||||
data-variant={variant}
|
||||
className={cn(itemMediaVariants({ variant, className }))}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ItemContent({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="item-content"
|
||||
className={cn(
|
||||
"flex flex-1 flex-col gap-1 group-data-[size=xs]/item:gap-0 [&+[data-slot=item-content]]:flex-none",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ItemTitle({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="item-title"
|
||||
className={cn(
|
||||
"line-clamp-1 flex w-fit items-center gap-2 text-sm leading-snug font-medium underline-offset-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ItemDescription({ className, ...props }: React.ComponentProps<"p">) {
|
||||
return (
|
||||
<p
|
||||
data-slot="item-description"
|
||||
className={cn(
|
||||
"line-clamp-2 text-left text-sm leading-normal font-normal text-muted-foreground group-data-[size=xs]/item:text-xs [&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ItemActions({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="item-actions"
|
||||
className={cn("flex items-center gap-2", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ItemHeader({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="item-header"
|
||||
className={cn(
|
||||
"flex basis-full items-center justify-between gap-2",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ItemFooter({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="item-footer"
|
||||
className={cn(
|
||||
"flex basis-full items-center justify-between gap-2",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Item,
|
||||
ItemMedia,
|
||||
ItemContent,
|
||||
ItemActions,
|
||||
ItemGroup,
|
||||
ItemSeparator,
|
||||
ItemTitle,
|
||||
ItemDescription,
|
||||
ItemHeader,
|
||||
ItemFooter,
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Label({ className, ...props }: React.ComponentProps<"label">) {
|
||||
return (
|
||||
<label
|
||||
data-slot="label"
|
||||
className={cn(
|
||||
"flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Label }
|
||||
@@ -0,0 +1,71 @@
|
||||
import * as React from "react"
|
||||
import { mergeProps } from "@base-ui/react/merge-props"
|
||||
import { useRender } from "@base-ui/react/use-render"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const markerVariants = cva(
|
||||
"group/marker relative flex min-h-4 w-full items-center gap-2 text-left text-sm text-muted-foreground [&_svg:not([class*='size-'])]:size-4 [a]:underline [a]:underline-offset-3 [a]:hover:text-foreground",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "",
|
||||
separator:
|
||||
"before:mr-1 before:h-px before:min-w-0 before:flex-1 before:bg-border after:ml-1 after:h-px after:min-w-0 after:flex-1 after:bg-border",
|
||||
border: "border-b border-border pb-2",
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function Marker({
|
||||
className,
|
||||
variant = "default",
|
||||
render,
|
||||
...props
|
||||
}: useRender.ComponentProps<"div"> & VariantProps<typeof markerVariants>) {
|
||||
return useRender({
|
||||
defaultTagName: "div",
|
||||
props: mergeProps<"div">(
|
||||
{
|
||||
className: cn(markerVariants({ variant, className })),
|
||||
},
|
||||
props
|
||||
),
|
||||
render,
|
||||
state: {
|
||||
slot: "marker",
|
||||
variant,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
function MarkerIcon({ className, ...props }: React.ComponentProps<"span">) {
|
||||
return (
|
||||
<span
|
||||
data-slot="marker-icon"
|
||||
aria-hidden="true"
|
||||
className={cn(
|
||||
"size-4 shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function MarkerContent({ className, ...props }: React.ComponentProps<"span">) {
|
||||
return (
|
||||
<span
|
||||
data-slot="marker-content"
|
||||
className={cn(
|
||||
"min-w-0 wrap-break-word group-data-[variant=separator]/marker:flex-none group-data-[variant=separator]/marker:text-center *:[a]:underline *:[a]:underline-offset-3 *:[a]:hover:text-foreground",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Marker, MarkerIcon, MarkerContent, markerVariants }
|
||||
@@ -0,0 +1,90 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import { Popover as PopoverPrimitive } from "@base-ui/react/popover"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Popover({ ...props }: PopoverPrimitive.Root.Props) {
|
||||
return <PopoverPrimitive.Root data-slot="popover" {...props} />
|
||||
}
|
||||
|
||||
function PopoverTrigger({ ...props }: PopoverPrimitive.Trigger.Props) {
|
||||
return <PopoverPrimitive.Trigger data-slot="popover-trigger" {...props} />
|
||||
}
|
||||
|
||||
function PopoverContent({
|
||||
className,
|
||||
align = "center",
|
||||
alignOffset = 0,
|
||||
side = "bottom",
|
||||
sideOffset = 4,
|
||||
...props
|
||||
}: PopoverPrimitive.Popup.Props &
|
||||
Pick<
|
||||
PopoverPrimitive.Positioner.Props,
|
||||
"align" | "alignOffset" | "side" | "sideOffset"
|
||||
>) {
|
||||
return (
|
||||
<PopoverPrimitive.Portal>
|
||||
<PopoverPrimitive.Positioner
|
||||
align={align}
|
||||
alignOffset={alignOffset}
|
||||
side={side}
|
||||
sideOffset={sideOffset}
|
||||
className="isolate z-50"
|
||||
>
|
||||
<PopoverPrimitive.Popup
|
||||
data-slot="popover-content"
|
||||
className={cn(
|
||||
"z-50 flex w-72 origin-(--transform-origin) flex-col gap-2.5 rounded-lg bg-popover p-2.5 text-sm text-popover-foreground shadow-md ring-1 ring-foreground/10 outline-hidden duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
</PopoverPrimitive.Positioner>
|
||||
</PopoverPrimitive.Portal>
|
||||
)
|
||||
}
|
||||
|
||||
function PopoverHeader({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="popover-header"
|
||||
className={cn("flex flex-col gap-0.5 text-sm", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
|
||||
return (
|
||||
<PopoverPrimitive.Title
|
||||
data-slot="popover-title"
|
||||
className={cn("font-medium", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function PopoverDescription({
|
||||
className,
|
||||
...props
|
||||
}: PopoverPrimitive.Description.Props) {
|
||||
return (
|
||||
<PopoverPrimitive.Description
|
||||
data-slot="popover-description"
|
||||
className={cn("text-muted-foreground", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverDescription,
|
||||
PopoverHeader,
|
||||
PopoverTitle,
|
||||
PopoverTrigger,
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
"use client"
|
||||
|
||||
import { Progress as ProgressPrimitive } from "@base-ui/react/progress"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Progress({
|
||||
className,
|
||||
children,
|
||||
value,
|
||||
...props
|
||||
}: ProgressPrimitive.Root.Props) {
|
||||
return (
|
||||
<ProgressPrimitive.Root
|
||||
value={value}
|
||||
data-slot="progress"
|
||||
className={cn("flex flex-wrap gap-3", className)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<ProgressTrack>
|
||||
<ProgressIndicator />
|
||||
</ProgressTrack>
|
||||
</ProgressPrimitive.Root>
|
||||
)
|
||||
}
|
||||
|
||||
function ProgressTrack({ className, ...props }: ProgressPrimitive.Track.Props) {
|
||||
return (
|
||||
<ProgressPrimitive.Track
|
||||
className={cn(
|
||||
"relative flex h-1 w-full items-center overflow-x-hidden rounded-full bg-muted",
|
||||
className
|
||||
)}
|
||||
data-slot="progress-track"
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ProgressIndicator({
|
||||
className,
|
||||
...props
|
||||
}: ProgressPrimitive.Indicator.Props) {
|
||||
return (
|
||||
<ProgressPrimitive.Indicator
|
||||
data-slot="progress-indicator"
|
||||
className={cn("h-full bg-primary transition-all", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ProgressLabel({ className, ...props }: ProgressPrimitive.Label.Props) {
|
||||
return (
|
||||
<ProgressPrimitive.Label
|
||||
className={cn("text-sm font-medium", className)}
|
||||
data-slot="progress-label"
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ProgressValue({ className, ...props }: ProgressPrimitive.Value.Props) {
|
||||
return (
|
||||
<ProgressPrimitive.Value
|
||||
className={cn(
|
||||
"ml-auto text-sm text-muted-foreground tabular-nums",
|
||||
className
|
||||
)}
|
||||
data-slot="progress-value"
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Progress,
|
||||
ProgressTrack,
|
||||
ProgressIndicator,
|
||||
ProgressLabel,
|
||||
ProgressValue,
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import { Select as SelectPrimitive } from "@base-ui/react/select"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
import { ChevronDownIcon, CheckIcon, ChevronUpIcon } from "lucide-react"
|
||||
|
||||
const Select = SelectPrimitive.Root
|
||||
|
||||
function SelectGroup({ className, ...props }: SelectPrimitive.Group.Props) {
|
||||
return (
|
||||
<SelectPrimitive.Group
|
||||
data-slot="select-group"
|
||||
className={cn("scroll-my-1 p-1", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectValue({ className, ...props }: SelectPrimitive.Value.Props) {
|
||||
return (
|
||||
<SelectPrimitive.Value
|
||||
data-slot="select-value"
|
||||
className={cn("flex flex-1 text-left", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectTrigger({
|
||||
className,
|
||||
size = "default",
|
||||
children,
|
||||
...props
|
||||
}: SelectPrimitive.Trigger.Props & {
|
||||
size?: "sm" | "default"
|
||||
}) {
|
||||
return (
|
||||
<SelectPrimitive.Trigger
|
||||
data-slot="select-trigger"
|
||||
data-size={size}
|
||||
className={cn(
|
||||
"flex w-fit items-center justify-between gap-1.5 rounded-lg border border-input bg-transparent py-2 pr-2 pl-2.5 text-sm whitespace-nowrap transition-colors outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 data-placeholder:text-muted-foreground data-[size=default]:h-8 data-[size=sm]:h-7 data-[size=sm]:rounded-[min(var(--radius-md),10px)] *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-1.5 dark:bg-input/30 dark:hover:bg-input/50 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<SelectPrimitive.Icon
|
||||
render={
|
||||
<ChevronDownIcon className="pointer-events-none size-4 text-muted-foreground" />
|
||||
}
|
||||
/>
|
||||
</SelectPrimitive.Trigger>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectContent({
|
||||
className,
|
||||
children,
|
||||
side = "bottom",
|
||||
sideOffset = 4,
|
||||
align = "center",
|
||||
alignOffset = 0,
|
||||
alignItemWithTrigger = true,
|
||||
...props
|
||||
}: SelectPrimitive.Popup.Props &
|
||||
Pick<
|
||||
SelectPrimitive.Positioner.Props,
|
||||
"align" | "alignOffset" | "side" | "sideOffset" | "alignItemWithTrigger"
|
||||
>) {
|
||||
return (
|
||||
<SelectPrimitive.Portal>
|
||||
<SelectPrimitive.Positioner
|
||||
side={side}
|
||||
sideOffset={sideOffset}
|
||||
align={align}
|
||||
alignOffset={alignOffset}
|
||||
alignItemWithTrigger={alignItemWithTrigger}
|
||||
className="isolate z-50"
|
||||
>
|
||||
<SelectPrimitive.Popup
|
||||
data-slot="select-content"
|
||||
data-align-trigger={alignItemWithTrigger}
|
||||
className={cn("relative isolate z-50 max-h-(--available-height) w-(--anchor-width) min-w-36 origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-lg bg-popover text-popover-foreground shadow-md ring-1 ring-foreground/10 duration-100 data-[align-trigger=true]:animate-none data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95", className )}
|
||||
{...props}
|
||||
>
|
||||
<SelectScrollUpButton />
|
||||
<SelectPrimitive.List>{children}</SelectPrimitive.List>
|
||||
<SelectScrollDownButton />
|
||||
</SelectPrimitive.Popup>
|
||||
</SelectPrimitive.Positioner>
|
||||
</SelectPrimitive.Portal>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectLabel({
|
||||
className,
|
||||
...props
|
||||
}: SelectPrimitive.GroupLabel.Props) {
|
||||
return (
|
||||
<SelectPrimitive.GroupLabel
|
||||
data-slot="select-label"
|
||||
className={cn("px-1.5 py-1 text-xs text-muted-foreground", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectItem({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: SelectPrimitive.Item.Props) {
|
||||
return (
|
||||
<SelectPrimitive.Item
|
||||
data-slot="select-item"
|
||||
className={cn(
|
||||
"relative flex w-full cursor-default items-center gap-1.5 rounded-md py-1 pr-8 pl-1.5 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<SelectPrimitive.ItemText className="flex flex-1 shrink-0 gap-2 whitespace-nowrap">
|
||||
{children}
|
||||
</SelectPrimitive.ItemText>
|
||||
<SelectPrimitive.ItemIndicator
|
||||
render={
|
||||
<span className="pointer-events-none absolute right-2 flex size-4 items-center justify-center" />
|
||||
}
|
||||
>
|
||||
<CheckIcon className="pointer-events-none" />
|
||||
</SelectPrimitive.ItemIndicator>
|
||||
</SelectPrimitive.Item>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectSeparator({
|
||||
className,
|
||||
...props
|
||||
}: SelectPrimitive.Separator.Props) {
|
||||
return (
|
||||
<SelectPrimitive.Separator
|
||||
data-slot="select-separator"
|
||||
className={cn("pointer-events-none -mx-1 my-1 h-px bg-border", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectScrollUpButton({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.ScrollUpArrow>) {
|
||||
return (
|
||||
<SelectPrimitive.ScrollUpArrow
|
||||
data-slot="select-scroll-up-button"
|
||||
className={cn(
|
||||
"top-0 z-10 flex w-full cursor-default items-center justify-center bg-popover py-1 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<ChevronUpIcon
|
||||
/>
|
||||
</SelectPrimitive.ScrollUpArrow>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectScrollDownButton({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<typeof SelectPrimitive.ScrollDownArrow>) {
|
||||
return (
|
||||
<SelectPrimitive.ScrollDownArrow
|
||||
data-slot="select-scroll-down-button"
|
||||
className={cn(
|
||||
"bottom-0 z-10 flex w-full cursor-default items-center justify-center bg-popover py-1 [&_svg:not([class*='size-'])]:size-4",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<ChevronDownIcon
|
||||
/>
|
||||
</SelectPrimitive.ScrollDownArrow>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectGroup,
|
||||
SelectItem,
|
||||
SelectLabel,
|
||||
SelectScrollDownButton,
|
||||
SelectScrollUpButton,
|
||||
SelectSeparator,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
"use client"
|
||||
|
||||
import { Separator as SeparatorPrimitive } from "@base-ui/react/separator"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Separator({
|
||||
className,
|
||||
orientation = "horizontal",
|
||||
...props
|
||||
}: SeparatorPrimitive.Props) {
|
||||
return (
|
||||
<SeparatorPrimitive
|
||||
data-slot="separator"
|
||||
orientation={orientation}
|
||||
className={cn(
|
||||
"shrink-0 bg-border data-horizontal:h-px data-horizontal:w-full data-vertical:w-px data-vertical:self-stretch",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Separator }
|
||||
@@ -0,0 +1,13 @@
|
||||
import { cn } from "cn"
|
||||
|
||||
function Skeleton({ className, ...props }: React.ComponentProps<"div">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="skeleton"
|
||||
className={cn("animate-pulse rounded-md bg-muted", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Skeleton }
|
||||
@@ -0,0 +1,15 @@
|
||||
"use client"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
import { Loader2Icon } from "lucide-react"
|
||||
import { useTranslations } from "next-intl"
|
||||
|
||||
function Spinner({ className, ...props }: React.ComponentProps<"svg">) {
|
||||
const t = useTranslations("Common")
|
||||
|
||||
return (
|
||||
<Loader2Icon data-slot="spinner" role="status" aria-label={t("loading")} className={cn("size-4 animate-spin", className)} {...props} />
|
||||
)
|
||||
}
|
||||
|
||||
export { Spinner }
|
||||
@@ -0,0 +1,82 @@
|
||||
"use client"
|
||||
|
||||
import { Tabs as TabsPrimitive } from "@base-ui/react/tabs"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Tabs({
|
||||
className,
|
||||
orientation = "horizontal",
|
||||
...props
|
||||
}: TabsPrimitive.Root.Props) {
|
||||
return (
|
||||
<TabsPrimitive.Root
|
||||
data-slot="tabs"
|
||||
data-orientation={orientation}
|
||||
className={cn(
|
||||
"group/tabs flex gap-2 data-horizontal:flex-col",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const tabsListVariants = cva(
|
||||
"group/tabs-list inline-flex w-fit items-center justify-center rounded-lg p-[3px] text-muted-foreground group-data-horizontal/tabs:h-8 group-data-vertical/tabs:h-fit group-data-vertical/tabs:flex-col data-[variant=line]:rounded-none",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-muted",
|
||||
line: "gap-1 bg-transparent",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
function TabsList({
|
||||
className,
|
||||
variant = "default",
|
||||
...props
|
||||
}: TabsPrimitive.List.Props & VariantProps<typeof tabsListVariants>) {
|
||||
return (
|
||||
<TabsPrimitive.List
|
||||
data-slot="tabs-list"
|
||||
data-variant={variant}
|
||||
className={cn(tabsListVariants({ variant }), className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function TabsTrigger({ className, ...props }: TabsPrimitive.Tab.Props) {
|
||||
return (
|
||||
<TabsPrimitive.Tab
|
||||
data-slot="tabs-trigger"
|
||||
className={cn(
|
||||
"relative inline-flex h-[calc(100%-1px)] flex-1 items-center justify-center gap-1.5 rounded-md border border-transparent px-1.5 py-0.5 text-sm font-medium whitespace-nowrap text-foreground/60 transition-all group-data-vertical/tabs:w-full group-data-vertical/tabs:justify-start hover:text-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:outline-1 focus-visible:outline-ring disabled:pointer-events-none disabled:opacity-50 has-data-[icon=inline-end]:pr-1 has-data-[icon=inline-start]:pl-1 aria-disabled:pointer-events-none aria-disabled:opacity-50 dark:text-muted-foreground dark:hover:text-foreground group-data-[variant=default]/tabs-list:data-active:shadow-sm group-data-[variant=line]/tabs-list:data-active:shadow-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
"group-data-[variant=line]/tabs-list:bg-transparent group-data-[variant=line]/tabs-list:data-active:bg-transparent dark:group-data-[variant=line]/tabs-list:data-active:border-transparent dark:group-data-[variant=line]/tabs-list:data-active:bg-transparent",
|
||||
"data-active:bg-background data-active:text-foreground dark:data-active:border-input dark:data-active:bg-input/30 dark:data-active:text-foreground",
|
||||
"after:absolute after:bg-foreground after:opacity-0 after:transition-opacity group-data-horizontal/tabs:after:inset-x-0 group-data-horizontal/tabs:after:bottom-[-5px] group-data-horizontal/tabs:after:h-0.5 group-data-vertical/tabs:after:inset-y-0 group-data-vertical/tabs:after:-right-1 group-data-vertical/tabs:after:w-0.5 group-data-[variant=line]/tabs-list:data-active:after:opacity-100",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function TabsContent({ className, ...props }: TabsPrimitive.Panel.Props) {
|
||||
return (
|
||||
<TabsPrimitive.Panel
|
||||
data-slot="tabs-content"
|
||||
className={cn("flex-1 text-sm outline-none", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Tabs, TabsList, TabsTrigger, TabsContent, tabsListVariants }
|
||||
@@ -0,0 +1,18 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Textarea({ className, ...props }: React.ComponentProps<"textarea">) {
|
||||
return (
|
||||
<textarea
|
||||
data-slot="textarea"
|
||||
className={cn(
|
||||
"flex field-sizing-content min-h-16 w-full rounded-lg border border-input bg-transparent px-2.5 py-2 text-base transition-colors outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:bg-input/50 disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Textarea }
|
||||
@@ -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