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

export default {
  i18n: {
    collapseReplies: s__('Notes|Collapse replies'),
    expandReplies: s__('Notes|Expand replies'),
    lastReplyBy: s__('Notes|Last reply by %{name}'),
  },
  components: {
    GlButton,
    GlLink,
    GlSprintf,
    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, 'username');
    },
    liClasses() {
      return this.collapsed
        ? 'gl-text-gray-500 gl-rounded-bottom-left-base gl-rounded-bottom-right-base'
        : 'gl-border-b';
    },
    buttonIcon() {
      return this.collapsed ? 'chevron-right' : 'chevron-down';
    },
    buttonLabel() {
      return this.collapsed ? this.$options.i18n.expandReplies : this.$options.i18n.collapseReplies;
    },
  },
  methods: {
    toggle() {
      this.$refs.toggle.$el.focus();
      this.$emit('toggle');
    },
  },
};
</script>

<template>
  <li
    :class="liClasses"
    class="toggle-replies-widget gl-display-flex! gl-align-items-center gl-flex-wrap gl-bg-gray-10 gl-py-3 gl-px-5 gl-border"
  >
    <gl-button
      ref="toggle"
      class="gl-my-2 gl-mr-3 gl-p-0!"
      category="tertiary"
      :icon="buttonIcon"
      :aria-label="buttonLabel"
      @click="toggle"
    />
    <template v-if="collapsed">
      <user-avatar-link
        v-for="author in uniqueAuthors"
        :key="author.username"
        class="gl-mr-3 reply-author-avatar"
        :link-href="author.path || author.webUrl"
        :img-alt="author.name"
        img-css-classes="gl-mr-0!"
        :img-src="author.avatar_url || author.avatarUrl"
        :img-size="24"
        :tooltip-text="author.name"
        tooltip-placement="bottom"
      />
      <gl-button
        class="gl-mr-2"
        variant="link"
        data-qa-selector="expand_replies_button"
        @click="toggle"
      >
        {{ n__('%d reply', '%d replies', replies.length) }}
      </gl-button>
      <gl-sprintf :message="$options.i18n.lastReplyBy">
        <template #name>
          <gl-link
            :href="lastReply.author.path || lastReply.author.webUrl"
            class="gl-text-body! gl-text-decoration-none! gl-mx-2"
          >
            {{ lastReply.author.name }}
          </gl-link>
        </template>
      </gl-sprintf>
      <time-ago-tooltip
        :time="lastReply.created_at || lastReply.createdAt"
        tooltip-placement="bottom"
      />
    </template>
    <gl-button
      v-else
      class="gl-text-body! gl-text-decoration-none!"
      variant="link"
      data-qa-selector="collapse_replies_button"
      @click="toggle"
    >
      {{ $options.i18n.collapseReplies }}
    </gl-button>
  </li>
</template>