Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add connected instances table (#6263)
This PR adds a first iteration of the connected instances table on a new connected instances tab of the application page (hidden behind a flag). As a first version, it only uses data that we currently return for each application (so no "connected through" or "last synchronized"). It also uses the existing version of the application data and just filters it for the "production" environment right now. Adding query parameters (and potentially a new URL?) to the applications page (to save state and fetch data) will be done in a follow-up. This should lay the groundwork for adding a new API too. <img width="1485" alt="image" src="https://github.com/Unleash/unleash/assets/17786332/4fb68456-d0f5-4f82-9246-5333a273df9c">
- Loading branch information
1 parent
5f781b4
commit 0e9102f
Showing
4 changed files
with
269 additions
and
0 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
53 changes: 53 additions & 0 deletions
53
frontend/src/component/application/ConnectedInstances/ConnectedInstances.tsx
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,53 @@ | ||
import { useMemo } from 'react'; | ||
import useApplication from 'hooks/api/getters/useApplication/useApplication'; | ||
import { formatDateYMDHMS } from 'utils/formatDate'; | ||
import { useRequiredPathParam } from 'hooks/useRequiredPathParam'; | ||
import { useConnectedInstancesTable } from './useConnectedInstancesTable'; | ||
import { ConnectedInstancesTable } from './ConnectedInstancesTable'; | ||
|
||
export const ConnectedInstances = () => { | ||
const name = useRequiredPathParam('name'); | ||
const { application } = useApplication(name); | ||
|
||
const tableData = useMemo(() => { | ||
return ( | ||
application.instances | ||
// @ts-expect-error: the type definition here is incomplete. It | ||
// should be updated as part of this project. | ||
.filter((instance) => instance.environment === 'production') | ||
.map(({ instanceId, sdkVersion, clientIp, lastSeen }) => { | ||
return { | ||
instanceId, | ||
ip: clientIp, | ||
sdkVersion, | ||
lastSeen: formatDateYMDHMS(lastSeen), | ||
}; | ||
}) | ||
); | ||
}, [application]); | ||
|
||
const { | ||
getTableProps, | ||
getTableBodyProps, | ||
headerGroups, | ||
rows, | ||
prepareRow, | ||
state: { globalFilter }, | ||
setHiddenColumns, | ||
columns, | ||
} = useConnectedInstancesTable(tableData); | ||
|
||
return ( | ||
<ConnectedInstancesTable | ||
loading={false} | ||
headerGroups={headerGroups} | ||
setHiddenColumns={setHiddenColumns} | ||
prepareRow={prepareRow} | ||
getTableBodyProps={getTableBodyProps} | ||
getTableProps={getTableProps} | ||
rows={rows} | ||
columns={columns} | ||
globalFilter={globalFilter} | ||
/> | ||
); | ||
}; |
109 changes: 109 additions & 0 deletions
109
frontend/src/component/application/ConnectedInstances/ConnectedInstancesTable.tsx
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,109 @@ | ||
import { | ||
Row, | ||
TablePropGetter, | ||
TableProps, | ||
TableBodyPropGetter, | ||
TableBodyProps, | ||
HeaderGroup, | ||
} from 'react-table'; | ||
import { | ||
SortableTableHeader, | ||
TableCell, | ||
TablePlaceholder, | ||
} from 'component/common/Table'; | ||
import { | ||
Box, | ||
styled, | ||
Table, | ||
TableBody, | ||
TableRow, | ||
useMediaQuery, | ||
} from '@mui/material'; | ||
import theme from 'themes/theme'; | ||
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender'; | ||
|
||
import { useConditionallyHiddenColumns } from 'hooks/useConditionallyHiddenColumns'; | ||
|
||
const hiddenColumnsSmall = ['ip', 'sdkVersion']; | ||
const hiddenColumnsCompact = ['ip', 'sdkVersion', 'lastSeen']; | ||
|
||
type ConnectedInstancesTableProps = { | ||
compact?: boolean; | ||
loading: boolean; | ||
setHiddenColumns: (param: any) => void; | ||
columns: any[]; | ||
rows: Row<object>[]; | ||
prepareRow: (row: Row<object>) => void; | ||
getTableProps: ( | ||
propGetter?: TablePropGetter<object> | undefined, | ||
) => TableProps; | ||
getTableBodyProps: ( | ||
propGetter?: TableBodyPropGetter<object> | undefined, | ||
) => TableBodyProps; | ||
headerGroups: HeaderGroup<object>[]; | ||
globalFilter: any; | ||
}; | ||
export const ConnectedInstancesTable = ({ | ||
compact = false, | ||
setHiddenColumns, | ||
columns, | ||
loading, | ||
rows, | ||
getTableProps, | ||
getTableBodyProps, | ||
headerGroups, | ||
globalFilter, | ||
prepareRow, | ||
}: ConnectedInstancesTableProps) => { | ||
const isSmallScreen = useMediaQuery(theme.breakpoints.down('md')); | ||
|
||
useConditionallyHiddenColumns( | ||
[ | ||
{ | ||
condition: isSmallScreen, | ||
columns: hiddenColumnsSmall, | ||
}, | ||
{ | ||
condition: compact, | ||
columns: hiddenColumnsCompact, | ||
}, | ||
], | ||
setHiddenColumns, | ||
columns, | ||
); | ||
|
||
return ( | ||
<> | ||
<Box sx={{ overflowX: 'auto' }}> | ||
<Table {...getTableProps()}> | ||
<SortableTableHeader headerGroups={headerGroups as any} /> | ||
<TableBody {...getTableBodyProps()}> | ||
{rows.map((row) => { | ||
prepareRow(row); | ||
return ( | ||
<TableRow hover {...row.getRowProps()}> | ||
{row.cells.map((cell) => ( | ||
<TableCell {...cell.getCellProps()}> | ||
{cell.render('Cell')} | ||
</TableCell> | ||
))} | ||
</TableRow> | ||
); | ||
})} | ||
</TableBody> | ||
</Table> | ||
</Box> | ||
<ConditionallyRender | ||
condition={rows.length === 0 && !loading} | ||
show={ | ||
<TablePlaceholder> | ||
<p> | ||
There's no data for any connected instances to | ||
display. Have you configured your clients correctly? | ||
</p> | ||
</TablePlaceholder> | ||
} | ||
/> | ||
</> | ||
); | ||
}; |
97 changes: 97 additions & 0 deletions
97
frontend/src/component/application/ConnectedInstances/useConnectedInstancesTable.tsx
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,97 @@ | ||
import { useMemo } from 'react'; | ||
import { IApiToken } from 'hooks/api/getters/useApiTokens/useApiTokens'; | ||
import { DateCell } from 'component/common/Table/cells/DateCell/DateCell'; | ||
import { HighlightCell } from 'component/common/Table/cells/HighlightCell/HighlightCell'; | ||
import { IconCell } from 'component/common/Table/cells/IconCell/IconCell'; | ||
import { TimeAgoCell } from 'component/common/Table/cells/TimeAgoCell/TimeAgoCell'; | ||
import { useTable, useGlobalFilter, useSortBy } from 'react-table'; | ||
import { sortTypes } from 'utils/sortTypes'; | ||
import { ProjectsList } from 'component/admin/apiToken/ProjectsList/ProjectsList'; | ||
import { Key } from '@mui/icons-material'; | ||
|
||
type ConnectedInstancesTableData = { | ||
instanceId: string; | ||
ip: string; | ||
sdkVersion: string; | ||
lastSeen: string; | ||
}; | ||
|
||
export const useConnectedInstancesTable = ( | ||
instanceData: ConnectedInstancesTableData[], | ||
) => { | ||
const initialState = useMemo( | ||
() => ({ sortBy: [{ id: 'instanceId' }] }), | ||
[], | ||
); | ||
|
||
const COLUMNS = useMemo(() => { | ||
return [ | ||
{ | ||
Header: 'Instances', | ||
accessor: 'instanceId', | ||
Cell: HighlightCell, | ||
styles: { | ||
width: '45%', | ||
}, | ||
}, | ||
{ | ||
Header: 'SDK', | ||
accessor: 'sdkVersion', | ||
Cell: HighlightCell, | ||
styles: { | ||
width: '20%', | ||
}, | ||
}, | ||
{ | ||
Header: 'Last seen', | ||
accessor: 'lastSeen', | ||
Cell: HighlightCell, | ||
styles: { | ||
width: '20%', | ||
}, | ||
}, | ||
{ | ||
Header: 'IP', | ||
accessor: 'ip', | ||
Cell: HighlightCell, | ||
styles: { | ||
width: '15%', | ||
}, | ||
}, | ||
]; | ||
}, []); | ||
|
||
const { | ||
getTableProps, | ||
getTableBodyProps, | ||
headerGroups, | ||
rows, | ||
prepareRow, | ||
state, | ||
setGlobalFilter, | ||
setHiddenColumns, | ||
} = useTable( | ||
{ | ||
columns: COLUMNS as any, | ||
data: instanceData as any, | ||
initialState, | ||
sortTypes, | ||
autoResetHiddenColumns: false, | ||
disableSortRemove: true, | ||
}, | ||
useGlobalFilter, | ||
useSortBy, | ||
); | ||
|
||
return { | ||
getTableProps, | ||
getTableBodyProps, | ||
headerGroups, | ||
rows, | ||
prepareRow, | ||
state, | ||
setGlobalFilter, | ||
setHiddenColumns, | ||
columns: COLUMNS, | ||
}; | ||
}; |