-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55e8e1e
commit 8312790
Showing
11 changed files
with
282 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
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
70 changes: 70 additions & 0 deletions
70
packages/apps/dev-wallet/src/Components/ProfileChanger/ProfileChanger.css.ts
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,70 @@ | ||
import { recipe } from '@kadena/kode-ui'; | ||
import { atoms, globalStyle, token } from '@kadena/kode-ui/styles'; | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const profileClass = recipe({ | ||
base: [ | ||
atoms({ | ||
borderRadius: 'round', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
fontWeight: 'primaryFont.medium', | ||
cursor: 'pointer', | ||
}), | ||
{ | ||
border: 0, | ||
width: '32px', | ||
height: '32px', | ||
aspectRatio: '1/1', | ||
color: token('color.background.base.default'), | ||
willChange: 'transform', | ||
transition: 'transform .4s ease', | ||
selectors: { | ||
'&:hover': { | ||
opacity: '.8', | ||
}, | ||
}, | ||
}, | ||
], | ||
variants: { | ||
isActive: { | ||
true: { | ||
selectors: { | ||
'&:after': { | ||
content: '', | ||
position: 'absolute', | ||
backgroundColor: token( | ||
'color.background.accent.primary.inverse.default', | ||
), | ||
borderRadius: '50%', | ||
width: '10px', | ||
height: '10px', | ||
bottom: 0, | ||
right: 0, | ||
}, | ||
}, | ||
}, | ||
false: {}, | ||
}, | ||
}, | ||
}); | ||
|
||
export const profileListClass = style([ | ||
atoms({ | ||
position: 'relative', | ||
padding: 'no', | ||
margin: 'no', | ||
listStyleType: 'none', | ||
alignItems: 'center', | ||
cursor: 'pointer', | ||
gap: 'xs', | ||
}), | ||
]); | ||
|
||
globalStyle( | ||
`${profileListClass}:hover ${profileClass()}[data-hasMoreOptions="true"]`, | ||
{ | ||
background: 'green', | ||
transform: 'translateX(0)!important', | ||
}, | ||
); |
98 changes: 98 additions & 0 deletions
98
packages/apps/dev-wallet/src/Components/ProfileChanger/ProfileChanger.tsx
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,98 @@ | ||
import { useWallet } from '@/modules/wallet/wallet.hook'; | ||
import { IProfilePicked, unlockWithWebAuthn } from '@/utils/unlockWithWebAuthn'; | ||
import { recoverPublicKey, retrieveCredential } from '@/utils/webAuthn'; | ||
import { MonoMoreHoriz } from '@kadena/kode-icons/system'; | ||
import { | ||
Button, | ||
ContextMenu, | ||
ContextMenuDivider, | ||
ContextMenuItem, | ||
Stack, | ||
} from '@kadena/kode-ui'; | ||
import { FC } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { Profile } from './components/Profile'; | ||
import { profileClass, profileListClass } from './ProfileChanger.css'; | ||
|
||
const getInitial = (name: string): string => { | ||
return name.at(0)?.toUpperCase() ?? '-'; | ||
}; | ||
|
||
export const ProfileChanger: FC = () => { | ||
const { | ||
profileList, | ||
profile: currentProfile, | ||
unlockProfile, | ||
lockProfile, | ||
} = useWallet(); | ||
const navigate = useNavigate(); | ||
|
||
const handleSelect = async (profile: IProfilePicked) => { | ||
if (profile.options.authMode === 'WEB_AUTHN') { | ||
await unlockWithWebAuthn(profile, unlockProfile); | ||
} else { | ||
navigate(`/unlock-profile/${profile.uuid}`); | ||
} | ||
}; | ||
|
||
const filteredProfile = profileList.filter( | ||
(p) => p.uuid !== currentProfile?.uuid, | ||
)[0]; | ||
|
||
const hasMoreOptions = profileList.length > 2; | ||
|
||
return ( | ||
<Stack as="section" className={profileListClass}> | ||
{hasMoreOptions && ( | ||
<ContextMenu | ||
placement="bottom start" | ||
trigger={ | ||
<Button className={profileClass()}> | ||
<MonoMoreHoriz /> | ||
</Button> | ||
} | ||
> | ||
{profileList.map((profile) => { | ||
return ( | ||
<ContextMenuItem | ||
key={profile.uuid} | ||
label={profile.name} | ||
onClick={() => { | ||
handleSelect(profile); | ||
}} | ||
/> | ||
); | ||
})} | ||
<ContextMenuDivider /> | ||
<ContextMenuItem label="logout" onClick={lockProfile} /> | ||
</ContextMenu> | ||
)} | ||
|
||
{filteredProfile && ( | ||
<Profile | ||
key={filteredProfile.uuid} | ||
color={filteredProfile.accentColor} | ||
idx={hasMoreOptions ? 1 : 0} | ||
hasMoreOptions={hasMoreOptions} | ||
isActive={currentProfile?.uuid === filteredProfile.uuid} | ||
onClick={() => handleSelect(filteredProfile)} | ||
> | ||
{getInitial(filteredProfile.name)} | ||
</Profile> | ||
)} | ||
|
||
{currentProfile && ( | ||
<Profile | ||
key={currentProfile.uuid} | ||
color={currentProfile.accentColor} | ||
idx={hasMoreOptions ? 2 : 0} | ||
hasMoreOptions={hasMoreOptions} | ||
isActive={currentProfile?.uuid === currentProfile.uuid} | ||
onClick={() => handleSelect(currentProfile)} | ||
> | ||
{getInitial(currentProfile.name)} | ||
</Profile> | ||
)} | ||
</Stack> | ||
); | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/apps/dev-wallet/src/Components/ProfileChanger/components/Profile.tsx
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,40 @@ | ||
import { IProfile } from '@/modules/wallet/wallet.repository'; | ||
import { Stack } from '@kadena/kode-ui'; | ||
import { FC, PropsWithChildren } from 'react'; | ||
import { profileClass } from '../ProfileChanger.css'; | ||
|
||
interface IProps extends PropsWithChildren { | ||
idx?: number; | ||
color: string; | ||
isActive: boolean; | ||
onClick: () => void; | ||
hasMoreOptions: boolean; | ||
} | ||
|
||
export const Profile: FC<IProps> = ({ | ||
children, | ||
color, | ||
idx, | ||
isActive, | ||
onClick, | ||
hasMoreOptions, | ||
}) => { | ||
return ( | ||
<Stack | ||
as="button" | ||
data-hasMoreOptions={hasMoreOptions} | ||
onClick={() => onClick()} | ||
className={profileClass({ isActive })} | ||
style={{ | ||
backgroundColor: color, | ||
zIndex: idx ?? 0 + 1, | ||
transform: | ||
idx && hasMoreOptions | ||
? `translateX(-${60 * idx}%)` | ||
: `translateX(-100%)`, | ||
}} | ||
> | ||
{children} | ||
</Stack> | ||
); | ||
}; |
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,43 @@ | ||
import { | ||
IAccount, | ||
IWatchedAccount, | ||
} from '@/modules/account/account.repository'; | ||
import { IKeySource, IProfile } from '@/modules/wallet/wallet.repository'; | ||
import { recoverPublicKey, retrieveCredential } from '@/utils/webAuthn'; | ||
|
||
export type IProfilePicked = Pick< | ||
IProfile, | ||
'name' | 'uuid' | 'accentColor' | 'options' | ||
>; | ||
|
||
type IUnlockType = ( | ||
uuid: string, | ||
key: string, | ||
) => Promise<{ | ||
profile: IProfile; | ||
accounts: Array<IAccount>; | ||
watchedAccounts: Array<IWatchedAccount>; | ||
keySources: IKeySource[]; | ||
} | null>; | ||
|
||
export const unlockWithWebAuthn = async ( | ||
profile: IProfilePicked, | ||
unlockProfile: IUnlockType, | ||
) => { | ||
if (profile.options.authMode !== 'WEB_AUTHN') { | ||
throw new Error('Profile does not support WebAuthn'); | ||
} | ||
const credentialId = profile.options.webAuthnCredential; | ||
const credential = await retrieveCredential(credentialId); | ||
if (!credential) { | ||
throw new Error('Failed to retrieve credential'); | ||
} | ||
const keys = await recoverPublicKey(credential); | ||
for (const key of keys) { | ||
const result = await unlockProfile(profile.uuid, key); | ||
if (result) { | ||
return; | ||
} | ||
} | ||
console.error('Failed to unlock profile'); | ||
}; |
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
Oops, something went wrong.