11import React , { useState } from "react" ;
2- import { Button , Table , Typography } from "@equinor/eds-core-react" ;
3- import { convertToSubscripts , removeSubsFromString } from "../functions/Formatting" ;
2+ import { Button , Typography } from "@equinor/eds-core-react" ;
43
5- const Reactions : React . FC < { commonPaths : any ; reactions : any } > = ( { commonPaths, reactions } ) => {
4+ import GenericTable from "../components/GenericTable" ;
5+ const Reactions : React . FC < { commonPaths : any ; reactions : Record < string , any > } > = ( { commonPaths, reactions } ) => {
66 const [ isReactionsLimited , setIsReactionsLimited ] = useState < boolean > ( true ) ;
77 const reactionLimit = 5 ;
8-
8+ const displayedReactions : Record < string , any > [ ] = reactions
9+ ? isReactionsLimited
10+ ? Object . values ( reactions ) . slice ( 0 , reactionLimit )
11+ : Object . values ( reactions )
12+ : [ ] ;
913 const handleShowMoreLessReactions = ( ) => {
1014 setIsReactionsLimited ( ! isReactionsLimited ) ;
1115 } ;
@@ -14,30 +18,10 @@ const Reactions: React.FC<{ commonPaths: any; reactions: any }> = ({ commonPaths
1418 < Typography variant = "h5" style = { { marginTop : "1rem" } } >
1519 Most frequent reactions
1620 </ Typography >
17-
18- { reactions && Object . keys ( reactions ) . length > 0 ? (
21+ { reactions ? (
1922 < >
20- < Table >
21- < Table . Head >
22- < Table . Row >
23- < Table . Cell > Reaction</ Table . Cell >
24- < Table . Cell > k</ Table . Cell >
25- < Table . Cell > Frequency</ Table . Cell >
26- </ Table . Row >
27- </ Table . Head >
28- < Table . Body >
29- { Object . keys ( reactions . index )
30- . filter ( ( key ) => Number ( key ) < reactionLimit || ! isReactionsLimited )
31- . map ( ( key , index ) => (
32- < Table . Row key = { index } >
33- < Table . Cell > { convertToSubscripts ( reactions . index [ key ] ) } </ Table . Cell >
34- < Table . Cell > { reactions . k [ key ] } </ Table . Cell >
35- < Table . Cell > { reactions . frequency [ key ] } </ Table . Cell >
36- </ Table . Row >
37- ) ) }
38- </ Table . Body >
39- </ Table >
40- { Object . keys ( reactions . index ) . length > reactionLimit && (
23+ < GenericTable data = { displayedReactions } />
24+ { Object . keys ( reactions ) . length > reactionLimit && (
4125 < Button variant = "ghost" onClick = { ( ) => handleShowMoreLessReactions ( ) } >
4226 { isReactionsLimited ? "Show more" : "Show less" }
4327 </ Button >
@@ -51,36 +35,7 @@ const Reactions: React.FC<{ commonPaths: any; reactions: any }> = ({ commonPaths
5135 Most frequent paths
5236 </ Typography >
5337
54- { commonPaths . paths && commonPaths . paths [ 0 ] !== null ? (
55- < Table >
56- < Table . Head >
57- < Table . Row >
58- < Table . Cell > Path</ Table . Cell >
59- < Table . Cell > k</ Table . Cell >
60- < Table . Cell > Frequency</ Table . Cell >
61- </ Table . Row >
62- </ Table . Head >
63- < Table . Body >
64- { Object . keys ( commonPaths . paths ) . map ( ( key ) => {
65- const pathReactions : string [ ] = commonPaths . paths [ key ] . split ( "\n" ) ;
66- const kValues = commonPaths . k [ key ] . split ( "\n" ) ;
67- return pathReactions . map ( ( reaction , reactionIndex ) => (
68- < Table . Row key = { `${ key } -${ reactionIndex } ` } >
69- < Table . Cell > { convertToSubscripts ( removeSubsFromString ( reaction ) ) } </ Table . Cell >
70- < Table . Cell > { kValues [ reactionIndex ] } </ Table . Cell >
71- { reactionIndex === 0 && (
72- < Table . Cell rowSpan = { pathReactions . length } >
73- { commonPaths . frequency [ key ] }
74- </ Table . Cell >
75- ) }
76- </ Table . Row >
77- ) ) ;
78- } ) }
79- </ Table . Body >
80- </ Table >
81- ) : (
82- < Typography > No paths available.</ Typography >
83- ) }
38+ { commonPaths ? < GenericTable data = { commonPaths } /> : < Typography > No paths available.</ Typography > }
8439 </ div >
8540 ) ;
8641} ;
0 commit comments