diff --git a/web/src/components/ui/badge.tsx b/web/src/components/ui/badge.tsx new file mode 100644 index 0000000..b20959d --- /dev/null +++ b/web/src/components/ui/badge.tsx @@ -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) { + return useRender({ + defaultTagName: "span", + props: mergeProps<"span">( + { + className: cn(badgeVariants({ variant }), className), + }, + props + ), + render, + state: { + slot: "badge", + variant, + }, + }) +} + +export { Badge, badgeVariants } diff --git a/web/src/components/ui/skeleton.tsx b/web/src/components/ui/skeleton.tsx new file mode 100644 index 0000000..0118624 --- /dev/null +++ b/web/src/components/ui/skeleton.tsx @@ -0,0 +1,13 @@ +import { cn } from "@/lib/utils" + +function Skeleton({ className, ...props }: React.ComponentProps<"div">) { + return ( +
+ ) +} + +export { Skeleton } diff --git a/web/src/objects/object-list.test.tsx b/web/src/objects/object-list.test.tsx new file mode 100644 index 0000000..4801876 --- /dev/null +++ b/web/src/objects/object-list.test.tsx @@ -0,0 +1,46 @@ +import { beforeEach, expect, test } from "vitest"; +import { screen } from "@testing-library/react"; +import { http, HttpResponse } from "msw"; +import { Routes, Route } from "react-router-dom"; +import { server } from "../test/server"; +import { renderApp } from "../test/render"; +import { ObjectList } from "./object-list"; +import i18n from "../i18n"; + +beforeEach(async () => { + await i18n.changeLanguage("en"); +}); + +function tree() { + return ( + + } /> + } /> + + ); +} + +test("renders object rows with number, name and visibility", async () => { + renderApp(tree(), { route: "/objects" }); + expect(await screen.findByText("LM-0042")).toBeInTheDocument(); + expect(screen.getByText("Amphora")).toBeInTheDocument(); + expect(screen.getByText("Public")).toBeInTheDocument(); +}); + +test("shows an empty state when there are no objects", async () => { + server.use( + http.get("/api/admin/objects", () => + HttpResponse.json({ items: [], total: 0, limit: 50, offset: 0 }), + ), + ); + renderApp(tree(), { route: "/objects" }); + expect(await screen.findByText(/no objects yet/i)).toBeInTheDocument(); +}); + +test("shows an error state on failure", async () => { + server.use( + http.get("/api/admin/objects", () => new HttpResponse(null, { status: 500 })), + ); + renderApp(tree(), { route: "/objects" }); + expect(await screen.findByText(/could not load objects/i)).toBeInTheDocument(); +}); diff --git a/web/src/objects/object-list.tsx b/web/src/objects/object-list.tsx new file mode 100644 index 0000000..b68cb89 --- /dev/null +++ b/web/src/objects/object-list.tsx @@ -0,0 +1,85 @@ +import { useState } from "react"; +import { NavLink, useParams } from "react-router-dom"; +import { useTranslation } from "react-i18next"; + +import { Button } from "@/components/ui/button"; +import { Skeleton } from "@/components/ui/skeleton"; +import { useObjectsPage } from "../api/queries"; +import { VisibilityBadge } from "./visibility-badge"; + +const LIMIT = 50; + +export function ObjectList() { + const { t } = useTranslation(); + const { id: selectedId } = useParams(); + const [offset, setOffset] = useState(0); + + const { data, isLoading, isError } = useObjectsPage(LIMIT, offset); + + if (isLoading) { + return ( +
+ {Array.from({ length: 6 }).map((_, i) => ( + + ))} +
+ ); + } + + if (isError) { + return

{t("objects.loadError")}

; + } + + if (!data || data.items.length === 0) { + return

{t("objects.empty")}

; + } + + const from = data.total === 0 ? 0 : offset + 1; + const to = Math.min(offset + LIMIT, data.total); + + return ( +
+
    + {data.items.map((object) => ( +
  • + + + {object.object_number}{" "} + {object.object_name} + + + +
  • + ))} +
+
+ + {from}–{to} {t("objects.of")} {data.total} + + + + + +
+
+ ); +} diff --git a/web/src/objects/visibility-badge.tsx b/web/src/objects/visibility-badge.tsx new file mode 100644 index 0000000..dd9b6a9 --- /dev/null +++ b/web/src/objects/visibility-badge.tsx @@ -0,0 +1,19 @@ +import { useTranslation } from "react-i18next"; + +import { Badge } from "@/components/ui/badge"; + +const STYLES: Record = { + draft: "bg-neutral-100 text-neutral-600", + internal: "bg-amber-100 text-amber-800", + public: "bg-green-100 text-green-800", +}; + +export function VisibilityBadge({ visibility }: { visibility: string }) { + const { t } = useTranslation(); + + return ( + + {t(`visibility.${visibility}`)} + + ); +}