Skip to content

Commit ca5a13a

Browse files
authored
Merge pull request #532 from Kamlesh72/feat/github-star-btn
Feat: CTA Button to Star Github Repo
2 parents e2d9963 + 135a304 commit ca5a13a

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import StarBorderOutlined from '@mui/icons-material/StarBorderOutlined';
2+
import { Button, ButtonProps, Divider, useTheme } from '@mui/material';
3+
import { FC, useMemo } from 'react';
4+
5+
import { FlexBox } from '@/components/FlexBox';
6+
import { Line } from '@/components/Text';
7+
import { useSelector } from '@/store';
8+
9+
const githubRepoUrl = `https://github.com/middlewarehq/middleware`;
10+
11+
const formatStarCount = (stars: number) => {
12+
if (isNaN(stars)) return '';
13+
if (stars < 1000) return `${stars}`;
14+
15+
return `${Number((stars / 1000).toFixed(1))}k`;
16+
};
17+
18+
export const GithubButton: FC<ButtonProps> = () => {
19+
const theme = useTheme();
20+
const githubRepoStars = useSelector((s) => s.app.githubRepoStarsCount);
21+
const githubRepoStarsCount = useMemo(
22+
() => formatStarCount(githubRepoStars),
23+
[githubRepoStars]
24+
);
25+
26+
return (
27+
<Button
28+
variant="outlined"
29+
sx={{
30+
borderRadius: 0.8,
31+
borderColor: theme.colors.alpha.trueWhite[10],
32+
color: 'lightgray',
33+
height: 40,
34+
padding: '0 18px'
35+
}}
36+
onClick={() => window.open(githubRepoUrl, '_blank')}
37+
>
38+
<FlexBox alignCenter>
39+
<StarBorderOutlined fontSize="small" />
40+
<Line bold marginLeft={1}>
41+
Star
42+
</Line>
43+
</FlexBox>
44+
<Divider
45+
orientation="vertical"
46+
sx={{
47+
height: '100%',
48+
marginX: 2
49+
}}
50+
/>
51+
<Line bold>{githubRepoStarsCount}</Line>
52+
</Button>
53+
);
54+
};

web-server/src/components/PageHeader.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { TeamSelectorModes } from '@/types/resources';
1717
import { AvatarPageTitle } from './AvatarPageTitle';
1818
import { BranchSelector } from './BranchSelector';
1919
import { FlexBox, FlexBoxProps } from './FlexBox';
20+
import { GithubButton } from './GithubButton';
2021
import { Hotkey } from './Hotkey';
2122
import { Logo } from './Logo/Logo';
2223
import { Tabs } from './Tabs';
@@ -100,6 +101,7 @@ export const PageHeader: FC<
100101
{children}
101102
</FlexBox>
102103
{Boolean(subRoutes?.length) && <PageTabs subRoutes={subRoutes} />}
104+
<GithubButton />
103105
</FlexBox>
104106
{showSelectorSection && (
105107
<>

web-server/src/components/PageTitleWrapper/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const PageTitleWrapper: FC<BoxProps> = ({ children, ...props }) => {
2828
className="MuiPageTitle-wrapper"
2929
{...props}
3030
p={4}
31-
py={{ lg: 2.5, xs: 2 }}
31+
py={{ lg: 2.6, xs: 2 }}
3232
sx={{ ...props.sx, marginBottom: `0 !important` }}
3333
>
3434
{children}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
import { FC } from 'react';
1+
import { FC, useEffect } from 'react';
22

33
import { useImageUpdateStatusWorker } from '@/hooks/useImageUpdateStatusWorker';
4+
import { getGithubRepoStars } from '@/slices/app';
5+
import { useDispatch } from '@/store';
46

57
export const TopLevelLogicComponent: FC = () => {
8+
const dispatch = useDispatch();
9+
10+
useEffect(() => {
11+
dispatch(getGithubRepoStars());
12+
}, [dispatch, getGithubRepoStars]);
13+
614
useImageUpdateStatusWorker();
15+
716
return null;
817
};

web-server/src/slices/app.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ type Error = {
2828

2929
type Name = string;
3030
type ErrorMap = Record<Name, Error>;
31+
type GithubStarsAPIResponse = {
32+
stargazers_count: number;
33+
};
3134
export type SerializableDateRange = [string, string];
3235
export type NetworkType = 'slow-2g' | '2g' | '3g' | '4g';
3336

37+
const githubRepoApiUrl = `https://api.github.com/repos/middlewarehq/middleware`;
38+
3439
// This state contains things that will generally be persisted to localStorage
3540
type State = StateFetchConfig<{
3641
networkType: NetworkType;
@@ -51,6 +56,7 @@ type State = StateFetchConfig<{
5156
lastDisabledImageUpdateBannerAt: DateString | null;
5257
latestImageStatus: ImageStatusApiResponse | null;
5358
selectedIndustry: Industries;
59+
githubRepoStarsCount: number | null;
5460
}>;
5561

5662
export const DEFAULT_PR_TABLE_COLUMN_STATE_MAP = {
@@ -91,7 +97,8 @@ const initialState: State = {
9197
lastSyncedAt: null,
9298
lastDisabledImageUpdateBannerAt: null,
9399
latestImageStatus: null,
94-
selectedIndustry: Industries.ALL_INDUSTRIES
100+
selectedIndustry: Industries.ALL_INDUSTRIES,
101+
githubRepoStarsCount: null
95102
};
96103

97104
export const appSlice = createSlice({
@@ -244,6 +251,14 @@ export const appSlice = createSlice({
244251
}
245252
}
246253
);
254+
addFetchCasesToReducer(
255+
builder,
256+
getGithubRepoStars,
257+
'githubRepoStarsCount',
258+
(state, action) => {
259+
state.githubRepoStarsCount = action.payload;
260+
}
261+
);
247262
}
248263
});
249264

@@ -275,6 +290,15 @@ export const updateTeamBranchesMap = createAsyncThunk(
275290
}
276291
);
277292

293+
export const getGithubRepoStars = createAsyncThunk(
294+
'app/getGithubRepoStars',
295+
async () => {
296+
const res = await fetch(githubRepoApiUrl);
297+
const data: GithubStarsAPIResponse = await res.json();
298+
return data.stargazers_count;
299+
}
300+
);
301+
278302
const getSelectedTeam = (
279303
selectedTeam: State['singleTeam'],
280304
allTeams: State['allTeams']

0 commit comments

Comments
 (0)