Skip to content

Commit e8e41fa

Browse files
committed
Fix commits loading & error display
1 parent c0d6769 commit e8e41fa

3 files changed

Lines changed: 42 additions & 20 deletions

File tree

src/app/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SerializedError } from '@reduxjs/toolkit';
2+
import { FetchBaseQueryError } from '@reduxjs/toolkit/dist/query';
23

34
export enum ErrorCode {
45
NotSignedIn = 'signIn.no',
@@ -25,3 +26,7 @@ export class LogicError extends Error {
2526
export function isSerializedError(error: any): error is SerializedError {
2627
return 'code' in error && 'message' in error;
2728
}
29+
30+
export function isFetchBaseQueryError(error: any): error is FetchBaseQueryError {
31+
return 'data' in error && 'status' in error;
32+
}

src/components/CommitsTable/CommitsTable.tsx

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { FC, useState } from 'react';
2-
import { DataGrid, GridColumns } from '@mui/x-data-grid';
2+
import { DataGrid, GridCellParams, GridColumns } from '@mui/x-data-grid';
33
import { Commit } from '../../models/commit';
44
import { useAppSelector } from '../../app/hooks';
55
import { useCommits } from '../../hooks/useCommits';
6+
import { Optional } from '../../app/types';
67
import Box from '@mui/material/Box';
78
import Toolbar from '@mui/material/Toolbar';
89
import Typography from '@mui/material/Typography';
@@ -11,7 +12,7 @@ import Container from '@mui/material/Container';
1112
const DEFAULT_ROWS_PER_PAGE = 10;
1213

1314
interface 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

101116
export default CommitsTable;

src/hooks/useCommits.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export function useCommits(owner: string, repo: string): CommitsHookReturnType {
2121

2222
useEffect(() => {
2323
const searchForRepos = setTimeout(async () => {
24-
await trigger({ owner, repo }, true);
24+
if (owner && repo) {
25+
await trigger({ owner, repo }, true);
26+
}
2527
}, 1500);
2628

2729
return () => {

0 commit comments

Comments
 (0)