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.

Simplified the reporting vocabulary and gave the tab track a token

Damian Legawiec087486ca86ff
diff --git a/packages/dashboard-ui/src/styles.css b/packages/dashboard-ui/src/styles.css
index 932029fdf3..2ab134778a 100644
--- a/packages/dashboard-ui/src/styles.css
+++ b/packages/dashboard-ui/src/styles.css
@@ -172,6 +172,11 @@
--card-container: var(--background);
/* Light mode's raised record is simply the card surface. */
--nested-raised: var(--card);
+ /* The recessed well a raised thing sits in — a tab strip's track, a
+ segmented control. Paired with --nested-raised: the track always sits one
+ step under it, which is why the two themes name different tokens rather
+ than sharing one. Light steps down from white to stone-100. */
+ --track-recessed: var(--accent);
--input: color-mix(in srgb, var(--border-base) 80%, transparent);
--ring: var(--brand-blue);
--chart-1: oklch(0.7663 0.1067 254.4);
@@ -280,6 +285,10 @@
container would sit if it had a fill of its own. */
--card-container: var(--background);
--nested-raised: oklch(23.5% 0.006 56.043);
+ /* Not --accent here: dark mode's accent (stone-800) is lighter than every
+ raised surface, so a shared token would put the selected tab below its own
+ track. --muted is the step under --nested-raised. */
+ --track-recessed: var(--muted);
--input: color-mix(in srgb, var(--border-base) 80%, transparent);
--ring: oklch(0.552 0.016 285); /* same blue ring as light, for focus parity */
--link: oklch(0.7 0.16 258);
@@ -407,6 +416,7 @@
--color-border-field: var(--border-field);
--color-card-container: var(--card-container);
--color-nested-raised: var(--nested-raised);
+ --color-track-recessed: var(--track-recessed);
--color-destructive: var(--destructive);
--color-destructive-foreground: var(--destructive-foreground);
--color-accent-foreground: var(--accent-foreground);
diff --git a/packages/dashboard-ui/src/ui/tabs.tsx b/packages/dashboard-ui/src/ui/tabs.tsx
index f31680ed80..7f3e99bd98 100644
--- a/packages/dashboard-ui/src/ui/tabs.tsx
+++ b/packages/dashboard-ui/src/ui/tabs.tsx
@@ -20,23 +20,13 @@ function Tabs({
)
}
-/**
- * The track a set of tabs sits in, recessed so the selected tab can be raised
- * against it.
- *
- * The two themes need different tokens because the palette is asymmetric:
- * light-mode `--muted` resolves to the page background, so a muted track is
- * invisible on a page and leaves only a 1.5% step under the selected tab,
- * while dark-mode `--accent` (stone-800) is lighter than every raised surface
- * and would put the selection *below* its own track. `--accent` in light and
- * `--muted` in dark both sit one step under the raised tab.
- */
+/** The track a set of tabs sits in, recessed so the selected tab reads as raised. */
function TabsList({ className, ...props }: React.ComponentProps<typeof TabsPrimitive.List>) {
return (
<TabsPrimitive.List
data-slot="tabs-list"
className={cn(
- 'inline-flex w-fit items-center justify-center gap-1 rounded-lg bg-accent p-1 text-muted-foreground dark:bg-muted',
+ 'inline-flex w-fit items-center justify-center gap-1 rounded-lg bg-track-recessed p-1 text-muted-foreground',
'group-data-[orientation=vertical]/tabs:h-auto group-data-[orientation=vertical]/tabs:w-full group-data-[orientation=vertical]/tabs:flex-col',
className,
)}
@@ -59,13 +49,12 @@ function TabsTrigger({ className, ...props }: React.ComponentProps<typeof TabsPr
// promotes to full foreground rather than only shifting the surface,
// which is the part a colour-blind or low-vision reader can see.
'text-muted-foreground hover:text-foreground',
- // A raised surface on the recessed track: +3% lightness in light,
- // +5% in dark. `--nested-raised` rather than `--card` because the
- // system raises by lightening in dark mode, where the two differ.
+ // The pair --track-recessed / --nested-raised is defined to hold this
+ // step in both themes: +3% lightness in light, +5% in dark.
'data-[active]:bg-nested-raised data-[active]:text-foreground data-[active]:shadow-xs',
// Same focus treatment as Button, so a keyboard user sees one
// consistent indicator across the dashboard.
- 'focus-visible:shadow-[0_0_0_3px_color-mix(in_srgb,var(--ring)_35%,transparent)]',
+ 'focus-visible:shadow-[0_0_0_3px_color-mix(in_srgb,var(--ring)_15%,transparent)]',
'disabled:pointer-events-none disabled:opacity-70 disabled:cursor-not-allowed',
"[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
className,
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/index.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/index.tsx
index bb319c7311..29eaa73b25 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/index.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/index.tsx
@@ -356,9 +356,10 @@ type RankingTab = 'customers' | 'categories' | 'companies' | 'sellers'
const RANKING_QUERIES: Record<
RankingTab,
{
- query: Omit<ReportingQuery, 'time_range' | 'filters'>
- /** The dimension the rows are keyed by — read from the row for its label. */
- dimension: string
+ /** Single-dimension by construction: the card renders one name per row. */
+ query: Omit<ReportingQuery, 'time_range' | 'filters' | 'dimensions'> & {
+ dimensions: [string]
+ }
revenueMetric: string
countMetric: string
}
@@ -370,7 +371,6 @@ const RANKING_QUERIES: Record<
sort: '-total_sales',
limit: 5,
},
- dimension: 'customer',
revenueMetric: 'total_sales',
countMetric: 'orders',
},
@@ -381,7 +381,6 @@ const RANKING_QUERIES: Record<
sort: '-net_sales',
limit: 5,
},
- dimension: 'category',
revenueMetric: 'net_sales',
countMetric: 'units_sold',
},
@@ -392,7 +391,6 @@ const RANKING_QUERIES: Record<
sort: '-total_sales',
limit: 5,
},
- dimension: 'company',
revenueMetric: 'total_sales',
countMetric: 'orders',
},
@@ -404,7 +402,6 @@ const RANKING_QUERIES: Record<
sort: '-net_sales',
limit: 5,
},
- dimension: 'seller',
revenueMetric: 'net_sales',
countMetric: 'units_sold',
},
@@ -422,7 +419,10 @@ function RankingsCard({
const { storeId } = Route.useParams()
const [tab, setTab] = useState<RankingTab>(tabs[0])
- const { query, dimension: dimensionName, revenueMetric, countMetric } = RANKING_QUERIES[tab]
+ const { query, revenueMetric, countMetric } = RANKING_QUERIES[tab]
+ // Read from the query rather than stored beside it: two copies can disagree,
+ // and a row asked for a dimension it was not grouped by renders unnamed.
+ const [dimensionName] = query.dimensions
const { data, error, isPlaceholderData } = useReportingQuery({ ...query, ...scope })
// Placeholder data belongs to the previous tab (other dimension, other
diff --git a/spree/core/app/services/spree/seeds/saved_reports.rb b/spree/core/app/services/spree/seeds/saved_reports.rb
index 07ef1b5757..4c12f18051 100644
--- a/spree/core/app/services/spree/seeds/saved_reports.rb
+++ b/spree/core/app/services/spree/seeds/saved_reports.rb
@@ -74,7 +74,6 @@ module Spree
existing = store.saved_reports.pluck(:name).map(&:downcase).to_set
REPORTS.each do |report|
- name = Spree.t("reporting.seeds.#{report[:key]}.name")
# Every locale's name for this report, not just the current one:
# the name is translated, so a store seeded in one language and
# re-seeded in another would otherwise recognise none of its own
@@ -82,7 +81,7 @@ module Spree
next if known_names(report[:key]).intersect?(existing)
store.saved_reports.create!(
- name: name,
+ name: Spree.t("reporting.seeds.#{report[:key]}.name"),
description: Spree.t("reporting.seeds.#{report[:key]}.description"),
query: report[:query],
seeded: true
@@ -96,7 +95,7 @@ module Spree
# The downcased name this report carries in every locale core ships.
def known_names(key)
@known_names ||= {}
- @known_names[key] ||= I18n.available_locales.filter_map do |locale|
+ @known_names[key] ||= Spree.available_locales.filter_map do |locale|
Spree.t("reporting.seeds.#{key}.name", locale: locale, default: nil)&.downcase
end.to_set
end
diff --git a/spree/core/db/migrate/20260910120000_index_reporting_time_columns.rb b/spree/core/db/migrate/20260910120000_index_reporting_time_columns.rb
new file mode 100644
index 0000000000..40f266f513
--- /dev/null
+++ b/spree/core/db/migrate/20260910120000_index_reporting_time_columns.rb
@@ -0,0 +1,12 @@
+class IndexReportingTimeColumns < ActiveRecord::Migration[8.1]
+ # The payments and inventory reporting bases filter on these columns and
+ # nothing else (docs/plans/6.0-analytics-semantic-layer.md). Movements are
+ # the highest-volume table a mature store has and carry no store column, so
+ # tenancy is a join up to the product — the composite serves that nested loop
+ # and the date filter together, rather than leaving the planner to scan the
+ # whole ledger.
+ def change
+ add_index :spree_stock_movements, %i[stock_level_id created_at]
+ add_index :spree_payments, :created_at
+ end
+end
diff --git a/spree/core/lib/spree/reporting/adapters/live.rb b/spree/core/lib/spree/reporting/adapters/live.rb
index cbfb20fb02..0dd962485e 100644
--- a/spree/core/lib/spree/reporting/adapters/live.rb
+++ b/spree/core/lib/spree/reporting/adapters/live.rb
@@ -221,7 +221,6 @@ module Spree
fees: Spree::Fee.table_name,
commission_lines: Spree::CommissionLine.table_name,
payments: Spree::Payment.table_name,
- payment_methods: Spree::PaymentMethod.table_name,
stock_movements: Spree::StockMovement.table_name,
stock_levels: Spree::StockLevel.table_name)
end
diff --git a/spree/core/lib/spree/reporting/default_vocabulary.rb b/spree/core/lib/spree/reporting/default_vocabulary.rb
index 223a82f8b1..d543129dc7 100644
--- a/spree/core/lib/spree/reporting/default_vocabulary.rb
+++ b/spree/core/lib/spree/reporting/default_vocabulary.rb
@@ -68,6 +68,19 @@ module Spree
WHERE pr.payment_id = %{payments}.id), 0))
SQL
+ # A variant reads the same wherever it is ranked — sold on the sales axis,
+ # moved on the inventory one — so both dimensions share one payload.
+ VARIANT_RESOLVE = ->(store, value) { store.variants.find_by_prefix_id!(value).id }
+ VARIANT_HYDRATE = lambda { |store, ids, _params|
+ store.variants.where(id: ids).includes(:product, option_values: :option_type).to_h do |variant|
+ [variant.id, {
+ id: variant.prefixed_id,
+ label: variant.descriptive_name,
+ meta: { sku: variant.sku, product_id: variant.product&.prefixed_id }
+ }]
+ end
+ }
+
# One movement kind's quantity. The kind carries the direction, so
# summing across kinds would net a receipt against a dispatch.
def self.movement_sum(kind)
@@ -300,16 +313,7 @@ module Spree
# variants of one product stay distinguishable in a ranking.
dimension :variant, base: :line_items, column: :variant_id, lookup: :variant,
subject: -> { Spree::Product }, key_scope: 'read_products',
- resolve: ->(store, value) { store.variants.find_by_prefix_id!(value).id },
- hydrate: lambda { |store, ids, _params|
- store.variants.where(id: ids).includes(:product, option_values: :option_type).to_h do |variant|
- [variant.id, {
- id: variant.prefixed_id,
- label: variant.descriptive_name,
- meta: { sku: variant.sku, product_id: variant.product&.prefixed_id }
- }]
- end
- }
+ resolve: VARIANT_RESOLVE, hydrate: VARIANT_HYDRATE
# Meta rides on the admin product serializer so thumbnails match the
# rest of the Admin API; the lambda only runs inside API requests.
@@ -369,16 +373,7 @@ module Spree
dimension :moved_variant, base: :stock_movements, column: '%{stock_levels}.variant_id',
joins: [:stock_level], lookup: :variant,
subject: -> { Spree::Product }, key_scope: 'read_products',
- resolve: ->(store, value) { store.variants.find_by_prefix_id!(value).id },
- hydrate: lambda { |store, ids, _params|
- store.variants.where(id: ids).includes(:product, option_values: :option_type).to_h do |variant|
- [variant.id, {
- id: variant.prefixed_id,
- label: variant.descriptive_name,
- meta: { sku: variant.sku, product_id: variant.product&.prefixed_id }
- }]
- end
- }
+ resolve: VARIANT_RESOLVE, hydrate: VARIANT_HYDRATE
dimension :stock_location, base: :stock_movements, column: '%{stock_levels}.stock_location_id',
joins: [:stock_level], lookup: :stock_location,
diff --git a/spree/core/lib/spree/reporting/query.rb b/spree/core/lib/spree/reporting/query.rb
index b44ede6d5b..06a362c11f 100644
--- a/spree/core/lib/spree/reporting/query.rb
+++ b/spree/core/lib/spree/reporting/query.rb
@@ -123,9 +123,7 @@ module Spree
# All metrics the adapter must aggregate: requested non-derived metrics
# plus the hidden components of requested ratios.
def aggregated_metrics
- base = metrics.reject(&:derived?)
- components = metrics.select(&:derived?).flat_map { |m| m.ratio.map { |name| registry.metric!(name) } }
- (base + components).uniq(&:name)
+ metrics.flat_map { |metric| registry.components(metric) }.uniq(&:name)
end
private
diff --git a/spree/core/lib/spree/reporting/registry.rb b/spree/core/lib/spree/reporting/registry.rb
index 301b1e7da2..13766e16bd 100644
--- a/spree/core/lib/spree/reporting/registry.rb
+++ b/spree/core/lib/spree/reporting/registry.rb
@@ -182,13 +182,6 @@ module Spree
components(metric).all? { |component| base!(component.base).reaches?(dimension.base) }
end
- # Whether two bases can appear in one query. Sales metrics combine with
- # each other; a payment total beside a units-received count is two
- # different questions wearing one answer, so the query refuses it.
- def same_family?(one, other)
- base!(one).family == base!(other).family
- end
-
# A derived metric aggregates nothing itself — its components do.
def components(metric)
metric.derived? ? metric.ratio.map { |name| metric!(name) } : [metric]