fix(products): absolutize image URLs and preserve them on SSE merge
Relative full_src now uses node_server, and empty live-list file payloads no longer wipe catalog images. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -57,22 +57,45 @@ function productFileId(product: Product): number {
|
||||
return Number(product.file_id ?? (product.file as { id?: number } | undefined)?.id ?? 0)
|
||||
}
|
||||
|
||||
function productHasDisplayImage(product: Product): boolean {
|
||||
const file = product.file as { full_src?: string; url?: string; src?: string } | null | undefined
|
||||
if (file && (file.full_src || file.url || file.src)) {
|
||||
return true
|
||||
}
|
||||
return !!(product.img && /^https?:\/\//i.test(product.img))
|
||||
}
|
||||
|
||||
/** استریم گاهی file خالی با همان file_id میفرستد — عکس خوب HTTP را پاک نکن. */
|
||||
function mergeLiveProductList(incoming: Product[], current?: Product[]): Product[] {
|
||||
if (!current?.length) return incoming
|
||||
|
||||
const currentById = new Map(current.map((item) => [item.id, item]))
|
||||
return incoming.map((item) => {
|
||||
const prev = currentById.get(item.id)
|
||||
if (!prev || productFileId(prev) <= productFileId(item)) {
|
||||
return item
|
||||
if (!prev) return item
|
||||
|
||||
const prevId = productFileId(prev)
|
||||
const nextId = productFileId(item)
|
||||
|
||||
if (!productHasDisplayImage(item) && productHasDisplayImage(prev)) {
|
||||
return {
|
||||
...item,
|
||||
file: prev.file,
|
||||
file_id: prev.file_id ?? (prev.file as { id?: number } | undefined)?.id ?? item.file_id,
|
||||
img: prev.img,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...item,
|
||||
file: prev.file,
|
||||
file_id: prev.file_id,
|
||||
img: prev.img,
|
||||
if (prevId > nextId) {
|
||||
return {
|
||||
...item,
|
||||
file: prev.file,
|
||||
file_id: prev.file_id,
|
||||
img: prev.img,
|
||||
}
|
||||
}
|
||||
|
||||
return item
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user