-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/user-info
Signed-off-by: neil <[email protected]>
- Loading branch information
Showing
8 changed files
with
1,392 additions
and
1,208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { GraphQLClient, gql } from 'graphql-request'; | ||
|
||
const endpoint = '/api/graphql'; | ||
const graphQLClient = new GraphQLClient(endpoint); | ||
|
||
export async function fetchUserInfo() { | ||
const query = gql` | ||
query userinfo { | ||
currentUser { | ||
id | ||
name | ||
emailVerified | ||
loginBinds { | ||
account | ||
avatarUrl | ||
nickname | ||
provider | ||
} | ||
} | ||
} | ||
`; | ||
|
||
return await graphQLClient.request(query); | ||
} | ||
|
||
export async function signOut() { | ||
const query = gql` | ||
mutation signOut { | ||
signOut | ||
} | ||
`; | ||
return await graphQLClient.request(query); | ||
} | ||
|
||
export function findSpecifyProvider({ | ||
loginBinds, | ||
provider, | ||
}: { | ||
loginBinds?: any[]; | ||
provider?: string; | ||
}) { | ||
let providerUser; | ||
|
||
if (provider && loginBinds && loginBinds.length > 1) { | ||
providerUser = loginBinds?.find( | ||
(bindInfo) => bindInfo.provider === provider | ||
); | ||
} else { | ||
providerUser = loginBinds?.[0]; | ||
} | ||
|
||
if (providerUser) { | ||
providerUser = { | ||
...providerUser, | ||
// todo Let the backend modify | ||
// The naming of the returned fields in the interface data is reversed. | ||
account: providerUser?.nickname, | ||
nickname: providerUser?.account, | ||
}; | ||
} | ||
|
||
return providerUser || null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import jsCookie from 'js-cookie'; | ||
|
||
export const cookieKeys = { | ||
AUTH_PROVIDER: 'auth.provider', | ||
}; | ||
|
||
export const getAuthProvider = () => { | ||
return jsCookie.get(cookieKeys.AUTH_PROVIDER); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React, { useState } from 'react'; | ||
import Translate from '@docusaurus/Translate'; | ||
import { AiOutlineUser } from 'react-icons/ai'; | ||
import { MdOutlineLogout } from 'react-icons/md'; | ||
import { | ||
fetchUserInfo, | ||
signOut, | ||
findSpecifyProvider, | ||
} from '../../common/api/user'; | ||
import { getAuthProvider } from '../../common/cookie'; | ||
|
||
const User = () => { | ||
const provider = getAuthProvider() || 'github'; | ||
const [user, setUser] = useState(null); | ||
|
||
const info = findSpecifyProvider({ | ||
loginBinds: user?.currentUser?.loginBinds, | ||
provider, | ||
}); | ||
|
||
console.log(provider); | ||
console.log(info); | ||
|
||
React.useEffect(() => { | ||
fetchUserInfo().then((data: any) => { | ||
if (data && data.currentUser) { | ||
setUser(data); | ||
} | ||
}); | ||
}, []); | ||
|
||
if (!user) { | ||
return ( | ||
<a className={'ml-6 font-medium text-white'} href="/auth/signin"> | ||
<Translate id={'header.signin'} /> | ||
</a> | ||
); | ||
} | ||
|
||
return ( | ||
<div className="group relative flex h-full items-center pl-6 transition"> | ||
<div className="flex h-[32px] cursor-pointer items-center justify-center overflow-hidden rounded-full group-hover:bg-[#333333]"> | ||
<img src={info.avatarUrl!} width={32} height={32} alt="" /> | ||
</div> | ||
|
||
<div className="absolute top-[100%] -right-4 z-dropdown hidden w-auto group-hover:block"> | ||
<div className="mt-[2px] bg-black/90 text-white"> | ||
{/*<Link href="/settings/subscribe">*/} | ||
{/* <a className="flex cursor-pointer border-b border-white/20 py-4 pl-6 text-center last:border-b-0 hover:bg-[#333333]">*/} | ||
{/* {t('common:subscribe')}*/} | ||
{/* </a>*/} | ||
{/*</Link>*/} | ||
|
||
<a | ||
href="/settings/profile" | ||
className="flex cursor-pointer items-center whitespace-nowrap border-b border-white/20 py-4 px-6 text-center last:border-b-0 hover:bg-[#333333]" | ||
> | ||
<AiOutlineUser className="mr-2 text-base" /> | ||
<Translate id={'header.profile_setting'} /> | ||
</a> | ||
|
||
<div | ||
className="flex cursor-pointer items-center whitespace-nowrap border-b border-white/20 py-4 pl-6 text-center last:border-b-0 hover:bg-[#333333]" | ||
onClick={() => { | ||
signOut().then(() => { | ||
window.location.href = '/auth/signin'; | ||
}); | ||
}} | ||
> | ||
<MdOutlineLogout className="mr-2 text-base" /> | ||
<Translate id={'header.signout'} /> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.