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.

Remember every dismissed option row, not just the last

Damian Legawiecad3ca2b34c89
diff --git a/packages/dashboard/src/components/spree/products/variants-matrix.test.ts b/packages/dashboard/src/components/spree/products/variants-matrix.test.ts
index ac1e202854..47f0eac6d8 100644
--- a/packages/dashboard/src/components/spree/products/variants-matrix.test.ts
+++ b/packages/dashboard/src/components/spree/products/variants-matrix.test.ts
@@ -1,6 +1,6 @@
import type { OptionType } from '@spree/admin-sdk'
import { describe, expect, it } from 'vitest'
-import { composeOptionsText } from './variants-matrix'
+import { autoExpandIndex, composeOptionsText, type SelectedOptionType } from './variants-matrix'
// composeOptionsText only reads name/label/position/option_values; cast minimal
// fixtures rather than fabricating timestamps + ids that the helper ignores.
@@ -121,3 +121,34 @@ describe('composeOptionsText', () => {
).toBe('Color: Red, finish: matte, and pattern: striped')
})
})
+
+describe('autoExpandIndex', () => {
+ const row = (id: string, values: string[] = []): SelectedOptionType => ({
+ id,
+ name: id,
+ label: id,
+ position: 0,
+ values: values.map((name) => ({ name })),
+ })
+
+ it('offers the first row still awaiting values', () => {
+ expect(autoExpandIndex([row('color', ['red']), row('size')], new Set())).toBe(1)
+ })
+
+ it('offers nothing once every row has values', () => {
+ expect(autoExpandIndex([row('color', ['red'])], new Set())).toBe(-1)
+ })
+
+ // A type seeding two option types produces two empty rows. Declining the
+ // second must not re-open the first.
+ it('moves past a dismissed row to the next empty one', () => {
+ const rows = [row('color'), row('size')]
+
+ expect(autoExpandIndex(rows, new Set(['color']))).toBe(1)
+ expect(autoExpandIndex(rows, new Set(['color', 'size']))).toBe(-1)
+ })
+
+ it('offers nothing when there are no rows', () => {
+ expect(autoExpandIndex([], new Set())).toBe(-1)
+ })
+})
diff --git a/packages/dashboard/src/components/spree/products/variants-matrix.ts b/packages/dashboard/src/components/spree/products/variants-matrix.ts
index 47b577ea80..d9354e6a43 100644
--- a/packages/dashboard/src/components/spree/products/variants-matrix.ts
+++ b/packages/dashboard/src/components/spree/products/variants-matrix.ts
@@ -24,6 +24,23 @@ export interface SelectedOptionType {
values: SelectedOptionValue[]
}
+/**
+ * Which option row the builder should open on its own: the first one still
+ * awaiting values that the merchant hasn't already dismissed. A product type
+ * seeds exactly such rows, so this puts the values picker in front of them
+ * without a click — while leaving them able to close it.
+ *
+ * @returns the row's index, or -1 when nothing should auto-expand.
+ */
+export function autoExpandIndex(
+ selected: SelectedOptionType[],
+ dismissedIds: ReadonlySet<string>,
+): number {
+ return selected.findIndex(
+ (optionType) => optionType.values.length === 0 && !dismissedIds.has(optionType.id),
+ )
+}
+
export interface VariantCombination {
options: { name: string; value: string }[]
}
diff --git a/packages/dashboard/src/components/spree/products/variants-options-builder.tsx b/packages/dashboard/src/components/spree/products/variants-options-builder.tsx
index e13a8247ce..9a1bb522d1 100644
--- a/packages/dashboard/src/components/spree/products/variants-options-builder.tsx
+++ b/packages/dashboard/src/components/spree/products/variants-options-builder.tsx
@@ -20,7 +20,7 @@ import {
useOptionTypes,
useUpdateOptionType,
} from '../../../hooks/use-option-types'
-import type { SelectedOptionType } from './variants-matrix'
+import { autoExpandIndex, type SelectedOptionType } from './variants-matrix'
interface Props {
selected: SelectedOptionType[]
@@ -62,18 +62,28 @@ export function VariantsOptionsBuilder({ selected, onChange, extraOptionTypes }:
// It is an offer they can decline: without tracking the dismissal, cancelling
// reopens the row on the next render (it is still empty, so it qualifies
// again) and its remove action stays unreachable.
- const [dismissedAutoExpand, setDismissedAutoExpand] = useState<string | null>(null)
- const firstEmpty = selected.find((optionType) => optionType.values.length === 0)
- const autoExpandIndex =
- isAdding || !firstEmpty || dismissedAutoExpand === firstEmpty.id
- ? null
- : selected.indexOf(firstEmpty)
- const expandedIndex = editingIndex ?? autoExpandIndex
+ // A set, not one id: a type seeding two option types produces two empty rows,
+ // and declining the second must not re-open the first.
+ const [dismissedAutoExpand, setDismissedAutoExpand] = useState<ReadonlySet<string>>(new Set())
+ const offeredIndex = autoExpandIndex(selected, dismissedAutoExpand)
+ const expandedIndex = editingIndex ?? (isAdding || offeredIndex < 0 ? null : offeredIndex)
const selectedIds = new Set(selected.map((s) => s.id))
const availableTypes = allOptionTypes.filter((ot) => !selectedIds.has(ot.id))
const removeAt = (index: number) => {
+ // Drop the dismissal with the row, so re-adding the same option type gets
+ // a fresh offer rather than inheriting a stale one.
+ const removed = selected[index]
+ if (removed) {
+ setDismissedAutoExpand((prev) => {
+ if (!prev.has(removed.id)) return prev
+
+ const next = new Set(prev)
+ next.delete(removed.id)
+ return next
+ })
+ }
onChange(selected.filter((_, i) => i !== index))
}
@@ -102,7 +112,9 @@ export function VariantsOptionsBuilder({ selected, onChange, extraOptionTypes }:
initialValues={ot.values}
onCancel={() => {
setEditingIndex(null)
- if (ot.values.length === 0) setDismissedAutoExpand(ot.id)
+ if (ot.values.length === 0) {
+ setDismissedAutoExpand((prev) => new Set(prev).add(ot.id))
+ }
}}
onSave={(values) =>
upsertAt(i, {