feat(18-04): add global item hooks, catalog browse page, and detail page
- useGlobalItems/useGlobalItem/useLinkItem/useUnlinkItem hooks - Global catalog browse page with search, debounce, and skeleton loading - Global item detail page with owner count badge - GlobalItemCard component with brand, model, specs badges
This commit is contained in:
83
src/client/components/GlobalItemCard.tsx
Normal file
83
src/client/components/GlobalItemCard.tsx
Normal file
@@ -0,0 +1,83 @@
|
||||
import { Link } from "@tanstack/react-router";
|
||||
import { useFormatters } from "../hooks/useFormatters";
|
||||
|
||||
interface GlobalItemCardProps {
|
||||
id: number;
|
||||
brand: string;
|
||||
model: string;
|
||||
category: string | null;
|
||||
weightGrams: number | null;
|
||||
priceCents: number | null;
|
||||
imageUrl: string | null;
|
||||
}
|
||||
|
||||
export function GlobalItemCard({
|
||||
id,
|
||||
brand,
|
||||
model,
|
||||
category,
|
||||
weightGrams,
|
||||
priceCents,
|
||||
imageUrl,
|
||||
}: GlobalItemCardProps) {
|
||||
const { weight, price } = useFormatters();
|
||||
|
||||
return (
|
||||
<Link
|
||||
to="/global-items/$globalItemId"
|
||||
params={{ globalItemId: String(id) }}
|
||||
className="block bg-white rounded-xl border border-gray-100 hover:border-gray-200 hover:shadow-sm transition-all overflow-hidden group"
|
||||
>
|
||||
<div className="aspect-[4/3] bg-gray-50">
|
||||
{imageUrl ? (
|
||||
<img
|
||||
src={imageUrl}
|
||||
alt={`${brand} ${model}`}
|
||||
className="w-full h-full object-cover"
|
||||
/>
|
||||
) : (
|
||||
<div className="w-full h-full flex flex-col items-center justify-center">
|
||||
<svg
|
||||
className="w-9 h-9 text-gray-300"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={1.5}
|
||||
d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="p-4">
|
||||
<p className="text-xs font-medium text-gray-400 uppercase tracking-wide mb-0.5">
|
||||
{brand}
|
||||
</p>
|
||||
<h3 className="text-sm font-semibold text-gray-900 truncate mb-2">
|
||||
{model}
|
||||
</h3>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{weightGrams != null && (
|
||||
<span className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-blue-50 text-blue-400">
|
||||
{weight(weightGrams)}
|
||||
</span>
|
||||
)}
|
||||
{priceCents != null && (
|
||||
<span className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-green-50 text-green-500">
|
||||
{price(priceCents)}
|
||||
</span>
|
||||
)}
|
||||
{category && (
|
||||
<span className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-gray-50 text-gray-600">
|
||||
{category}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
74
src/client/hooks/useGlobalItems.ts
Normal file
74
src/client/hooks/useGlobalItems.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { ApiError, apiDelete, apiGet, apiPost } from "../lib/api";
|
||||
|
||||
interface GlobalItem {
|
||||
id: number;
|
||||
brand: string;
|
||||
model: string;
|
||||
category: string | null;
|
||||
weightGrams: number | null;
|
||||
priceCents: number | null;
|
||||
imageUrl: string | null;
|
||||
description: string | null;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
interface GlobalItemWithOwnerCount extends GlobalItem {
|
||||
ownerCount: number;
|
||||
}
|
||||
|
||||
interface ItemGlobalLink {
|
||||
id: number;
|
||||
itemId: number;
|
||||
globalItemId: number;
|
||||
}
|
||||
|
||||
export function useGlobalItems(query?: string) {
|
||||
return useQuery({
|
||||
queryKey: ["global-items", query ?? ""],
|
||||
queryFn: () =>
|
||||
apiGet<GlobalItem[]>(
|
||||
`/api/global-items${query ? `?q=${encodeURIComponent(query)}` : ""}`,
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function useGlobalItem(id: number | null) {
|
||||
return useQuery({
|
||||
queryKey: ["global-items", id],
|
||||
queryFn: () => apiGet<GlobalItemWithOwnerCount>(`/api/global-items/${id}`),
|
||||
enabled: id != null,
|
||||
retry: (count, error) =>
|
||||
error instanceof ApiError && error.status === 404 ? false : count < 3,
|
||||
});
|
||||
}
|
||||
|
||||
export function useLinkItem() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
itemId,
|
||||
globalItemId,
|
||||
}: {
|
||||
itemId: number;
|
||||
globalItemId: number;
|
||||
}) =>
|
||||
apiPost<ItemGlobalLink>(`/api/items/${itemId}/link`, { globalItemId }),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["items"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["global-items"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useUnlinkItem() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (itemId: number) =>
|
||||
apiDelete<{ success: boolean }>(`/api/items/${itemId}/link`),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["items"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["global-items"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
125
src/client/routes/global-items/$globalItemId.tsx
Normal file
125
src/client/routes/global-items/$globalItemId.tsx
Normal file
@@ -0,0 +1,125 @@
|
||||
import { createFileRoute, Link } from "@tanstack/react-router";
|
||||
import { useFormatters } from "../../hooks/useFormatters";
|
||||
import { useGlobalItem } from "../../hooks/useGlobalItems";
|
||||
|
||||
export const Route = createFileRoute("/global-items/$globalItemId")({
|
||||
component: GlobalItemDetail,
|
||||
});
|
||||
|
||||
function GlobalItemDetail() {
|
||||
const { globalItemId } = Route.useParams();
|
||||
const { data: item, isLoading, error } = useGlobalItem(Number(globalItemId));
|
||||
const { weight, price } = useFormatters();
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
|
||||
<div className="animate-pulse space-y-6">
|
||||
<div className="h-4 bg-gray-100 rounded w-24" />
|
||||
<div className="aspect-[16/9] bg-gray-100 rounded-xl" />
|
||||
<div className="space-y-3">
|
||||
<div className="h-3 bg-gray-100 rounded w-20" />
|
||||
<div className="h-6 bg-gray-100 rounded w-48" />
|
||||
<div className="h-4 bg-gray-100 rounded w-full" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error || !item) {
|
||||
return (
|
||||
<div className="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
|
||||
<Link
|
||||
to="/global-items"
|
||||
className="text-sm text-gray-400 hover:text-gray-600 transition-colors"
|
||||
>
|
||||
← Back to catalog
|
||||
</Link>
|
||||
<div className="text-center py-16">
|
||||
<p className="text-sm text-gray-500">Global item not found</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
|
||||
<div className="mb-6">
|
||||
<Link
|
||||
to="/global-items"
|
||||
className="text-sm text-gray-400 hover:text-gray-600 transition-colors"
|
||||
>
|
||||
← Back to catalog
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{/* Image */}
|
||||
{item.imageUrl && (
|
||||
<div className="aspect-[16/9] bg-gray-50 rounded-xl overflow-hidden mb-6">
|
||||
<img
|
||||
src={item.imageUrl}
|
||||
alt={`${item.brand} ${item.model}`}
|
||||
className="w-full h-full object-cover"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Header */}
|
||||
<div className="mb-6">
|
||||
<p className="text-xs font-medium text-gray-400 uppercase tracking-wide mb-1">
|
||||
{item.brand}
|
||||
</p>
|
||||
<h1 className="text-2xl font-bold text-gray-900 mb-3">{item.model}</h1>
|
||||
|
||||
{/* Owner count badge */}
|
||||
<div className="inline-flex items-center gap-1.5 px-3 py-1.5 bg-amber-50 text-amber-600 rounded-full text-xs font-medium">
|
||||
<svg
|
||||
className="w-3.5 h-3.5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z"
|
||||
/>
|
||||
</svg>
|
||||
{item.ownerCount === 0
|
||||
? "Be the first to add this"
|
||||
: item.ownerCount === 1
|
||||
? "1 user owns this"
|
||||
: `${item.ownerCount} users own this`}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Specs */}
|
||||
<div className="flex flex-wrap gap-2 mb-6">
|
||||
{item.weightGrams != null && (
|
||||
<span className="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-50 text-blue-500">
|
||||
{weight(item.weightGrams)}
|
||||
</span>
|
||||
)}
|
||||
{item.priceCents != null && (
|
||||
<span className="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-50 text-green-500">
|
||||
{price(item.priceCents)}
|
||||
</span>
|
||||
)}
|
||||
{item.category && (
|
||||
<span className="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-gray-50 text-gray-600">
|
||||
{item.category}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Description */}
|
||||
{item.description && (
|
||||
<div className="prose prose-sm prose-gray max-w-none">
|
||||
<p className="text-gray-600 leading-relaxed">{item.description}</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
142
src/client/routes/global-items/index.tsx
Normal file
142
src/client/routes/global-items/index.tsx
Normal file
@@ -0,0 +1,142 @@
|
||||
import { createFileRoute, Link } from "@tanstack/react-router";
|
||||
import { useEffect, useState } from "react";
|
||||
import { GlobalItemCard } from "../../components/GlobalItemCard";
|
||||
import { useGlobalItems } from "../../hooks/useGlobalItems";
|
||||
|
||||
export const Route = createFileRoute("/global-items/")({
|
||||
component: GlobalItemsCatalog,
|
||||
});
|
||||
|
||||
function GlobalItemsCatalog() {
|
||||
const [searchInput, setSearchInput] = useState("");
|
||||
const [debouncedQuery, setDebouncedQuery] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => {
|
||||
setDebouncedQuery(searchInput);
|
||||
}, 300);
|
||||
return () => clearTimeout(timer);
|
||||
}, [searchInput]);
|
||||
|
||||
const { data: items, isLoading } = useGlobalItems(
|
||||
debouncedQuery || undefined,
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
|
||||
<div className="mb-6">
|
||||
<Link
|
||||
to="/"
|
||||
className="text-sm text-gray-400 hover:text-gray-600 transition-colors"
|
||||
>
|
||||
← Dashboard
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
<div className="mb-8">
|
||||
<h1 className="text-2xl font-bold text-gray-900 mb-1">
|
||||
Global Gear Catalog
|
||||
</h1>
|
||||
<p className="text-sm text-gray-500">
|
||||
Browse and discover gear from the shared catalog
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Search */}
|
||||
<div className="mb-8">
|
||||
<div className="relative max-w-md">
|
||||
<svg
|
||||
className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
|
||||
/>
|
||||
</svg>
|
||||
<input
|
||||
type="text"
|
||||
value={searchInput}
|
||||
onChange={(e) => setSearchInput(e.target.value)}
|
||||
placeholder="Search gear by brand or model..."
|
||||
className="w-full pl-10 pr-4 py-2.5 bg-white border border-gray-200 rounded-lg text-sm text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-200 focus:border-gray-300 transition-colors"
|
||||
/>
|
||||
{searchInput && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSearchInput("")}
|
||||
className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600"
|
||||
>
|
||||
<svg
|
||||
className="w-4 h-4"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M6 18L18 6M6 6l12 12"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Results */}
|
||||
{isLoading ? (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
{["a", "b", "c", "d", "e", "f"].map((id) => (
|
||||
<div
|
||||
key={id}
|
||||
className="bg-white rounded-xl border border-gray-100 overflow-hidden animate-pulse"
|
||||
>
|
||||
<div className="aspect-[4/3] bg-gray-100" />
|
||||
<div className="p-4 space-y-2">
|
||||
<div className="h-3 bg-gray-100 rounded w-16" />
|
||||
<div className="h-4 bg-gray-100 rounded w-32" />
|
||||
<div className="flex gap-1.5">
|
||||
<div className="h-5 bg-gray-100 rounded-full w-14" />
|
||||
<div className="h-5 bg-gray-100 rounded-full w-14" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : items && items.length > 0 ? (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
{items.map((item) => (
|
||||
<GlobalItemCard key={item.id} {...item} />
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-center py-16">
|
||||
<svg
|
||||
className="w-12 h-12 text-gray-300 mx-auto mb-4"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={1.5}
|
||||
d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"
|
||||
/>
|
||||
</svg>
|
||||
<p className="text-sm text-gray-500">
|
||||
{debouncedQuery
|
||||
? "No items found matching your search"
|
||||
: "No items in the global catalog yet"}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user