-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProjectGroupPicker.vue
More file actions
411 lines (375 loc) · 11.4 KB
/
Copy pathProjectGroupPicker.vue
File metadata and controls
411 lines (375 loc) · 11.4 KB
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
<template>
<div class="position-relative project-group-picker" ref="pickerRef" @focusout="onFocusOut">
<input
v-model="searchText"
:id="props.id"
type="text"
class="form-select"
:disabled="props.disabled"
placeholder="Search project groups..."
@focus="onFocus"
@click="onInputClick"
@input="onInput"
@keydown="onKeydown"
/>
<div
v-if="isOpen"
class="pg-dropdown position-absolute w-100 mt-1"
@mousedown.prevent
>
<div class="pg-header">
<span v-if="projectGroups.length > 0" class="pg-count">
<template v-if="totalCount !== undefined">
Showing first {{ projectGroups.length }} of {{ totalCount }} project groups
<span v-if="hasMore && !loading" class="pg-scroll-hint">· Scroll to continue loading</span>
</template>
<template v-else-if="!showScrollHint">Showing all {{ projectGroups.length }} project group{{ projectGroups.length !== 1 ? 's' : '' }}</template>
<template v-else>
Showing first {{ projectGroups.length }} results
<span v-if="!loading" class="pg-scroll-hint">· Scroll to continue loading</span>
</template>
</span>
<span v-if="loading" class="spinner-border spinner-border-sm ms-auto" role="status" aria-hidden="true"></span>
</div>
<div
class="pg-list-wrap"
:class="{ 'pg-has-more': showScrollHint && !loading }"
ref="listRef"
@scroll="onScroll"
>
<ul class="list-group list-group-flush">
<li
v-if="projectGroups.length === 0 && !loading"
class="list-group-item text-muted"
>
No project groups found.
</li>
<li
v-for="(pg, index) in projectGroups"
:key="pg.tdei_project_group_id"
:id="'pg-item-' + index"
class="list-group-item list-group-item-action cursor-pointer"
:class="{ highlighted: activeIndex === index, 'fw-bold': model === pg.tdei_project_group_id }"
@click="selectGroup(pg.tdei_project_group_id)"
@mouseenter="activeIndex = index"
>
{{ pg.name }}
</li>
</ul>
</div>
</div>
</div>
</template>
<script lang="ts">
const STORAGE_KEY_PROJECT_GROUP = 'tdei-selected-project-group'
function loadCachedName(id: string): string | undefined {
if (typeof window === 'undefined') return undefined
try {
const raw = sessionStorage.getItem(STORAGE_KEY_PROJECT_GROUP)
if (!raw) return undefined
const stored = JSON.parse(raw) as { id: string; name: string }
return stored.id === id ? stored.name : undefined
} catch {
return undefined
}
}
function persistCachedName(id: string, name: string) {
if (typeof window === 'undefined') return
try {
sessionStorage.setItem(STORAGE_KEY_PROJECT_GROUP, JSON.stringify({ id, name }))
} catch { /* silently fail */ }
}
</script>
<script setup lang="ts">
import { ref, watch, onMounted, onUnmounted, nextTick } from 'vue'
import { tdeiUserClient } from '~/services/index'
import type { TdeiProjectGroupItem } from '~/types/tdei'
const props = withDefaults(defineProps<{ id?: string; disabled?: boolean; options?: TdeiProjectGroupItem[]; rememberSelection?: boolean }>(), {
disabled: false,
rememberSelection: false,
})
const model = defineModel({ required: true })
const searchText = ref('')
const isOpen = ref(false)
const fetchedGroups = ref<TdeiProjectGroupItem[]>([])
const selectedGroupName = ref('')
const loading = ref(false)
const totalCount = ref<number | undefined>(undefined)
const pickerRef = ref<HTMLElement | null>(null)
const listRef = ref<HTMLElement | null>(null)
const activeIndex = ref(-1)
const projectGroups = computed(() => props.options ?? fetchedGroups.value)
const showScrollHint = computed(() => !props.options && hasMore.value)
let pageNo = 1
const hasMore = ref(true)
let pendingReset = false
const pageSize = 10
let hasUnfilteredResults = false
const loadGroups = async (reset = false) => {
if (props.options) return
if (loading.value) {
pendingReset = pendingReset || reset
return
}
if (reset) {
pageNo = 1
hasMore.value = true
fetchedGroups.value = []
activeIndex.value = -1
totalCount.value = undefined
}
if (!hasMore.value) return
loading.value = true
try {
let query = searchText.value
// If the text is exactly the selected group's name, fetch all options instead of filtering
if (query === selectedGroupName.value) {
query = ''
}
if (reset) {
hasUnfilteredResults = query === ''
}
const { items: newGroups, total } = await tdeiUserClient.getMyProjectGroups(pageNo, query, pageSize)
if (total !== undefined) totalCount.value = total
fetchedGroups.value.push(...newGroups)
const selected = newGroups.find(g => g.tdei_project_group_id === model.value)
if (selected && props.rememberSelection) {
persistCachedName(selected.tdei_project_group_id, selected.name)
}
if (newGroups.length < pageSize) {
hasMore.value = false
} else {
pageNo++
}
} catch (e) {
console.error(e)
} finally {
loading.value = false
if (pendingReset) {
const resetNext = pendingReset
pendingReset = false
await loadGroups(resetNext)
}
}
}
let timeoutId: ReturnType<typeof setTimeout>
const onInputClick = () => {
if (!isOpen.value) {
isOpen.value = true
if (!hasUnfilteredResults || projectGroups.value.length === 0) {
loadGroups(true)
}
}
}
const onInput = () => {
isOpen.value = true
clearTimeout(timeoutId)
timeoutId = setTimeout(() => {
loadGroups(true)
}, 300)
}
watch(model, (newId) => {
const pg = projectGroups.value.find(p => p.tdei_project_group_id === newId)
if (pg && !isOpen.value) {
searchText.value = pg.name
selectedGroupName.value = pg.name
}
})
const onScroll = (e: Event) => {
const target = e.target as HTMLElement
if (target.scrollTop + target.clientHeight >= target.scrollHeight * 0.8) {
loadGroups()
}
}
const selectGroup = (id: string) => {
model.value = id
isOpen.value = false
const pg = projectGroups.value.find(p => p.tdei_project_group_id === id)
if (pg) {
searchText.value = pg.name
selectedGroupName.value = pg.name
if (props.rememberSelection) {
persistCachedName(pg.tdei_project_group_id, pg.name)
}
}
}
const onFocus = (e: Event) => {
isOpen.value = true
if (!hasUnfilteredResults || projectGroups.value.length === 0) {
loadGroups(true)
}
const target = e.target as HTMLInputElement
if (target) {
target.select()
}
}
const scrollToActive = () => {
nextTick(() => {
if (!listRef.value || activeIndex.value < 0) return
const activeEl = listRef.value.querySelector(`#pg-item-${activeIndex.value}`) as HTMLElement
if (activeEl) {
const list = listRef.value
const top = activeEl.offsetTop
const bottom = top + activeEl.offsetHeight
if (top < list.scrollTop) {
list.scrollTop = top
} else if (bottom > list.scrollTop + list.clientHeight) {
list.scrollTop = bottom - list.clientHeight
}
}
})
}
const onKeydown = (e: KeyboardEvent) => {
if (!isOpen.value) {
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
onFocus(e)
e.preventDefault()
}
return
}
if (e.key === 'ArrowDown') {
e.preventDefault()
if (activeIndex.value < projectGroups.value.length - 1) {
activeIndex.value++
scrollToActive()
}
} else if (e.key === 'ArrowUp') {
e.preventDefault()
if (activeIndex.value > 0) {
activeIndex.value--
scrollToActive()
}
} else if (e.key === 'Enter') {
e.preventDefault()
if (activeIndex.value >= 0 && activeIndex.value < projectGroups.value.length) {
const pg = projectGroups.value[activeIndex.value]
if (pg) selectGroup(pg.tdei_project_group_id)
}
} else if (e.key === 'Escape') {
e.preventDefault()
closeDropdown()
}
}
const applyCachedName = () => {
const cached = loadCachedName(model.value as string) ?? ''
searchText.value = cached
selectedGroupName.value = cached
}
const closeDropdown = () => {
isOpen.value = false
const pg = projectGroups.value.find(p => p.tdei_project_group_id === model.value)
const name = pg?.name ?? selectedGroupName.value
searchText.value = name
if (pg) selectedGroupName.value = name
}
const onFocusOut = (e: FocusEvent) => {
if (!pickerRef.value?.contains(e.relatedTarget as Node)) {
if (isOpen.value) closeDropdown()
}
}
watch(
projectGroups,
(groups) => {
if (groups.length > 0) {
const pgId = model.value as string | undefined
if (!pgId || (props.options && !groups.some(pg => pg.tdei_project_group_id === pgId))) {
model.value = groups[0]?.tdei_project_group_id
}
const selected = groups.find(pg => pg.tdei_project_group_id === model.value)
if (selected && !isOpen.value) {
searchText.value = selected.name
selectedGroupName.value = selected.name
}
}
},
{ immediate: true },
)
onMounted(async () => {
// Show cached name immediately before the API call completes
if (props.rememberSelection && model.value && loadCachedName(model.value as string)) {
applyCachedName()
}
if (!props.options) {
await loadGroups(true)
if (fetchedGroups.value.length > 0) {
const selected = fetchedGroups.value.find(pg => pg.tdei_project_group_id === model.value)
if (selected) {
searchText.value = selected.name
selectedGroupName.value = selected.name
} else if (props.rememberSelection && model.value && loadCachedName(model.value as string)) {
// Group is beyond page 1 — use the cached name for display
applyCachedName()
} else if (model.value) {
// model is set but name is unknown — paginate until the group is found
while (hasMore.value) {
await loadGroups()
const found = fetchedGroups.value.find(pg => pg.tdei_project_group_id === model.value)
if (found) {
searchText.value = found.name
selectedGroupName.value = found.name
break
}
}
} else if (!model.value) {
const first = fetchedGroups.value[0]!
model.value = first.tdei_project_group_id
searchText.value = first.name
selectedGroupName.value = first.name
}
}
}
})
onUnmounted(() => {
clearTimeout(timeoutId)
})
</script>
<style lang="scss" scoped>
@import "assets/scss/theme.scss";
.cursor-pointer {
cursor: pointer;
}
.pg-dropdown {
background: #fff;
border: 1px solid rgba(0, 0, 0, 0.15);
border-radius: 0.375rem;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
overflow: hidden;
z-index: 1000;
}
.pg-header {
display: flex;
align-items: center;
gap: 8px;
padding: 5px 12px;
border-bottom: 1px solid $gray-200;
background: $gray-100;
min-height: 30px;
}
.pg-count {
font-size: 0.74rem;
color: $gray-700;
flex: 1;
}
.pg-scroll-hint {
color: $primary;
}
.pg-list-wrap {
position: relative;
max-height: 220px;
overflow-y: auto;
}
.pg-list-wrap.pg-has-more::after {
content: '';
display: block;
position: sticky;
bottom: 0;
height: 44px;
margin-top: -44px;
background: linear-gradient(to bottom, transparent, rgba(255, 255, 255, 0.95));
pointer-events: none;
}
.list-group-item.highlighted {
background-color: rgba(13, 110, 253, 0.15);
color: inherit;
}
</style>