All files / src/views ModerationPage.vue

98.88% Statements 89/90
93.15% Branches 68/73
90.47% Functions 19/21
100% Lines 76/76

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244  40x           88x 1x             1x                                             1x     1x 1x 1x   1x 1x 1x   1x 1x 1x               1x                       40x       41x         1x                   9x           1x 1x 1x 1x                                                                                                 40x 40x 40x 40x 40x   40x   83x 80x 79x 80x 40x 40x   40x 39x 39x 39x         40x 39x           9x 8x 7x       41x 41x 41x   1x         14x 14x 12x 12x     11x   1x     12x       2x 2x   1x             12x 11x 11x 1x 10x   9x 9x 5x 5x 4x 3x 3x       40x 40x 40x 40x 40x     40x    
<template>
  <PageLayout
    :title="t('moderation.title')"
    :description="t('moderation.description')"
    class="moderation-page"
    scrollable
  >
    <!-- No container-fluid: the scrollable layout already applies the page gutter -->
    <div class="moderation-content">
      <!-- Standing: quota progress, reputation, votes -->
      <section
        class="moderation-standing card mb-3"
        data-tour="moderation-standing"
        :aria-label="t('moderation.standing')"
      >
        <div class="card-body">
          <div v-if="quotaMet" class="quota-status text-success">
            <font-awesome-icon icon="fa-solid fa-check" class="me-2" />
            {{ t('moderation.quotaMet') }}
          </div>
          <div v-else-if="quota" class="quota-status">
            {{ quotaMissingLabel }}
          </div>
          <div
            v-if="quota"
            class="progress mt-2"
            role="progressbar"
            :aria-valuenow="Math.min(quota.have, quota.required)"
            aria-valuemin="0"
            :aria-valuemax="quota.required"
            :aria-label="t('moderation.quotaProgress')"
          >
            <div
              class="progress-bar"
              :class="quotaMet ? 'bg-success' : 'bg-primary'"
              :style="{ width: quotaPercent + '%' }"
            />
          </div>
          <p class="text-muted small mt-2 mb-0">{{ t('moderation.quotaHelp') }}</p>
 
          <dl v-if="stats" class="moderation-stats row text-center mt-3 mb-0">
            <div class="col-4">
              <dt class="small text-muted fw-normal">{{ t('moderation.reputationLabel') }}</dt>
              <dd class="fs-4 mb-0 stat-reputation">{{ stats.reputationScore }}</dd>
            </div>
            <div class="col-4">
              <dt class="small text-muted fw-normal">{{ t('moderation.votesCast') }}</dt>
              <dd class="fs-4 mb-0 stat-votes">{{ stats.votesCast }}</dd>
            </div>
            <div class="col-4">
              <dt class="small text-muted fw-normal">{{ t('moderation.agreement') }}</dt>
              <dd class="fs-4 mb-0 stat-agreement">{{ agreementLabel }}</dd>
            </div>
          </dl>
        </div>
      </section>
 
      <!-- Contents to review, one at a time -->
      <section class="moderation-review mb-4" :aria-label="t('moderation.reviewSection')">
        <h2 class="h5 mb-3">{{ t('moderation.reviewSection') }}</h2>
 
        <LoadingSpinner v-if="moderationStore.loadingTasks && !currentTask" />
        <ErrorAlert v-else-if="loadError" :title="t('common.error')" :message="loadError" />
 
        <ModerationReviewCard
          v-else-if="currentTask"
          :task="currentTask"
          :disabled="!!moderationStore.voting"
          @vote="castVote"
        >
          <template #footer>
            <p class="text-muted small mt-2 mb-0">{{ t('moderation.shortcuts') }}</p>
          </template>
        </ModerationReviewCard>
 
        <EmptyState v-else variant="info">{{ t('moderation.empty') }}</EmptyState>
      </section>
 
      <!-- The user's own submissions and what became of them -->
      <section class="moderation-submissions" :aria-label="t('moderation.submissionsSection')">
        <h2 class="h5 mb-3">{{ t('moderation.submissionsSection') }}</h2>
 
        <p
          v-if="!moderationStore.loadingSubmissions && moderationStore.submissions.length === 0"
          class="text-muted"
        >
          {{ t('moderation.noSubmissions') }}
        </p>
 
        <ul v-else class="list-unstyled">
          <li
            v-for="submission in moderationStore.submissions"
            :key="submission.taskId"
            class="submission-row card mb-2"
            :data-status="submission.status"
          >
            <div class="card-body py-2 d-flex gap-3 align-items-start">
              <div class="flex-grow-1">
                <p class="mb-1 submission-text">{{ submission.view.comment.body }}</p>
                <p class="small text-muted mb-0">
                  <template v-if="submission.view.pack">
                    {{ t('store.title') }} · {{ getTranslation(submission.view.pack.name) }}
                  </template>
                  <template v-else-if="submission.view.entity">
                    {{ getTranslation(submission.view.entity.name) }}
                    <template v-if="submission.view.item?.label">
                      · {{ submission.view.item.label }}
                    </template>
                  </template>
                  · {{ formatDateTime(submission.createdAt) }}
                </p>
              </div>
              <span class="badge submission-status" :class="statusClass(submission.status)">
                {{ t(`moderation.status.${submission.status}`) }}
              </span>
            </div>
          </li>
        </ul>
 
        <button
          v-if="moderationStore.submissionsNextToken"
          type="button"
          class="btn btn-outline-secondary btn-sm"
          :disabled="moderationStore.loadingSubmissions"
          @click="loadMoreSubmissions"
        >
          {{ t('common.loadMore') }}
        </button>
      </section>
    </div>
  </PageLayout>
