feat(quotes): show world copper, dollar, ton, and kilo on home

Add assets chart hover so the portfolio line shows the value under the pointer.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-03 19:57:39 +03:30
co-authored by Cursor
parent c21738950d
commit 5596fc75d0
8 changed files with 380 additions and 109 deletions
@@ -0,0 +1,119 @@
import { computed, ref, watch } from 'vue'
import { lastPrice } from '@/utils/price'
import { usePriceWithoutType } from '@/utils/usePrice'
const BOARD_FEED_URL = 'https://price.zaarbox.ir/api/free'
type BoardRefs = { copper: number; dollar: number }
const boardCache = ((globalThis as typeof globalThis & {
__boursecuBoardRefs?: { started: boolean; refs: BoardRefs | null }
}).__boursecuBoardRefs ??= { started: false, refs: null })
function boardRowTotal(row: Record<string, unknown>): number {
const price = Math.round(Number(row.price) || 0)
if (price <= 0) return 0
return price + Math.round(Number(row.profit) || 0)
}
function worldCopperScore(row: Record<string, unknown>): number | null {
const key = String(row.key ?? '').trim().toLowerCase()
const name = String(row.name ?? '').trim()
if (key.includes('basemetal_london_copper')) return 0
if (name === 'TGJU فلز مس') return 1
if (name.includes('مس جهانی')) return 2
return null
}
function freeDollarScore(row: Record<string, unknown>): number | null {
const key = String(row.key ?? '').trim().toLowerCase()
const name = String(row.name ?? '').trim()
if (key.includes('copper') || name.includes('کاتد') || name.includes('مس')) return null
if (key === 'tgju_usd') return 0
if (name === 'TGJU دلار' || name === 'tgju دلار آمریکا') return 1
if (name.includes('دلار') && name.includes('آزاد')) return 2
return null
}
function pickBoardRefs(rows: unknown[]) {
let copper = 0
let dollar = 0
let copperScore = Number.POSITIVE_INFINITY
let dollarScore = Number.POSITIVE_INFINITY
for (const item of rows) {
if (!item || typeof item !== 'object') continue
const row = item as Record<string, unknown>
const total = boardRowTotal(row)
if (total <= 0) continue
const copperRank = worldCopperScore(row)
if (copperRank !== null && copperRank < copperScore) {
copperScore = copperRank
copper = total
}
const dollarRank = freeDollarScore(row)
if (dollarRank !== null && dollarRank < dollarScore) {
dollarScore = dollarRank
dollar = total
}
}
return copper > 0 && dollar > 0 ? { copper, dollar } : null
}
const sharedBoardRefs = ref<BoardRefs | null>(boardCache.refs)
export function useHomeQuoteRows() {
function ensureBoardRefs() {
const liveCopper = Number(lastPrice.value?.copper_world ?? 0)
const liveDollar = Number(lastPrice.value?.dollar_free ?? 0)
if (liveCopper > 0 && liveDollar > 0) return
if (boardCache.refs) {
sharedBoardRefs.value = boardCache.refs
return
}
if (boardCache.started) return
boardCache.started = true
void fetch(BOARD_FEED_URL)
.then((res) => res.json())
.then((rows: unknown) => {
if (!Array.isArray(rows)) return
const picked = pickBoardRefs(rows)
if (!picked) return
boardCache.refs = picked
sharedBoardRefs.value = picked
})
.catch(() => {
boardCache.started = false
})
}
watch(lastPrice, () => ensureBoardRefs(), { immediate: true })
const quoteRows = computed(() => {
const fromServer = Number(lastPrice.value?.copper_world ?? 0) > 0
&& Number(lastPrice.value?.dollar_free ?? 0) > 0
const copper = fromServer
? Number(lastPrice.value?.copper_world)
: (sharedBoardRefs.value?.copper ?? 0)
const dollar = fromServer
? Number(lastPrice.value?.dollar_free)
: (sharedBoardRefs.value?.dollar ?? 0)
const tonText = String(lastPrice.value?.copper_ton ?? '')
const kiloText = String(lastPrice.value?.copper_kilo ?? '')
const ton = fromServer && tonText !== '' && tonText !== '0'
? Number(tonText)
: (copper > 0 && dollar > 0 ? copper * dollar : 0)
const kilo = fromServer && kiloText !== '' && kiloText !== '0'
? Number(kiloText)
: (ton > 0 ? ton / 1000 : 0)
const text = (value: number) => (value > 0 ? usePriceWithoutType(value) : '—')
return [
{ label: 'قیمت مس جهانی', value: text(copper), unit: copper > 0 ? 'دلار' : '' },
{ label: 'قیمت دلار بازار آزاد', value: text(dollar), unit: dollar > 0 ? 'ریال' : '' },
{ label: 'قیمت تن', value: text(ton), unit: ton > 0 ? 'ریال' : '' },
{ label: 'قیمت کیلوگرم', value: text(kilo), unit: kilo > 0 ? 'ریال' : '' },
]
})
return { quoteRows }
}