Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add reusable BackButton component preserving list page state

Damian Legawiec0f88bad6d1a8
diff --git a/packages/admin/src/components/back-button.tsx b/packages/admin/src/components/back-button.tsx
new file mode 100644
index 0000000000..bd787661f5
--- /dev/null
+++ b/packages/admin/src/components/back-button.tsx
@@ -0,0 +1,46 @@
+import { useRouter, useParams } from '@tanstack/react-router'
+import { ArrowLeftIcon } from 'lucide-react'
+
+interface BackButtonProps {
+ /**
+ * Fallback path segment under the current store when there is no history to go back to.
+ * Example: "products" → navigates to `/$storeId/products`
+ */
+ fallback: string
+ className?: string
+}
+
+/**
+ * Back button that goes to the previous page in history (preserving that page's
+ * state — filters, column selection, etc.), falling back to `/$storeId/{fallback}`
+ * when there's nothing to go back to (e.g. deep-link, new tab).
+ */
+export function BackButton({ fallback, className }: BackButtonProps) {
+ const router = useRouter()
+ const { storeId } = useParams({ strict: false }) as { storeId?: string }
+
+ const handleClick = (e: React.MouseEvent) => {
+ e.preventDefault()
+ // If there's history within the app, go back to preserve previous state
+ if (window.history.length > 1 && document.referrer.includes(window.location.host)) {
+ router.history.back()
+ } else {
+ // Fallback to the list page
+ router.navigate({ to: `/${storeId}/${fallback}` as string })
+ }
+ }
+
+ return (
+ <button
+ type="button"
+ onClick={handleClick}
+ className={
+ className ??
+ 'inline-flex items-center justify-center rounded-lg p-1.5 text-muted-foreground hover:bg-gray-200/50 hover:text-foreground transition-colors'
+ }
+ aria-label="Back"
+ >
+ <ArrowLeftIcon className="size-5" />
+ </button>
+ )
+}
diff --git a/packages/admin/src/routes/_authenticated/$storeId/orders/$orderId.tsx b/packages/admin/src/routes/_authenticated/$storeId/orders/$orderId.tsx
index 58b7705c66..76058a88e0 100644
--- a/packages/admin/src/routes/_authenticated/$storeId/orders/$orderId.tsx
+++ b/packages/admin/src/routes/_authenticated/$storeId/orders/$orderId.tsx
@@ -1,8 +1,8 @@
import type { Address, Order } from '@spree/admin-sdk'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
-import { createFileRoute, Link } from '@tanstack/react-router'
+import { createFileRoute } from '@tanstack/react-router'
+import { BackButton } from '@/components/back-button'
import {
- ArrowLeftIcon,
CreditCardIcon,
EllipsisVerticalIcon,
ExternalLinkIcon,
@@ -156,9 +156,9 @@ function OrderDetailPage() {
// ---------------------------------------------------------------------------
function OrderHeader({ order }: { order: Order }) {
- const { orderId, storeId } = Route.useParams()
+ const { orderId } = Route.useParams()
- const backPath = order.completed_at ? '/$storeId/orders' : '/$storeId/orders/drafts'
+ const backFallback = order.completed_at ? 'orders' : 'orders/drafts'
const cancelMutation = useOrderMutation(orderId, () =>
adminClient.orders.cancel(orderId, {}),
@@ -169,14 +169,7 @@ function OrderHeader({ order }: { order: Order }) {
return (
<div className="flex items-center gap-3">
- <Link
- to={backPath}
- params={{ storeId }}
- search={{ filters: [], columns: [] }}
- className="inline-flex items-center justify-center rounded-lg p-1.5 text-muted-foreground hover:bg-gray-200/50 hover:text-foreground transition-colors"
- >
- <ArrowLeftIcon className="size-5" />
- </Link>
+ <BackButton fallback={backFallback} />
<h1 className="text-2xl font-medium">{order.number}</h1>
diff --git a/packages/admin/src/routes/_authenticated/$storeId/products/$productId.tsx b/packages/admin/src/routes/_authenticated/$storeId/products/$productId.tsx
index 940a4ed4e8..e6e4907fbe 100644
--- a/packages/admin/src/routes/_authenticated/$storeId/products/$productId.tsx
+++ b/packages/admin/src/routes/_authenticated/$storeId/products/$productId.tsx
@@ -13,9 +13,9 @@ type Product = Omit<BaseProduct, 'master_variant' | 'variants'> & {
variants?: Variant[]
}
import { zodResolver } from '@hookform/resolvers/zod'
-import { createFileRoute, Link, useRouter } from '@tanstack/react-router'
+import { createFileRoute, useRouter } from '@tanstack/react-router'
+import { BackButton } from '@/components/back-button'
import {
- ArrowLeftIcon,
ImagePlusIcon,
Loader2Icon,
SaveIcon,
@@ -167,14 +167,7 @@ function ProductForm({ product }: { product: Product }) {
<form onSubmit={form.handleSubmit(onSubmit)} className="flex flex-col gap-6">
{/* Header */}
<div className="flex items-center gap-3">
- <Link
- to="/$storeId/products"
- params={{ storeId }}
- search={{ filters: [], columns: [] }}
- className="inline-flex items-center justify-center rounded-lg p-1.5 text-muted-foreground hover:bg-gray-100 hover:text-foreground transition-colors"
- >
- <ArrowLeftIcon className="size-5" />
- </Link>
+ <BackButton fallback="products" />
<h1 className="text-2xl font-medium truncate">{product.name}</h1>
<StatusBadge status={product.status} />
diff --git a/packages/admin/src/tables/orders.tsx b/packages/admin/src/tables/orders.tsx
index 0f4fd443f0..dff3d2be35 100644
--- a/packages/admin/src/tables/orders.tsx
+++ b/packages/admin/src/tables/orders.tsx
@@ -20,8 +20,8 @@ defineTable('orders', {
default: true,
render: (order) => (
<Link
- to="/$storeId/orders/$orderId"
- params={{ orderId: order.id } as Record<string, string>}
+ to={"/$storeId/orders/$orderId" as string}
+ params={{ orderId: order.id }}
className="font-medium text-zinc-950 no-underline"
>
#{order.number}
diff --git a/packages/admin/src/tables/products.tsx b/packages/admin/src/tables/products.tsx
index df0295065f..ba3bfc39b9 100644
--- a/packages/admin/src/tables/products.tsx
+++ b/packages/admin/src/tables/products.tsx
@@ -20,8 +20,8 @@ defineTable("products", {
default: true,
render: (product) => (
<Link
- to="/$storeId/products/$productId"
- params={{ productId: product.id } as Record<string, string>}
+ to={"/$storeId/products/$productId" as string}
+ params={{ productId: product.id }}
className="flex items-center gap-3 no-underline"
>
<div className="flex size-10 shrink-0 items-center justify-center rounded-lg border border-gray-200 bg-gray-50 overflow-hidden">