-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathSearchMessagesTab.vue
454 lines (407 loc) · 12.8 KB
/
SearchMessagesTab.vue
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<!--
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<script setup lang="ts">
import debounce from 'debounce'
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import type { Route } from 'vue-router'
import IconCalendarRange from 'vue-material-design-icons/CalendarRange.vue'
import IconFilter from 'vue-material-design-icons/Filter.vue'
import IconMessageOutline from 'vue-material-design-icons/MessageOutline.vue'
import { showError } from '@nextcloud/dialogs'
import { t } from '@nextcloud/l10n'
import NcAvatar from '@nextcloud/vue/dist/Components/NcAvatar.js'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import NcChip from '@nextcloud/vue/dist/Components/NcChip.js'
import NcDateTime from '@nextcloud/vue/dist/Components/NcDateTime.js'
import NcDateTimePickerNative from '@nextcloud/vue/dist/Components/NcDateTimePickerNative.js'
import NcEmptyContent from '@nextcloud/vue/dist/Components/NcEmptyContent.js'
import NcListItem from '@nextcloud/vue/dist/Components/NcListItem.js'
import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js'
import NcSelect from '@nextcloud/vue/dist/Components/NcSelect.js'
import AvatarWrapper from '../../AvatarWrapper/AvatarWrapper.vue'
import SearchBox from '../../UIShared/SearchBox.vue'
import TransitionWrapper from '../../UIShared/TransitionWrapper.vue'
import { useArrowNavigation } from '../../../composables/useArrowNavigation.js'
import { useIsInCall } from '../../../composables/useIsInCall.js'
import { useStore } from '../../../composables/useStore.js'
import { ATTENDEE } from '../../../constants.ts'
import { searchMessages } from '../../../services/coreService.ts'
import { EventBus } from '../../../services/EventBus.ts'
import type {
UnifiedSearchResultEntry,
UserFilterObject,
SearchMessagePayload,
UnifiedSearchResponse,
Participant,
} from '../../../types/index.ts'
import CancelableRequest from '../../../utils/cancelableRequest.js'
const props = defineProps<{
isActive: boolean,
}>()
const emit = defineEmits<{
(event: 'close'): void
}>()
const searchMessagesTab = ref<HTMLElement | null>(null)
const searchBox = ref<InstanceType<typeof SearchBox> | null>(null)
const { initializeNavigation, resetNavigation } = useArrowNavigation(searchMessagesTab, searchBox)
const isFocused = ref(false)
const searchResults = ref<(UnifiedSearchResultEntry &
{
to: {
name: string;
hash: string;
params: {
token: string;
skipLeaveWarning: boolean;
};
}
})[]>([])
const searchText = ref('')
const fromUser = ref<UserFilterObject | null>(null)
const sinceDate = ref<Date | null>(null)
const untilDate = ref<Date | null>(null)
const searchLimit = ref(10)
const searchCursor = ref<number | string | null>(0)
const searchDetailsOpened = ref(false)
const isFetchingResults = ref(false)
const isSearchExhausted = ref(false)
const store = useStore()
const isInCall = useIsInCall()
const token = computed(() => store.getters.getToken())
const participantsInitialised = computed(() => store.getters.participantsInitialised(token.value))
const participants = computed<UserFilterObject>(() => {
return store.getters.participantsList(token.value)
.filter(({ actorType }: Participant) => actorType === ATTENDEE.ACTOR_TYPE.USERS) // FIXME: federated users are not supported by the search provider
.map(({ actorId, displayName, actorType }: { actorId: string; displayName: string; actorType: string}) => ({
id: actorId,
displayName,
isNoUser: actorType !== 'users',
user: actorId,
disableMenu: true,
showUserStatus: false,
}))
})
const canLoadMore = computed(() => !isSearchExhausted.value && !isFetchingResults.value && searchCursor.value !== 0)
const hasFilter = computed(() => fromUser.value || sinceDate.value || untilDate.value)
watch(() => props.isActive, (isActive) => {
if (isActive) {
// NcAppSidebarTabs renders tabs in 2 ticks, so need to wait here
nextTick(() => searchBox.value!.focus())
}
}, { immediate: true })
onMounted(() => {
EventBus.on('route-change', onRouteChange)
})
onBeforeUnmount(() => {
EventBus.off('route-change', onRouteChange)
abortSearch()
})
const onRouteChange = ({ from, to }: { from: Route, to: Route }): void => {
if (to.name !== 'conversation' || from.params.token !== to.params.token || (to.hash && isInCall.value)) {
abortSearch()
emit('close')
}
}
watch(searchText, (value) => {
if (value.trim().length === 0) {
searchResults.value = []
searchCursor.value = 0
isSearchExhausted.value = false
}
})
/**
* Cancel search and cleanup the search fields and results.
*/
function abortSearch() {
cancelSearchFn()
searchText.value = ''
fromUser.value = null
sinceDate.value = null
untilDate.value = null
searchDetailsOpened.value = false
searchResults.value = []
searchCursor.value = 0
}
/**
* Continue fetching more search results
*/
function loadMore() {
return fetchSearchResults(false)
}
/**
*
*/
function fetchNewSearchResult() {
return fetchSearchResults(true)
}
let cancelSearchFn = () => {}
type SearchMessageCancelableRequest = {
request: (payload: SearchMessagePayload) => UnifiedSearchResponse,
cancel: () => void,
}
/**
* @param [isNew=true] Is it a new search (search parameters changed)?
* Fetch the search results from the server
*/
async function fetchSearchResults(isNew = true): Promise<void> {
const term = searchText.value.trim()
// Don't search if the search text is empty
if (term.length === 0) {
return
}
isFetchingResults.value = true
try {
// cancel the previous search request and reset the navigation
cancelSearchFn()
resetNavigation()
const { request, cancel } = CancelableRequest(searchMessages) as SearchMessageCancelableRequest
cancelSearchFn = cancel
if (isNew) {
searchCursor.value = 0
}
if (searchCursor.value === 0) {
searchResults.value = []
}
if (term.length === 0 && !fromUser.value && !sinceDate.value && !untilDate.value) {
return
}
const response = await request({
term,
person: fromUser.value?.id,
since: sinceDate.value?.toISOString(),
until: untilDate.value?.toISOString(),
limit: searchLimit.value,
cursor: searchCursor.value || null,
from: `/call/${token.value}`,
})
const data = response?.data?.ocs?.data
if (data && data.entries.length > 0) {
let entries = data.entries as UnifiedSearchResultEntry[]
isSearchExhausted.value = entries.length < searchLimit.value
searchCursor.value = data.cursor
// FIXME: remove the filter after the person filter is fixed on the server
if (fromUser.value) {
entries = entries.filter((entry) => entry.attributes.actorId === fromUser.value?.id)
if (entries.length === 0 && !isSearchExhausted.value) {
return await loadMore()
}
}
searchResults.value = searchResults.value.concat(entries.map((entry: UnifiedSearchResultEntry) => {
return {
...entry,
to: {
name: 'conversation',
hash: `#message_${entry.attributes.messageId}`,
params: {
token: entry.attributes.conversation,
skipLeaveWarning: true
}
}
}
})
)
nextTick(() => initializeNavigation())
}
} catch (exception) {
if (CancelableRequest.isCancel(exception)) {
return
}
console.error('Error searching for messages', exception)
showError(t('spreed', 'An error occurred while performing the search'))
} finally {
isFetchingResults.value = false
}
}
const debounceFetchSearchResults = debounce(fetchNewSearchResult, 250)
watch([searchText, fromUser, sinceDate, untilDate], debounceFetchSearchResults)
</script>
<template>
<div ref="searchMessagesTab" class="search-messages-tab">
<div class="search-form">
<div class="search-form__main">
<div class="search-form__search-box-wrapper">
<SearchBox ref="searchBox"
:value.sync="searchText"
:placeholder-text="t('spreed', 'Search messages …')"
:is-focused.sync="isFocused" />
<NcButton :pressed.sync="searchDetailsOpened"
:aria-label="t('spreed', 'Search options')"
:title="t('spreed', 'Search options')"
type="tertiary-no-background">
<template #icon>
<IconFilter :size="15" />
</template>
</NcButton>
</div>
<TransitionWrapper name="radial-reveal">
<div v-show="searchDetailsOpened" class="search-form__search-detail">
<NcSelect v-model="fromUser"
class="search-form__search-detail__from-user"
:aria-label-combobox="t('spreed', 'From User')"
:placeholder="t('spreed', 'From User')"
user-select
:loading="!participantsInitialised"
:options="participants" />
<div class="search-form__search-detail__date-picker-wrapper">
<NcDateTimePickerNative id="search-form__search-detail__date-picker--since"
v-model="sinceDate"
class="search-form__search-detail__date-picker"
format="YYYY-MM-DD"
type="date"
:step="1"
:max="new Date()"
:aria-label="t('spreed', 'Since')"
:label="t('spreed', 'Since')" />
<NcDateTimePickerNative id="search-form__search-detail__date-picker--until"
v-model="untilDate"
class="search-form__search-detail__date-picker"
format="YYYY-MM-DD"
type="date"
:max="new Date()"
:aria-label="t('spreed', 'Until')"
:label="t('spreed', 'Until')"
:minute-step="1" />
</div>
</div>
</TransitionWrapper>
<TransitionWrapper name="fade">
<div v-show="hasFilter && !searchDetailsOpened"
class="search-form__search-bubbles">
<NcChip v-if="fromUser"
type="tertiary"
:text="fromUser.displayName"
@close="fromUser = null">
<template #icon>
<NcAvatar :size="24"
:user="fromUser.id"
:display-name="fromUser.displayName"
:show-user-status="false" />
</template>
</NcChip>
<NcChip v-if="sinceDate"
type="tertiary"
:text="t('spreed', 'Since') + ' ' + sinceDate?.toLocaleDateString()"
@close="sinceDate = null">
<template #icon>
<IconCalendarRange :size="15" />
</template>
</NcChip>
<NcChip v-if="untilDate"
type="tertiary"
:text="t('spreed', 'Until') + ' ' + untilDate?.toLocaleDateString()"
@close="untilDate = null">
<template #icon>
<IconCalendarRange :size="15" />
</template>
</NcChip>
</div>
</TransitionWrapper>
</div>
</div>
<div class="search-results">
<template v-if="searchResults.length !== 0">
<NcListItem v-for="item of searchResults"
:key="`message_${item.attributes.messageId}`"
:data-nav-id="`message_${item.attributes.messageId}`"
:name="item.title"
:to="item.to"
:v-tooltip="item.subline">
<template #icon>
<AvatarWrapper :id="item.attributes.actorId"
:name="item.title"
:source="item.attributes.actorType"
:disable-menu="true"
:token="item.attributes.conversation" />
</template>
<template #subname>
{{ item.subline }}
</template>
<template #details>
<NcDateTime :timestamp="parseInt(item.attributes.timestamp) * 1000"
class="search-results__date"
relative-time="narrow"
ignore-seconds />
</template>
</NcListItem>
</template>
<NcEmptyContent v-else-if="!isFetchingResults && searchText.trim().length !== 0"
class="search-results__empty"
:name="t('spreed', 'No results found')">
<template #icon>
<IconMessageOutline :size="64" />
</template>
</NcEmptyContent>
<template v-if="canLoadMore">
<NcButton wide type="tertiary" @click="fetchSearchResults(false)">
{{ t('spreed', 'Load more results') }}
</NcButton>
</template>
<template v-if="isFetchingResults">
<NcLoadingIcon class="search-results__loading" />
</template>
</div>
</div>
</template>
<style lang="scss" scoped>
.search-messages-tab {
display: flex;
flex-direction: column;
height: 100%;
}
.search-form {
display: flex;
flex-direction: column;
padding-bottom: var(--default-grid-baseline);
&__search-box-wrapper {
display: flex;
gap: var(--default-grid-baseline);
}
&__main {
display: flex;
flex-direction: column;
flex: 1;
}
&__search-detail {
display: flex;
flex-direction: column;
width: 100%;
margin-top: calc(var(--default-grid-baseline) * 4);
&__from-user {
margin-top: 8px;
:deep(.vs__dropdown-toggle) {
overflow-y: clip;
}
}
&__date-picker {
width: 100%;
min-width: 100px;
&-wrapper {
display: flex;
gap: var(--default-grid-baseline);
}
}
}
&__search-bubbles {
display: flex;
flex-wrap: wrap;
gap: var(--default-grid-baseline);
margin-top: var(--default-grid-baseline);
}
}
.search-results {
transition: all 0.15s ease;
height: 100%;
overflow-y: auto;
&__date {
font-size: x-small;
}
&__loading {
height: var(--default-clickable-area);
}
&__empty {
height: 100%;
}
}
</style>