[IMPROVE] Use PaginatedSelectFiltered in department edition (#23054)
* Fix only first 10 channels getting listed in channels autocomplete * Use paginated select component for displaying channel select data * Fix lint issues Co-authored-by: Tiago Evangelista Pinto <tiago.evangelista@rocket.chat> Co-authored-by: Kevin Aleman <kevin.aleman@rocket.chat>pull/22332/head^2
parent
e72aea440f
commit
995c55831c
@ -0,0 +1,63 @@ |
||||
import { useCallback, useState } from 'react'; |
||||
|
||||
import { IRoom } from '../../../../definition/IRoom'; |
||||
import { useEndpoint } from '../../../contexts/ServerContext'; |
||||
import { useScrollableRecordList } from '../../../hooks/lists/useScrollableRecordList'; |
||||
import { useComponentDidUpdate } from '../../../hooks/useComponentDidUpdate'; |
||||
import { RecordList } from '../../../lib/lists/RecordList'; |
||||
|
||||
type RoomListOptions = { |
||||
text: string; |
||||
}; |
||||
|
||||
export const useRoomsList = ( |
||||
options: RoomListOptions, |
||||
): { |
||||
itemsList: RecordList<IRoom>; |
||||
initialItemCount: number; |
||||
reload: () => void; |
||||
loadMoreItems: (start: number, end: number) => void; |
||||
} => { |
||||
const [itemsList, setItemsList] = useState(() => new RecordList<IRoom>()); |
||||
const reload = useCallback(() => setItemsList(new RecordList<IRoom>()), []); |
||||
const endpoint = 'rooms.autocomplete.channelAndPrivate.withPagination'; |
||||
|
||||
const getRooms = useEndpoint('GET', endpoint); |
||||
|
||||
useComponentDidUpdate(() => { |
||||
options && reload(); |
||||
}, [options, reload]); |
||||
|
||||
const fetchData = useCallback( |
||||
async (start, end) => { |
||||
const { items: rooms, total } = await getRooms({ |
||||
selector: JSON.stringify({ name: options.text || '' }), |
||||
offset: start, |
||||
count: start + end, |
||||
sort: JSON.stringify({ name: 1 }), |
||||
}); |
||||
|
||||
const items = rooms.map((room: any) => { |
||||
room._updatedAt = new Date(room._updatedAt); |
||||
room.label = room.name; |
||||
room.value = room.name; |
||||
return room; |
||||
}); |
||||
|
||||
return { |
||||
items, |
||||
itemCount: total, |
||||
}; |
||||
}, |
||||
[getRooms, options.text], |
||||
); |
||||
|
||||
const { loadMoreItems, initialItemCount } = useScrollableRecordList(itemsList, fetchData, 25); |
||||
|
||||
return { |
||||
reload, |
||||
itemsList, |
||||
loadMoreItems, |
||||
initialItemCount, |
||||
}; |
||||
}; |
@ -0,0 +1,10 @@ |
||||
import { IRoom } from '../../../../../../definition/IRoom'; |
||||
|
||||
export type AutocompleteChannelAndPrivateEndpointWithPagination = { |
||||
GET: (params: { selector: string; offset?: number; count?: number; sort?: string }) => { |
||||
items: IRoom[]; |
||||
count: number; |
||||
offset: number; |
||||
total: number; |
||||
}; |
||||
}; |
Loading…
Reference in new issue