All files / src/components/admin SupportThreadView.vue

95.89% Statements 257/268
77.77% Branches 35/45
75% Functions 12/16
95.89% Lines 257/268

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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 3141x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 16x 16x 16x 16x 16x 16x   16x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x           1x 1x   1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   30x 30x   30x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x   1x     1x   1x   1x 1x   1x 2x 2x 2x 2x 2x   1x 1x 1x 1x   1x       1x 80x 80x 2x 80x 56x 80x 22x 80x   80x   80x   80x 80x   1x 40x 40x 1x 40x 28x 40x 11x 40x   40x   40x   40x 40x   1x 46x 46x 46x   1x 1x   1x 1x 1x 1x 34x       34x 34x 1x 1x 10x 1x 1x    
<template>
  <div data-testid="support-thread-view">
    <!-- Header bar -->
    <div class="flex items-center justify-between mb-4 flex-wrap gap-3">
      <button
        type="button"
        class="text-sm text-brand-700 dark:text-brand-300 hover:underline"
        data-testid="thread-back"
        @click="$emit('back')"
      >
        ← Back to inbox
      </button>
      <div class="flex items-center gap-2">
        <button
          v-if="thread.unread_count > 0"
          type="button"
          class="px-3 py-1.5 text-sm bg-surface-alt hover:bg-line rounded-md text-fg-muted"
          data-testid="thread-mark-all-read"
          @click="$emit('mark-all-read')"
        >
          Mark all read
        </button>
        <div class="relative" v-click-outside="closeDropdown">
          <button
            type="button"
            class="px-3 py-1.5 text-sm border border-line rounded-md text-fg-muted hover:bg-surface-alt flex items-center gap-1"
            data-testid="thread-status-dropdown"
            :aria-expanded="dropdownOpen"
            @click="dropdownOpen = !dropdownOpen"
          >
            Set status
            <svg class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor">
              <path
                fill-rule="evenodd"
                d="M5.23 7.21a.75.75 0 011.06.02L10 11.06l3.71-3.83a.75.75 0 011.08 1.04l-4.25 4.4a.75.75 0 01-1.08 0L5.21 8.27a.75.75 0 01.02-1.06z"
                clip-rule="evenodd"
              />
            </svg>
          </button>
          <ul
            v-if="dropdownOpen"
            class="absolute right-0 mt-1 w-48 bg-card border border-line rounded-md shadow-lg z-10 py-1"
            role="menu"
          >
            <li>
              <button
                type="button"
                class="w-full text-left px-3 py-2 text-sm hover:bg-surface-alt"
                data-testid="thread-status-resolved"
                @click="onPickStatus('resolved')"
              >
                Mark resolved
              </button>
            </li>
            <li>
              <button
                type="button"
                class="w-full text-left px-3 py-2 text-sm hover:bg-surface-alt"
                data-testid="thread-status-spam"
                @click="onPickStatus('spam')"
              >
                Mark spam
              </button>
            </li>
            <li>
              <button
                type="button"
                class="w-full text-left px-3 py-2 text-sm hover:bg-surface-alt"
                data-testid="thread-status-reopen"
                @click="onPickStatus('awaiting_admin')"
              >
                Reopen
              </button>
            </li>
          </ul>
        </div>
      </div>
    </div>
 
    <!-- Thread metadata -->
    <div class="mb-4 pb-4 border-b border-line">
      <div class="flex items-center gap-2 mb-2">
        <span
          class="inline-flex items-center px-2 py-0.5 rounded text-xs font-semibold bg-surface-alt text-fg"
          data-testid="thread-case-number"
        >
          MT-{{ thread.case_number }}
        </span>
        <span
          class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
          :class="statusPillClasses(thread.status)"
          :aria-label="`Status: ${statusLabel(thread.status)}`"
          data-testid="thread-status-pill"
        >
          {{ statusLabel(thread.status) }}
        </span>
      </div>
      <h3 class="text-lg font-semibold text-fg">
        {{ thread.subject || '(no subject)' }}
      </h3>
      <p class="text-sm text-fg-muted">
        {{ thread.participant_name || thread.participant_email }}
        <span v-if="thread.participant_name">
          &lt;{{ thread.participant_email }}&gt;</span
        >
      </p>
    </div>
 
    <!-- Messages -->
    <ul class="space-y-3 mb-6">
      <li
        v-for="msg in thread.messages || []"
        :key="msg.id"
        :data-testid="`message-${msg.id}`"
        :data-direction="msg.direction"
        class="flex"
        :class="msg.direction === 'outbound' ? 'justify-end' : 'justify-start'"
      >
        <div
          class="max-w-[80%] rounded-lg px-4 py-3"
          :class="
            msg.direction === 'outbound'
              ? 'bg-brand-50 border border-brand-200 dark:bg-brand-900/40 dark:border-brand-700'
              : 'bg-surface-alt border border-line'
          "
        >
          <div class="flex items-center gap-2 mb-1 text-xs text-fg-muted">
            <span class="font-medium text-fg-muted">
              {{
                msg.direction === 'outbound'
                  ? msg.from_name || 'Support'
                  : msg.from_name || msg.from_email
              }}
            </span>
            <span>·</span>
            <time :datetime="msg.created_at">{{
              formatTime(msg.created_at)
            }}</time>
          </div>
          <div
            v-if="msg.body_html"
            class="text-sm text-fg prose prose-sm max-w-none"
            data-testid="message-body-html"
            v-html="msg.body_html"
          />
          <div
            v-else
            class="text-sm text-fg whitespace-pre-wrap"
            data-testid="message-body-text"
          >
            {{ msg.body_text }}
          </div>
          <p
            v-if="msg.had_attachments"
            class="mt-2 text-xs italic text-fg-muted"
            data-testid="message-attachments-notice"
          >
            Attachments stripped. Reply via your email client to view them.
          </p>
          <div
            v-if="msg.direction === 'outbound' && emailDeliveryChip(msg)"
            class="mt-2 text-right"
          >
            <span
              class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
              :class="emailDeliveryChip(msg).classes"
              :title="emailDeliveryChip(msg).title"
              :data-testid="`message-delivery-${msg.id}`"
              >{{ emailDeliveryChip(msg).label }}</span
            >
          </div>
        </div>
      </li>
    </ul>
 
    <!-- Reply composer -->
    <form
      class="border-t border-line pt-4"
      data-testid="reply-composer"
      @submit.prevent="onReply"
    >
      <label for="reply-body" class="sr-only">Reply</label>
      <textarea
        id="reply-body"
        v-model="replyBody"
        rows="4"
        placeholder="Write a reply…"
        data-testid="reply-textarea"
        class="w-full px-3 py-2 bg-card text-fg border border-line rounded-md focus:outline-none focus:ring-2 focus:ring-brand-500 text-sm"
      ></textarea>
      <div class="flex justify-end mt-2">
        <button
          type="submit"
          class="px-4 py-2 text-sm bg-brand-600 text-white rounded-md hover:bg-brand-700 disabled:opacity-50 disabled:cursor-not-allowed inline-flex items-center gap-2"
          data-testid="reply-submit"
          :disabled="sending || !replyBody.trim()"
        >
          <svg
            v-if="sending"
            class="animate-spin h-4 w-4"
            xmlns="http://www.w3.org/2000/svg"
            fill="none"
            viewBox="0 0 24 24"
          >
            <circle
              class="opacity-25"
              cx="12"
              cy="12"
              r="10"
              stroke="currentColor"
              stroke-width="4"
            />
            <path
              class="opacity-75"
              fill="currentColor"
              d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"
            />
          </svg>
          {{ sending ? 'Sending…' : 'Send reply' }}
        </button>
      </div>
    </form>
  </div>
