Welcome to mirror list, hosted at ThFree Co, Russian Federation.

user_popover.vue « user_popover « components « vue_shared « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 768cd005727f0028f2847121907107a767895ead (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
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
<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';
import { USER_POPOVER_DELAY } from './constants';

const MAX_SKELETON_LINES = 4;

export default {
  name: 'UserPopover',
  maxSkeletonLines: MAX_SKELETON_LINES,
  USER_POPOVER_DELAY,
  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',
    },
    show: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  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>
  <!-- Delayed so not every mouseover triggers Popover -->
  <gl-popover
    :css-classes="['gl-max-w-48']"
    :show="show"
    :target="target"
    :delay="$options.USER_POPOVER_DELAY"
    :placement="placement"
    boundary="viewport"
    triggers="hover focus manual"
  >
    <div class="gl-py-3 gl-line-height-normal gl-display-flex" data-testid="user-popover">
      <div class="gl-mr-4 gl-flex-shrink-0">
        <user-avatar-image :img-src="user.avatarUrl" :size="64" css-classes="gl-m-0!" />
      </div>
      <div class="gl-w-full gl-word-break-word gl-display-flex gl-align-items-center">
        <template v-if="userIsLoading">
          <gl-skeleton-loader
            :lines="$options.maxSkeletonLines"
            preserve-aspect-ratio="none"
            equal-width-lines
            :height="52"
          />
        </template>
        <template v-else>
          <div>
            <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 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>
        </template>
      </div>
    </div>
    <div class="gl-mt-2 gl-w-full gl-word-break-word">
      <template v-if="userIsLoading">
        <gl-skeleton-loader
          :lines="$options.maxSkeletonLines"
          preserve-aspect-ratio="none"
          equal-width-lines
          :height="24"
        />
      </template>
      <template v-else>
        <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>
  </gl-popover>
</template>