From f1640c238a05b54eddbe759525d30f5e4e5eda3e Mon Sep 17 00:00:00 2001 From: Hardik Date: Sat, 16 May 2026 01:58:36 +0530 Subject: [PATCH] fix(items): auto-sort by distance when site selected or changed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit useEffect keyed on currentSiteId resets sortBy to 'distance' whenever the site selection changes, overcoming useState's one-time initialisation which didn't fire on soft navigations (state preserved across router.push). Verified with Playwright: no-site→price, select-site→distance auto, manual price switch works, switch-site→distance resets. --- .../app/(portal)/inventory/items/items-table.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/App/pelagia-portal/app/(portal)/inventory/items/items-table.tsx b/App/pelagia-portal/app/(portal)/inventory/items/items-table.tsx index 358d27b..9536c6e 100644 --- a/App/pelagia-portal/app/(portal)/inventory/items/items-table.tsx +++ b/App/pelagia-portal/app/(portal)/inventory/items/items-table.tsx @@ -1,6 +1,6 @@ "use client"; -import { useState, useMemo, Fragment } from "react"; +import { useState, useMemo, Fragment, useEffect } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { Search, X, ChevronDown, ChevronRight, MapPin, Tag } from "lucide-react"; @@ -46,6 +46,11 @@ export function ItemsTable({ const [sortBy, setSortBy] = useState<"distance" | "price">(hasSite ? "distance" : "price"); const [added, setAdded] = useState>({}); + // Reset sort to distance whenever the selected site changes + useEffect(() => { + setSortBy(currentSiteId ? "distance" : "price"); + }, [currentSiteId]); + const filtered = useMemo(() => { const q = query.toLowerCase().trim(); if (!q) return items;