-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMPROVE] Paginated multiselect for EE tags (#22315)
* [IMPROVE] Paginated multiselect for EE tags * add missing tag interface * rename ILivechatTag interface * Add type to endpoint Co-authored-by: Rafael Ferreira <rafaelblink@gmail.com>
- Loading branch information
Showing
8 changed files
with
127 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
client/contexts/ServerContext/endpoints/v1/livechat/tagsList.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ILivechatTag } from '../../../../../../definition/ILivechatTag'; | ||
import { ObjectFromApi } from '../../../../../../definition/ObjectFromApi'; | ||
|
||
export type LivechatTagsList = { | ||
GET: (params: { | ||
text: string; | ||
offset: number; | ||
count: number; | ||
}) => { | ||
tags: ObjectFromApi<ILivechatTag>[]; | ||
total: number; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface ILivechatTag { | ||
_id: string; | ||
name: string; | ||
description: string; | ||
numDepartments: number; | ||
departments: Array<string>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { IRocketChatRecord } from './IRocketChatRecord'; | ||
|
||
|
||
export interface ILivechatTagRecord extends IRocketChatRecord { | ||
_id: string; | ||
name: string; | ||
description: string; | ||
numDepartments: number; | ||
departments: Array<string>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useCallback, useState } from 'react'; | ||
|
||
import { useEndpoint } from '../../../client/contexts/ServerContext'; | ||
import { useScrollableRecordList } from '../../../client/hooks/lists/useScrollableRecordList'; | ||
import { useComponentDidUpdate } from '../../../client/hooks/useComponentDidUpdate'; | ||
import { RecordList } from '../../../client/lib/lists/RecordList'; | ||
import { ILivechatTagRecord } from '../../../definition/ILivechatTagRecord'; | ||
|
||
type TagsListOptions = { | ||
filter: string; | ||
}; | ||
|
||
export const useTagsList = ( | ||
options: TagsListOptions, | ||
): { | ||
itemsList: RecordList<ILivechatTagRecord>; | ||
initialItemCount: number; | ||
reload: () => void; | ||
loadMoreItems: (start: number, end: number) => void; | ||
} => { | ||
const [itemsList, setItemsList] = useState(() => new RecordList<ILivechatTagRecord>()); | ||
const reload = useCallback(() => setItemsList(new RecordList<ILivechatTagRecord>()), []); | ||
|
||
const getTags = useEndpoint('GET', 'livechat/tags.list'); | ||
|
||
useComponentDidUpdate(() => { | ||
options && reload(); | ||
}, [options, reload]); | ||
|
||
const fetchData = useCallback( | ||
async (start, end) => { | ||
const { tags, total } = await getTags({ | ||
text: options.filter, | ||
offset: start, | ||
count: end + start, | ||
}); | ||
return { | ||
items: tags.map((tag: any) => { | ||
tag._updatedAt = new Date(tag._updatedAt); | ||
tag.label = tag.name; | ||
tag.value = { value: tag._id, label: tag.name }; | ||
return tag; | ||
}), | ||
itemCount: total, | ||
}; | ||
}, | ||
[getTags, options.filter], | ||
); | ||
|
||
const { loadMoreItems, initialItemCount } = useScrollableRecordList(itemsList, fetchData, 25); | ||
|
||
return { | ||
reload, | ||
itemsList, | ||
loadMoreItems, | ||
initialItemCount, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,35 @@ | ||
import { MultiSelect } from '@rocket.chat/fuselage'; | ||
import React, { useMemo } from 'react'; | ||
import { PaginatedMultiSelectFiltered } from '@rocket.chat/fuselage'; | ||
import React, { useMemo, useState } from 'react'; | ||
|
||
import { useEndpointData } from '../../../../client/hooks/useEndpointData'; | ||
import { useRecordList } from '../../../../client/hooks/lists/useRecordList'; | ||
import { AsyncStatePhase } from '../../../../client/hooks/useAsyncState'; | ||
import { useTagsList } from '../../hooks/useTagsList'; | ||
|
||
const CurrentChatTags = ({ value, handler, ...props }) => { | ||
const { value: data } = useEndpointData('livechat/tags.list'); | ||
const options = useMemo( | ||
() => (data && data.tags ? data.tags.map(({ name }) => [name, name]) : []), | ||
[data], | ||
const CurrentChatTags = ({ value, handler }) => { | ||
const [tagsFilter, setTagsFilter] = useState(''); | ||
|
||
const { itemsList: tagsList, loadMoreItems: loadMoreTags } = useTagsList( | ||
useMemo(() => ({ filter: tagsFilter }), [tagsFilter]), | ||
); | ||
|
||
return <MultiSelect options={options} value={value} onChange={handler} flexGrow={1} {...props} />; | ||
const { phase: tagsPhase, items: tagsItems, itemCount: tagsTotal } = useRecordList(tagsList); | ||
|
||
return ( | ||
<PaginatedMultiSelectFiltered | ||
maxWidth={'100%'} | ||
flexGrow={1} | ||
filter={tagsFilter} | ||
setFilter={setTagsFilter} | ||
onChange={handler} | ||
options={tagsItems} | ||
value={value} | ||
endReached={ | ||
tagsPhase === AsyncStatePhase.LOADING | ||
? () => {} | ||
: (start) => loadMoreTags(start, Math.min(50, tagsTotal)) | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
export default CurrentChatTags; |