summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes/components/toggle_replies_widget.vue
blob: 65b3fd6f8b3a5b48053d2309454b11e1a037ad2f (plain)
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
<script>
import { GlButton, GlIcon } from '@gitlab/ui';
import { uniqBy } from 'lodash';
import TimeAgoTooltip from '~/vue_shared/components/time_ago_tooltip.vue';
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';

export default {
  components: {
    GlButton,
    GlIcon,
    UserAvatarLink,
    TimeAgoTooltip,
  },
  props: {
    collapsed: {
      type: Boolean,
      required: true,
    },
    replies: {
      type: Array,
      required: true,
    },
  },
  computed: {
    lastReply() {
      return this.replies[this.replies.length - 1];
    },
    uniqueAuthors() {
      const authors = this.replies.map((reply) => reply.author || {});

      return uniqBy(authors, (author) => author.username);
    },
    className() {
      return this.collapsed ? 'collapsed' : 'expanded';
    },
  },
  methods: {
    toggle() {
      this.$emit('toggle');
    },
  },
  ICON_CLASS: 'gl-mr-3 gl-cursor-pointer',
};
</script>

<template>
  <li
    :class="className"
    class="replies-toggle js-toggle-replies gl-display-flex! gl-align-items-center gl-flex-wrap"
  >
    <template v-if="collapsed">
      <gl-icon :class="$options.ICON_CLASS" name="chevron-right" @click.native="toggle" />
      <div>
        <user-avatar-link
          v-for="author in uniqueAuthors"
          :key="author.username"
          :link-href="author.path"
          :img-alt="author.name"
          :img-src="author.avatar_url"
          :img-size="24"
          :tooltip-text="author.name"
          tooltip-placement="bottom"
        />
      </div>
      <gl-button
        class="js-replies-text gl-mr-2"
        category="tertiary"
        variant="link"
        data-qa-selector="expand_replies_button"
        @click="toggle"
      >
        {{ replies.length }} {{ n__('reply', 'replies', replies.length) }}
      </gl-button>
      {{ __('Last reply by') }}
      <a :href="lastReply.author.path" class="btn btn-link author-link gl-mx-2">
        {{ lastReply.author.name }}
      </a>
      <time-ago-tooltip :time="lastReply.created_at" tooltip-placement="bottom" />
    </template>
    <div
      v-else
      class="collapse-replies-btn js-collapse-replies gl-display-flex align-items-center"
      data-qa-selector="collapse_replies_button"
      @click="toggle"
    >
      <gl-icon :class="$options.ICON_CLASS" name="chevron-down" />
      <span class="gl-cursor-pointer">{{ s__('Notes|Collapse replies') }}</span>
    </div>
  </li>
</template>