Skip to content

Commit

Permalink
[Feature] Add sort capabilities to ChatFlow and AgentFlow tables (Flo…
Browse files Browse the repository at this point in the history
…wiseAI#2609)

Add sort capabilities to ChatFlow and AgentFlow tables
  • Loading branch information
danieldabate authored Jun 12, 2024
1 parent 3ab0d99 commit 76c5e6a
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions packages/ui/src/ui-component/table/FlowListTable.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useState } from 'react'
import PropTypes from 'prop-types'
import { useSelector } from 'react-redux'
import moment from 'moment'
Expand All @@ -14,6 +15,7 @@ import {
TableContainer,
TableHead,
TableRow,
TableSortLabel,
Tooltip,
Typography,
useTheme
Expand Down Expand Up @@ -41,10 +43,42 @@ const StyledTableRow = styled(TableRow)(() => ({
}
}))

const getLocalStorageKeyName = (name, isAgentCanvas) => {
return (isAgentCanvas ? 'agentcanvas' : 'chatflowcanvas') + '_' + name
}

export const FlowListTable = ({ data, images, isLoading, filterFunction, updateFlowsApi, setError, isAgentCanvas }) => {
const theme = useTheme()
const customization = useSelector((state) => state.customization)

const localStorageKeyOrder = getLocalStorageKeyName('order', isAgentCanvas)
const localStorageKeyOrderBy = getLocalStorageKeyName('orderBy', isAgentCanvas)

const [order, setOrder] = useState(localStorage.getItem(localStorageKeyOrder) || 'desc')
const [orderBy, setOrderBy] = useState(localStorage.getItem(localStorageKeyOrderBy) || 'updatedDate')

const handleRequestSort = (property) => {
const isAsc = orderBy === property && order === 'asc'
const newOrder = isAsc ? 'desc' : 'asc'
setOrder(newOrder)
setOrderBy(property)
localStorage.setItem(localStorageKeyOrder, newOrder)
localStorage.setItem(localStorageKeyOrderBy, property)
}

const sortedData = data
? [...data].sort((a, b) => {
if (orderBy === 'name') {
return order === 'asc' ? (a.name || '').localeCompare(b.name || '') : (b.name || '').localeCompare(a.name || '')
} else if (orderBy === 'updatedDate') {
return order === 'asc'
? new Date(a.updatedDate) - new Date(b.updatedDate)
: new Date(b.updatedDate) - new Date(a.updatedDate)
}
return 0
})
: []

return (
<>
<TableContainer sx={{ border: 1, borderColor: theme.palette.grey[900] + 25, borderRadius: 2 }} component={Paper}>
Expand All @@ -57,7 +91,9 @@ export const FlowListTable = ({ data, images, isLoading, filterFunction, updateF
>
<TableRow>
<StyledTableCell component='th' scope='row' style={{ width: '20%' }} key='0'>
Name
<TableSortLabel active={orderBy === 'name'} direction={order} onClick={() => handleRequestSort('name')}>
Name
</TableSortLabel>
</StyledTableCell>
<StyledTableCell style={{ width: '25%' }} key='1'>
Category
Expand All @@ -66,7 +102,13 @@ export const FlowListTable = ({ data, images, isLoading, filterFunction, updateF
Nodes
</StyledTableCell>
<StyledTableCell style={{ width: '15%' }} key='3'>
Last Modified Date
<TableSortLabel
active={orderBy === 'updatedDate'}
direction={order}
onClick={() => handleRequestSort('updatedDate')}
>
Last Modified Date
</TableSortLabel>
</StyledTableCell>
<StyledTableCell style={{ width: '10%' }} key='4'>
Actions
Expand Down Expand Up @@ -113,7 +155,7 @@ export const FlowListTable = ({ data, images, isLoading, filterFunction, updateF
</>
) : (
<>
{data?.filter(filterFunction).map((row, index) => (
{sortedData.filter(filterFunction).map((row, index) => (
<StyledTableRow key={index}>
<StyledTableCell key='0'>
<Tooltip title={row.templateName || row.name}>
Expand Down

0 comments on commit 76c5e6a

Please sign in to comment.