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
|
<script>
import { GlCollapsibleListbox, GlTooltip, GlButton } from '@gitlab/ui';
import fuzzaldrinPlus from 'fuzzaldrin-plus';
import { getDerivedMergeRequestInformation } from '~/diffs/utils/merge_request';
import { InternalEvents } from '~/tracking';
import savedRepliesQuery from './saved_replies.query.graphql';
import {
TRACKING_SAVED_REPLIES_USE,
TRACKING_SAVED_REPLIES_USE_IN_MR,
TRACKING_SAVED_REPLIES_USE_IN_OTHER,
} from './constants';
export default {
apollo: {
savedReplies: {
query: savedRepliesQuery,
update: (r) => r.currentUser?.savedReplies?.nodes,
skip() {
return !this.shouldFetchCommentTemplates;
},
},
},
components: {
GlCollapsibleListbox,
GlButton,
GlTooltip,
},
mixins: [InternalEvents.mixin()],
props: {
newCommentTemplatePath: {
type: String,
required: true,
},
},
data() {
return {
shouldFetchCommentTemplates: false,
savedReplies: [],
commentTemplateSearch: '',
loadingSavedReplies: false,
};
},
computed: {
filteredSavedReplies() {
const savedReplies = this.commentTemplateSearch
? fuzzaldrinPlus.filter(this.savedReplies, this.commentTemplateSearch, { key: ['name'] })
: this.savedReplies;
return savedReplies.map((r) => ({ value: r.id, text: r.name, content: r.content }));
},
},
mounted() {
this.tooltipTarget = this.$el.querySelector('.js-comment-template-toggle');
},
methods: {
fetchCommentTemplates() {
this.shouldFetchCommentTemplates = true;
},
setCommentTemplateSearch(search) {
this.commentTemplateSearch = search;
},
onSelect(id) {
const isInMr = Boolean(getDerivedMergeRequestInformation({ endpoint: window.location }).id);
const savedReply = this.savedReplies.find((r) => r.id === id);
if (savedReply) {
this.$emit('select', savedReply.content);
this.trackEvent(TRACKING_SAVED_REPLIES_USE);
this.trackEvent(
isInMr ? TRACKING_SAVED_REPLIES_USE_IN_MR : TRACKING_SAVED_REPLIES_USE_IN_OTHER,
);
}
},
},
};
</script>
<template>
<span>
<gl-collapsible-listbox
:header-text="__('Insert comment template')"
:items="filteredSavedReplies"
:toggle-text="__('Insert comment template')"
text-sr-only
no-caret
toggle-class="js-comment-template-toggle"
icon="comment-lines"
category="tertiary"
placement="right"
searchable
size="small"
class="comment-template-dropdown gl-mr-2"
positioning-strategy="fixed"
:searching="$apollo.queries.savedReplies.loading"
@shown="fetchCommentTemplates"
@search="setCommentTemplateSearch"
@select="onSelect"
>
<template #list-item="{ item }">
<div class="gl-display-flex js-comment-template-content">
<div class="gl-text-truncate">
<strong>{{ item.text }}</strong
><span class="gl-ml-2">{{ item.content }}</span>
</div>
</div>
</template>
<template #footer>
<div
class="gl-border-t-solid gl-border-t-1 gl-border-t-gray-200 gl-display-flex gl-justify-content-center gl-p-2"
>
<gl-button
:href="newCommentTemplatePath"
category="tertiary"
block
class="gl-justify-content-start! gl-mt-0! gl-mb-0! gl-px-3!"
>{{ __('Add a new comment template') }}</gl-button
>
</div>
</template>
</gl-collapsible-listbox>
<gl-tooltip :target="() => tooltipTarget">
{{ __('Insert comment template') }}
</gl-tooltip>
</span>
</template>
<style>
.comment-template-dropdown .gl-new-dropdown-panel {
width: 350px;
}
.comment-template-dropdown .gl-new-dropdown-item-check-icon {
display: none;
}
.comment-template-dropdown input {
border-radius: 0;
}
</style>
|