All files / src/views InboxPage.vue

100% Statements 50/50
100% Branches 46/46
100% Functions 15/15
100% Lines 48/48

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  12x 1x 1x 1x         1x           1x         3x                                       12x   20x                       7x       1x     1x 1x 1x           1x                       1x 1x                                                                 12x 12x 12x 12x 12x 12x                 12x     12x 20x 12x 15x   12x 7x       15x 15x 3x   12x     1x 1x         4x 3x 3x 4x       2x 1x   1x         1x     12x 12x     12x 1x                                                                              
<template>
  <PageLayout :title="t('messages.title')" class="inbox-page">
    <div class="container-fluid">
      <ul class="nav nav-tabs mb-3">
        <li class="nav-item">
          <button
            type="button"
            class="nav-link"
            :class="{ active: folder === 'inbox' }"
            @click="selectFolder('inbox')"
          >
            <font-awesome-icon icon="fa-solid fa-inbox" class="me-2" />
            {{ t('messages.inbox') }}
          </button>
        </li>
        <li class="nav-item">
          <button
            type="button"
            class="nav-link"
            :class="{ active: folder === 'requests' }"
            @click="selectFolder('requests')"
          >
            <font-awesome-icon icon="fa-solid fa-envelope" class="me-2" />
            {{ t('messages.requests') }}
            <span
              v-if="unreadRequestCount > 0"
              class="badge rounded-pill bg-danger ms-1 requests-badge"
              :aria-label="
                t('messages.unreadRequestsAriaLabel').replace('{count}', String(unreadRequestCount))
              "
            >
              {{ unreadRequestCount }}
            </span>
          </button>
        </li>
      </ul>
 
      <LoadingSpinner v-if="loading && items.length === 0" />
 
      <EmptyState v-else-if="!loading && items.length === 0">
        {{ folder === 'requests' ? t('messages.noRequests') : t('messages.noConversations') }}
      </EmptyState>
    </div>
 
    <VirtualCardGrid
      v-if="items.length > 0"
      :key="folder"
      :items="items"
      :has-more="hasMore"
      :is-loading="loading"
      single-column
      :estimate-size="88"
      @load-more="loadMore"
    >
      <template #card="{ item }">
        <div
          class="conversation-row"
          :class="{ unread: item.unreadCount > 0 }"
          @click="openConversation(item)"
        >
          <UserAvatar :picture-key="item.otherUserProfilePicture" class="conversation-avatar" />
          <div class="conversation-body">
            <div class="d-flex justify-content-between align-items-center">
              <span class="conversation-name">
                {{ item.otherUserName || item.otherUserNickname || item.otherUserId }}
                <span v-if="item.otherUserNickname && item.otherUserName" class="text-muted small">
                  @{{ item.otherUserNickname }}
                </span>
              </span>
              <span class="conversation-time text-muted small">
                <span
                  v-if="folder === 'inbox' && item.status === 'pending'"
                  class="badge text-bg-secondary me-2 request-sent-badge"
                >
                  {{ t('messages.requestSent') }}
                </span>
                <time :datetime="item.lastMessageAt" :title="formatDateTime(item.lastMessageAt)">{{
                  formatRelative(item.lastMessageAt)
                }}</time>
              </span>
            </div>
            <div class="d-flex justify-content-between align-items-center">
              <span class="conversation-preview text-muted small">
                {{ item.lastMessagePreview }}
              </span>
              <span v-if="item.unreadCount > 0" class="badge rounded-pill bg-danger">
                {{ item.unreadCount }}
              </span>
            </div>
          </div>
        </div>
      </template>
    </VirtualCardGrid>
  </PageLayout>
</template>
 
<script setup lang="ts">
// @implements UC-DM-004.2, UC-DM-007.1
import { computed, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { storeToRefs } from 'pinia'
import PageLayout from '@/components/PageLayout.vue'
import VirtualCardGrid from '@/components/VirtualCardGrid.vue'
import LoadingSpinner from '@/components/LoadingSpinner.vue'
import EmptyState from '@/components/EmptyState.vue'
import UserAvatar from '@/components/UserAvatar.vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useDateFormat } from '@/composables/useDateFormat'
import {
  useMessagesStore,
  type ConversationFolder,
  type ConversationSummary
} from '@/stores/messages'
import { useToastStore } from '@/stores/toast'
 
const route = useRoute()
const router = useRouter()
const { t } = useUILanguage()
const { formatRelative, formatDateTime } = useDateFormat()
const messagesStore = useMessagesStore()
const toastStore = useToastStore()
const {
  conversations,
  conversationsLoading,
  conversationsHasMore,
  requests,
  requestsLoading,
  requestsHasMore,
  unreadRequestCount
} = storeToRefs(messagesStore)
 
// Kept in the URL so coming back from a request's thread reopens the Requests tab
const folder = ref<ConversationFolder>(route.query.folder === 'requests' ? 'requests' : 'inbox')
const items = computed(() => (folder.value === 'requests' ? requests.value : conversations.value))
const loading = computed(() =>
  folder.value === 'requests' ? requestsLoading.value : conversationsLoading.value
)
const hasMore = computed(() =>
  folder.value === 'requests' ? requestsHasMore.value : conversationsHasMore.value
)
 
async function loadFolder() {
  try {
    if (folder.value === 'requests') {
      await messagesStore.fetchRequests()
    } else {
      await messagesStore.fetchInbox()
    }
  } catch (error) {
    console.error('Failed to load inbox:', error)
    toastStore.error(t('messages.loadError'))
  }
}
 
function selectFolder(next: ConversationFolder) {
  if (folder.value === next) return
  folder.value = next
  router.replace({ query: next === 'requests' ? { folder: 'requests' } : {} })
  void loadFolder()
}
 
function loadMore() {
  if (folder.value === 'requests') {
    void messagesStore.loadMoreRequests()
  } else {
    void messagesStore.loadMoreInbox()
  }
}
 
function openConversation(conversation: ConversationSummary) {
  router.push({ name: 'conversation', params: { conversationId: conversation.conversationId } })
}
 
onMounted(() => {
  void loadFolder()
  // Refreshes the Requests tab's badge, which the socket otherwise only
  // updates on (re)connect and on a pushed message
  messagesStore.fetchUnreadCount().catch((error) => {
    console.error('Failed to load unread count:', error)
  })
})
</script>
 
<style scoped>
.conversation-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  border-radius: 0.5rem;
  background: var(--bw-surface-muted);
  border: 1px solid var(--bw-border);
  cursor: pointer;
  height: 100%;
  box-sizing: border-box;
}
 
.conversation-row.unread {
  border-color: var(--bs-primary, #0d6efd);
}
 
.conversation-row.unread .conversation-name {
  font-weight: 600;
}
 
.conversation-body {
  flex: 1;
  min-width: 0;
}
 
.conversation-preview {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 80%;
}
</style>