steam-backlog-enforcer/web/src/library/gameCard.ts
Krzysztof kuhy Rudnicki 8dbcc462c0 web: library, store, picks and jobs paint glyphs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:56:01 +01:00

76 lines
2.8 KiB
TypeScript

// One tile of the library grid (was GameCard.tsx). Built once per game and
// reused across searches, so a keystroke never recreates a cover <img>;
// `paintCard` updates the parts that depend on the browser's state.
import { ENDPOINTS, type LibraryGame } from '../api/contract'
import { h } from '../lib/dom'
import { pill } from '../ui/build'
import { metaGlyphs, metaLine, reasonGlyph, spokenGlyphs } from './view'
export interface CardPaint {
/** Why it cannot be picked here, or null. Greys the card out. */
reason: string | null
selected: boolean
pickMode: boolean
}
/** Portrait cover, lazily loaded; a title tile when the server has none. */
function cover(game: LibraryGame): HTMLImageElement {
const img = h('img', { class: 'cover', src: ENDPOINTS.art(game.app_id), alt: '', loading: 'lazy', decoding: 'async' })
img.addEventListener(
'error',
() => {
img.replaceWith(h('div', { class: 'cover cover-tile', 'aria-hidden': 'true' }, h('span', {}, game.name)))
},
{ once: true },
)
return img
}
/**
* A button in both modes so it is a focus stop; in browse mode it only takes
* focus, in pick mode it selects. Clicks are handled by the grid.
*/
export function gameCard(game: LibraryGame): HTMLButtonElement {
return h(
'button',
{ type: 'button', class: 'game-card', 'data-app-id': game.app_id, tabindex: -1 },
cover(game),
h('span', { class: 'game-title' }, game.name),
spokenGlyphs(metaGlyphs(game), metaLine(game), 'game-meta'),
game.assigned || game.installed
? h(
'span',
{ class: 'game-badges' },
game.assigned ? pill('accent', 'assigned', '🎯') : null,
game.installed ? pill('neutral', 'installed', '💾') : null,
)
: null,
)
}
/** Apply pick state: selection, eligibility and the reason line. */
export function paintCard(card: HTMLButtonElement, { reason, selected, pickMode }: CardPaint): void {
card.classList.toggle('ineligible', reason !== null)
card.classList.toggle('selected', selected)
if (pickMode) card.setAttribute('aria-pressed', String(selected))
else card.removeAttribute('aria-pressed')
if (pickMode && reason !== null) card.setAttribute('aria-disabled', 'true')
else card.removeAttribute('aria-disabled')
const line = card.querySelector('.game-reason')
if (reason === null) {
line?.remove()
return
}
// The reason is spoken; the card paints its glyph (and any date) greyed out.
const fresh = spokenGlyphs(reasonGlyph(reason), reason, 'game-reason')
if (line) line.replaceWith(fresh)
else card.append(fresh)
}
/** Place the card in the grid: its position and whether it is the tab stop. */
export function placeCard(card: HTMLButtonElement, index: number, focusable: boolean): void {
card.dataset.index = String(index)
card.tabIndex = focusable ? 0 : -1
}