Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"react-hot-toast": "^2.4.1",
"react-icons": "^5.3.0",
"react-router-dom": "^6.28.0",
"recharts": "^3.8.1",
"tailwindcss": "^3.4.14"
},
"devDependencies": {
Expand All @@ -45,12 +46,15 @@
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.14",
"express": "^5.2.1",
"express-session": "^1.18.2",
"globals": "^15.11.0",
"jasmine": "^5.9.0",
"mongoose": "^9.6.2",
"passport": "^0.7.0",
"passport-local": "^1.0.0",
"supertest": "^7.1.4",
"typescript-eslint": "^8.59.3",
"vite": "^5.4.10"
}
}
142 changes: 142 additions & 0 deletions src/components/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import React from 'react';
import {
PieChart,
Pie,
Cell,
BarChart,
Bar,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Legend,
ResponsiveContainer
} from 'recharts';
import { Paper, Typography, Box, Grid, Theme } from '@mui/material';

interface GitHubItem {
id: number;
title: string;
state: string;
created_at: string;
pull_request?: { merged_at: string | null };
repository_url: string;
html_url: string;
}

interface DashboardProps {
totalIssues: number;
totalPrs: number;
data: GitHubItem[];
theme: Theme;
}

const Dashboard: React.FC<DashboardProps> = ({ totalIssues, totalPrs, data, theme }) => {

// Data for Pie Chart
const pieData = [
{ name: 'Issues', value: totalIssues },
{ name: 'Pull Requests', value: totalPrs },
];

// Use theme-aware colors
const COLORS = [theme.palette.primary.main, theme.palette.secondary.main];

// Data for Bar Chart (Top 5 Repositories) - Improved safety
const repoCounts: { [key: string]: number } = {};
data.forEach(item => {
if (item?.repository_url) {
const repoName = item.repository_url
.split('/')
.filter(Boolean)
.pop();

if (repoName) {
repoCounts[repoName] = (repoCounts[repoName] || 0) + 1;
}
}
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

const barData = Object.entries(repoCounts)
.map(([name, count]) => ({ name, count }))
.sort((a, b) => b.count - a.count)
.slice(0, 5);

const hasData = totalIssues > 0 || totalPrs > 0;

if (!hasData) {
return (
<Paper elevation={1} sx={{ p: 4, mb: 4, textAlign: 'center', backgroundColor: theme.palette.background.paper }}>
<Typography variant="h6" color="textSecondary">
No data available. Enter a username to view analytics.
</Typography>
</Paper>
);
}

return (
<Box sx={{ mb: 4 }}>
<Grid container spacing={3}>
{/* Pie Chart: Issues vs PRs */}
<Grid item xs={12} md={6}>
<Paper elevation={2} sx={{ p: 2, height: 350, backgroundColor: theme.palette.background.paper }}>
<Typography variant="h6" gutterBottom align="center" color="textPrimary">
Contribution Mix (Total)
</Typography>
<ResponsiveContainer width="100%" height="90%">
<PieChart>
<Pie
data={pieData}
cx="50%"
cy="50%"
innerRadius={60}
outerRadius={80}
fill={theme.palette.primary.main}
paddingAngle={5}
dataKey="value"
label
>
{pieData.map((_entry, index) => (
<Cell key={`cell-${index}`} fill={COLORS[index % COLORS.length]} />
))}
</Pie>
<Tooltip
contentStyle={{ backgroundColor: theme.palette.background.paper, color: theme.palette.text.primary }}
/>
<Legend />
</PieChart>
</ResponsiveContainer>
</Paper>
</Grid>

{/* Bar Chart: Activity by Repository */}
<Grid item xs={12} md={6}>
<Paper elevation={2} sx={{ p: 2, height: 350, backgroundColor: theme.palette.background.paper }}>
<Typography variant="h6" gutterBottom align="center" color="textPrimary">
Top Repositories (Current View)
</Typography>
{barData.length > 0 ? (
<ResponsiveContainer width="100%" height="90%">
<BarChart data={barData}>
<CartesianGrid strokeDasharray="3 3" stroke={theme.palette.divider} />
<XAxis dataKey="name" stroke={theme.palette.text.secondary} />
<YAxis stroke={theme.palette.text.secondary} />
<Tooltip
contentStyle={{ backgroundColor: theme.palette.background.paper, color: theme.palette.text.primary }}
/>
<Bar dataKey="count" fill={theme.palette.primary.light} radius={[4, 4, 0, 0]} />
</BarChart>
</ResponsiveContainer>
) : (
<Box display="flex" justifyContent="center" alignItems="center" height="100%">
<Typography color="textSecondary">No repository data found in this view.</Typography>
</Box>
)}
</Paper>
</Grid>
</Grid>
</Box>
);
};

export default Dashboard;
17 changes: 17 additions & 0 deletions src/hooks/useDebounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useState, useEffect } from 'react';

export function useDebounce<T>(value: T, delay: number): T {
const [debouncedValue, setDebouncedValue] = useState<T>(value);

useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);

return () => {
clearTimeout(handler);
};
}, [value, delay]);

