game-village-politics-frontend/app/pages/reports/[worldId]/index.vue

101 lines
5.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
import type { Report, ReportType } from '~/types/game'
definePageMeta({ layout: 'game' })
const route = useRoute()
const worldId = Number(route.params.worldId)
const { page, type, error, busy, load, remove, readAll } = useReports(worldId)
useCurrentWorld().enter(worldId)
const { t } = useI18n()
const clock = useNow()
// The kind of reports comes from the URL, so the menu's dropdown and the side menu link here.
const KINDS = ['attack', 'defense', 'support'] as const
watch(
() => route.query.type,
(q) => {
type.value = KINDS.includes(q as ReportType) ? (q as ReportType) : null
load(Number(route.query.page) || 1)
},
{ immediate: true },
)
const kinds = computed(() =>
(['all', ...KINDS] as const).map((k) => ({
key: k,
label: t(`reports.kinds.${k}`),
to: k === 'all' ? `/reports/${worldId}` : { path: `/reports/${worldId}`, query: { type: k } },
active: (type.value ?? 'all') === k,
count: k === 'all' ? page.value?.unread : undefined,
})),
)
const selected = ref<number[]>([])
watch(page, () => (selected.value = []))
const allSelected = computed(() => !!page.value?.reports.length && selected.value.length === page.value.reports.length)
function toggleAll() {
selected.value = allSelected.value ? [] : (page.value?.reports.map((r) => r.id) ?? [])
}
function subject(r: Report) {
if (r.type === 'support') return t('reports.title_support', { from: villageLabel(r.payload.from), to: villageLabel(r.payload.to) })
return t('reports.title_attack', { from: villageLabel(r.payload.attacker.village), to: villageLabel(r.payload.defender.village) })
}
const DOT = { won: 'bg-moss-600', losses: 'bg-gold-500', lost: 'bg-blood-700', info: 'bg-iron-400' }
</script>
<!-- The report list, like Die Stämme's: kinds on the left; a dot for the outcome, unread in bold, newest first; act on checked rows. -->
<template>
<div class="space-y-2 p-2.5">
<div class="grid gap-2 @2xl:grid-cols-[10rem_minmax(0,1fr)]">
<UiSideMenu :items="kinds" class="self-start" />
<section class="panel min-w-0">
<p v-if="error" class="alert m-2" role="alert">{{ error }}</p>
<p v-if="!page" class="py-6 text-center text-sm text-ink-500">{{ $t('common.loading') }}</p>
<p v-else-if="!page.reports.length" class="py-6 text-center text-sm text-ink-500">{{ $t('reports.none') }}</p>
<template v-else>
<table class="grid-table">
<thead>
<tr>
<th class="w-6"><input type="checkbox" :checked="allSelected" :aria-label="$t('reports.select_all')" @change="toggleAll" /></th>
<th>{{ $t('reports.subject') }}</th>
<th class="text-right">{{ $t('reports.received') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="r in page.reports" :key="r.id" class="hover:bg-parchment-100">
<td><input v-model="selected" type="checkbox" :value="r.id" :aria-label="$t('reports.select', { subject: subject(r) })" /></td>
<td>
<NuxtLink :to="`/reports/${worldId}/${r.id}`" class="flex items-center gap-1.5 hover:text-blood-700 hover:underline" :class="r.read ? '' : 'font-bold'">
<span class="size-2 shrink-0 rounded-full" :class="DOT[reportTone(r)]" :title="$t(`reports.tone.${reportTone(r)}`)" />
<IconUi :name="r.type === 'support' ? 'support' : 'attack'" class="size-3.5 shrink-0 text-ink-500" />
<span class="min-w-0">{{ subject(r) }}</span>
<span v-if="!r.read" class="sr-only">({{ $t('reports.unread') }})</span>
</NuxtLink>
</td>
<td class="text-right whitespace-nowrap text-ink-700 tabular-nums">{{ formatArrival(Date.parse(r.happened_at), clock, t) }}</td>
</tr>
</tbody>
</table>
<div class="flex flex-wrap items-center gap-2 bg-parchment-200 px-1.5 py-1">
<label class="flex items-center gap-1 text-sm">
<input type="checkbox" :checked="allSelected" @change="toggleAll" />
{{ $t('reports.select_all') }}
</label>
<button type="button" class="btn btn-primary" :disabled="busy || !selected.length" @click="remove(selected)">{{ $t('reports.delete') }}</button>
<button v-if="page.unread" type="button" class="btn btn-outline" :disabled="busy" @click="readAll()">{{ $t('reports.read_all') }}</button>
<div v-if="page.pages > 1" class="ml-auto flex items-center gap-1 text-sm">
<button type="button" class="btn btn-ghost" :disabled="busy || page.page <= 1" @click="load(page.page - 1)">‹ {{ $t('reports.prev') }}</button>
<span class="tabular-nums text-ink-700">{{ $t('reports.page', { page: page.page, pages: page.pages }) }}</span>
<button type="button" class="btn btn-ghost" :disabled="busy || page.page >= page.pages" @click="load(page.page + 1)">{{ $t('reports.next') }} ›</button>
</div>
</div>
</template>
</section>
</div>
</div>
</template>