11import React from 'react' ;
22import {
3- type DataViewState ,
43 DataView ,
54 DataViewTextFilter ,
65 useDataViewPagination ,
@@ -57,23 +56,16 @@ const SystemsViewTable: React.FC = () => {
5756 perPage : pagination . perPage ,
5857 } ) ;
5958
60- type ActiveState = DataViewState | 'ready' | undefined ;
61- const getActiveState = ( ) : ActiveState => {
62- if ( isLoading ) {
63- return 'loading' ;
64- }
65- if ( isError ) {
66- return 'error' ;
67- }
68- if ( data && data . length === 0 ) {
69- return 'empty' ;
70- }
71-
72- return 'ready' ;
73- } ;
59+ const activeState = isLoading
60+ ? 'loading'
61+ : isError
62+ ? 'error'
63+ : data ?. length === 0
64+ ? 'empty'
65+ : 'active' ;
7466
75- const createRows = ( data : System [ ] ) : DataViewTr [ ] => {
76- return data . map ( ( system ) => ( {
67+ const mapSystemToRow = ( system : System ) : DataViewTr => {
68+ return {
7769 id : system . id ,
7870 // FIXME types in column components
7971 row : [
@@ -102,21 +94,24 @@ const SystemsViewTable: React.FC = () => {
10294 per_reporter_staleness = { system ?. per_reporter_staleness }
10395 /> ,
10496 ] ,
105- } ) ) ;
97+ } ;
10698 } ;
10799
108- const rows = createRows ( data ?? [ ] ) ;
100+ const rows = ( data ?? [ ] ) . map ( mapSystemToRow ) ;
109101 const columns : DataViewTh [ ] = [
110102 { cell : 'Name' } ,
111103 { cell : 'Workspace' } ,
112104 { cell : 'Tags' } ,
113- { cell : 'OS' } ,
114- {
115- cell : 'Last Seen' ,
116- props : { tooltip : 'Operating system' } ,
117- } ,
105+ { cell : 'OS' , props : { tooltip : 'Operating system' } } ,
106+ { cell : 'Last Seen' } ,
118107 ] ;
119108
109+ const isPageSelected = ( rows : DataViewTr [ ] ) =>
110+ rows . length > 0 && rows . every ( ( row ) => isSelected ( row ) ) ;
111+
112+ const isPagePartiallySelected = ( rows : DataViewTr [ ] ) =>
113+ rows . some ( ( row ) => isSelected ( row ) ) && ! isPageSelected ( rows ) ;
114+
120115 // TODO Define filters
121116 const filters = { } ;
122117
@@ -125,6 +120,7 @@ const SystemsViewTable: React.FC = () => {
125120 case 'none' :
126121 case 'nonePage' :
127122 setSelected ( [ ] ) ;
123+ break ;
128124 case 'page' :
129125 if ( selected . length === 0 ) {
130126 onSelect ( true , rows ) ;
@@ -138,13 +134,13 @@ const SystemsViewTable: React.FC = () => {
138134 perPage : pagination . perPage ,
139135 queryClient,
140136 } ) ;
141- onSelect ( true , createRows ( allSystems ) ) ;
137+ onSelect ( true , allSystems . map ( mapSystemToRow ) ) ;
142138 break ;
143139 }
144140 } ;
145141
146142 return (
147- < DataView selection = { selection } activeState = { getActiveState ( ) } >
143+ < DataView selection = { selection } activeState = { activeState } >
148144 < DataViewToolbar
149145 ouiaId = "systems-view-header"
150146 clearAllFilters = { ( ) => {
@@ -156,11 +152,8 @@ const SystemsViewTable: React.FC = () => {
156152 // canSelectAll disabled as we miss spinner & ability to disable controls
157153 totalCount = { total }
158154 selectedCount = { selected . length }
159- pagePartiallySelected = {
160- rows . some ( ( row ) => isSelected ( row ) ) &&
161- ! rows . every ( ( row ) => isSelected ( row ) )
162- }
163- pageSelected = { rows . every ( ( row ) => isSelected ( row ) ) }
155+ pagePartiallySelected = { isPagePartiallySelected ( rows ) }
156+ pageSelected = { isPageSelected ( rows ) }
164157 onSelect = { onBulkSelect }
165158 />
166159 }
@@ -211,7 +204,7 @@ const SystemsViewTable: React.FC = () => {
211204 < ErrorState
212205 ouiaId = "error-systems-state"
213206 titleText = "Unable to load data"
214- bodyText = { ` There was an error retrieving data. ${ error ? ` ${ error . name } ${ error . message } ` : ' Check your connection and reload the page.' } ` }
207+ bodyText = " There was an error retrieving data. Check your connection and reload the page."
215208 />
216209 ) ,
217210 } }
0 commit comments