Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[website] Add star count fallback #3278

Merged
merged 6 commits into from
Mar 8, 2024
Merged
Changes from 5 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
20 changes: 13 additions & 7 deletions docs/src/components/landing/GithubStars.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { useTheme } from '@mui/material/styles';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import StarRateRoundedIcon from '@mui/icons-material/StarRateRounded';
import StarBorderOutlinedIcon from '@mui/icons-material/StarBorderOutlined';
import GitHubIcon from '@mui/icons-material/GitHub';
import ROUTES from '../../route';

Expand All @@ -16,16 +16,19 @@ export default function GithubStars() {
const response = await fetch('https://api.github.com/repos/mui/mui-toolpad');
const data = await response.json();
setFetching(false);
if (typeof window !== 'undefined') {
if (response.status !== 200) {
console.error('Failed to fetch stars count', data);
}
if (typeof window !== 'undefined' && data.stargazers_count) {
localStorage.setItem('mui-toolpad-stars', `${Date.now()}_${data.stargazers_count}`);
}
setStars(data.stargazers_count);
setStars(data.stargazers_count ?? '');
}, []);

React.useEffect(() => {
const cached = localStorage.getItem('mui-toolpad-stars');
if (cached && Date.now() - parseFloat(cached.split('_')[0]) < 1000 * 60 * 60) {
setStars(parseFloat(cached.split('_')[1]));
if (cached && Date.now() - parseFloat(cached.split('_')?.[0]) < 1000 * 60 * 60) {
setStars(cached.split('_')?.[1] ? parseFloat(cached.split('_')?.[1]) : '');
setFetching(false);
} else {
fetchStars();
Expand Down Expand Up @@ -55,7 +58,7 @@ export default function GithubStars() {
}}
startIcon={<GitHubIcon />}
>
Stars
Star
<Box
sx={{
display: 'flex',
Expand All @@ -67,7 +70,10 @@ export default function GithubStars() {
}),
}}
>
<StarRateRoundedIcon fontSize="small" />
<StarBorderOutlinedIcon
sx={{ mt: stars ? 0.1 : 0, mr: stars ? 0.5 : 0 }}
fontSize="small"
/>
{fetching ? '...' : stars}
bharatkashyap marked this conversation as resolved.
Show resolved Hide resolved
</Box>
</Button>
Expand Down
Loading