-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show avatar in companion app (#506)
* render avatar address * make new route config look a little better * repair lockfile
- Loading branch information
1 parent
bd26c43
commit 1e548a3
Showing
7 changed files
with
166 additions
and
11 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,109 @@ | ||
import { Blockie, Select, selectStyles, TextInput } from '@zodiac/ui' | ||
import { getAddress } from 'ethers' | ||
import { useEffect, useState } from 'react' | ||
|
||
type Props = { | ||
value: string | ||
availableSafes?: string[] | ||
onChange(value: string): void | ||
} | ||
|
||
type Option = { | ||
value: string | ||
label: string | ||
} | ||
|
||
export const AvatarInput = ({ | ||
value, | ||
onChange, | ||
availableSafes = [], | ||
}: Props) => { | ||
const [pendingValue, setPendingValue] = useState(value) | ||
|
||
useEffect(() => { | ||
setPendingValue(value) | ||
}, [value]) | ||
|
||
const checksumAvatarAddress = validateAddress(pendingValue) | ||
|
||
if (availableSafes.length > 0 || checksumAvatarAddress) { | ||
return ( | ||
<Select | ||
allowCreate | ||
blurInputOnSelect | ||
isClearable | ||
label="Piloted Safe" | ||
clearLabel="Clear piloted Safe" | ||
dropdownLabel="View all available Safes" | ||
formatOptionLabel={SafeOptionLabel} | ||
placeholder="Paste an address or select from the list" | ||
classNames={selectStyles<{ value: string; label: string }>()} | ||
value={ | ||
checksumAvatarAddress !== '' | ||
? { | ||
value: checksumAvatarAddress, | ||
label: checksumAvatarAddress, | ||
} | ||
: undefined | ||
} | ||
options={availableSafes.map((address) => ({ | ||
value: address, | ||
label: address, | ||
}))} | ||
onChange={(option) => { | ||
if (option) { | ||
const sanitized = (option as Option).value | ||
.trim() | ||
.replace(/^[a-z]{3}:/g, '') | ||
|
||
if (validateAddress(sanitized)) { | ||
onChange(sanitized.toLowerCase()) | ||
} | ||
} else { | ||
onChange('') | ||
} | ||
}} | ||
isValidNewOption={(option) => { | ||
return !!validateAddress(option) | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
return ( | ||
<TextInput | ||
label="Piloted Safe" | ||
value={pendingValue} | ||
placeholder="Paste in Safe address" | ||
onChange={(ev) => { | ||
const sanitized = ev.target.value.trim().replace(/^[a-z]{3}:/g, '') | ||
setPendingValue(sanitized) | ||
if (validateAddress(sanitized)) { | ||
onChange(sanitized.toLowerCase()) | ||
} | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
const SafeOptionLabel = (option: Option) => { | ||
const checksumAddress = getAddress(option.value).toLowerCase() | ||
|
||
return ( | ||
<div className="flex items-center gap-4 py-2"> | ||
<Blockie address={option.value} className="size-5 shrink-0" /> | ||
|
||
<code className="overflow-hidden text-ellipsis whitespace-nowrap font-mono"> | ||
{checksumAddress} | ||
</code> | ||
</div> | ||
) | ||
} | ||
|
||
const validateAddress = (address: string) => { | ||
try { | ||
return getAddress(address).toLowerCase() | ||
} catch { | ||
return '' | ||
} | ||
} |
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 +1,2 @@ | ||
export { AvatarInput } from './AvatarInput' | ||
export { ChainSelect } from './ChainSelect' |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.