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
|
<script>
import {
GlPopover,
GlLink,
GlSkeletonLoader,
GlIcon,
GlSafeHtmlDirective,
GlSprintf,
GlButton,
GlAvatarLabeled,
} from '@gitlab/ui';
import { glEmojiTag } from '~/emoji';
import createFlash from '~/flash';
import { followUser, unfollowUser } from '~/rest_api';
import { isUserBusy } from '~/set_status_modal/utils';
import Tracking from '~/tracking';
import {
I18N_ERROR_FOLLOW,
I18N_ERROR_UNFOLLOW,
I18N_USER_BLOCKED,
I18N_USER_BUSY,
I18N_USER_LEARN,
I18N_USER_FOLLOW,
I18N_USER_UNFOLLOW,
USER_POPOVER_DELAY,
} from './constants';
const MAX_SKELETON_LINES = 4;
export default {
name: 'UserPopover',
maxSkeletonLines: MAX_SKELETON_LINES,
I18N_USER_BLOCKED,
I18N_USER_BUSY,
I18N_USER_LEARN,
USER_POPOVER_DELAY,
components: {
GlIcon,
GlLink,
GlPopover,
GlSkeletonLoader,
GlSprintf,
GlButton,
GlAvatarLabeled,
},
directives: {
SafeHtml: GlSafeHtmlDirective,
},
mixins: [Tracking.mixin()],
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 ? I18N_USER_UNFOLLOW : I18N_USER_FOLLOW;
},
toggleFollowButtonVariant() {
return this.user?.isFollowed ? 'default' : 'confirm';
},
hasPronouns() {
return Boolean(this.user?.pronouns?.trim());
},
isBlocked() {
return this.user?.state === 'blocked';
},
isBusy() {
return isUserBusy(this.availabilityStatus);
},
username() {
return `@${this.user?.username}`;
},
},
methods: {
async toggleFollow() {
if (this.user.isFollowed) {
this.unfollow();
} else {
this.follow();
}
},
async follow() {
this.toggleFollowLoading = true;
this.track('click_button', {
label: 'follow_from_user_popover',
});
try {
await followUser(this.user.id);
this.$emit('follow');
} catch (error) {
createFlash({
message: I18N_ERROR_FOLLOW,
error,
captureError: true,
});
} finally {
this.toggleFollowLoading = false;
}
},
async unfollow() {
this.toggleFollowLoading = true;
this.track('click_button', {
label: 'unfollow_from_user_popover',
});
try {
await unfollowUser(this.user.id);
this.$emit('unfollow');
} catch (error) {
createFlash({
message: I18N_ERROR_UNFOLLOW,
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"
data-testid="user-popover"
>
<div class="gl-mb-3">
<div v-if="userIsLoading" class="gl-w-20">
<gl-skeleton-loader :width="160" :height="64">
<rect x="70" y="19" rx="3" ry="3" width="88" height="9" />
<rect x="70" y="36" rx="3" ry="3" width="64" height="8" />
<circle cx="32" cy="32" r="32" />
</gl-skeleton-loader>
</div>
<gl-avatar-labeled
v-else
:size="64"
:src="user.avatarUrl"
:label="user.name"
:sub-label="username"
>
<template v-if="isBlocked">
<span class="gl-mt-4 gl-font-style-italic">{{ $options.I18N_USER_BLOCKED }}</span>
</template>
<template v-else>
<gl-button
v-if="shouldRenderToggleFollowButton"
class="gl-mt-3 gl-align-self-start"
:variant="toggleFollowButtonVariant"
:loading="toggleFollowLoading"
size="small"
data-testid="toggle-follow-button"
@click="toggleFollow"
>{{ toggleFollowButtonText }}</gl-button
>
</template>
<template #meta>
<span
v-if="hasPronouns"
class="gl-text-gray-500 gl-font-sm gl-font-weight-normal gl-p-1"
data-testid="user-popover-pronouns"
>({{ user.pronouns }})</span
>
<span v-if="isBusy" class="gl-text-gray-500 gl-font-sm gl-font-weight-normal gl-p-1"
>({{ $options.I18N_USER_BUSY }})</span
>
</template>
</gl-avatar-labeled>
</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>
<template v-if="!isBlocked">
<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="$options.I18N_USER_LEARN">
<template #name>{{ user.name }}</template>
</gl-sprintf>
</gl-link>
</div>
</template>
</template>
</div>
</gl-popover>
</template>
|