Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Convert AutoCompleteAgent to tsx #26704

Merged
merged 6 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { PaginatedSelectFiltered } from '@rocket.chat/fuselage';
import { useDebouncedValue } from '@rocket.chat/fuselage-hooks';
import React, { memo, useMemo, useState } from 'react';
import React, { memo, ReactElement, useMemo, useState } from 'react';

import { useRecordList } from '../hooks/lists/useRecordList';
import { AsyncStatePhase } from '../lib/asyncState';
import { useAgentsList } from './Omnichannel/hooks/useAgentsList';

const AutoCompleteAgent = (props) => {
const { value, onChange = () => {}, haveAll = false, haveNoAgentsSelectedOption = false } = props;
const [agentsFilter, setAgentsFilter] = useState('');
type AutoCompleteAgentProps = {
value: string;
onChange: (value: string) => void;
haveAll?: boolean;
haveNoAgentsSelectedOption?: boolean;
};
const AutoCompleteAgent = ({
value,
onChange,
haveAll = false,
haveNoAgentsSelectedOption = false,
}: AutoCompleteAgentProps): ReactElement => {
const [agentsFilter, setAgentsFilter] = useState<string>('');

const debouncedAgentsFilter = useDebouncedValue(agentsFilter, 500);

Expand All @@ -19,17 +29,13 @@ const AutoCompleteAgent = (props) => {
),
);

const { phase: agentsPhase, items: agentsItems, itemCount: agentsTotal } = useRecordList(AgentsList);
const { phase: agentsPhase, itemCount: agentsTotal, items: agentsItems } = useRecordList(AgentsList);

const sortedByName = agentsItems.sort((a, b) => {
if (['all', 'no-agent-selected'].includes(a.value)) {
return -1;
}

if (a.username > b.username) {
if (a.label > b.label) {
return 1;
}
if (a.username < b.username) {
if (a.label < b.label) {
return -1;
}

Expand All @@ -42,10 +48,12 @@ const AutoCompleteAgent = (props) => {
onChange={onChange}
flexShrink={0}
filter={agentsFilter}
setFilter={setAgentsFilter}
setFilter={setAgentsFilter as (value: string | number | undefined) => void}
juliajforesti marked this conversation as resolved.
Show resolved Hide resolved
options={sortedByName}
data-qa='autocomplete-agent'
endReached={agentsPhase === AsyncStatePhase.LOADING ? () => {} : (start) => loadMoreAgents(start, Math.min(50, agentsTotal))}
endReached={
agentsPhase === AsyncStatePhase.LOADING ? (): void => undefined : (start): void => loadMoreAgents(start, Math.min(50, agentsTotal))
}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ILivechatAgent } from '@rocket.chat/core-typings';
import { useEndpoint, useTranslation } from '@rocket.chat/ui-contexts';
import { useCallback, useState } from 'react';

Expand All @@ -12,17 +11,19 @@ type AgentsListOptions = {
haveNoAgentsSelectedOption: boolean;
};

type AgentOption = { value: string; label: string; _updatedAt: Date; _id: string };

export const useAgentsList = (
options: AgentsListOptions,
): {
itemsList: RecordList<ILivechatAgent>;
itemsList: RecordList<AgentOption>;
initialItemCount: number;
reload: () => void;
loadMoreItems: (start: number, end: number) => void;
} => {
const t = useTranslation();
const [itemsList, setItemsList] = useState(() => new RecordList<ILivechatAgent>());
const reload = useCallback(() => setItemsList(new RecordList<ILivechatAgent>()), []);
const [itemsList, setItemsList] = useState(() => new RecordList<AgentOption>());
const reload = useCallback(() => setItemsList(new RecordList<AgentOption>()), []);

const getAgents = useEndpoint('GET', '/v1/livechat/users/agent');

Expand All @@ -39,25 +40,30 @@ export const useAgentsList = (
sort: `{ "name": 1 }`,
});

const items = agents.map((agent: any) => {
agent._updatedAt = new Date(agent._updatedAt);
agent.label = agent.username;
agent.value = agent._id;
return agent;
const items = agents.map<AgentOption>((agent) => {
const agentOption = {
_updatedAt: new Date(agent._updatedAt),
label: agent.username || agent._id,
value: agent._id,
_id: agent._id,
};
return agentOption;
});

options.haveAll &&
items.unshift({
label: t('All'),
value: 'all',
_updatedAt: new Date(),
_id: 'all',
});

options.haveNoAgentsSelectedOption &&
items.unshift({
label: t('Empty_no_agent_selected'),
value: 'no-agent-selected',
_updatedAt: new Date(),
_id: 'no-agent-selected',
});

return {
Expand Down