</template>
 
<script setup lang="ts">
// @implements UC-MOD-002.1, UC-MOD-003.1, UC-MOD-004.1, UC-MOD-005.1, UC-MOD-009.1, UC-MOD-010.1
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import PageLayout from '@/components/PageLayout.vue'
import LoadingSpinner from '@/components/LoadingSpinner.vue'
import ErrorAlert from '@/components/ErrorAlert.vue'
import EmptyState from '@/components/EmptyState.vue'
import ModerationReviewCard from '@/components/ModerationReviewCard.vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useDateFormat } from '@/composables/useDateFormat'
import { useLanguage } from '@/composables/useLanguage'
import { useModerationStore, type ModerationStatus, type ModerationVote } from '@/stores/moderation'
import { useToastStore } from '@/stores/toast'
import { getErrorMessage } from '@/utils/errors'
 
const { t } = useUILanguage()
const { formatDateTime } = useDateFormat()
const { getTranslation } = useLanguage()
const moderationStore = useModerationStore()
const toastStore = useToastStore()
 
const loadError = ref('')
 
const currentTask = computed(() => moderationStore.tasks[0] ?? null)
const quota = computed(() => moderationStore.quota)
const stats = computed(() => moderationStore.stats)
const quotaMet = computed(() => !!quota.value && quota.value.have >= quota.value.required)
const quotaPercent = computed(() =>
  quota.value ? Math.min(100, Math.round((quota.value.have / quota.value.required) * 100)) : 0
)
const quotaMissingLabel = computed(() => {
  Iif (!quota.value) return ''
  const missing = quota.value.required - quota.value.have
  return (missing === 1 ? t('moderation.quotaMissingOne') : t('moderation.quotaMissing')).replace(
    '{count}',
    String(missing)
  )
})
const agreementLabel = computed(() =>
  stats.value && stats.value.votesCast > 0
    ? `${Math.round((stats.value.votesAgreed / stats.value.votesCast) * 100)}%`
    : '—'
)
 
function statusClass(status: ModerationStatus): string {
  if (status === 'approved') return 'bg-success'
  if (status === 'rejected') return 'bg-danger'
  return 'bg-secondary'
}
 
async function loadTasks() {
  loadError.value = ''
  try {
    await moderationStore.fetchTasks()
  } catch (err) {
    loadError.value = getErrorMessage(err, t('moderation.loadError'), t)
  }
}
 
async function castVote(choice: ModerationVote) {
  const task = currentTask.value
  if (!task || moderationStore.voting) return
  try {
    const status = await moderationStore.vote(task.taskId, choice)
    // The vote may have completed the consensus: the author's counters and the
    // statuses in "my submissions" can have moved
    if (status !== 'pending') void moderationStore.fetchSubmissions().catch(() => {})
  } catch (err) {
    toastStore.error(getErrorMessage(err, t('moderation.voteError'), t))
  }
  // Top the list up once it runs dry; the server never offers a voted content again
  if (moderationStore.tasks.length === 0) await loadTasks()
}
 
async function loadMoreSubmissions() {
  try {
    await moderationStore.fetchSubmissions(true)
  } catch (err) {
    toastStore.error(getErrorMessage(err, t('moderation.loadError'), t))
  }
}
 
// Keyboard shortcuts: ArrowRight or A approves, ArrowLeft or R rejects — but never
// while the user is typing somewhere or a dialog is open.
function onKeydown(event: KeyboardEvent) {
  if (event.ctrlKey || event.metaKey || event.altKey) return
  const target = event.target as HTMLElement | null
  if (target && (target.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName)))
    return
  if (document.querySelector('[role="dialog"]')) return
 
  const key = event.key.toLowerCase()
  if (key === 'a' || key === 'arrowright') {
    event.preventDefault()
    void castVote('approve')
  } else if (key === 'r' || key === 'arrowleft') {
    event.preventDefault()
    void castVote('reject')
  }
}
 
onMounted(() => {
  window.addEventListener('keydown', onKeydown)
  void loadTasks()
  void moderationStore.fetchStats().catch(() => {})
  void moderationStore.fetchSubmissions().catch(() => {})
})
 
onBeforeUnmount(() => window.removeEventListener('keydown', onKeydown))
</script>