Products
The Global tab holds the catalogue shared across every region; each region tab lists that
region's own products (which can be elevated to global).
{tab === 'global' ? : }
)
}
function RegionProductsTab({ regionId }: { regionId: number }) {
const utils = trpc.useUtils()
const storesQuery = trpc.superadmin.globalStores.list.useQuery()
const [searchInput, setSearchInput] = useState('')
const [search, setSearch] = useState('')
const [page, setPage] = useState(0)
// Debounce the search box; reset to page 0 when the query changes.
useEffect(() => {
const t = setTimeout(() => {
setSearch(searchInput)
setPage(0)
}, 300)
return () => clearTimeout(t)
}, [searchInput])
// Tabs reuse this component, so start each region on its first page.
useEffect(() => {
setPage(0)
}, [regionId])
const listQuery = trpc.superadmin.products.list.useQuery({
regionId,
search: search || undefined,
limit: PAGE_SIZE,
offset: page * PAGE_SIZE,
})
const elevate = trpc.superadmin.products.elevate.useMutation()
const [elevatingId, setElevatingId] = useState(null)
const [storeChoice, setStoreChoice] = useState('')
const products = (listQuery.data?.products ?? []) as any[]
const total = listQuery.data?.total ?? 0
const pageCount = Math.max(1, Math.ceil(total / PAGE_SIZE))
const stores = storesQuery.data ?? []
const doElevate = async (productId: number) => {
if (storeChoice === '') {
void alert('Choose a global store')
return
}
try {
await elevate.mutateAsync({ productId, storeId: Number(storeChoice) })
setElevatingId(null)
setStoreChoice('')
await utils.superadmin.products.list.invalidate()
await utils.superadmin.globalProducts.list.invalidate()
} catch (e: any) {
void alert(e.message || 'Failed to elevate product')
}
}
return (
setSearchInput(e.target.value)}
placeholder="Search by product name, SKU or description…"
className="min-w-[220px] flex-1 rounded-lg border border-gray-200 px-3 py-2 text-gray-900"
/>
{listQuery.isLoading && Loading products…}
{!listQuery.isLoading && products.length === 0 && (
No products in this region yet.
)}
Products shared across every region. Creating one seeds the initial price & stock into
each region; editing the structure leaves each region's market data untouched.
{editing ? (
{ setEditingId(null); refresh() }}
onCancel={() => setEditingId(null)}
/>
) : (
)}
{listQuery.isLoading && Loading products…}