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

Improve custom-transfer-directory performance with many items #700

Merged
merged 6 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions docs/docs/feature-library/custom-transfer-directory.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Enable the feature in the flex-config asset for your environment.
```javascript
"custom_transfer_directory": {
"enabled": true, // globally enable or disable the feature
"max_items": 200, // max number of items to show (search field allows accessing the remaining items)
"worker" : {
"enabled": true, // enable the custom worker tab
"show_only_available_workers": false
Expand Down
1 change: 1 addition & 0 deletions flex-config/ui_attributes.common.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
},
"custom_transfer_directory": {
"enabled": true,
"max_items": 200,
"worker": {
"enabled": true,
"show_only_available_workers": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CustomTransferDirectoryConfig from './types/ServiceConfiguration';

const {
enabled = false,
max_items = 200,
queue: queue_config,
worker: worker_config,
external_directory: external_directory_config,
Expand Down Expand Up @@ -37,6 +38,10 @@ export const isFeatureEnabled = (): boolean => {
return enabled;
};

export const getMaxItems = (): number => {
return max_items;
};

export const isCustomQueueTransferEnabled = (): boolean => {
return isFeatureEnabled() && queueEnabled;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as React from 'react';
import { TaskHelper, ITask, templates } from '@twilio/flex-ui';
import React, { useState } from 'react';
import { TaskHelper, ITask, styled, templates } from '@twilio/flex-ui';
import { Box } from '@twilio-paste/core/box';
import { ButtonGroup } from '@twilio-paste/core/button-group';
import { Button } from '@twilio-paste/core/button';
import { Flex } from '@twilio-paste/core/flex';
import { Tooltip } from '@twilio-paste/core/tooltip';
import { Text } from '@twilio-paste/core/text';
import { AgentIcon } from '@twilio-paste/icons/esm/AgentIcon';
Expand All @@ -23,8 +22,17 @@ export interface DirectoryItemProps {
onTransferClick: (options: any) => void;
}

const DirectoryItemContainer = styled('div')`
padding-inline: 0.5rem;
min-height: 40px;
display: flex;
justify-content: flex-start;
align-items: center;
`;

const DirectoryItem = (props: DirectoryItemProps) => {
const { entry, task, onTransferClick } = props;
const [isHovered, setIsHovered] = useState(false);

const onWarmTransferClick = () => {
onTransferClick({ mode: 'WARM' });
Expand All @@ -36,7 +44,7 @@ const DirectoryItem = (props: DirectoryItemProps) => {

const renderIcon = (): React.JSX.Element => {
if (entry.icon) {
return entry.icon;
return entry.icon();
}

switch (entry.type) {
Expand All @@ -51,7 +59,9 @@ const DirectoryItem = (props: DirectoryItemProps) => {

const renderLabel = (): React.JSX.Element => (
<Box key={`directory-item-label-${entry.type}-${entry.key}`} element="TRANSFER_DIR_COMMON_ROW_LABEL">
{entry.labelComponent || (
{entry.labelComponent ? (
entry.labelComponent()
) : (
<Text as="div" className="Twilio" element="TRANSFER_DIR_COMMON_ROW_NAME">
{entry.label}
</Text>
Expand All @@ -60,16 +70,15 @@ const DirectoryItem = (props: DirectoryItemProps) => {
);

return (
<Flex
element="TRANSFER_DIR_COMMON_HORIZONTAL_ROW_CONTAINER"
vertical={false}
vAlignContent="center"
<DirectoryItemContainer
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
key={`directory-item-container-${entry.type}-${entry.key}`}
>
<Box key={`directory-item-icon-${entry.type}-${entry.key}`} element="TRANSFER_DIR_COMMON_ROW_ICON">
{renderIcon()}
</Box>
{entry.tooltip ? (
{isHovered && entry.tooltip ? (
<Tooltip
key={`directory-item-label-tooltip-${entry.type}-${entry.key}`}
element="TRANSFER_DIR_COMMON_TOOLTIP"
Expand All @@ -81,75 +90,73 @@ const DirectoryItem = (props: DirectoryItemProps) => {
renderLabel()
)}

<ButtonGroup
element="TRANSFER_DIR_COMMON_ROW_BUTTONGROUP"
key={`directory-item-buttongroup-${entry.type}-${entry.key}`}
attached
>
{entry.warm_transfer_enabled ? (
<Tooltip
key={`directory-item-buttons-warm-transfer-tooltip-${entry.type}-${entry.key}`}
element="TRANSFER_DIR_COMMON_TOOLTIP"
text={templates[StringTemplates.WarmTransfer]()}
>
<Button
element="TRANSFER_DIR_COMMON_ROW_BUTTON"
key={`directory-item-warm-transfer-button-${entry.type}-${entry.key}`}
variant="secondary_icon"
size="circle"
onClick={onWarmTransferClick}
{isHovered && (
<ButtonGroup key={`directory-item-buttongroup-${entry.type}-${entry.key}`} attached>
{entry.warm_transfer_enabled ? (
<Tooltip
key={`directory-item-buttons-warm-transfer-tooltip-${entry.type}-${entry.key}`}
element="TRANSFER_DIR_COMMON_TOOLTIP"
text={templates[StringTemplates.WarmTransfer]()}
>
{task && TaskHelper.isChatBasedTask(task) ? (
<ChatIcon
key={`directory-item-warm-transfer-icon-${entry.type}-${entry.key}`}
decorative={false}
title=""
/>
) : (
<CallTransferIcon
key={`directory-item-warm-transfer-icon-${entry.type}-${entry.key}`}
decorative={false}
title=""
/>
)}
</Button>
</Tooltip>
) : (
<div></div>
)}
{entry.cold_transfer_enabled ? (
<Tooltip
key={`directory-item-buttons-cold-transfer-tooltip-${entry.type}-${entry.key}`}
element="TRANSFER_DIR_COMMON_TOOLTIP"
text={templates[StringTemplates.ColdTransfer]()}
>
<Button
element="TRANSFER_DIR_COMMON_ROW_BUTTON"
key={`directory-item-warm-transfer-button-${entry.type}-${entry.key}`}
variant="secondary_icon"
size="circle"
onClick={onColdTransferClick}
<Button
element="TRANSFER_DIR_COMMON_ROW_BUTTON"
key={`directory-item-warm-transfer-button-${entry.type}-${entry.key}`}
variant="secondary_icon"
size="circle"
onClick={onWarmTransferClick}
>
{task && TaskHelper.isChatBasedTask(task) ? (
<ChatIcon
key={`directory-item-warm-transfer-icon-${entry.type}-${entry.key}`}
decorative={false}
title=""
/>
) : (
<CallTransferIcon
key={`directory-item-warm-transfer-icon-${entry.type}-${entry.key}`}
decorative={false}
title=""
/>
)}
</Button>
</Tooltip>
) : (
<div></div>
)}
{entry.cold_transfer_enabled ? (
<Tooltip
key={`directory-item-buttons-cold-transfer-tooltip-${entry.type}-${entry.key}`}
element="TRANSFER_DIR_COMMON_TOOLTIP"
text={templates[StringTemplates.ColdTransfer]()}
>
{task && TaskHelper.isChatBasedTask(task) ? (
<SendIcon
key={`directory-item-cold-transfer-icon-${entry.type}-${entry.key}`}
decorative={false}
title=""
/>
) : (
<CallOutgoingIcon
key={`directory-item-cold-transfer-icon-${entry.type}-${entry.key}`}
decorative={false}
title=""
/>
)}
</Button>
</Tooltip>
) : (
<div></div>
)}
</ButtonGroup>
</Flex>
<Button
element="TRANSFER_DIR_COMMON_ROW_BUTTON"
key={`directory-item-warm-transfer-button-${entry.type}-${entry.key}`}
variant="secondary_icon"
size="circle"
onClick={onColdTransferClick}
>
{task && TaskHelper.isChatBasedTask(task) ? (
<SendIcon
key={`directory-item-cold-transfer-icon-${entry.type}-${entry.key}`}
decorative={false}
title=""
/>
) : (
<CallOutgoingIcon
key={`directory-item-cold-transfer-icon-${entry.type}-${entry.key}`}
decorative={false}
title=""
/>
)}
</Button>
</Tooltip>
) : (
<div></div>
)}
</ButtonGroup>
)}
</DirectoryItemContainer>
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Alert } from '@twilio-paste/core/alert';
import { Button } from '@twilio-paste/core/button';
import { Flex } from '@twilio-paste/core/flex';
import { Spinner } from '@twilio-paste/core/spinner';
import { withTaskContext, ITask, Actions, Template, templates } from '@twilio/flex-ui';
import { LoadingIcon } from '@twilio-paste/icons/esm/LoadingIcon';
import { withTaskContext, ITask, Actions, styled, Template, templates } from '@twilio/flex-ui';
import { useState, useRef, useEffect } from 'react';
import debounce from 'lodash/debounce';

import DirectoryItem from './DirectoryItem';
import SearchBox from './SearchBox';
import { StringTemplates } from '../flex-hooks/strings/CustomTransferDirectory';
import { DirectoryEntry } from '../types/DirectoryEntry';
import { getMaxItems } from '../config';

export interface TransferClickPayload {
mode: 'WARM' | 'COLD';
Expand All @@ -20,8 +23,18 @@ export interface OwnProps {
isLoading: boolean;
noEntriesMessage?: string;
onTransferClick: (entry: DirectoryEntry, transferOptions: TransferClickPayload) => void;
onReloadClick?: () => void;
}

const SearchRow = styled('div')`
display: flex;
align-items: center;
column-gap: 1rem;
padding: 1rem;
padding-top: 1.25rem;
width: 100%;
`;

const DirectoryTab = (props: OwnProps) => {
const [filteredDirectory, setFilteredDirectory] = useState([] as Array<DirectoryEntry>);

Expand Down Expand Up @@ -58,7 +71,14 @@ const DirectoryTab = (props: OwnProps) => {

return (
<Flex key="external-directory-tab-list" vertical wrap={false} grow={1} shrink={1}>
<SearchBox key="key-tab-search-box" onInputChange={onQueueSearchInputChange} inputRef={searchInputRef} />
<SearchRow key="search-row">
<SearchBox key="key-tab-search-box" onInputChange={onQueueSearchInputChange} inputRef={searchInputRef} />
{props.onReloadClick && (
<Button variant="secondary" onClick={props.onReloadClick}>
<LoadingIcon decorative={false} title={templates[StringTemplates.UpdateList]()} />
</Button>
)}
</SearchRow>
<Flex key="external-tab-results" vertical element="TRANSFER_DIR_COMMON_ROWS_CONTAINER">
{props.isLoading && (
<Flex hAlignContent="center">
Expand All @@ -76,7 +96,9 @@ const DirectoryTab = (props: OwnProps) => {
/>
</Alert>
) : (
Array.from(filteredDirectory).map((entry: DirectoryEntry) => {
!props.isLoading &&
Array.from(filteredDirectory).map((entry, index) => {
if (index >= getMaxItems()) return null;
return (
<DirectoryItem
task={props.task}
Expand All @@ -87,6 +109,11 @@ const DirectoryTab = (props: OwnProps) => {
);
})
)}
{filteredDirectory.length > getMaxItems() && (
<Alert variant="neutral">
<Template source={templates[StringTemplates.MoreItems]} />
</Alert>
)}
</Flex>
</Flex>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,30 +136,6 @@ const QueueDirectoryTab = (props: OwnProps) => {
// make sure all queues are loaded
const insightQueues = await getAllSyncMapItems(queueMap.current);

// update the queue item
queueMap.current.on('itemUpdated', (updatedItem) => {
const {
item: { key, data },
} = updatedItem;

const queue = transferQueues.current.find((transferQueue) => transferQueue.sid === key);
if (queue && data) {
mapRealTimeDataToTransferQueueItem(queue, data as IRealTimeQueueData);
}

filterQueues();
});

// if a queue is added trigger a reload
queueMap.current.on('itemAdded', () => {
fetchSDKTaskQueues();
});

// if a queue is removed trigger a reload
queueMap.current.on('itemRemoved', () => {
fetchSDKTaskQueues();
});

setInsightsQueues(insightQueues);
};

Expand Down Expand Up @@ -243,13 +219,22 @@ const QueueDirectoryTab = (props: OwnProps) => {
});
};

// initial render
useEffect(() => {
// fetch the queues from the taskrouter sdk on initial render
const onReloadClick = () => {
setIsLoading(true);
fetchQueues();
};

const fetchQueues = () => {
// fetch the queues from the taskrouter sdk
fetchSDKTaskQueues().catch(logger.error);

// fetch the queues from the insights client on initial render
// fetch the queues from the insights client
fetchInsightsQueueData().catch(logger.error);
};

// initial render
useEffect(() => {
fetchQueues();

return () => {
if (queueMap.current) {
Expand All @@ -274,6 +259,7 @@ const QueueDirectoryTab = (props: OwnProps) => {
entries={filteredQueues}
isLoading={isLoading}
onTransferClick={onTransferQueueClick}
onReloadClick={onReloadClick}
noEntriesMessage={templates[StringTemplates.NoQueuesAvailable]}
/>
);
Expand Down
Loading
Loading