1+ import { useEffect , useState } from 'react' ;
2+ import { useLazyGetRepositoryCommitsQuery } from '../features/commits/commitsAPI' ;
3+ import { DataGrid , GridColumns } from '@mui/x-data-grid' ;
4+ import { Commit } from '../models/commits' ;
5+ import Link from '@mui/material/Link' ;
6+ import Box from '@mui/material/Box' ;
7+ import Toolbar from '@mui/material/Toolbar' ;
8+ import Typography from '@mui/material/Typography' ;
9+
10+ const DEFAULT_ROWS_PER_PAGE = 10 ;
11+
12+ interface CommitRow {
13+ id : number ;
14+ author : string ;
15+ committer : string ;
16+ message : JSX . Element ;
17+ date : string ;
18+ }
19+
20+ const commitsTableColumns : GridColumns < CommitRow > = [
21+ { field : 'author' , headerName : 'Author' , width : 150 } ,
22+ { field : 'committer' , headerName : 'Committer' , width : 150 } ,
23+ { field : 'message' , headerName : 'Message' , width : 600 } ,
24+ { field : 'date' , headerName : 'Date' , width : 150 } ,
25+ ] ;
26+
27+ type CommitsTableProps = {
28+ org : string ;
29+ repo : string ;
30+ }
31+
32+ export default function CommitsTable ( { org, repo } : CommitsTableProps ) {
33+ const [ page , setPage ] = useState ( 0 ) ;
34+ const [ rowsPerPage , setRowsPerPage ] = useState ( DEFAULT_ROWS_PER_PAGE ) ;
35+ const [ commits , setCommits ] = useState < Commit [ ] > ( ) ;
36+ const [ triggerCommitsQuery ] = useLazyGetRepositoryCommitsQuery ( { refetchOnReconnect : true } ) ;
37+
38+ useEffect ( ( ) => {
39+ const searchForCommits = setTimeout ( async ( ) => {
40+ const { data } = await triggerCommitsQuery ( {
41+ username : org ,
42+ repository : repo
43+ } , true ) ;
44+ setCommits ( data ?. commits ) ;
45+ } , 600 ) ;
46+ return ( ) => clearTimeout ( searchForCommits ) ;
47+ // eslint-disable-next-line react-hooks/exhaustive-deps
48+ } , [ repo , triggerCommitsQuery ] ) ;
49+
50+ const handleChangeRowsPerPage = ( newPage : number ) => {
51+ setRowsPerPage ( newPage ) ;
52+ setPage ( 0 ) ;
53+ } ;
54+
55+ return (
56+ < Box sx = { { height : 400 , width : '100%' } } >
57+ { commits && ( < >
58+ < Toolbar sx = { {
59+ pl : { sm : 2 } ,
60+ pr : { xs : 1 , sm : 1 } ,
61+ } } >
62+ < Typography
63+ sx = { { flex : '1 1 100%' } }
64+ variant = "h6"
65+ id = "tableTitle"
66+ component = "div"
67+ >
68+ Commits List
69+ </ Typography >
70+ </ Toolbar >
71+ { /* TODO: error / loading */ }
72+ < DataGrid
73+ disableSelectionOnClick
74+ page = { page }
75+ pageSize = { rowsPerPage }
76+ rowsPerPageOptions = { [ 5 , 10 , 15 ] }
77+ onPageChange = { ( newPage ) => setPage ( newPage ) }
78+ onPageSizeChange = { handleChangeRowsPerPage }
79+ density = "compact"
80+ rows = { commits . map ( ( commit , i ) => ( {
81+ id : i ,
82+ author : commit . commit . author . name ,
83+ committer : commit . commit . committer . name ,
84+ message : (
85+ < Link href = { commit . html_url } > { commit . commit . message } </ Link >
86+ ) ,
87+ date : commit . commit . committer . date ,
88+ } ) ) }
89+ columns = { commitsTableColumns }
90+ />
91+ </ > ) }
92+ </ Box >
93+ ) ;
94+ }
0 commit comments