All files / src/components ModerationThread.vue

98.27% Statements 57/58
98.11% Branches 52/53
100% Functions 12/12
100% Lines 51/51

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  20x                       2x                   8x 8x                 17x               8x 8x           8x 8x                   1x                                       6x                   6x   6x 6x           8x   6x 6x 6x 6x 6x 6x 6x                     6x 6x 9x         6x 6x 8x     6x         6x       9x       10x           7x 7x 7x 7x 7x 7x 7x 6x 7x           8x 6x 7x 7x 5x 5x   6x   1x   7x       6x                                            
<template>
  <div class="moderation-thread" :aria-label="t('moderation.discussion')">
    <div v-if="loading && !loaded" class="text-center text-muted small py-2">
      <span class="spinner-border spinner-border-sm me-2" aria-hidden="true" />
      {{ t('common.loading') }}
    </div>
    <div v-else-if="error" class="alert alert-danger small mb-0" role="alert">{{ error }}</div>
 
    <template v-else-if="loaded">
      <p v-if="ancestorsTruncated" class="thread-earlier small text-muted fst-italic mb-1">
        {{ t('moderation.discussionEarlier') }}
      </p>
      <ol v-if="ancestors.length" class="thread-ancestors list-unstyled mb-1">
        <li
          v-for="(ancestor, index) in ancestors"
          :key="ancestor.id"
          class="thread-comment thread-ancestor"
          :style="{ marginLeft: indent(index) }"
        >
          <span v-if="ancestor.deleted" class="text-muted fst-italic">{{
            t('comment.deleted')
          }}</span>
          <template v-else>
            <span class="thread-author fw-semibold me-1">{{ authorLabel(ancestor) }}</span>
            <span class="thread-body">{{ ancestor.body }}</span>
          </template>
        </li>
      </ol>
 
      <ul
        class="thread-siblings list-unstyled mb-0"
        :style="{ marginLeft: indent(ancestors.length) }"
      >
        <li
          v-for="entry in entries"
          :key="entry.id"
          class="thread-comment"
          :class="{ 'thread-under-review': entry.underReview }"
          :data-under-review="entry.underReview || undefined"
        >
          <template v-if="entry.underReview">
            <span class="badge bg-warning text-dark me-1">{{ t('moderation.underReview') }}</span>
            <span class="thread-body">{{ entry.body }}</span>
          </template>
          <span v-else-if="entry.comment!.deleted" class="text-muted fst-italic">{{
            t('comment.deleted')
          }}</span>
          <template v-else>
            <span class="thread-author fw-semibold me-1">{{ authorLabel(entry.comment!) }}</span>
            <span class="thread-body">{{ entry.comment!.body }}</span>
          </template>
        </li>
      </ul>
 
      <button
        v-if="nextToken"
        type="button"
        class="btn btn-link btn-sm p-0 mt-1"
        :disabled="loading"
        @click="load(true)"
      >
        {{ t('common.loadMore') }}
      </button>
    </template>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-MOD-002.1
// The discussion around a comment under review, read-only: the comments above
// it and the ones next to it, with the comment under review in its place.
// Keeps its own state: the comments store holds the one item (or pack) whose
// panel is open, and this may show another one's discussion inside that very panel.
import { computed, onMounted, ref } from 'vue'
import api from '@/services/api'
import type { Comment } from '@/stores/comments'
import { useUILanguage } from '@/composables/useUILanguage'
import { getErrorMessage } from '@/utils/errors'
 
const props = defineProps<{
  // The thread: an item (entityId and itemId), or a store pack (packId)
  entityId?: string
  itemId?: string
  packId?: string
  // The comment the one under review replies to, or null for a top-level comment
  parentId: string | null
  comment: { id: string; body: string; createdAt: string }
}>()
 
const { t } = useUILanguage()
 
const threadPath = computed(() =>
  props.packId
    ? `/v1/store/${props.packId}`
    : `/v1/entities/${props.entityId}/items/${props.itemId}`
)
 
// Indentation stops growing past a few levels, as in the comments panel
const MAX_INDENT_LEVEL = 4
 
const ancestors = ref<Comment[]>([])
const ancestorsTruncated = ref(false)
const siblings = ref<Comment[]>([])
const nextToken = ref<string | null>(null)
const loading = ref(false)
const loaded = ref(false)
const error = ref('')
 
interface Entry {
  id: string
  underReview: boolean
  body?: string
  comment?: Comment
}
 
// The comment under review slots in among the visible ones by date, in the
// list's own order: top-level comments come newest first, replies oldest first.
const entries = computed<Entry[]>(() => {
  const newestFirst = props.parentId === null
  const list: Entry[] = siblings.value.map((comment) => ({
    id: comment.id,
    underReview: false,
    comment
  }))
  const at = props.comment.createdAt
  const index = list.findIndex((entry) =>
    newestFirst ? entry.comment!.createdAt < at : entry.comment!.createdAt > at
  )
  // Past the loaded page it goes last, and moves into place as pages load
  list.splice(index === -1 ? list.length : index, 0, {
    id: props.comment.id,
    underReview: true,
    body: props.comment.body
  })
  return list
})
 
function indent(level: number): string {
  return `${Math.min(level, MAX_INDENT_LEVEL) * 0.75}rem`
}
 
function authorLabel(comment: Comment): string {
  return comment.authorNickname
    ? `@${comment.authorNickname}`
    : comment.authorName || t('comment.unknownAuthor')
}
 
async function load(more = false) {
  Iif (loading.value) return
  loading.value = true
  error.value = ''
  try {
    const params: Record<string, string> = {}
    if (props.parentId) params.parentId = props.parentId
    if (more && nextToken.value) params.nextToken = nextToken.value
    else if (props.parentId) params.withAncestors = 'true'
    const response = await api.get<{
      comments: Comment[]
      nextToken: string | null
      ancestors?: Comment[]
      ancestorsTruncated?: boolean
    }>(`${threadPath.value}/comments`, { params })
    const page = response.data.comments.filter((comment) => comment.id !== props.comment.id)
    siblings.value = more ? [...siblings.value, ...page] : page
    nextToken.value = response.data.nextToken
    if (!more) {
      ancestors.value = response.data.ancestors ?? []
      ancestorsTruncated.value = response.data.ancestorsTruncated ?? false
    }
    loaded.value = true
  } catch (err) {
    error.value = getErrorMessage(err, t('comment.loadError'), t)
  } finally {
    loading.value = false
  }
}
 
onMounted(() => void load())
</script>
 
<style scoped>
.moderation-thread {
  border-left: 3px solid var(--bw-border, rgba(0, 0, 0, 0.15));
  padding-left: 0.75rem;
  font-size: 0.875rem;
}
 
.thread-comment {
  padding: 0.125rem 0;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
 
.thread-under-review {
  background: var(--bw-surface-muted, rgba(255, 193, 7, 0.12));
  border-radius: 4px;
  padding: 0.25rem 0.375rem;
}
</style>