Skip to content

Commit

Permalink
[apache#4362] feat(web): Add GitHub stars and forks number for WebUI (a…
Browse files Browse the repository at this point in the history
…pache#5567)

### What changes were proposed in this pull request?

1. Add GitHub image, stars icon and fork icon on the Navbar.
2. Send GET request to GitHub API to fetch the number of stars and forks
and show on the Navbar
3. If the number of stars and forks more than 1000, it will show 1.x k.
4. Github image and stars icon can link to Gravitino repo, fork icon can
link to Gravitino fork page.
5. Position version text next to Gravitino logo.

### Why are the changes needed?

These change can improve user experience and efficiency.

Fix: apache#4362 

### Does this PR introduce _any_ user-facing change?

1. Add Github image, stars icon and fork icon.
2. Position version text next to Gravitino logo.

### How was this patch tested?

1. Check the data was fetched from GitHub API.
2. console.log the number more than 1000, manual check it will change to
1.x k.


![image](https://github.com/user-attachments/assets/1add9bfc-ae53-4492-96ef-c09b84f045a3)

---------

Co-authored-by: Xun <[email protected]>
Co-authored-by: Qian Xia <[email protected]>
  • Loading branch information
3 people authored Nov 21, 2024
1 parent 85e9918 commit af259c3
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 9 deletions.
20 changes: 20 additions & 0 deletions web/web/public/icons/git-fork.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions web/web/public/icons/github-mark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion web/web/src/app/rootLayout/AppBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import clsx from 'clsx'

import VersionView from './VersionView'
import LogoutButton from './Logout'
import GitHubInfo from './GitHubInfo'
import { useSearchParams } from 'next/navigation'
import { useRouter } from 'next/navigation'
import { useAppSelector, useAppDispatch } from '@/lib/hooks/useStore'
Expand Down Expand Up @@ -94,6 +95,7 @@ const AppBar = () => {
>
Gravitino
</Typography>
<VersionView />
</Link>
<Box className={'app-bar-content-right twc-flex twc-items-center'}>
<Stack direction='row' spacing={2} alignItems='center'>
Expand Down Expand Up @@ -135,7 +137,7 @@ const AppBar = () => {
</Select>
</FormControl>
) : null}
<VersionView />
<GitHubInfo />
<LogoutButton />
</Stack>
</Box>
Expand Down
78 changes: 78 additions & 0 deletions web/web/src/app/rootLayout/GitHubInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import Link from 'next/link'
import Image from 'next/image'
import { Box, Typography } from '@mui/material'
import { Star } from '@mui/icons-material'
import { useAppSelector } from '@/lib/hooks/useStore'

const GitHubInfo = () => {
const githubUrl = 'https://github.com/apache/gravitino'
const githubForkUrl = 'https://github.com/apache/gravitino/fork'
const githubLogoUrl = (process.env.NEXT_PUBLIC_BASE_PATH ?? '') + '/icons/github-mark.svg'
const forkLogoUrl = (process.env.NEXT_PUBLIC_BASE_PATH ?? '') + '/icons/git-fork.svg'
const store = useAppSelector(state => state.sys)

return (
<Box className={'twc-flex twc-gap-x-3 twc-bg-customs-lightBg twc-px-3 twc-py-2 twc-rounded-full'}>
<Link href={githubUrl}>
<Image
className={'twc-align-middle'}
src={githubLogoUrl}
overrideSrc={githubLogoUrl}
width={24}
height={24}
alt='logo'
/>
</Link>
<Box className={'twc-flex twc-items-center twc-gap-x-3 twc-ml-2'}>
<Link href={githubForkUrl} className={'twc-no-underline twc-bg-customs-dark twc-rounded-full '}>
<Typography
className={
'twc-flex twc-items-center twc-gap-2 twc-text-customs-black twc-px-2.5 twc-py-1 twc-text-[0.75rem] twc-font-bold'
}
>
<Image
className={'twc-align-middle twc-text-customs-white'}
src={forkLogoUrl}
overrideSrc={forkLogoUrl}
width={24}
height={24}
alt='logo'
/>
{store.forks} Forks
</Typography>
</Link>
<Link href={githubUrl} className={'twc-no-underline twc-bg-customs-dark twc-rounded-full '}>
<Typography
className={
'twc-flex twc-items-center twc-gap-2 twc-text-customs-black twc-px-2.5 twc-py-1 twc-text-[0.75rem] twc-font-bold'
}
>
<Star className={'twc-text-customs-black'} />
{store.stars} Stars
</Typography>
</Link>
</Box>
</Box>
)
}

export default GitHubInfo
2 changes: 1 addition & 1 deletion web/web/src/app/rootLayout/VersionView.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const VersionView = () => {
const store = useAppSelector(state => state.sys)

return (
<Typography variant='subtitle2' id='gravitino_version' className={'twc-flex twc-justify-end'}>
<Typography variant='subtitle2' id='gravitino_version' className={'twc-flex twc-justify-end twc-ml-2'}>
{store.version}
</Typography>
)
Expand Down
33 changes: 33 additions & 0 deletions web/web/src/lib/api/github/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { defHttp } from '@/lib/utils/axios'

const githubApis = {
GET: 'https://api.github.com/repos/apache/gravitino'
}

export const getGitHubApi = () => {
return defHttp.get({
url: `${githubApis.GET}`,
headers: {
Accept: 'application/vnd.github+json'
}
})
}
4 changes: 3 additions & 1 deletion web/web/src/lib/provider/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { useRouter } from 'next/navigation'
import { useSearchParams } from 'next/navigation'

import { useAppDispatch } from '@/lib/hooks/useStore'
import { initialVersion } from '@/lib/store/sys'
import { initialVersion, fetchGitHubInfo } from '@/lib/store/sys'

import { to } from '../utils'
import { getAuthConfigs, setAuthToken } from '../store/auth'
Expand Down Expand Up @@ -78,11 +78,13 @@ const AuthProvider = ({ children }) => {

if (authType === 'simple') {
dispatch(initialVersion())
dispatch(fetchGitHubInfo())
goToMetalakeListPage()
} else if (authType === 'oauth') {
if (token) {
dispatch(setAuthToken(token))
dispatch(initialVersion())
dispatch(fetchGitHubInfo())
goToMetalakeListPage()
} else {
router.push('/login')
Expand Down
45 changes: 39 additions & 6 deletions web/web/src/lib/store/sys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'

import { loggerVersion, to } from '@/lib/utils'
import { loggerVersion, to, formatNumber, loggerGitHubInfo } from '@/lib/utils'

import { getVersionApi } from '@/lib/api/version'

import { getGitHubApi } from '@/lib/api/github'

export const initialVersion = createAsyncThunk('sys/fetchVersion', async (params, { getState }) => {
let version = null
const [err, res] = await to(getVersionApi())
Expand All @@ -39,23 +41,54 @@ export const initialVersion = createAsyncThunk('sys/fetchVersion', async (params
return version
})

export const fetchGitHubInfo = createAsyncThunk('sys/fetchGitHubInfo', async (params, { getState }) => {
let stars = 0
let forks = 0
const [err, res] = await to(getGitHubApi())
if (err || !res) {
console.error('Error fetching repository status : ', err)
}

stars = formatNumber(res.stargazers_count)
forks = formatNumber(res.forks_count)
loggerGitHubInfo(stars, forks)

return {
stars: stars,
forks: forks
}
})

export const sysSlice = createSlice({
name: 'sys',
initialState: {
version: ''
version: '',
stars: 0,
forks: 0
},
reducers: {
setVersion(state, action) {
state.version = action.payload
},
setStars(state, action) {
state.stars = action.payload
},
setForks(state, action) {
state.forks = action.payload
}
},
extraReducers: builder => {
builder.addCase(initialVersion.fulfilled, (state, action) => {
state.version = action.payload.version
})
builder
.addCase(initialVersion.fulfilled, (state, action) => {
state.version = action.payload.version
})
.addCase(fetchGitHubInfo.fulfilled, (state, action) => {
state.stars = action.payload.stars
state.forks = action.payload.forks
})
}
})

export const { setVersion } = sysSlice.actions
export const { setVersion, setStars, setForks } = sysSlice.actions

export default sysSlice.reducer
17 changes: 17 additions & 0 deletions web/web/src/lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export const loggerVersion = version => {
)
}

export const loggerGitHubInfo = (stars, forks) => {
console.log(
`Gravitino GitHubInfo: %c Stars ${stars}, Forks ${forks}`,
`color: white; background-color: #6062E0; padding: 2px; border-radius: 4px;`
)
}

export const genUpdates = (originalData, newData) => {
const updates = []

Expand Down Expand Up @@ -227,3 +234,13 @@ export const findInTree = (tree, key, value) => {

return result
}

export const formatNumber = num => {
if (num >= 1000000) {
return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'm'
} else if (num >= 1000) {
return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'k'
}

return num.toString()
}

0 comments on commit af259c3

Please sign in to comment.