summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/user_popover/user_popover.vue
blob: ec7a7cd72aec8be78c1158510ea87ff3283214f6 (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
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
<script>
import {
  GlPopover,
  GlLink,
  GlSkeletonLoader,
  GlIcon,
  GlSafeHtmlDirective,
  GlSprintf,
  GlButton,
} from '@gitlab/ui';
import { __ } from '~/locale';
import UserNameWithStatus from '~/sidebar/components/assignees/user_name_with_status.vue';
import { glEmojiTag } from '~/emoji';
import createFlash from '~/flash';
import { followUser, unfollowUser } from '~/rest_api';
import UserAvatarImage from '../user_avatar/user_avatar_image.vue';

const MAX_SKELETON_LINES = 4;

export default {
  name: 'UserPopover',
  maxSkeletonLines: MAX_SKELETON_LINES,
  components: {
    GlIcon,
    GlLink,
    GlPopover,
    GlSkeletonLoader,
    UserAvatarImage,
    UserNameWithStatus,
    GlSprintf,
    GlButton,
  },
  directives: {
    SafeHtml: GlSafeHtmlDirective,
  },
  props: {
    target: {
      type: HTMLElement,
      required: true,
    },
    user: {
      type: Object,
      required: true,
      default: null,
    },
    placement: {
      type: String,
      required: false,
      default: 'top',
    },
  },
  data() {
    return {
      toggleFollowLoading: false,
    };
  },
  computed: {
    statusHtml() {
      if (!this.user.status) {
        return '';
      }

      if (this.user.status.emoji && this.user.status.message_html) {
        return `${glEmojiTag(this.user.status.emoji)} ${this.user.status.message_html}`;
      } else if (this.user.status.message_html) {
        return this.user.status.message_html;
      }

      return '';
    },
    userIsLoading() {
      return !this.user?.loaded;
    },
    availabilityStatus() {
      return this.user?.status?.availability || '';
    },
    isNotCurrentUser() {
      return !this.userIsLoading && this.user.username !== gon.current_username;
    },
    shouldRenderToggleFollowButton() {
      return this.isNotCurrentUser && typeof this.user?.isFollowed !== 'undefined';
    },
    toggleFollowButtonText() {
      if (this.toggleFollowLoading) return null;

      return this.user?.isFollowed ? __('Unfollow') : __('Follow');
    },
    toggleFollowButtonVariant() {
      return this.user?.isFollowed ? 'default' : 'confirm';
    },
  },
  methods: {
    async toggleFollow() {
      if (this.user.isFollowed) {
        this.unfollow();
      } else {
        this.follow();
      }
    },
    async follow() {
      this.toggleFollowLoading = true;
      try {
        await followUser(this.user.id);
        this.$emit('follow');
      } catch (error) {
        createFlash({
          message: __('An error occurred while trying to follow this user, please try again.'),
          error,
          captureError: true,
        });
      } finally {
        this.toggleFollowLoading = false;
      }
    },
    async unfollow() {
      this.toggleFollowLoading = true;
      try {
        await unfollowUser(this.user.id);
        this.$emit('unfollow');
      } catch (error) {
        createFlash({
          message: __('An error occurred while trying to unfollow this user, please try again.'),
          error,
          captureError: true,
        });
      } finally {
        this.toggleFollowLoading = false;
      }
    },
  },
  safeHtmlConfig: { ADD_TAGS: ['gl-emoji'] },
};
</script>

<template>
  <!-- 200ms delay so not every mouseover triggers Popover -->
  <gl-popover :target="target" :delay="200" :placement="placement" boundary="viewport">
    <div class="gl-p-3 gl-line-height-normal gl-display-flex" data-testid="user-popover">
      <div
        class="gl-p-2 flex-shrink-1 gl-display-flex gl-flex-direction-column align-items-center gl-w-70p"
      >
        <user-avatar-image :img-src="user.avatarUrl" :size="64" css-classes="gl-m-0!" />
        <div v-if="shouldRenderToggleFollowButton" class="gl-mt-3">
          <gl-button
            :variant="toggleFollowButtonVariant"
            :loading="toggleFollowLoading"
            size="small"
            data-testid="toggle-follow-button"
            @click="toggleFollow"
            >{{ toggleFollowButtonText }}</gl-button
          >
        </div>
      </div>
      <div class="gl-w-full gl-min-w-0 gl-word-break-word">
        <template v-if="userIsLoading">
          <gl-skeleton-loader
            :lines="$options.maxSkeletonLines"
            preserve-aspect-ratio="none"
            equal-width-lines
            :height="52"
          />
        </template>
        <template v-else>
          <div class="gl-mb-3">
            <h5 class="gl-m-0">
              <user-name-with-status
                :name="user.name"
                :availability="availabilityStatus"
                :pronouns="user.pronouns"
              />
            </h5>
            <span class="gl-text-gray-500">@{{ user.username }}</span>
          </div>
          <div class="gl-text-gray-500">
            <div v-if="user.bio" class="gl-display-flex gl-mb-2">
              <gl-icon name="profile" class="gl-flex-shrink-0" />
              <span ref="bio" class="gl-ml-2">{{ user.bio }}</span>
            </div>
            <div v-if="user.workInformation" class="gl-display-flex gl-mb-2">
              <gl-icon name="work" class="gl-flex-shrink-0" />
              <span ref="workInformation" class="gl-ml-2">{{ user.workInformation }}</span>
            </div>
            <div v-if="user.location" class="gl-display-flex gl-mb-2">
              <gl-icon name="location" class="gl-flex-shrink-0" />
              <span class="gl-ml-2">{{ user.location }}</span>
            </div>
            <div
              v-if="user.localTime && !user.bot"
              class="gl-display-flex gl-mb-2"
              data-testid="user-popover-local-time"
            >
              <gl-icon name="clock" class="gl-flex-shrink-0" />
              <span class="gl-ml-2">{{ user.localTime }}</span>
            </div>
          </div>
          <div v-if="statusHtml" class="gl-mb-2" data-testid="user-popover-status">
            <span v-safe-html:[$options.safeHtmlConfig]="statusHtml"></span>
          </div>
          <div v-if="user.bot && user.websiteUrl" class="gl-text-blue-500">
            <gl-icon name="question" />
            <gl-link data-testid="user-popover-bot-docs-link" :href="user.websiteUrl">
              <gl-sprintf :message="__('Learn more about %{username}')">
                <template #username>{{ user.name }}</template>
              </gl-sprintf>
            </gl-link>
          </div>
        </template>
      </div>
    </div>
  </gl-popover>
</template>