@stacksjs/defaults 0.75.490 → 0.75.492
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ai/skills/stacks-actions/SKILL.md +1 -1
- package/ai/skills/stacks-native/CATALOG.md +2 -2
- package/app/Actions/Dashboard/Commerce/ProductUnitIndexAction.ts +23 -19
- package/app/Actions/Dashboard/Commerce/ProductVariantIndexAction.ts +25 -18
- package/app/Actions/Dashboard/Commerce/commerce-product-unit-list.test.ts +146 -0
- package/app/Actions/Dashboard/Commerce/commerce-product-unit-list.ts +178 -0
- package/app/Actions/Dashboard/Commerce/commerce-product-variant-list.test.ts +165 -0
- package/app/Actions/Dashboard/Commerce/commerce-product-variant-list.ts +209 -0
- package/app/Actions/Dashboard/Commerce/product-unit-records.test.ts +0 -13
- package/app/Actions/Dashboard/Commerce/product-unit-records.ts +0 -17
- package/app/Actions/Dashboard/Commerce/product-variant-records.test.ts +0 -13
- package/app/Actions/Dashboard/Commerce/product-variant-records.ts +0 -17
- package/functions/dashboard-list.test.ts +14 -0
- package/functions/dashboard-list.ts +21 -3
- package/ide/vscode/package.json +1 -1
- package/package.json +2 -2
- package/resources/components/Dashboard/Commerce/CommerceUnitDialog.stx +63 -8
- package/resources/components/Dashboard/Commerce/CommerceUnitsDashboard.stx +48 -60
- package/resources/components/Dashboard/Commerce/CommerceUnitsTable.stx +29 -3
- package/resources/components/Dashboard/Commerce/CommerceVariantDialog.stx +57 -3
- package/resources/components/Dashboard/Commerce/CommerceVariantsDashboard.stx +59 -61
- package/resources/components/Dashboard/Commerce/CommerceVariantsTable.stx +28 -2
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import type { QueryBuilderDialect } from '@stacksjs/database/runtime'
|
|
2
|
+
import { db, getDatabaseDialect } from '@stacksjs/database/runtime'
|
|
3
|
+
import type { QueryReader } from '../Content/content-list'
|
|
4
|
+
import { containsPattern, countOf, LIST_PAGE_SIZES, pageWithin, queryText } from '../Content/content-list'
|
|
5
|
+
import { commerceIdentifier, commerceRequiredString, commerceValue, referencedIds } from './commerce-record'
|
|
6
|
+
import type { CommerceSqlRunner } from './commerce-sql'
|
|
7
|
+
import { asText, binder, idPlaceholders, sqlRows } from './commerce-sql'
|
|
8
|
+
import type { ProductVariantRecord, ProductVariantSummary } from './product-variant-records'
|
|
9
|
+
import { normalizeProductVariantRecord } from './product-variant-records'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* One page of product variants, searched, filtered, sorted and counted by the
|
|
13
|
+
* database, with the summary of every variant, every variant type, and every
|
|
14
|
+
* product that has a variant (stacksjs/stacks#3218).
|
|
15
|
+
*
|
|
16
|
+
* The Variants page used to read the newest 500 variants and do all of that
|
|
17
|
+
* in the browser, so an older variant could not be found, its cards counted
|
|
18
|
+
* those 500, and the type filter only knew those 500's types. It also read
|
|
19
|
+
* the first 500 products by name, for the product filter, the dialog's
|
|
20
|
+
* product select and to name each variant's product, so a variant of any
|
|
21
|
+
* later product failed the whole page.
|
|
22
|
+
*/
|
|
23
|
+
export const PRODUCT_VARIANT_SORTS = ['variant', 'product', 'options', 'status', 'newest', 'created'] as const
|
|
24
|
+
export type ProductVariantSort = typeof PRODUCT_VARIANT_SORTS[number]
|
|
25
|
+
|
|
26
|
+
export const PRODUCT_VARIANT_STATUSES = ['active', 'inactive', 'draft'] as const
|
|
27
|
+
|
|
28
|
+
export interface ProductVariantListQuery {
|
|
29
|
+
search: string
|
|
30
|
+
/** '' for any. */
|
|
31
|
+
status: '' | typeof PRODUCT_VARIANT_STATUSES[number]
|
|
32
|
+
/** A variant type, or '' for any. */
|
|
33
|
+
type: string
|
|
34
|
+
/** A product id, or '' for any. */
|
|
35
|
+
product: string
|
|
36
|
+
sort: ProductVariantSort
|
|
37
|
+
direction: 'asc' | 'desc'
|
|
38
|
+
page: number
|
|
39
|
+
perPage: number
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function parseProductVariantListQuery(read: QueryReader): ProductVariantListQuery | { error: string } {
|
|
43
|
+
const pageText = queryText(read, 'page')
|
|
44
|
+
const page = pageText === '' ? 1 : Number(pageText)
|
|
45
|
+
if (!Number.isInteger(page) || page < 1)
|
|
46
|
+
return { error: 'page must be a whole number of 1 or more.' }
|
|
47
|
+
|
|
48
|
+
const sizeText = queryText(read, 'per_page')
|
|
49
|
+
const perPage = sizeText === '' ? 25 : Number(sizeText)
|
|
50
|
+
if (!(LIST_PAGE_SIZES as readonly number[]).includes(perPage))
|
|
51
|
+
return { error: `per_page must be one of ${LIST_PAGE_SIZES.join(', ')}.` }
|
|
52
|
+
|
|
53
|
+
const sort = queryText(read, 'sort') || 'variant'
|
|
54
|
+
if (!(PRODUCT_VARIANT_SORTS as readonly string[]).includes(sort))
|
|
55
|
+
return { error: `sort must be one of ${PRODUCT_VARIANT_SORTS.join(', ')}.` }
|
|
56
|
+
|
|
57
|
+
const directionText = queryText(read, 'direction').toLowerCase()
|
|
58
|
+
const direction = directionText === '' ? (['options', 'newest', 'created'].includes(sort) ? 'desc' : 'asc') : directionText
|
|
59
|
+
if (direction !== 'asc' && direction !== 'desc')
|
|
60
|
+
return { error: 'direction must be asc or desc.' }
|
|
61
|
+
|
|
62
|
+
const search = queryText(read, 'q')
|
|
63
|
+
if (search.length > 200)
|
|
64
|
+
return { error: 'q must be 200 characters or fewer.' }
|
|
65
|
+
|
|
66
|
+
const status = queryText(read, 'status')
|
|
67
|
+
if (status && !(PRODUCT_VARIANT_STATUSES as readonly string[]).includes(status))
|
|
68
|
+
return { error: `status must be one of ${PRODUCT_VARIANT_STATUSES.join(', ')}.` }
|
|
69
|
+
|
|
70
|
+
const type = queryText(read, 'type')
|
|
71
|
+
if (type.length > 100)
|
|
72
|
+
return { error: 'type must be 100 characters or fewer.' }
|
|
73
|
+
|
|
74
|
+
const product = queryText(read, 'product')
|
|
75
|
+
if (product && !/^\d+$/.test(product))
|
|
76
|
+
return { error: 'product must be a product id.' }
|
|
77
|
+
|
|
78
|
+
return { search, status: status as ProductVariantListQuery['status'], type, product, sort: sort as ProductVariantSort, direction, page, perPage }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const FROM = ' FROM product_variants v LEFT JOIN products p ON p.id = v.product_id'
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* How many options a variant lists. `options` is text holding a JSON array
|
|
85
|
+
* or a list split by commas, bars, semicolons or new lines, as
|
|
86
|
+
* commerceStringList reads it; an array is measured by the dialect's JSON
|
|
87
|
+
* functions and a list by its separators. A list that repeats an option, which
|
|
88
|
+
* the page shows once, counts it each time.
|
|
89
|
+
*/
|
|
90
|
+
function optionCount(dialect: QueryBuilderDialect): string {
|
|
91
|
+
const column = `COALESCE(TRIM(v.options), '')`
|
|
92
|
+
const json = dialect === 'postgres'
|
|
93
|
+
? `json_array_length(CAST(v.options AS JSON))`
|
|
94
|
+
: dialect === 'sqlite' ? 'json_array_length(v.options)' : 'JSON_LENGTH(v.options)'
|
|
95
|
+
const newline = dialect === 'sqlite' ? 'char(10)' : dialect === 'postgres' ? 'chr(10)' : 'CHAR(10)'
|
|
96
|
+
const stripped = `REPLACE(REPLACE(REPLACE(REPLACE(${column}, ',', ''), '|', ''), ';', ''), ${newline}, '')`
|
|
97
|
+
return `CASE WHEN ${column} = '' THEN 0 WHEN ${column} LIKE '[%' THEN ${json} ELSE LENGTH(${column}) - LENGTH(${stripped}) + 1 END`
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function sortExpression(sort: ProductVariantSort, dialect: QueryBuilderDialect): string {
|
|
101
|
+
switch (sort) {
|
|
102
|
+
case 'product': return `LOWER(COALESCE(p.name, ''))`
|
|
103
|
+
case 'options': return optionCount(dialect)
|
|
104
|
+
// As text: PostgreSQL and MySQL keep the status as an enum, which sorts by declaration order.
|
|
105
|
+
case 'status': return asText('v.status', dialect)
|
|
106
|
+
case 'newest': return 'v.id'
|
|
107
|
+
case 'created': return 'v.created_at'
|
|
108
|
+
default: return 'LOWER(v.variant)'
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function productVariantFilter(query: Pick<ProductVariantListQuery, 'search' | 'status' | 'type' | 'product'>, dialect: QueryBuilderDialect): { where: string, values: unknown[] } {
|
|
113
|
+
const { bind, values } = binder(dialect)
|
|
114
|
+
const clauses: string[] = []
|
|
115
|
+
if (query.status)
|
|
116
|
+
clauses.push(`${asText('v.status', dialect)} = ${bind(query.status)}`)
|
|
117
|
+
if (query.type)
|
|
118
|
+
clauses.push(`v.type = ${bind(query.type)}`)
|
|
119
|
+
if (query.product)
|
|
120
|
+
clauses.push(`v.product_id = ${bind(Number(query.product))}`)
|
|
121
|
+
if (query.search) {
|
|
122
|
+
const pattern = containsPattern(query.search)
|
|
123
|
+
const columns = ['v.variant', 'v.type', `COALESCE(v.description, '')`, `COALESCE(p.name, '')`, `COALESCE(v.options, '')`]
|
|
124
|
+
clauses.push(`(${columns.map(column => `LOWER(${column}) LIKE ${bind(pattern)} ESCAPE '!'`).join(' OR ')})`)
|
|
125
|
+
}
|
|
126
|
+
return { where: clauses.length > 0 ? ` WHERE ${clauses.join(' AND ')}` : '', values }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function orderBy(query: Pick<ProductVariantListQuery, 'sort' | 'direction'>, dialect: QueryBuilderDialect): string {
|
|
130
|
+
const direction = query.direction === 'asc' ? 'ASC' : 'DESC'
|
|
131
|
+
// Ties read by name, as the page sorted them; the id breaks every
|
|
132
|
+
// remaining tie, so pages never repeat or skip a variant.
|
|
133
|
+
if (query.sort === 'newest')
|
|
134
|
+
return ` ORDER BY v.id ${direction}`
|
|
135
|
+
const byName = query.sort === 'variant' ? '' : ', LOWER(v.variant) ASC'
|
|
136
|
+
return ` ORDER BY ${sortExpression(query.sort, dialect)} ${direction}${byName}, v.id ${direction}`
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function productNames(rows: unknown): Map<string, string> {
|
|
140
|
+
return new Map(sqlRows(rows).map((row) => {
|
|
141
|
+
const id = commerceIdentifier(commerceValue(row, 'id'), 'Product')
|
|
142
|
+
return [id, commerceRequiredString(commerceValue(row, 'name'), `Product ${id}`, 'name')]
|
|
143
|
+
}))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Variants turned into records, against exactly the products they reference. */
|
|
147
|
+
async function recordsFor(variants: Array<Record<string, unknown>>, run: CommerceSqlRunner, dialect: QueryBuilderDialect): Promise<ProductVariantRecord[]> {
|
|
148
|
+
if (variants.length === 0)
|
|
149
|
+
return []
|
|
150
|
+
const productIds = referencedIds(variants, 'product_id')
|
|
151
|
+
const products = productIds.length > 0
|
|
152
|
+
? await run.unsafe(`SELECT id, name FROM products WHERE id IN (${idPlaceholders(productIds, dialect)})`, productIds).execute()
|
|
153
|
+
: []
|
|
154
|
+
const names = productNames(products)
|
|
155
|
+
return variants.map(variant => normalizeProductVariantRecord(variant, names))
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** The summary cards, over every variant as before. */
|
|
159
|
+
async function summarize(run: CommerceSqlRunner, dialect: QueryBuilderDialect): Promise<ProductVariantSummary> {
|
|
160
|
+
const status = asText('status', dialect)
|
|
161
|
+
const row = sqlRows(await run.unsafe(
|
|
162
|
+
`SELECT COUNT(*) AS total_count, SUM(CASE WHEN ${status} = 'active' THEN 1 ELSE 0 END) AS active_count, SUM(CASE WHEN ${status} = 'draft' THEN 1 ELSE 0 END) AS draft_count, COUNT(DISTINCT product_id) AS product_count FROM product_variants`,
|
|
163
|
+
).execute())[0] ?? {}
|
|
164
|
+
return {
|
|
165
|
+
total: countOf(row.total_count),
|
|
166
|
+
active: countOf(row.active_count),
|
|
167
|
+
draft: countOf(row.draft_count),
|
|
168
|
+
linkedProducts: countOf(row.product_count),
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export interface ProductVariantPage {
|
|
173
|
+
records: ProductVariantRecord[]
|
|
174
|
+
total: number
|
|
175
|
+
page: number
|
|
176
|
+
summary: ProductVariantSummary
|
|
177
|
+
/** Every type any variant has, for the filter and the dialog's suggestions. */
|
|
178
|
+
types: string[]
|
|
179
|
+
/** Every product that has a variant, for the product filter, by name ignoring case. */
|
|
180
|
+
products: Array<{ id: string, name: string }>
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export async function readProductVariantPage(
|
|
184
|
+
query: ProductVariantListQuery,
|
|
185
|
+
run: CommerceSqlRunner = db,
|
|
186
|
+
dialect: QueryBuilderDialect = getDatabaseDialect(),
|
|
187
|
+
): Promise<ProductVariantPage> {
|
|
188
|
+
const { where, values } = productVariantFilter(query, dialect)
|
|
189
|
+
const [counted, summary, typeRows, productRows] = await Promise.all([
|
|
190
|
+
run.unsafe(`SELECT COUNT(*) AS total_count${FROM}${where}`, values).execute(),
|
|
191
|
+
summarize(run, dialect),
|
|
192
|
+
run.unsafe(`SELECT DISTINCT type AS value FROM product_variants WHERE type IS NOT NULL AND type <> ''`).execute(),
|
|
193
|
+
run.unsafe('SELECT p.id, p.name FROM products p WHERE EXISTS (SELECT 1 FROM product_variants v WHERE v.product_id = p.id) ORDER BY LOWER(p.name) ASC, p.id ASC').execute(),
|
|
194
|
+
])
|
|
195
|
+
const total = countOf(sqlRows(counted)[0]?.total_count)
|
|
196
|
+
const page = pageWithin(query.page, total, query.perPage)
|
|
197
|
+
const variants = await run.unsafe(
|
|
198
|
+
`SELECT v.*${FROM}${where}${orderBy(query, dialect)} LIMIT ${query.perPage} OFFSET ${(page - 1) * query.perPage}`,
|
|
199
|
+
values,
|
|
200
|
+
).execute()
|
|
201
|
+
return {
|
|
202
|
+
records: await recordsFor(sqlRows(variants), run, dialect),
|
|
203
|
+
total,
|
|
204
|
+
page,
|
|
205
|
+
summary,
|
|
206
|
+
types: [...new Set(sqlRows(typeRows).map(row => String(row.value ?? '').trim()).filter(Boolean))].sort(),
|
|
207
|
+
products: [...productNames(productRows)].map(([id, name]) => ({ id, name })),
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it } from 'bun:test'
|
|
2
2
|
import {
|
|
3
3
|
normalizeProductUnitRecord,
|
|
4
|
-
productUnitOptions,
|
|
5
4
|
summarizeProductUnits,
|
|
6
5
|
} from './product-unit-records'
|
|
7
6
|
|
|
@@ -75,16 +74,4 @@ describe('dashboard product unit records', () => {
|
|
|
75
74
|
|
|
76
75
|
expect(summarizeProductUnits(records).defaultConflicts).toBe(1)
|
|
77
76
|
})
|
|
78
|
-
|
|
79
|
-
it('builds stable, validated product options from model rows', () => {
|
|
80
|
-
expect(productUnitOptions([
|
|
81
|
-
{ id: 2, name: 'Tea' },
|
|
82
|
-
{ id: 1, name: 'Coffee' },
|
|
83
|
-
])).toEqual([
|
|
84
|
-
{ id: '1', name: 'Coffee' },
|
|
85
|
-
{ id: '2', name: 'Tea' },
|
|
86
|
-
])
|
|
87
|
-
expect(() => productUnitOptions([{ id: 3, name: '' }]))
|
|
88
|
-
.toThrow('Product 3.name must be a non-empty string')
|
|
89
|
-
})
|
|
90
77
|
})
|
|
@@ -20,11 +20,6 @@ export interface ProductUnitRecord {
|
|
|
20
20
|
createdAt: string
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
export interface ProductUnitOption {
|
|
24
|
-
id: string
|
|
25
|
-
name: string
|
|
26
|
-
}
|
|
27
|
-
|
|
28
23
|
export interface ProductUnitSummary {
|
|
29
24
|
total: number
|
|
30
25
|
defaults: number
|
|
@@ -33,18 +28,6 @@ export interface ProductUnitSummary {
|
|
|
33
28
|
defaultConflicts: number
|
|
34
29
|
}
|
|
35
30
|
|
|
36
|
-
export function productUnitOptions(products: any[]): ProductUnitOption[] {
|
|
37
|
-
return products
|
|
38
|
-
.map((product) => {
|
|
39
|
-
const id = commerceIdentifier(commerceValue(product, 'id', 'uuid'), 'Product')
|
|
40
|
-
return {
|
|
41
|
-
id,
|
|
42
|
-
name: commerceRequiredString(commerceValue(product, 'name'), `Product ${id}`, 'name'),
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
.sort((left, right) => left.name.localeCompare(right.name))
|
|
46
|
-
}
|
|
47
|
-
|
|
48
31
|
export function normalizeProductUnitRecord(
|
|
49
32
|
record: any,
|
|
50
33
|
products = new Map<string, string>(),
|
|
@@ -2,7 +2,6 @@ import { describe, expect, it } from 'bun:test'
|
|
|
2
2
|
import {
|
|
3
3
|
normalizeProductVariantRecord,
|
|
4
4
|
parseVariantOptions,
|
|
5
|
-
productVariantOptions,
|
|
6
5
|
summarizeProductVariants,
|
|
7
6
|
} from './product-variant-records'
|
|
8
7
|
|
|
@@ -70,16 +69,4 @@ describe('dashboard product variant records', () => {
|
|
|
70
69
|
linkedProducts: 2,
|
|
71
70
|
})
|
|
72
71
|
})
|
|
73
|
-
|
|
74
|
-
it('builds validated Product options', () => {
|
|
75
|
-
expect(productVariantOptions([
|
|
76
|
-
{ id: 2, name: 'Tea' },
|
|
77
|
-
{ id: 1, name: 'Coffee' },
|
|
78
|
-
])).toEqual([
|
|
79
|
-
{ id: '1', name: 'Coffee' },
|
|
80
|
-
{ id: '2', name: 'Tea' },
|
|
81
|
-
])
|
|
82
|
-
expect(() => productVariantOptions([{ id: 3, name: '' }]))
|
|
83
|
-
.toThrow('Product 3.name must be a non-empty string')
|
|
84
|
-
})
|
|
85
72
|
})
|
|
@@ -21,11 +21,6 @@ export interface ProductVariantRecord {
|
|
|
21
21
|
createdAt: string
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export interface ProductVariantOption {
|
|
25
|
-
id: string
|
|
26
|
-
name: string
|
|
27
|
-
}
|
|
28
|
-
|
|
29
24
|
export interface ProductVariantSummary {
|
|
30
25
|
total: number
|
|
31
26
|
active: number
|
|
@@ -37,18 +32,6 @@ export function parseVariantOptions(input: unknown): string[] {
|
|
|
37
32
|
return commerceStringList(input, 'ProductVariant', 'options')
|
|
38
33
|
}
|
|
39
34
|
|
|
40
|
-
export function productVariantOptions(products: any[]): ProductVariantOption[] {
|
|
41
|
-
return products
|
|
42
|
-
.map((product) => {
|
|
43
|
-
const id = commerceIdentifier(commerceValue(product, 'id', 'uuid'), 'Product')
|
|
44
|
-
return {
|
|
45
|
-
id,
|
|
46
|
-
name: commerceRequiredString(commerceValue(product, 'name'), `Product ${id}`, 'name'),
|
|
47
|
-
}
|
|
48
|
-
})
|
|
49
|
-
.sort((left, right) => left.name.localeCompare(right.name))
|
|
50
|
-
}
|
|
51
|
-
|
|
52
35
|
export function normalizeProductVariantRecord(
|
|
53
36
|
record: any,
|
|
54
37
|
productNames = new Map<string, string>(),
|
|
@@ -302,6 +302,20 @@ describe('useServerList', () => {
|
|
|
302
302
|
expect(api.asked[1]!.get('direction')).toBe('desc')
|
|
303
303
|
})
|
|
304
304
|
|
|
305
|
+
it('follows a sort select, handing the order back from a header sort', () => {
|
|
306
|
+
const api = server()
|
|
307
|
+
const sort = state<{ sort: string, direction: 'asc' | 'desc' }>({ sort: 'name', direction: 'asc' })
|
|
308
|
+
const list = useServerList<string>({ fetch: api.fetch, defaultSort: () => sort(), columnSorts: { typeLabel: 'type' } })
|
|
309
|
+
list.changeSort({ key: 'typeLabel', direction: 'desc' })
|
|
310
|
+
expect(api.asked[0]!.get('sort')).toBe('type')
|
|
311
|
+
list.page.set(4)
|
|
312
|
+
sort.set({ sort: 'newest', direction: 'desc' })
|
|
313
|
+
expect(api.asked.length).toBe(2)
|
|
314
|
+
expect(api.asked[1]!.get('sort')).toBe('newest')
|
|
315
|
+
expect(api.asked[1]!.get('direction')).toBe('desc')
|
|
316
|
+
expect(api.asked[1]!.get('page')).toBe('1')
|
|
317
|
+
})
|
|
318
|
+
|
|
305
319
|
it('reports a failed load, and ignores one that a newer load replaced', async () => {
|
|
306
320
|
const errors: unknown[] = []
|
|
307
321
|
let calls = 0
|
|
@@ -269,8 +269,12 @@ export interface ServerListOptions<T> {
|
|
|
269
269
|
* `page` and `per_page`, as the commerce list endpoints read them.
|
|
270
270
|
*/
|
|
271
271
|
fetch: (params: URLSearchParams) => Promise<ServerListPage<T>>
|
|
272
|
-
/**
|
|
273
|
-
|
|
272
|
+
/**
|
|
273
|
+
* The order before any column header is clicked. An accessor when the page
|
|
274
|
+
* has a sort select: changing it hands the order back from a header sort
|
|
275
|
+
* and asks for page 1.
|
|
276
|
+
*/
|
|
277
|
+
defaultSort: { sort: string, direction: 'asc' | 'desc' } | (() => { sort: string, direction: 'asc' | 'desc' })
|
|
274
278
|
/** The table's sortable column keys, as the server's sort names. A column not listed returns to `defaultSort`. */
|
|
275
279
|
columnSorts?: Record<string, string>
|
|
276
280
|
/** The filter values, read reactively: a change asks for page 1. `''` or `'all'` leaves a filter out. */
|
|
@@ -332,10 +336,11 @@ export function useServerList<T>(options: ServerListOptions<T>): ServerList<T> {
|
|
|
332
336
|
const headerSort = state<{ sort: string, direction: string } | null>(null)
|
|
333
337
|
let request = 0
|
|
334
338
|
let timer: ReturnType<typeof setTimeout> | null = null
|
|
339
|
+
const defaultOrder = () => typeof options.defaultSort === 'function' ? options.defaultSort() : options.defaultSort
|
|
335
340
|
|
|
336
341
|
const params = (): URLSearchParams => {
|
|
337
342
|
const query = new URLSearchParams()
|
|
338
|
-
const order = headerSort() ??
|
|
343
|
+
const order = headerSort() ?? defaultOrder()
|
|
339
344
|
query.set('sort', order.sort)
|
|
340
345
|
query.set('direction', order.direction)
|
|
341
346
|
if (appliedSearch())
|
|
@@ -387,6 +392,19 @@ export function useServerList<T>(options: ServerListOptions<T>): ServerList<T> {
|
|
|
387
392
|
})
|
|
388
393
|
}
|
|
389
394
|
|
|
395
|
+
if (typeof options.defaultSort === 'function') {
|
|
396
|
+
let shownOrder = JSON.stringify(defaultOrder())
|
|
397
|
+
effect(() => {
|
|
398
|
+
const order = JSON.stringify(defaultOrder())
|
|
399
|
+
if (order === shownOrder)
|
|
400
|
+
return
|
|
401
|
+
shownOrder = order
|
|
402
|
+
headerSort.set(null)
|
|
403
|
+
page.set(1)
|
|
404
|
+
void load()
|
|
405
|
+
})
|
|
406
|
+
}
|
|
407
|
+
|
|
390
408
|
let shownSearch = search().trim()
|
|
391
409
|
effect(() => {
|
|
392
410
|
const term = search().trim()
|
package/ide/vscode/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"publisher": "Stacks",
|
|
3
3
|
"name": "vscode-stacks",
|
|
4
4
|
"displayName": "Stacks",
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.492",
|
|
6
6
|
"description": "Stacks, built into VS Code: stx templates, pickier linting and formatting, encrypted env files, the dev server preview and every buddy command. No other extensions required.",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"funding": "https://github.com/sponsors/chrisbbreuer",
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/defaults",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.492",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@iconify-json/f7": "^1.2.2",
|
|
47
47
|
"@iconify-json/hugeicons": "^1.2.35",
|
|
48
|
-
"@stacksjs/mobile": "0.75.
|
|
48
|
+
"@stacksjs/mobile": "0.75.492",
|
|
49
49
|
"@stacksjs/sanitizer": "0.2.807",
|
|
50
50
|
"@stacksjs/stx": "0.2.807",
|
|
51
51
|
"@xterm/addon-fit": "^0.11.0",
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
<script client>
|
|
2
|
+
import type { ProductChoice } from '../../../../app/Actions/Dashboard/Commerce/commerce-choice-records'
|
|
3
|
+
import type { ProductUnitRecord } from '../../../../app/Actions/Dashboard/Commerce/product-unit-records'
|
|
4
|
+
import { dashboardApi } from '../../../../functions/dashboard-api'
|
|
2
5
|
import { formErrorMessages } from '../../../../functions/dashboard-form'
|
|
3
|
-
import type {
|
|
4
|
-
ProductUnitOption,
|
|
5
|
-
ProductUnitRecord,
|
|
6
|
-
} from '../../../../app/Actions/Dashboard/Commerce/product-unit-records'
|
|
7
6
|
|
|
8
7
|
const open = useReactiveProp('open', false)
|
|
9
8
|
const mode = useReactiveProp('mode', 'add')
|
|
10
9
|
const record = useReactiveProp('record', null as ProductUnitRecord | null)
|
|
11
|
-
const products = useReactiveProp('products', [] as ProductUnitOption[])
|
|
12
10
|
const types = useReactiveProp('types', [] as string[])
|
|
13
11
|
const busy = useReactiveProp('busy', false)
|
|
14
12
|
const errors = useReactiveProp('errors', {} as Record<string, string>)
|
|
@@ -21,6 +19,57 @@ const productId = state('')
|
|
|
21
19
|
const isDefault = state(false)
|
|
22
20
|
let hydrationKey = ''
|
|
23
21
|
|
|
22
|
+
// The product choices are a server search (stacksjs/stacks#3218): this used
|
|
23
|
+
// to be handed the first 500 products by name, so a unit could not be given
|
|
24
|
+
// any later product.
|
|
25
|
+
const productChoices = state<ProductChoice[]>([])
|
|
26
|
+
const productMatches = state(0)
|
|
27
|
+
const productQuery = state('')
|
|
28
|
+
let productRequest = 0
|
|
29
|
+
let productTimer: ReturnType<typeof setTimeout> | null = null
|
|
30
|
+
|
|
31
|
+
async function loadProducts(term: string): Promise<void> {
|
|
32
|
+
const request = ++productRequest
|
|
33
|
+
const params = new URLSearchParams()
|
|
34
|
+
if (term)
|
|
35
|
+
params.set('q', term)
|
|
36
|
+
let found: ProductChoice[] = []
|
|
37
|
+
let count = 0
|
|
38
|
+
try {
|
|
39
|
+
const data = await dashboardApi<{ choices?: ProductChoice[], total?: number }>(`/api/dashboard/commerce/product-choices${term ? `?${params}` : ''}`)
|
|
40
|
+
found = Array.isArray(data.choices) ? data.choices : []
|
|
41
|
+
count = Number(data.total) || found.length
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// Soft-fail: the unit's own product stays selectable.
|
|
45
|
+
}
|
|
46
|
+
if (request !== productRequest)
|
|
47
|
+
return
|
|
48
|
+
productChoices.set(found)
|
|
49
|
+
productMatches.set(count)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function searchProductsSoon(): void {
|
|
53
|
+
if (productTimer)
|
|
54
|
+
clearTimeout(productTimer)
|
|
55
|
+
productTimer = setTimeout(() => { void loadProducts(productQuery().trim()) }, 250)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The choices to show: the search results, with the unit's own product kept in. */
|
|
59
|
+
function products(): ProductChoice[] {
|
|
60
|
+
const found = productChoices()
|
|
61
|
+
const current = record()
|
|
62
|
+
if (!current?.productId || found.some(choice => choice.id === current.productId))
|
|
63
|
+
return found
|
|
64
|
+
return [{ id: current.productId, label: current.productName || `Product ${current.productId}`, detail: '' }, ...found]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function moreProductsLabel(): string {
|
|
68
|
+
const shown = productChoices().length
|
|
69
|
+
const total = productMatches()
|
|
70
|
+
return total > shown ? `Showing ${shown} of ${total.toLocaleString()} matching products. Search to narrow it down.` : ''
|
|
71
|
+
}
|
|
72
|
+
|
|
24
73
|
effect(() => {
|
|
25
74
|
if (!open()) {
|
|
26
75
|
hydrationKey = ''
|
|
@@ -37,6 +86,8 @@ effect(() => {
|
|
|
37
86
|
description.set(current?.description || '')
|
|
38
87
|
productId.set(current?.productId || '')
|
|
39
88
|
isDefault.set(current?.isDefault || false)
|
|
89
|
+
productQuery.set('')
|
|
90
|
+
void loadProducts('')
|
|
40
91
|
})
|
|
41
92
|
|
|
42
93
|
function submit(): void {
|
|
@@ -90,13 +141,17 @@ function errorMessages(): string[] {
|
|
|
90
141
|
</label>
|
|
91
142
|
<label class="block">
|
|
92
143
|
<span class="font-medium text-gray-700 text-sm dark:text-neutral-200">Product</span>
|
|
93
|
-
<
|
|
144
|
+
<input x-model="productQuery" @input="searchProductsSoon()" type="search" name="product_search" aria-label="Search products by name or id" placeholder="Search by name or id" class="mt-1 px-3 py-2 w-full text-gray-900 text-sm dark:text-white bg-white dark:bg-neutral-900 border border-gray-300 rounded-md dark:border-neutral-700" />
|
|
145
|
+
</label>
|
|
146
|
+
<div>
|
|
147
|
+
<select x-model="productId" aria-label="Product" class="px-3 py-2 w-full text-gray-900 text-sm dark:text-white bg-white dark:bg-neutral-900 border border-gray-300 rounded-md dark:border-neutral-700">
|
|
94
148
|
<option value="">Unassigned</option>
|
|
95
149
|
<template :for="product in products()" :key="product.id">
|
|
96
|
-
<option :value="product.id">{{ product.
|
|
150
|
+
<option :value="product.id" :selected="product.id === productId()">{{ product.label }}</option>
|
|
97
151
|
</template>
|
|
98
152
|
</select>
|
|
99
|
-
|
|
153
|
+
<p :if="moreProductsLabel()" class="mt-1 text-gray-500 text-xs dark:text-neutral-400" :text="moreProductsLabel()"></p>
|
|
154
|
+
</div>
|
|
100
155
|
<label class="block">
|
|
101
156
|
<span class="font-medium text-gray-700 text-sm dark:text-neutral-200">Description</span>
|
|
102
157
|
<textarea x-model="description" rows="3" maxlength="255" class="mt-1 px-3 py-2 w-full text-gray-900 text-sm dark:text-white bg-white dark:bg-neutral-900 border border-gray-300 rounded-md dark:border-neutral-700"></textarea>
|