Skip to content

Commit

Permalink
feat: make total stars configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
liby committed Feb 24, 2024
1 parent f7aae9d commit ffc2b51
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 33 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ Github Stats Card Options

| Name | Description | Default value |
| --- |------------------------------|---------------|
| `username` | GitHub username | '' |
| `show_rank` | Display rank value | `true` |
| `background` | Set background color/image. Supports a subset of CSS background property values | `#434343` |
| `color` | Set text color to any valid CSS color value | `white` |
| `include_all_commits` | Count all commits | `false` |
| `pixelate_avatar` | Apply pixelation to avatar | `true` |
| `screen_effect` | Enable curved screen effect | `false` |
| `show_avatar` | Display avatar | `true` |
| `pixelate_avatar` | Apply pixelation to avatar | `true` |
| `color` | Set text color to any valid CSS color value | `white` |
| `background` | Set background color/image. Supports a subset of CSS background property values | `#434343` |
| `show_rank` | Display rank value | `true` |
| `show_total_stars` | Display total stars earned | `true` |
| `username` | GitHub username | '' |

## Deploy on your own

Expand Down
19 changes: 11 additions & 8 deletions api/github-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import { clamp, fetchStats, renderStats } from 'pixel-profile'

export default async (req: VercelRequest, res: VercelResponse) => {
const {
username,
screen_effect,
show_avatar,
pixelate_avatar,
color,
background,
show_rank,
include_all_commits,
cache_seconds = `${CONSTANTS.CARD_CACHE_SECONDS}`,
color,
exclude_repo,
show
include_all_commits,
pixelate_avatar,
screen_effect,
show,
show_avatar,
show_rank,
show_total_stars,
username
} = req.query

res.setHeader('Content-Type', 'image/png')
Expand All @@ -23,6 +24,7 @@ export default async (req: VercelRequest, res: VercelResponse) => {
const showStats = parseArray(show)
const showAvatar = parseBoolean(show_avatar) ?? true
const showRank = parseBoolean(show_rank) ?? true
const showTotalStars = parseBoolean(show_total_stars) ?? true
const includeAllCommits = parseBoolean(include_all_commits)

const stats: Parameters<typeof renderStats>[0] = await fetchStats(
Expand All @@ -36,6 +38,7 @@ export default async (req: VercelRequest, res: VercelResponse) => {

stats.avatarUrl = showAvatar ? stats.avatarUrl : ''
stats.rank = showRank ? stats.rank : null
stats.totalStars = showTotalStars ? stats.totalStars : null

let cacheSeconds = clamp(parseInt(parseString(cache_seconds) ?? '0', 10), CONSTANTS.SIX_HOURS, CONSTANTS.ONE_DAY)

Expand Down
11 changes: 6 additions & 5 deletions packages/pixel-profile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ Github Stats Card Options

| Name | Description | Default value |
| --------------------- | ------------------------------------------------------------------------------- | ------------- |
| `username` | GitHub username | '' |
| `show_rank` | Display rank value | `true` |
| `background` | Set background color/image. Supports a subset of CSS background property values | `#434343` |
| `color` | Set text color to any valid CSS color value | `white` |
| `include_all_commits` | Count all commits | `false` |
| `pixelate_avatar` | Apply pixelation to avatar | `true` |
| `screen_effect` | Enable curved screen effect | `false` |
| `show_avatar` | Display avatar | `true` |
| `pixelate_avatar` | Apply pixelation to avatar | `true` |
| `color` | Set text color to any valid CSS color value | `white` |
| `background` | Set background color/image. Supports a subset of CSS background property values | `#434343` |
| `show_rank` | Display rank value | `true` |
| `show_total_stars` | Display total stars earned | `true` |
| `username` | GitHub username | '' |

## Contribute

Expand Down
4 changes: 2 additions & 2 deletions packages/pixel-profile/src/cards/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import satori from 'satori'
export type Stats = {
name: string
username: string
totalStars: number
totalStars: number | null
totalCommits: number
totalIssues: number
totalPRs: number
Expand Down Expand Up @@ -54,7 +54,7 @@ export async function renderStats(stats: Stats, options: Options = {}): Promise<
const _stats = {
name,
avatar,
totalStars: kFormatter(totalStars),
totalStars: totalStars ? kFormatter(totalStars) : '',
totalCommits: kFormatter(totalCommits),
totalIssues: kFormatter(totalIssues),
totalPRs: kFormatter(totalPRs),
Expand Down
29 changes: 16 additions & 13 deletions packages/pixel-profile/src/templates/github-stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Rank } from '../utils'

export type Stats = {
name: string
totalStars: string
totalStars: string | null
totalCommits: string
totalPRs: string
totalIssues: string
Expand Down Expand Up @@ -110,18 +110,21 @@ export function makeGithubStats(stats: Stats, options: Options) {
paddingRight: avatar ? 40 : 0
}}
>
<div
style={{
display: 'flex',
alignItems: 'flex-start',
justifyContent: 'space-between',
flexDirection: 'row',
width: '100%'
}}
>
<div>Total Stars Earned: </div>
<div>{`${totalStars}`}</div>
</div>
{totalStars ? (
<div
style={{
display: 'flex',
alignItems: 'flex-start',
justifyContent: 'space-between',
flexDirection: 'row',
width: '100%'
}}
>
<div>Total Stars Earned: </div>
<div>{`${totalStars}`}</div>
</div>
) : null}

<div
style={{
display: 'flex',
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions packages/pixel-profile/test/github-stats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,23 @@ describe('Github stats', () => {
)
expect(png).toMatchImageSnapshot()
})

it('Render card with totalStars set to null', async () => {
const png = await renderStats({
name: 'LuciNyan',
username: 'username',
totalStars: null,
totalCommits: 99999,
totalIssues: 99,
totalPRs: 9,
contributedTo: 9999,
avatarUrl: '',
rank: {
level: 'S',
percentile: 0,
score: 0
}
})
expect(png).toMatchImageSnapshot()
})
})

0 comments on commit ffc2b51

Please sign in to comment.