return debouncedValue;
}
3 changes: 0 additions & 3 deletions src/hooks/useGitHubAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Octokit } from '@octokit/core';
export const useGitHubAuth = () => {
const [username, setUsername] = useState('');
const [token, setToken] = useState('');
const [error, setError] = useState('');

const octokit = useMemo(() => {
if (!username || !token) return null;
Expand All @@ -18,8 +17,6 @@ export const useGitHubAuth = () => {
setUsername,
token,
setToken,
error,
setError,
getOctokit,
};
};
93 changes: 71 additions & 22 deletions src/hooks/useGitHubData.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,57 @@
import { useState, useCallback } from 'react';
import { useState, useCallback, useRef } from 'react';
import { Octokit } from '@octokit/core';

export const useGitHubData = (getOctokit: () => any) => {
const [issues, setIssues] = useState([]);
const [prs, setPrs] = useState([]);
interface GitHubItem {
id: number;
title: string;
state: string;
created_at: string;
pull_request?: { merged_at: string | null };
repository_url: string;
html_url: string;
}

interface FetchFilters {
search?: string;
repo?: string;
startDate?: string;
endDate?: string;
state?: string;
}

export const useGitHubData = (getOctokit: () => Octokit | null) => {
const [issues, setIssues] = useState<GitHubItem[]>([]);
const [prs, setPrs] = useState<GitHubItem[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const [totalIssues, setTotalIssues] = useState(0);
const [totalPrs, setTotalPrs] = useState(0);
const [rateLimited, setRateLimited] = useState(false);

// Track the latest request ID to prevent stale overwrites
const lastRequestId = useRef(0);

const fetchPaginated = async (
octokit: Octokit,
username: string,
type: string,
page = 1,
per_page = 10,
filters: FetchFilters = {}
) => {
let q = `author:${username} is:${type}`;

if (filters.search) q += ` ${filters.search} in:title`;
if (filters.repo) q += ` repo:${filters.repo}`;
if (filters.startDate) q += ` created:>=${filters.startDate}`;
if (filters.endDate) q += ` created:<=${filters.endDate}`;

if (filters.state === 'open' || filters.state === 'closed') {
q += ` is:${filters.state}`;
} else if (filters.state === 'merged') {
q += ` is:merged`;
}

const fetchPaginated = async (octokit: any, username: string, type: string, page = 1, per_page = 10) => {
const q = `author:${username} is:${type}`;
const response = await octokit.request('GET /search/issues', {
q,
sort: 'created',
Expand All @@ -26,34 +67,42 @@ export const useGitHubData = (getOctokit: () => any) => {
};

const fetchData = useCallback(
async (username: string, page = 1, perPage = 10) => {

async (username: string, page = 1, perPage = 10, activeTab: 'issue' | 'pr' = 'issue', filters: FetchFilters = {}) => {
const octokit = getOctokit();

if (!octokit || !username || rateLimited) return;

const requestId = ++lastRequestId.current;
setLoading(true);
setError('');

try {
// We fetch BOTH even if one tab is active to keep totals synchronized as requested
const [issueRes, prRes] = await Promise.all([
fetchPaginated(octokit, username, 'issue', page, perPage),
fetchPaginated(octokit, username, 'pr', page, perPage),
fetchPaginated(octokit, username, 'issue', activeTab === 'issue' ? page : 1, perPage, filters),
fetchPaginated(octokit, username, 'pr', activeTab === 'pr' ? page : 1, perPage, filters),
]);

setIssues(issueRes.items);
setPrs(prRes.items);
setTotalIssues(issueRes.total);
setTotalPrs(prRes.total);
} catch (err: any) {
if (err.status === 403) {
setError('GitHub API rate limit exceeded. Please wait or use a token.');
setRateLimited(true); // Prevent further fetches
} else {
setError(err.message || 'Failed to fetch data');
// Only update state if this is still the latest request
if (requestId === lastRequestId.current) {
setIssues(issueRes.items);
setTotalIssues(issueRes.total);
setPrs(prRes.items);
setTotalPrs(prRes.total);
}
} catch (err: unknown) {
if (requestId === lastRequestId.current) {
const error = err as { status?: number; message?: string };
if (error.status === 403) {
setError('GitHub API rate limit exceeded. Please wait or use a token.');
setRateLimited(true);
} else {
setError(error.message || 'Failed to fetch data');
}
}
} finally {
setLoading(false);
if (requestId === lastRequestId.current) {
setLoading(false);
}
}
},
[getOctokit, rateLimited]
Expand Down
10 changes: 8 additions & 2 deletions src/pages/ContributorProfile/ContributorProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ type PR = {
repository_url: string;
};

type Profile = {
avatar_url: string;
login: string;
bio: string;
};

export default function ContributorProfile() {
const { username } = useParams();
const [profile, setProfile] = useState<any>(null);
const [profile, setProfile] = useState<Profile | null>(null);
const [prs, setPRs] = useState<PR[]>([]);
const [loading, setLoading] = useState(true);

Expand All @@ -28,7 +34,7 @@ export default function ContributorProfile() {
);
const prsData = await prsRes.json();
setPRs(prsData.items);
} catch (error) {
} catch {
toast.error("Failed to fetch user data.");
} finally {
setLoading(false);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Contributors/Contributors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const ContributorsPage = () => {
withCredentials: false,
});
setContributors(response.data);
} catch (err) {
} catch {
setError("Failed to fetch contributors. Please try again later.");
} finally {
setLoading(false);
Expand Down
8 changes: 6 additions & 2 deletions src/pages/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ const Login: React.FC = () => {
if (response.data.message === 'Login successful') {
navigate("/home");
}
} catch (error: any) {
setMessage(error.response?.data?.message || "Something went wrong");
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
setMessage(error.response?.data?.message || "Something went wrong");
} else {
setMessage("Something went wrong");
}
} finally {
setIsLoading(false);
}
Expand Down
Loading