4545 </li >
4646 <li
4747 v-for =" (pg, index) in projectGroups"
48- :key =" pg.id "
48+ :key =" pg.tdei_project_group_id "
4949 :id =" 'pg-item-' + index"
5050 class =" list-group-item list-group-item-action cursor-pointer"
51- :class =" { highlighted: activeIndex === index, 'fw-bold': model === pg.id }"
52- @click =" selectGroup(pg.id )"
51+ :class =" { highlighted: activeIndex === index, 'fw-bold': model === pg.tdei_project_group_id }"
52+ @click =" selectGroup(pg.tdei_project_group_id )"
5353 @mouseenter =" activeIndex = index"
5454 >
5555 {{ pg.name }}
@@ -86,29 +86,34 @@ function persistCachedName(id: string, name: string) {
8686<script setup lang="ts">
8787import { ref , watch , onMounted , onUnmounted , nextTick } from ' vue'
8888import { tdeiUserClient } from ' ~/services/index'
89+ import type { TdeiProjectGroupItem } from ' ~/types/tdei'
8990
90- const props = withDefaults (defineProps <{ disabled? : boolean }>(), {
91+ const props = withDefaults (defineProps <{ disabled? : boolean ; options ? : TdeiProjectGroupItem [] }>(), {
9192 disabled: false ,
9293})
9394
9495const model = defineModel ({ required: true })
9596const searchText = ref (' ' )
9697const isOpen = ref (false )
97- const projectGroups = ref <{ id : string ; name : string } []>([])
98+ const fetchedGroups = ref <TdeiProjectGroupItem []>([])
9899const selectedGroupName = ref (' ' )
99100const loading = ref (false )
100101const totalCount = ref <number | undefined >(undefined )
101102const pickerRef = ref <HTMLElement | null >(null )
102103const listRef = ref <HTMLElement | null >(null )
103104const activeIndex = ref (- 1 )
104105
106+ const projectGroups = computed (() => props .options ?? fetchedGroups .value )
107+
105108let pageNo = 1
106109const hasMore = ref (true )
107110let pendingReset = false
108111const pageSize = 10
109112let hasUnfilteredResults = false
110113
111114const loadGroups = async (reset = false ) => {
115+ if (props .options ) return
116+
112117 if (loading .value ) {
113118 pendingReset = pendingReset || reset
114119 return
@@ -117,7 +122,7 @@ const loadGroups = async (reset = false) => {
117122 if (reset ) {
118123 pageNo = 1
119124 hasMore .value = true
120- projectGroups .value = []
125+ fetchedGroups .value = []
121126 activeIndex .value = - 1
122127 totalCount .value = undefined
123128 }
@@ -136,9 +141,9 @@ const loadGroups = async (reset = false) => {
136141
137142 const { items : newGroups, total } = await tdeiUserClient .getMyProjectGroups (pageNo , query , pageSize )
138143 if (total !== undefined ) totalCount .value = total
139- projectGroups .value .push (... newGroups )
140- const selected = newGroups .find (g => g .id === model .value )
141- if (selected ) persistCachedName (selected .id , selected .name )
144+ fetchedGroups .value .push (... newGroups )
145+ const selected = newGroups .find (g => g .tdei_project_group_id === model .value )
146+ if (selected ) persistCachedName (selected .tdei_project_group_id , selected .name )
142147
143148 if (newGroups .length < pageSize ) {
144149 hasMore .value = false
@@ -178,7 +183,7 @@ const onInput = () => {
178183}
179184
180185watch (model , (newId ) => {
181- const pg = projectGroups .value .find (p => p .id === newId )
186+ const pg = projectGroups .value .find (p => p .tdei_project_group_id === newId )
182187 if (pg && ! isOpen .value ) {
183188 searchText .value = pg .name
184189 selectedGroupName .value = pg .name
@@ -195,11 +200,11 @@ const onScroll = (e: Event) => {
195200const selectGroup = (id : string ) => {
196201 model .value = id
197202 isOpen .value = false
198- const pg = projectGroups .value .find (p => p .id === id )
203+ const pg = projectGroups .value .find (p => p .tdei_project_group_id === id )
199204 if (pg ) {
200205 searchText .value = pg .name
201206 selectedGroupName .value = pg .name
202- persistCachedName (pg .id , pg .name )
207+ persistCachedName (pg .tdei_project_group_id , pg .name )
203208 }
204209}
205210
@@ -257,7 +262,7 @@ const onKeydown = (e: KeyboardEvent) => {
257262 e .preventDefault ()
258263 if (activeIndex .value >= 0 && activeIndex .value < projectGroups .value .length ) {
259264 const pg = projectGroups .value [activeIndex .value ]
260- if (pg ) selectGroup (pg .id )
265+ if (pg ) selectGroup (pg .tdei_project_group_id )
261266 }
262267 } else if (e .key === ' Escape' ) {
263268 e .preventDefault ()
@@ -273,7 +278,7 @@ const applyCachedName = () => {
273278
274279const closeDropdown = () => {
275280 isOpen .value = false
276- const pg = projectGroups .value .find (p => p .id === model .value )
281+ const pg = projectGroups .value .find (p => p .tdei_project_group_id === model .value )
277282 const name = pg ?.name ?? selectedGroupName .value
278283 searchText .value = name
279284 if (pg ) selectedGroupName .value = name
@@ -285,38 +290,58 @@ const onFocusOut = (e: FocusEvent) => {
285290 }
286291}
287292
293+ watch (
294+ projectGroups ,
295+ (groups ) => {
296+ if (groups .length > 0 ) {
297+ const pgId = model .value as string | undefined
298+ if (! pgId || ! groups .some (pg => pg .tdei_project_group_id === pgId )) {
299+ model .value = groups [0 ]?.tdei_project_group_id
300+ }
301+ const selected = groups .find (pg => pg .tdei_project_group_id === model .value )
302+ if (selected && ! isOpen .value ) {
303+ searchText .value = selected .name
304+ selectedGroupName .value = selected .name
305+ }
306+ }
307+ },
308+ { immediate: true },
309+ )
310+
288311onMounted (async () => {
289312 // Show cached name immediately before the API call completes
290313 if (model .value && loadCachedName (model .value as string )) {
291314 applyCachedName ()
292315 }
293316
294- await loadGroups (true )
295-
296- if (projectGroups .value .length > 0 ) {
297- const selected = projectGroups .value .find (pg => pg .id === model .value )
298- if (selected ) {
299- searchText .value = selected .name
300- selectedGroupName .value = selected .name
301- } else if (model .value && loadCachedName (model .value as string )) {
302- // Group is beyond page 1 — use the cached name for display
303- applyCachedName ()
304- } else if (model .value ) {
305- // model is set but name is unknown — paginate until the group is found
306- while (hasMore .value ) {
307- await loadGroups ()
308- const found = projectGroups .value .find (pg => pg .id === model .value )
309- if (found ) {
310- searchText .value = found .name
311- selectedGroupName .value = found .name
312- break
317+ if (! props .options ) {
318+ await loadGroups (true )
319+
320+ if (fetchedGroups .value .length > 0 ) {
321+ const selected = fetchedGroups .value .find (pg => pg .tdei_project_group_id === model .value )
322+ if (selected ) {
323+ searchText .value = selected .name
324+ selectedGroupName .value = selected .name
325+ } else if (model .value && loadCachedName (model .value as string )) {
326+ // Group is beyond page 1 — use the cached name for display
327+ applyCachedName ()
328+ } else if (model .value ) {
329+ // model is set but name is unknown — paginate until the group is found
330+ while (hasMore .value ) {
331+ await loadGroups ()
332+ const found = fetchedGroups .value .find (pg => pg .tdei_project_group_id === model .value )
333+ if (found ) {
334+ searchText .value = found .name
335+ selectedGroupName .value = found .name
336+ break
337+ }
313338 }
339+ } else if (! model .value ) {
340+ const first = fetchedGroups .value [0 ]!
341+ model .value = first .tdei_project_group_id
342+ searchText .value = first .name
343+ selectedGroupName .value = first .name
314344 }
315- } else if (! model .value ) {
316- const first = projectGroups .value [0 ]!
317- model .value = first .id
318- searchText .value = first .name
319- selectedGroupName .value = first .name
320345 }
321346 }
322347})
0 commit comments