1- import { useEffect , useState } from 'react' ;
2- import { useLazyGetRepositoryCommitsQuery } from '../features/commits/commitsAPI' ;
1+ import { FC , useState } from 'react' ;
32import { DataGrid , GridColumns } from '@mui/x-data-grid' ;
43import { Commit } from '../models/commits' ;
5- import Link from '@mui/material/Link' ;
4+ import { useAppSelector } from '../app/hooks' ;
5+ import { useCommits } from '../hooks/useCommits' ;
66import Box from '@mui/material/Box' ;
77import Toolbar from '@mui/material/Toolbar' ;
88import Typography from '@mui/material/Typography' ;
9+ import Container from '@mui/material/Container' ;
910
1011const DEFAULT_ROWS_PER_PAGE = 10 ;
1112
1213interface CommitRow {
1314 id : number ;
1415 author : string ;
1516 committer : string ;
16- message : JSX . Element ;
17+ message : string ;
1718 date : string ;
1819}
1920
2021const commitsTableColumns : GridColumns < CommitRow > = [
2122 { field : 'author' , headerName : 'Author' , width : 150 } ,
2223 { field : 'committer' , headerName : 'Committer' , width : 150 } ,
23- { field : 'message' , headerName : 'Message' , width : 600 } ,
24- { field : 'date' , headerName : 'Date' , width : 150 } ,
24+ { field : 'message' , headerName : 'Message' , width : 650 } ,
25+ { field : 'date' , headerName : 'Date' , width : 175 } ,
2526] ;
2627
2728type CommitsTableProps = {
2829 org : string ;
29- repo : string ;
30- }
30+ } ;
3131
32- export default function CommitsTable ( { org, repo } : CommitsTableProps ) {
32+ const CommitsTable : FC < CommitsTableProps > = ( { org } ) => {
3333 const [ page , setPage ] = useState ( 0 ) ;
3434 const [ rowsPerPage , setRowsPerPage ] = useState ( DEFAULT_ROWS_PER_PAGE ) ;
35- const [ commits , setCommits ] = useState < Commit [ ] > ( ) ;
36- const [ triggerCommitsQuery ] = useLazyGetRepositoryCommitsQuery ( { refetchOnReconnect : true } ) ;
3735
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 ] ) ;
36+ const repo = useAppSelector ( ( state ) => state . repoReducer . name ) ;
37+ const [ commits , isLoading , , error , isUninitialized ] = useCommits ( org , repo ) ;
4938
5039 const handleChangeRowsPerPage = ( newPage : number ) => {
5140 setRowsPerPage ( newPage ) ;
5241 setPage ( 0 ) ;
5342 } ;
5443
5544 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"
45+ < Container maxWidth = "xl" >
46+ < Box sx = { { height : 400 , width : '100%' } } >
47+ { ! isUninitialized && (
48+ < Toolbar
49+ sx = { {
50+ pl : { sm : 2 } ,
51+ pr : { xs : 1 , sm : 1 } ,
52+ } }
6753 >
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 >
54+ < Typography
55+ sx = { {
56+ flex : '1 1 100%' ,
57+ alignItems : 'center' ,
58+ alignContent : 'center' ,
59+ justifyContent : 'center' ,
60+ justifyItems : 'center' ,
61+ } }
62+ variant = "h6"
63+ id = "tableTitle"
64+ component = "div"
65+ >
66+ Commits List
67+ </ Typography >
68+ </ Toolbar >
69+ ) }
70+ { commits ? (
71+ < DataGrid
72+ disableSelectionOnClick
73+ page = { page }
74+ pageSize = { rowsPerPage }
75+ rowsPerPageOptions = { [ 5 , 10 , 15 ] }
76+ onPageChange = { ( newPage ) => setPage ( newPage ) }
77+ onPageSizeChange = { handleChangeRowsPerPage }
78+ density = "compact"
79+ rows = { mapCommits ( commits ) }
80+ columns = { commitsTableColumns }
81+ loading = { isLoading }
82+ />
83+ ) : (
84+ < DataGrid columns = { commitsTableColumns } rows = { [ ] } error = { error } loading = { isLoading } />
85+ ) }
86+ </ Box >
87+ </ Container >
9388 ) ;
94- }
89+ } ;
90+
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+ } ) ) ;
99+ }
100+
101+ export default CommitsTable ;
0 commit comments