forked from coinbase/onchainkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ConnectWallet component (coinbase#720)
- Loading branch information
Showing
35 changed files
with
594 additions
and
260 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
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 |
---|---|---|
@@ -1,52 +1,16 @@ | ||
import { Children, useMemo } from 'react'; | ||
import { IdentityContext } from '../context'; | ||
import { Avatar } from './Avatar'; | ||
import { Name } from './Name'; | ||
import { Address } from './Address'; | ||
import type { IdentityReact } from '../types'; | ||
import { background, cn } from '../../styles/theme'; | ||
import { IdentityProvider } from './IdentityProvider'; | ||
import { IdentityLayout } from './IdentityLayout'; | ||
|
||
export function Identity({ | ||
address, | ||
children, | ||
className, | ||
schemaId, | ||
}: IdentityReact) { | ||
const value = useMemo(() => { | ||
return { | ||
address, | ||
schemaId, | ||
}; | ||
}, [address, schemaId]); | ||
|
||
const { avatar, name, addressComponent } = useMemo(() => { | ||
const childrenArray = Children.toArray(children); | ||
return { | ||
// @ts-ignore | ||
avatar: childrenArray.find(({ type }) => type === Avatar), | ||
// @ts-ignore | ||
name: childrenArray.find(({ type }) => type === Name), | ||
// @ts-ignore | ||
addressComponent: childrenArray.find(({ type }) => type === Address), | ||
}; | ||
}, [children]); | ||
|
||
return ( | ||
<IdentityContext.Provider value={value}> | ||
<div | ||
className={cn( | ||
background.default, | ||
'flex h-14 items-center space-x-4 px-2 py-1', | ||
className, | ||
)} | ||
data-testid="ockIdentity_container" | ||
> | ||
{avatar} | ||
<div className="flex flex-col"> | ||
{name} | ||
{addressComponent} | ||
</div> | ||
</div> | ||
</IdentityContext.Provider> | ||
<IdentityProvider address={address} schemaId={schemaId}> | ||
<IdentityLayout className={className}>{children}</IdentityLayout> | ||
</IdentityProvider> | ||
); | ||
} |
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,41 @@ | ||
import { Children, useMemo, type ReactNode } from 'react'; | ||
import { Avatar } from './Avatar'; | ||
import { Name } from './Name'; | ||
import { Address } from './Address'; | ||
import { background, cn } from '../../styles/theme'; | ||
|
||
type IdentityLayoutReact = { | ||
children: ReactNode; | ||
className?: string; | ||
}; | ||
|
||
export function IdentityLayout({ children, className }: IdentityLayoutReact) { | ||
const { avatar, name, addressComponent } = useMemo(() => { | ||
const childrenArray = Children.toArray(children); | ||
return { | ||
// @ts-ignore | ||
avatar: childrenArray.find(({ type }) => type === Avatar), | ||
// @ts-ignore | ||
name: childrenArray.find(({ type }) => type === Name), | ||
// @ts-ignore | ||
addressComponent: childrenArray.find(({ type }) => type === Address), | ||
}; | ||
}, [children]); | ||
|
||
return ( | ||
<div | ||
className={cn( | ||
background.default, | ||
'flex items-center space-x-4 px-2 py-1', | ||
className, | ||
)} | ||
data-testid="ockIdentity_container" | ||
> | ||
{avatar} | ||
<div className="flex flex-col"> | ||
{name} | ||
{addressComponent} | ||
</div> | ||
</div> | ||
); | ||
} |
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,39 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import React from 'react'; | ||
import '@testing-library/jest-dom'; | ||
import { render, renderHook } from '@testing-library/react'; | ||
import type { Address } from 'viem'; | ||
import { IdentityProvider, useIdentityContext } from './IdentityProvider'; | ||
|
||
describe('IdentityProvider', () => { | ||
it('provides context values from props', () => { | ||
const address: Address = '0x1234567890abcdef1234567890abcdef12345678'; | ||
const schemaId: Address = '0xabcdefabcdefabcdefabcdefabcdefabcdef'; | ||
|
||
const { result } = renderHook(() => useIdentityContext(), { | ||
wrapper: ({ children }) => ( | ||
<IdentityProvider address={address} schemaId={schemaId}> | ||
{children} | ||
</IdentityProvider> | ||
), | ||
}); | ||
expect(result.current.address).toEqual(address); | ||
expect(result.current.schemaId).toEqual(schemaId); | ||
}); | ||
|
||
it('should return default context when no props are passed', () => { | ||
render( | ||
<IdentityProvider> | ||
<div /> | ||
</IdentityProvider>, | ||
); | ||
|
||
const { result } = renderHook(() => useIdentityContext(), { | ||
wrapper: IdentityProvider, | ||
}); | ||
expect(result.current.address).toEqual(''); | ||
expect(result.current.schemaId).toEqual(undefined); | ||
}); | ||
}); |
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,34 @@ | ||
import { type ReactNode, useState, createContext, useContext } from 'react'; | ||
import type { Address } from 'viem'; | ||
import { useValue } from '../../internal/hooks/useValue'; | ||
import type { IdentityContextType } from '../types'; | ||
|
||
const emptyContext = {} as IdentityContextType; | ||
|
||
export const IdentityContext = createContext<IdentityContextType>(emptyContext); | ||
|
||
export function useIdentityContext() { | ||
return useContext(IdentityContext); | ||
} | ||
|
||
type IdentityProvider = { | ||
address?: Address; | ||
children: ReactNode; | ||
schemaId?: Address | null; | ||
}; | ||
|
||
export function IdentityProvider(props: IdentityProvider) { | ||
const [address, setAddress] = useState(props.address ?? ('' as Address)); | ||
|
||
const value = useValue({ | ||
address, | ||
schemaId: props.schemaId, | ||
setAddress, | ||
}); | ||
|
||
return ( | ||
<IdentityContext.Provider value={value}> | ||
{props.children} | ||
</IdentityContext.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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
import { useMemo } from 'react'; | ||
|
||
export function useValue<T>(object: T): T { | ||
return useMemo(() => object, [object]); | ||
} |
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.