Files
commander/frontend/app/pages/cards.vue
2026-10-29 15:21:05 +01:00

460 lines
16 KiB
Vue

<script setup lang="ts">
import SourceGuide from '../components/SourceGuide.vue'
type Status = 'supported' | 'partial' | 'unsupported'
const statusLabels: Record<Status, string> = {
supported: 'Supported',
partial: 'Partial',
unsupported: 'Unsupported',
}
interface CardSummary {
oracle_id: string
name: string
mana_cost?: string
type_line: string
oracle_text?: string
card_faces?: Array<{
name: string
mana_cost?: string
type_line?: string
oracle_text?: string
}>
implementation: { status: Status; manual: boolean } | null
}
interface ImplementationResponse {
card: CardSummary
compiled: { status: Status }
source: string
manual: boolean
updatedAt?: string
error?: string
}
interface Stats {
supported: number
partial: number
unsupported: number
manual: number
}
const { public: { apiBase } } = useRuntimeConfig()
const pageSize = 50
const formatOptions = [
{ value: 'commander', label: 'Commander' },
{ value: 'brawl', label: 'Brawl' },
{ value: 'standard', label: 'Standard' },
{ value: 'pioneer', label: 'Pioneer' },
{ value: 'modern', label: 'Modern' },
{ value: 'legacy', label: 'Legacy' },
{ value: 'vintage', label: 'Vintage' },
{ value: 'pauper', label: 'Pauper' },
{ value: 'historic', label: 'Historic' },
{ value: 'timeless', label: 'Timeless' },
{ value: 'explorer', label: 'Explorer' },
{ value: 'alchemy', label: 'Alchemy' },
{ value: 'oathbreaker', label: 'Oathbreaker' },
{ value: 'duel', label: 'Duel Commander' },
{ value: 'paupercommander', label: 'Pauper Commander' },
{ value: 'standardbrawl', label: 'Standard Brawl' },
{ value: 'gladiator', label: 'Gladiator' },
{ value: 'competitivebrawl', label: 'Competitive Brawl' },
{ value: 'premodern', label: 'Premodern' },
{ value: 'predh', label: 'PreDH' },
{ value: 'oldschool', label: 'Old School' },
{ value: 'penny', label: 'Penny Dreadful' },
]
const colorOptions = [
{ value: 'W', label: 'White' },
{ value: 'U', label: 'Blue' },
{ value: 'B', label: 'Black' },
{ value: 'R', label: 'Red' },
{ value: 'G', label: 'Green' },
{ value: 'M', label: 'Multicolor' },
{ value: 'C', label: 'Colorless' },
]
const typeOptions = [
{ value: 'creature', label: 'Creature' },
{ value: 'instant', label: 'Instant' },
{ value: 'sorcery', label: 'Sorcery' },
{ value: 'artifact', label: 'Artifact' },
{ value: 'enchantment', label: 'Enchantment' },
{ value: 'planeswalker', label: 'Planeswalker' },
{ value: 'battle', label: 'Battle' },
{ value: 'land', label: 'Land' },
{ value: 'kindred', label: 'Kindred' },
{ value: 'legendary', label: 'Legendary' },
]
const rarityOptions = ['common', 'uncommon', 'rare', 'mythic', 'special', 'bonus']
const cmcOptions = ['0', '1', '2', '3', '4', '5', '6', '7+']
const view = ref<'editor' | 'guide'>('editor')
const search = ref('')
const statusFilter = ref('needs-work')
const formatFilter = ref('')
const colorFilter = ref('')
const typeFilter = ref('')
const rarityFilter = ref('')
const cmcFilter = ref('')
const cards = ref<CardSummary[]>([])
const total = ref(0)
const offset = ref(0)
const loadingList = ref(false)
const listError = ref('')
const stats = ref<Stats | null>(null)
const selectedId = ref('')
const detail = ref<ImplementationResponse | null>(null)
const source = ref('')
const detailLoading = ref(false)
const saving = ref(false)
const translating = ref(false)
const editorError = ref('')
const page = computed(() => Math.floor(offset.value / pageSize) + 1)
const pageCount = computed(() => Math.max(1, Math.ceil(total.value / pageSize)))
const dirty = computed(() => Boolean(detail.value) && source.value !== detail.value!.source)
function errorMessage(error: unknown): string {
const data = (error as { data?: { error?: string } })?.data
return data?.error ?? (error instanceof Error ? error.message : String(error))
}
function manaCostOf(card: CardSummary): string | undefined {
const faces = card.card_faces?.map(face => face.mana_cost).filter(Boolean) ?? []
return card.mana_cost ?? (faces.length ? faces.join(' // ') : undefined)
}
async function loadStats() {
try {
stats.value = await $fetch<Stats>(`${apiBase}/stats`)
} catch {
stats.value = null
}
}
async function loadCards(reset = false) {
loadingList.value = true
listError.value = ''
if (reset) offset.value = 0
try {
const data = await $fetch<{ total: number; cards: CardSummary[] }>(`${apiBase}/cards`, {
query: {
q: search.value,
status: statusFilter.value,
format: formatFilter.value || undefined,
color: colorFilter.value || undefined,
type: typeFilter.value || undefined,
rarity: rarityFilter.value || undefined,
cmcMin: cmcFilter.value === '7+' ? '7' : cmcFilter.value || undefined,
cmcMax: cmcFilter.value === '7+' ? undefined : cmcFilter.value || undefined,
limit: pageSize,
offset: offset.value,
},
})
cards.value = data.cards
total.value = data.total
} catch (error) {
listError.value = errorMessage(error)
} finally {
loadingList.value = false
}
}
async function selectCard(card: CardSummary) {
selectedId.value = card.oracle_id
detailLoading.value = true
editorError.value = ''
try {
detail.value = await $fetch<ImplementationResponse>(
`${apiBase}/cards/${card.oracle_id}/implementation`,
)
source.value = detail.value.source
} catch (error) {
detail.value = null
editorError.value = errorMessage(error)
} finally {
detailLoading.value = false
}
}
async function save() {
if (!detail.value || saving.value) return
saving.value = true
editorError.value = ''
try {
const data = await $fetch<ImplementationResponse>(
`${apiBase}/cards/${detail.value.card.oracle_id}/implementation`,
{ method: 'PUT', body: { source: source.value } },
)
detail.value = data
source.value = data.source
updateSummary(data)
await loadStats()
} catch (error) {
editorError.value = errorMessage(error)
} finally {
saving.value = false
}
}
async function translate() {
if (!detail.value || translating.value) return
if (dirty.value && !window.confirm('Replace the current edits with an AI draft?')) return
translating.value = true
editorError.value = ''
try {
const data = await $fetch<{ source: string }>(
`${apiBase}/cards/${detail.value.card.oracle_id}/translate`,
{ method: 'POST' },
)
source.value = data.source
} catch (error) {
editorError.value = errorMessage(error)
} finally {
translating.value = false
}
}
async function reset() {
if (!detail.value || saving.value) return
if (!window.confirm('Delete the verified implementation and restore the generated one?')) return
saving.value = true
editorError.value = ''
try {
const data = await $fetch<ImplementationResponse>(
`${apiBase}/cards/${detail.value.card.oracle_id}/implementation`,
{ method: 'DELETE' },
)
detail.value = data
source.value = data.source
updateSummary(data)
await loadStats()
} catch (error) {
editorError.value = errorMessage(error)
} finally {
saving.value = false
}
}
function updateSummary(data: ImplementationResponse) {
const card = cards.value.find(entry => entry.oracle_id === data.card.oracle_id)
if (card) card.implementation = { status: data.compiled.status, manual: data.manual }
}
function previousPage() {
if (offset.value === 0) return
offset.value = Math.max(0, offset.value - pageSize)
loadCards()
}
function nextPage() {
if (offset.value + pageSize >= total.value) return
offset.value += pageSize
loadCards()
}
onMounted(() => {
loadStats()
loadCards(true)
})
</script>
<template>
<main class="fill-page">
<header>
<div>
<h1>Card implementations</h1>
<p class="muted">
<template v-if="stats">
{{ stats.supported.toLocaleString() }} supported ·
{{ stats.partial.toLocaleString() }} partial ·
{{ stats.unsupported.toLocaleString() }} unsupported ·
{{ stats.manual.toLocaleString() }} verified
</template>
<template v-else>Loading stats…</template>
</p>
</div>
<div class="head-actions">
<div class="tabs">
<button type="button" :class="{ active: view === 'editor' }" @click="view = 'editor'">Editor</button>
<button type="button" :class="{ active: view === 'guide' }" @click="view = 'guide'">Guide</button>
</div>
<a :href="`${apiBase}/health`" target="_blank" rel="noreferrer">API health</a>
</div>
</header>
<div v-if="view === 'editor'" class="layout">
<section class="list-panel">
<form class="search" @submit.prevent="loadCards(true)">
<input v-model="search" type="search" placeholder="Search card names…">
<select v-model="statusFilter" aria-label="Implementation status" @change="loadCards(true)">
<option value="needs-work">Needs work</option>
<option value="unsupported">Not implemented</option>
<option value="partial">Partial</option>
<option value="supported">Supported</option>
<option value="manual">Verified</option>
<option value="all">All cards</option>
</select>
<button type="submit">Search</button>
</form>
<div class="filters">
<select v-model="formatFilter" aria-label="Format legality" @change="loadCards(true)">
<option value="">Any format</option>
<option v-for="format in formatOptions" :key="format.value" :value="format.value">
{{ format.label }}
</option>
</select>
<select v-model="colorFilter" aria-label="Color identity" @change="loadCards(true)">
<option value="">Any color</option>
<option v-for="color in colorOptions" :key="color.value" :value="color.value">
{{ color.label }}
</option>
</select>
<select v-model="typeFilter" aria-label="Card type" @change="loadCards(true)">
<option value="">Any type</option>
<option v-for="type in typeOptions" :key="type.value" :value="type.value">
{{ type.label }}
</option>
</select>
<select v-model="rarityFilter" aria-label="Rarity" @change="loadCards(true)">
<option value="">Any rarity</option>
<option v-for="rarity in rarityOptions" :key="rarity" :value="rarity">
{{ rarity }}
</option>
</select>
<select v-model="cmcFilter" aria-label="Mana value" @change="loadCards(true)">
<option value="">Any mana value</option>
<option v-for="cmc in cmcOptions" :key="cmc" :value="cmc">
{{ cmc === '7+' ? '7 or more' : cmc }}
</option>
</select>
</div>
<p v-if="listError" class="error">{{ listError }}</p>
<p v-else-if="loadingList" class="muted">Loading…</p>
<p v-else-if="!cards.length" class="muted">No cards match.</p>
<ul class="card-list">
<li v-for="card in cards" :key="card.oracle_id">
<CardHover :oracle-id="card.oracle_id" :name="card.name">
<button
type="button"
class="card-row"
:class="{ selected: card.oracle_id === selectedId }"
@click="selectCard(card)"
>
<span class="card-main">
<span class="card-name">{{ card.name }}</span>
<ManaCost :cost="manaCostOf(card)" />
</span>
<span class="badges">
<span
v-if="card.implementation"
class="badge"
:class="card.implementation.status"
:title="statusLabels[card.implementation.status]"
:aria-label="statusLabels[card.implementation.status]"
>{{ card.implementation.status[0] }}</span>
<span
v-if="card.implementation"
class="badge"
:class="card.implementation.manual ? 'verified' : 'generated'"
:title="card.implementation.manual ? 'Verified' : 'Generated'"
:aria-label="card.implementation.manual ? 'Verified' : 'Generated'"
>{{ card.implementation.manual ? 'V' : 'G' }}</span>
</span>
</button>
</CardHover>
</li>
</ul>
<div class="pager">
<button type="button" :disabled="offset === 0 || loadingList" @click="previousPage">Previous</button>
<span class="muted">Page {{ page }} of {{ pageCount }} · {{ total.toLocaleString() }} cards</span>
<button type="button" :disabled="offset + pageSize >= total || loadingList" @click="nextPage">Next</button>
</div>
</section>
<section class="detail-panel">
<p v-if="!selectedId" class="muted">Select a card to view or edit its implementation.</p>
<p v-else-if="detailLoading" class="muted">Loading card…</p>
<template v-else-if="detail">
<div class="detail-head">
<div>
<h2>{{ detail.card.name }}</h2>
<p class="muted">
{{ detail.card.type_line }}
<ManaCost :cost="manaCostOf(detail.card)" />
</p>
</div>
<div class="badges">
<span
class="badge"
:class="detail.compiled.status"
:title="statusLabels[detail.compiled.status]"
:aria-label="statusLabels[detail.compiled.status]"
>{{ detail.compiled.status[0] }}</span>
<span
class="badge"
:class="detail.manual ? 'verified' : 'generated'"
:title="detail.manual ? 'Verified' : 'Generated'"
:aria-label="detail.manual ? 'Verified' : 'Generated'"
>{{ detail.manual ? 'V' : 'G' }}</span>
</div>
</div>
<template v-if="detail.card.card_faces?.length">
<div v-for="face in detail.card.card_faces" :key="face.name" class="oracle">
<h3>{{ face.name }}</h3>
<p class="muted">{{ face.type_line }}</p>
<p><SymbolText :text="face.oracle_text" /></p>
</div>
</template>
<div v-else class="oracle">
<p><SymbolText :text="detail.card.oracle_text" /></p>
</div>
<p v-if="detail.error" class="error">
The saved implementation does not parse: {{ detail.error }}
</p>
<p v-else-if="editorError" class="error">{{ editorError }}</p>
<span class="muted">Card source</span>
<textarea
v-model="source"
spellcheck="false"
aria-label="Card implementation"
@keydown.ctrl.s.prevent="save"
@keydown.meta.s.prevent="save"
/>
<div class="actions">
<button type="button" class="primary" :disabled="saving || !dirty" @click="save">
{{ saving ? 'Saving…' : 'Save' }}
</button>
<span class="muted">{{ dirty ? 'Unsaved changes' : 'Saved' }}</span>
<button type="button" :disabled="saving || translating" @click="translate">
{{ translating ? 'Drafting…' : 'Draft with AI' }}
</button>
<button type="button" :disabled="saving || !detail.manual" @click="reset">
Reset to generated
</button>
<span v-if="detail.updatedAt" class="muted saved-at">
Updated {{ new Date(detail.updatedAt).toLocaleString() }}
</span>
</div>
</template>
</section>
</div>
<SourceGuide v-else />
</main>
</template>