11import { FC , useState } from 'react' ;
2- import { DataGrid , GridColumns } from '@mui/x-data-grid' ;
2+ import { DataGrid , GridCellParams , GridColumns } from '@mui/x-data-grid' ;
33import { Commit } from '../../models/commit' ;
44import { useAppSelector } from '../../app/hooks' ;
55import { useCommits } from '../../hooks/useCommits' ;
6+ import { Optional } from '../../app/types' ;
67import Box from '@mui/material/Box' ;
78import Toolbar from '@mui/material/Toolbar' ;
89import Typography from '@mui/material/Typography' ;
@@ -11,7 +12,7 @@ import Container from '@mui/material/Container';
1112const DEFAULT_ROWS_PER_PAGE = 10 ;
1213
1314interface CommitRow {
14- id : number ;
15+ id : string ;
1516 author : string ;
1617 committer : string ;
1718 message : string ;
@@ -34,17 +35,24 @@ const CommitsTable: FC<CommitsTableProps> = ({ org }) => {
3435 const [ rowsPerPage , setRowsPerPage ] = useState ( DEFAULT_ROWS_PER_PAGE ) ;
3536
3637 const repo = useAppSelector ( ( state ) => state . repoReducer . name ) ;
37- const [ commits , isLoading , , error , isUninitialized ] = useCommits ( org , repo ) ;
38+ const [ commits , isLoading , isError , , isUninitialized ] = useCommits ( org , repo ) ;
3839
3940 const handleChangeRowsPerPage = ( newPage : number ) => {
4041 setRowsPerPage ( newPage ) ;
4142 setPage ( 0 ) ;
4243 } ;
4344
45+ const handleRowClick = ( params : GridCellParams ) => {
46+ if ( commits ?. length ) {
47+ const commit = commits . find ( ( commit ) => commit . commit . message === params . row . message ) ;
48+ window . open ( commit ?. html_url , '_blank' ) ;
49+ }
50+ } ;
51+
4452 return (
4553 < Container maxWidth = "xl" >
46- < Box sx = { { height : 400 , width : '100%' } } >
47- { ! isUninitialized && (
54+ { ! isUninitialized && (
55+ < Box sx = { { height : 400 , width : '100%' } } >
4856 < Toolbar
4957 sx = { {
5058 pl : { sm : 2 } ,
@@ -66,8 +74,6 @@ const CommitsTable: FC<CommitsTableProps> = ({ org }) => {
6674 Commits List
6775 </ Typography >
6876 </ Toolbar >
69- ) }
70- { commits ? (
7177 < DataGrid
7278 disableSelectionOnClick
7379 page = { page }
@@ -79,23 +85,32 @@ const CommitsTable: FC<CommitsTableProps> = ({ org }) => {
7985 rows = { mapCommits ( commits ) }
8086 columns = { commitsTableColumns }
8187 loading = { isLoading }
88+ error = { isError === true || undefined }
89+ onCellClick = { handleRowClick }
90+ sx = { {
91+ '& .MuiDataGrid-cell:hover' : {
92+ color : 'primary.main' ,
93+ textDecoration : 'underline' ,
94+ cursor : 'pointer' ,
95+ } ,
96+ } }
8297 />
83- ) : (
84- < DataGrid columns = { commitsTableColumns } rows = { [ ] } error = { error } loading = { isLoading } />
85- ) }
86- </ Box >
98+ </ Box >
99+ ) }
87100 </ Container >
88101 ) ;
89102} ;
90103
91- export function mapCommits ( commits : Commit [ ] ) {
92- return commits . map ( ( commit , i ) => ( {
93- id : i ,
94- author : commit . commit . author . name ,
95- committer : commit . commit . committer . name ,
96- message : commit . commit . message ,
97- date : commit . commit . committer . date ,
98- } ) ) ;
104+ export function mapCommits ( commits : Optional < Commit [ ] > ) {
105+ return commits
106+ ? commits . map ( ( commit ) => ( {
107+ id : commit . sha ,
108+ author : commit . commit . author . name ,
109+ committer : commit . commit . committer . name ,
110+ message : commit . commit . message ,
111+ date : commit . commit . committer . date ,
112+ } ) )
113+ : [ ] ;
99114}
100115
101116export default CommitsTable ;
0 commit comments