</template>
 
<script setup>
import { ref } from 'vue';
import { emailDeliveryChip } from '../../utils/emailDelivery';
 
const props = defineProps({
  thread: { type: Object, required: true },
  sending: { type: Boolean, default: false },
});
 
const emit = defineEmits(['back', 'reply', 'set-status', 'mark-all-read']);
 
const replyBody = ref('');
const dropdownOpen = ref(false);
 
const onReply = () => {
  const body = replyBody.value.trim();
  if (!body) return;
  emit('reply', body);
  replyBody.value = '';
};
 
const onPickStatus = status => {
  dropdownOpen.value = false;
  emit('set-status', status);
};
 
const closeDropdown = () => {
  dropdownOpen.value = false;
};
 
const statusLabel = s => {
  switch (s) {
    case 'new':
      return 'New';
    case 'awaiting_admin':
      return 'Awaiting admin';
    case 'awaiting_user':
      return 'Awaiting user';
    case 'resolved':
      return 'Resolved';
    case 'spam':
      return 'Spam';
    default:
      return s;
  }
};
 
const statusPillClasses = s => {
  switch (s) {
    case 'new':
      return 'bg-red-100 text-red-800';
    case 'awaiting_admin':
      return 'bg-orange-100 text-orange-800';
    case 'awaiting_user':
      return 'bg-blue-100 text-blue-800';
    case 'resolved':
      return 'bg-gray-100 text-gray-700';
    case 'spam':
      return 'bg-gray-100 text-gray-500';
    default:
      return 'bg-gray-100 text-gray-700';
  }
};
 
const formatTime = iso => {
  if (!iso) return '';
  return new Date(iso).toLocaleString();
};
 
// Reference props to keep linter happy (we destructure later in tests).
void props;
 
// Local v-click-outside directive (Vue 3 <script setup> auto-registers
// any `vFoo` const as a directive).
const vClickOutside = {
  mounted(el, binding) {
    el._clickOutsideHandler = event => {
      if (!(el === event.target || el.contains(event.target))) {
        binding.value(event);
      }
    };
    document.addEventListener('click', el._clickOutsideHandler);
  },
  unmounted(el) {
    document.removeEventListener('click', el._clickOutsideHandler);
  },
};
</script>