Add settings for currency and inline player

This commit is contained in:
Cadence Ember 2025-04-08 14:45:18 +12:00
parent 6cc0003120
commit 82234f9706
10 changed files with 104 additions and 15 deletions

View file

@ -1,17 +1,16 @@
// @ts-check
const {z} = require("zod")
const {defineEventHandler, getQuery, getValidatedQuery, sendRedirect, createError, getValidatedRouterParams} = require("h3")
const {defineEventHandler, getQuery, getValidatedQuery, sendRedirect, createError, getValidatedRouterParams, getCookie} = require("h3")
const {router, db, sync, select, from} = require("../passthrough")
const pugSync = sync.require("../pug-sync")
/** @type {import("./load-tags")} */
const loadTags = sync.require("./load-tags")
const displayCurrency = "NZD"
const displayCurrencySymbol = "$"
const currencyExchange = new Map([
["AUD", 0.63],
["BRL", 0.17],
["CAD", 0.7],
["CHF", 1.13],
["EUR", 1.08],
@ -63,6 +62,24 @@ function loadPreviews(locals, field, number, whereClause, account, filter_field,
}
}
pugSync.addGlobals({
getAlbumCoverAttributes(event, item) {
/** @type {any} */
let attributes = {
"hx-get": `/api/play/${item.item_type}/${item.item_id}`,
"hx-target": "#player",
"hx-select": "#player",
"hx-indicator": "null",
"hx-push-url": "false"
}
if (getCookie(event, "bcex-inline-player-disabled") === "true") {
attributes = {"target": "_blank"}
}
attributes.href = item.item_url
return attributes
}
})
const schema = {
query: z.object({
arrange: z.enum(["album", "artist", "label", "tag", "track"]),
@ -132,6 +149,11 @@ router.get("/:account/", defineEventHandler({
console.error(sql, params)
throw e
}
let displayCurrency = getCookie(event, "bcex-currency") || ""
if (!currencyExchange.has(displayCurrency)) displayCurrency = "NZD"
const currencyRoundTo = displayCurrency === "JPY" ? 1000 : 10
const locals = {
items,
account,
@ -149,12 +171,12 @@ router.get("/:account/", defineEventHandler({
avgTags: Math.round(db.prepare("SELECT avg(count) FROM (SELECT count(*) AS count FROM item_tag WHERE account = ? GROUP BY item_id)").pluck().get(account)*10)/10,
lonelyTags: db.prepare("SELECT count(*) FROM (SELECT tag FROM item_tag WHERE account = ? GROUP BY tag HAVING count(*) = 1)").pluck().get(account),
value: Math.round(select("item", ["currency", "price"], {account}).all().map(c => {
return (currencyExchange.get(c.currency) || 0.6) * c.price / (currencyExchange.get(displayCurrency) || 1) / 10
}).reduce((a, c) => a + c, 0)) * 10,
return (currencyExchange.get(c.currency) || 0.6) * c.price / (currencyExchange.get(displayCurrency) || 1) / currencyRoundTo
}).reduce((a, c) => a + c, 0)) * currencyRoundTo,
displayCurrency,
displayCurrencySymbol,
taste: db.prepare("with popularity (a) as (select avg(also_collected_count) from item WHERE account = ? group by band_url) select sum(iif(a >= 0 and a < 20, 1, 0)) as cold, sum(iif(a >= 20 and a < 200, 1, 0)) as warm, sum(iif(a >= 200 and a < 2000, 1, 0)) as hot, sum(iif(a >= 2000, 1, 0)) as supernova from popularity").raw().get(account)
}
},
currencies: [...currencyExchange.keys()]
}
if (mode === "artist_grid") {
loadPreviews(locals, "band_name", 4, whereClause, account, filter_field, filter, filter_fuzzy)

28
routes/settings.js Normal file
View file

@ -0,0 +1,28 @@
// @ts-check
const {z} = require("zod")
const {router} = require("../passthrough")
const {defineEventHandler, readValidatedBody, setCookie, setResponseHeader, sendRedirect} = require("h3")
const schema = {
inline_player: z.object({
inline_player: z.string().optional()
}),
currency: z.object({
currency: z.string().regex(/^[A-Z]{3}$/),
account: z.string()
})
}
router.post("/api/settings/inline-player", defineEventHandler(async event => {
const {inline_player} = await readValidatedBody(event, schema.inline_player.parse)
setCookie(event, "bcex-inline-player-disabled", String(!inline_player))
setResponseHeader(event, "HX-Refresh", "true")
return null
}))
router.post("/api/settings/currency", defineEventHandler(async event => {
const {currency, account} = await readValidatedBody(event, schema.currency.parse)
setCookie(event, "bcex-currency", currency)
return sendRedirect(event, `/${account}/?arrange=tag&shape=grid`, 302)
}))