283 lines
8.2 KiB
TypeScript
283 lines
8.2 KiB
TypeScript
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
|||
|
|
import { useFullscreen } from '@vueuse/core'
|
||
|
|
import type { Product } from '@/models/products'
|
||
|
|
import { getTvProducts } from '@/api/products'
|
||
|
|
import { getConfigApp } from '@/utils/config'
|
||
|
|
import { getProductBuyPrice, getProductSellPrice, isProductPriceInactive } from '@/utils/productPrice'
|
||
|
|
import { getLastPrices, getTradeBuyPrice, getTradeSellPrice, lastPrice, startLastPricePolling, stopLastPricePolling } from '@/utils/price'
|
||
|
|
import { getPriceDisplayMessage, getPriceDisplayState, NO_QUOTE_MESSAGE } from '@/utils/storeHours'
|
||
|
|
import { isMeltedTradeGroup } from '@/utils/productCategories'
|
||
|
|
import { priceSocketConnected, priceStreamLifecycle } from '@/utils/priceSocketState'
|
||
|
|
import { usePrice } from '@/utils/usePrice'
|
||
|
|
import logoFallback from '@/assets/img/logo_full_.png'
|
||
|
|
//@ts-ignore
|
||
|
|
import moment from 'moment-jalaali'
|
||
|
|
//@ts-ignore
|
||
|
|
import fa from 'moment/src/locale/fa'
|
||
|
|
|
||
|
|
moment.locale('fa', fa)
|
||
|
|
moment.loadPersian({ usePersianDigits: false })
|
||
|
|
|
||
|
|
const CURSOR_HIDE_MS = 2500
|
||
|
|
const TV_HTML_CLASS = 'app-layout--tv'
|
||
|
|
const REFRESH_MS = 15000
|
||
|
|
|
||
|
|
function productText(product: Product) {
|
||
|
|
return `${product.group_type ?? ''} ${product.title ?? ''}`
|
||
|
|
}
|
||
|
|
|
||
|
|
function isShowOnTv(product: Product) {
|
||
|
|
const value = product.show_on_tv as unknown
|
||
|
|
return value === true || value === 1 || value === '1'
|
||
|
|
}
|
||
|
|
|
||
|
|
function isParsianCoin(product: Product) {
|
||
|
|
return productText(product).includes('پارسیان')
|
||
|
|
}
|
||
|
|
|
||
|
|
function isBankCoin(product: Product) {
|
||
|
|
if (isParsianCoin(product)) return false
|
||
|
|
const text = productText(product)
|
||
|
|
const group = (product.group_type ?? '').trim()
|
||
|
|
if (group === 'سکه' || group === 'سکه بانکی' || group.includes('بانکی')) return true
|
||
|
|
return text.includes('سکه') && /تمام|نیم|ربع/.test(text)
|
||
|
|
}
|
||
|
|
|
||
|
|
function bankCoinRank(product: Product) {
|
||
|
|
const text = productText(product)
|
||
|
|
if (text.includes('نیم')) return 2
|
||
|
|
if (text.includes('ربع')) return 3
|
||
|
|
if (text.includes('تمام') || text.includes('امامی') || text.includes('بهار')) return 1
|
||
|
|
return 99
|
||
|
|
}
|
||
|
|
|
||
|
|
function isBankCoinDenomination(product: Product) {
|
||
|
|
return bankCoinRank(product) <= 3
|
||
|
|
}
|
||
|
|
|
||
|
|
function isSimorghBar(product: Product) {
|
||
|
|
return productText(product).includes('سیمرغ')
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useTvMonitorPage() {
|
||
|
|
const rootEl = ref<HTMLElement | null>(null)
|
||
|
|
const { isFullscreen, enter: enterFullscreen } = useFullscreen(rootEl)
|
||
|
|
|
||
|
|
const products = ref<Product[]>([])
|
||
|
|
const loading = ref(true)
|
||
|
|
const nowLabel = ref('')
|
||
|
|
const dateLabel = ref('')
|
||
|
|
const cursorHidden = ref(false)
|
||
|
|
const wakeLockSentinel = ref<{ release: () => Promise<void> } | null>(null)
|
||
|
|
|
||
|
|
let clockTimer: ReturnType<typeof setInterval> | undefined
|
||
|
|
let cursorTimer: ReturnType<typeof setTimeout> | undefined
|
||
|
|
let listTimer: ReturnType<typeof setInterval> | undefined
|
||
|
|
|
||
|
|
const brandName = computed(
|
||
|
|
() => String(getConfigApp('name_fa') || getConfigApp('name') || 'تابلو قیمت'),
|
||
|
|
)
|
||
|
|
const brandLogo = computed(() => String(getConfigApp('avatar') || logoFallback))
|
||
|
|
|
||
|
|
const catalogProducts = computed(() => products.value.filter(isShowOnTv))
|
||
|
|
|
||
|
|
const parsianCoins = computed(() =>
|
||
|
|
catalogProducts.value
|
||
|
|
.filter(isParsianCoin)
|
||
|
|
.sort((a, b) => Number(a.amount ?? 0) - Number(b.amount ?? 0)),
|
||
|
|
)
|
||
|
|
|
||
|
|
const bankCoins = computed(() =>
|
||
|
|
catalogProducts.value
|
||
|
|
.filter(isBankCoin)
|
||
|
|
.filter(isBankCoinDenomination)
|
||
|
|
.sort((a, b) => bankCoinRank(a) - bankCoinRank(b) || Number(b.amount ?? 0) - Number(a.amount ?? 0)),
|
||
|
|
)
|
||
|
|
|
||
|
|
const hasCoinBoard = computed(
|
||
|
|
() => parsianCoins.value.length > 0 || bankCoins.value.length > 0,
|
||
|
|
)
|
||
|
|
|
||
|
|
const coinRowCount = computed(() =>
|
||
|
|
Math.max(parsianCoins.value.length, bankCoins.value.length, 1),
|
||
|
|
)
|
||
|
|
|
||
|
|
const simorghBars = computed(() =>
|
||
|
|
catalogProducts.value
|
||
|
|
.filter(isSimorghBar)
|
||
|
|
.sort((a, b) => Number(a.amount ?? 0) - Number(b.amount ?? 0)),
|
||
|
|
)
|
||
|
|
|
||
|
|
const statusBanner = computed(() => {
|
||
|
|
void lastPrice.value
|
||
|
|
const state = getPriceDisplayState()
|
||
|
|
return state === 'open' ? '' : getPriceDisplayMessage(state)
|
||
|
|
})
|
||
|
|
|
||
|
|
const liveBuy = computed(() => getTradeBuyPrice(lastPrice.value))
|
||
|
|
const liveSell = computed(() => getTradeSellPrice(lastPrice.value))
|
||
|
|
|
||
|
|
const isLive = computed(
|
||
|
|
() => priceSocketConnected.value || priceStreamLifecycle.value === 'open',
|
||
|
|
)
|
||
|
|
|
||
|
|
async function loadProducts() {
|
||
|
|
try {
|
||
|
|
const list = await getTvProducts()
|
||
|
|
products.value = Array.isArray(list) ? list : []
|
||
|
|
} catch {
|
||
|
|
if (!products.value.length) {
|
||
|
|
products.value = []
|
||
|
|
}
|
||
|
|
} finally {
|
||
|
|
loading.value = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function updateClock() {
|
||
|
|
const now = moment()
|
||
|
|
nowLabel.value = now.format('HH:mm:ss')
|
||
|
|
dateLabel.value = now.format('dddd jD jMMMM jYYYY')
|
||
|
|
}
|
||
|
|
|
||
|
|
function hideCursorSoon() {
|
||
|
|
cursorHidden.value = false
|
||
|
|
if (cursorTimer) clearTimeout(cursorTimer)
|
||
|
|
cursorTimer = setTimeout(() => {
|
||
|
|
cursorHidden.value = true
|
||
|
|
}, CURSOR_HIDE_MS)
|
||
|
|
}
|
||
|
|
|
||
|
|
async function requestWakeLock() {
|
||
|
|
try {
|
||
|
|
const nav = navigator as Navigator & {
|
||
|
|
wakeLock?: { request: (type: 'screen') => Promise<{ release: () => Promise<void> }> }
|
||
|
|
}
|
||
|
|
wakeLockSentinel.value = (await nav.wakeLock?.request('screen')) ?? null
|
||
|
|
} catch {
|
||
|
|
wakeLockSentinel.value = null
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function releaseWakeLock() {
|
||
|
|
try {
|
||
|
|
await wakeLockSentinel.value?.release()
|
||
|
|
} catch {
|
||
|
|
/* ignore */
|
||
|
|
}
|
||
|
|
wakeLockSentinel.value = null
|
||
|
|
}
|
||
|
|
|
||
|
|
async function onVisibilityChange() {
|
||
|
|
if (document.visibilityState === 'visible') {
|
||
|
|
await requestWakeLock()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function handleBoardClick() {
|
||
|
|
hideCursorSoon()
|
||
|
|
if (!isFullscreen.value) {
|
||
|
|
try {
|
||
|
|
await enterFullscreen()
|
||
|
|
} catch {
|
||
|
|
/* browser may require a later gesture */
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function productWeightLabel(product: Product) {
|
||
|
|
if (isMeltedTradeGroup(product.group_type)) {
|
||
|
|
return 'قیمت هر گرم'
|
||
|
|
}
|
||
|
|
const amount = Number(product.amount ?? 0)
|
||
|
|
if (!amount) return ''
|
||
|
|
if (amount < 1) return `${amount * 1000} سوت`
|
||
|
|
return `${amount} گرم`
|
||
|
|
}
|
||
|
|
|
||
|
|
function simorghWeightLabel(product: Product) {
|
||
|
|
const amount = Number(product.amount ?? 0)
|
||
|
|
if (!amount) return product.title
|
||
|
|
return `${amount} گرم`
|
||
|
|
}
|
||
|
|
|
||
|
|
function productBuyLabel(product: Product) {
|
||
|
|
if (isProductPriceInactive(product)) return NO_QUOTE_MESSAGE
|
||
|
|
const value = getProductBuyPrice(product)
|
||
|
|
return value > 0 ? usePrice(value) : '—'
|
||
|
|
}
|
||
|
|
|
||
|
|
function productSellLabel(product: Product) {
|
||
|
|
if (isProductPriceInactive(product)) return NO_QUOTE_MESSAGE
|
||
|
|
const value = getProductSellPrice(product)
|
||
|
|
return value > 0 ? usePrice(value) : '—'
|
||
|
|
}
|
||
|
|
|
||
|
|
function liveQuoteLabel(value: number) {
|
||
|
|
return value > 0 ? usePrice(value) : '—'
|
||
|
|
}
|
||
|
|
|
||
|
|
onMounted(async () => {
|
||
|
|
document.documentElement.classList.add(TV_HTML_CLASS)
|
||
|
|
updateClock()
|
||
|
|
hideCursorSoon()
|
||
|
|
clockTimer = setInterval(updateClock, 1000)
|
||
|
|
window.addEventListener('mousemove', hideCursorSoon)
|
||
|
|
document.addEventListener('visibilitychange', onVisibilityChange)
|
||
|
|
await requestWakeLock()
|
||
|
|
|
||
|
|
startLastPricePolling()
|
||
|
|
void getLastPrices()
|
||
|
|
void loadProducts()
|
||
|
|
listTimer = setInterval(() => {
|
||
|
|
void loadProducts()
|
||
|
|
}, REFRESH_MS)
|
||
|
|
|
||
|
|
document.title = `${brandName.value} | تابلو قیمت`
|
||
|
|
|
||
|
|
try {
|
||
|
|
await enterFullscreen()
|
||
|
|
} catch {
|
||
|
|
/* ignored until user clicks */
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
onUnmounted(() => {
|
||
|
|
document.documentElement.classList.remove(TV_HTML_CLASS)
|
||
|
|
document.title = String(getConfigApp('name_fa') || getConfigApp('name') || '')
|
||
|
|
if (clockTimer) clearInterval(clockTimer)
|
||
|
|
if (cursorTimer) clearTimeout(cursorTimer)
|
||
|
|
if (listTimer) clearInterval(listTimer)
|
||
|
|
window.removeEventListener('mousemove', hideCursorSoon)
|
||
|
|
document.removeEventListener('visibilitychange', onVisibilityChange)
|
||
|
|
void releaseWakeLock()
|
||
|
|
stopLastPricePolling()
|
||
|
|
})
|
||
|
|
|
||
|
|
return {
|
||
|
|
rootEl,
|
||
|
|
brandName,
|
||
|
|
brandLogo,
|
||
|
|
nowLabel,
|
||
|
|
dateLabel,
|
||
|
|
parsianCoins,
|
||
|
|
bankCoins,
|
||
|
|
hasCoinBoard,
|
||
|
|
coinRowCount,
|
||
|
|
simorghBars,
|
||
|
|
statusBanner,
|
||
|
|
liveBuy,
|
||
|
|
liveSell,
|
||
|
|
isLive,
|
||
|
|
isFullscreen,
|
||
|
|
cursorHidden,
|
||
|
|
loading,
|
||
|
|
productWeightLabel,
|
||
|
|
simorghWeightLabel,
|
||
|
|
productBuyLabel,
|
||
|
|
productSellLabel,
|
||
|
|
liveQuoteLabel,
|
||
|
|
handleBoardClick,
|
||
|
|
}
|
||
|
|
}
|