Skip to content

Commit

Permalink
feat: add github access token settings
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Feb 18, 2021
1 parent 3af645c commit 166ad8a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
3 changes: 2 additions & 1 deletion core/core/src/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface TokenOptions {
figmaAccessToken: string;
figmaAccessToken?: string;
githubAccessToken?: string;
}
4 changes: 3 additions & 1 deletion examples/gatsby/.config/buildtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ module.exports = {
adminAPIKey: process.env.ALGOLIA_SEARCH_ADMIN_KEY,
},
},

tokens: {
githubAccessToken: process.env.GITHUB_AUTH_TOKEN,
},
webpack: (config = {}, options = {}) => {
return {
...config,
Expand Down
3 changes: 3 additions & 0 deletions examples/nextjs/.config/buildtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ module.exports = {
isCaseSensitive: true,
},
},
tokens: {
githubAccessToken: process.env.GITHUB_AUTH_TOKEN,
},
webpack: (config = {}, options = {}) => {
return {
...config,
Expand Down
11 changes: 9 additions & 2 deletions ui/blocks/src/ComponentContributors/ComponentContributors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
GithubAvatarUser,
} from '@component-controls/components';
import { useCustomProps } from '../context';
import { useConfig } from '@component-controls/store';

export interface ComponentContributorsProps {
caption?: string;
Expand All @@ -20,7 +21,8 @@ export const ComponentContributors: FC<ComponentContributorsProps &
'component_contributors',
rest,
);

const config = useConfig();
const { tokens } = config;
const contributors = useMemo(() => {
if (!component?.fileInfo?.commits?.length) {
return [];
Expand Down Expand Up @@ -51,7 +53,12 @@ export const ComponentContributors: FC<ComponentContributorsProps &
return !!contributors.length ? (
<div sx={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}>
{caption && <div sx={{ mr: 1 }}>{caption}</div>}
<GithubAvatarList users={contributors} size={24} {...props} />
<GithubAvatarList
githubAccessToken={tokens?.githubAccessToken}
users={contributors}
size={24}
{...props}
/>
</div>
) : null;
};
7 changes: 6 additions & 1 deletion ui/components/src/GithubAvatarList/GithubAvatarItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export interface GithubAvatarItemProps {
* whether to freeze the size of the avataro on hover
*/
freeze?: boolean;
/**
* to increase access rate for github user profile info
*/
githubAccessToken?: string;
}

/**
Expand All @@ -44,8 +48,9 @@ export const GithubAvatarItem: FC<GithubAvatarItemProps> = ({
size = 48,
overlap = 0.4,
freeze = false,
githubAccessToken,
}) => {
const profile = useGithubProfile(username, useremail);
const profile = useGithubProfile(username, useremail, githubAccessToken);
const profileBox = (
<div sx={{ p: 2 }}>
<div sx={{ fontSize: 4, fontWeight: 'bold' }}>
Expand Down
2 changes: 2 additions & 0 deletions ui/components/src/GithubAvatarList/GithubAvatarList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const GithubAvatarList: FC<GithubAvatarListProps> = ({
size = 32,
overlap = 0.4,
maxItems = 7,
githubAccessToken,
...rest
}) => {
const width = useMemo(() => {
Expand All @@ -55,6 +56,7 @@ export const GithubAvatarList: FC<GithubAvatarListProps> = ({
<GithubAvatarItem
key={`avatar_item${user.username}`}
size={size}
githubAccessToken={githubAccessToken}
username={user.username}
useremail={user.useremail}
overlap={overlap}
Expand Down
10 changes: 9 additions & 1 deletion ui/components/src/GithubAvatarList/useGithubProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const profilesCache: Record<string, GithubProfile> = {};
export const useGithubProfile = (
username: string,
useremail?: string,
githubToken?: string,
): GithubProfile => {
const [profile, setProfile] = useState<GithubProfile>(
profilesCache[username] || {
Expand All @@ -54,8 +55,15 @@ export const useGithubProfile = (
},
);
useEffect(() => {
const headers = githubToken
? {
Authorization: `token ${githubToken}`,
}
: undefined;
const fetchData = async () => {
fetch(`https://api.github.com/users/${username}`)
fetch(`https://api.github.com/users/${username}`, {
headers,
})
.then(res => res.json())
.then(result => {
profilesCache[username] = result;
Expand Down

0 comments on commit 166ad8a

Please sign in to comment.