Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support video avatars #3700

Merged
merged 8 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,11 +1059,19 @@ export default class Main extends BaseService<never> {
this.store.dispatch(updateAccountName({ ...addressOnNetwork, name }))
},
)

this.nameService.emitter.on(
"resolvedAvatar",
async ({ from: { addressOnNetwork }, resolved: { avatar } }) => {
const fileTypeResponse = await fetch(avatar, { method: "HEAD" })
const avatarType = fileTypeResponse.headers.get("Content-Type")

this.store.dispatch(
updateENSAvatar({ ...addressOnNetwork, avatar: avatar.toString() }),
updateENSAvatar({
...addressOnNetwork,
avatar: avatar.toString(),
avatarType: avatarType ?? undefined,
}),
)
},
)
Expand Down
9 changes: 6 additions & 3 deletions background/redux-slices/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type AccountData = {
ens: {
name?: DomainName
avatarURL?: URI
avatarType?: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: This is changing shape of the redux and in the extension we are persisting redux state between reloads. Usually we are adding redux migration but in this case I think we are fine because we are ok with this field being undefined. Anyway - I'll test the migration to make sure it is ok.

EDIT:
ok so we will need a migration unfortunately
Reproduction steps:

  • checkout main
  • add read-only account with avatar - expected: no avatar is loaded (not even the default one)
  • checkout this branch
  • reload extension background script

result: no avatar at all, not even the light green background
expected: there is a video avatar visible

image

}
defaultName: string
defaultAvatar: string
Expand Down Expand Up @@ -386,8 +387,10 @@ const accountSlice = createSlice({
updateENSAvatar: (
immerState,
{
payload: { address, network, avatar },
}: { payload: AddressOnNetwork & { avatar: URI } },
payload: { address, network, avatar, avatarType },
}: {
payload: AddressOnNetwork & { avatar: URI; avatarType?: string }
},
) => {
const normalizedAddress = normalizeEVMAddress(address)

Expand All @@ -412,7 +415,7 @@ const accountSlice = createSlice({

immerState.accountsData.evm[network.chainID][normalizedAddress] = {
...baseAccountData,
ens: { ...baseAccountData.ens, avatarURL: avatar },
ens: { ...baseAccountData.ens, avatarURL: avatar, avatarType },
}
},
/**
Expand Down
8 changes: 6 additions & 2 deletions background/redux-slices/selectors/accountsSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ export type AccountTotal = AddressOnNetwork & {
accountSigner: AccountSigner
name?: string
avatarURL?: string
avatarType?: string
localizedTotalMainCurrencyAmount?: string
}

Expand Down Expand Up @@ -458,6 +459,8 @@ function getNetworkAccountTotalsByCategory(
}
}

const { name, avatarURL, avatarType } = accountData.ens

return {
address,
network,
Expand All @@ -466,8 +469,9 @@ function getNetworkAccountTotalsByCategory(
signerId,
path,
accountSigner,
name: accountData.ens.name ?? accountData.defaultName,
avatarURL: accountData.ens.avatarURL ?? accountData.defaultAvatar,
name: name ?? accountData.defaultName,
avatarURL: avatarURL ?? accountData.defaultAvatar,
avatarType,
localizedTotalMainCurrencyAmount: formatCurrencyAmount(
mainCurrencySymbol,
getTotalBalance(accountData.balances, prices, mainCurrencySymbol),
Expand Down
32 changes: 20 additions & 12 deletions ui/components/Shared/SharedAccountItemSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ import { AccountTotal } from "@tallyho/tally-background/redux-slices/selectors"

import { useTranslation } from "react-i18next"
import SharedLoadingSpinner from "./SharedLoadingSpinner"
import SharedAvatar from "./SharedAvatar"

type AvatarProps = {
avatarURL?: string
avatarType?: string
}

function Avatar({ avatarURL, avatarType }: AvatarProps) {
return (
<SharedAvatar
avatarURL={avatarURL}
avatarType={avatarType}
width="48px"
backupAvatar="./images/avatar@2x.png"
/>
)
}

interface Props {
isSelected?: boolean
Expand All @@ -20,6 +37,7 @@ export default function SharedAccountItemSummary(props: Props): ReactElement {
shortenedAddress,
name,
avatarURL,
avatarType,
localizedTotalMainCurrencyAmount,
} = accountTotal

Expand All @@ -33,10 +51,10 @@ export default function SharedAccountItemSummary(props: Props): ReactElement {
<div className="left">
{isSelected ? (
<div className="avatar_selected_outline">
<div className="avatar" />
<Avatar avatarURL={avatarURL} avatarType={avatarType} />
</div>
) : (
<div className="avatar" />
<Avatar avatarURL={avatarURL} avatarType={avatarType} />
)}

<div className="info">
Expand Down Expand Up @@ -91,16 +109,6 @@ export default function SharedAccountItemSummary(props: Props): ReactElement {
padding: 5px 0;
overflow: hidden;
}
.avatar {
background: url("${avatarURL ?? "./images/avatar@2x.png"}") center
no-repeat;
background-color: var(--green-40);
background-size: cover;
width: 48px;
height: 48px;
border-radius: 12px;
flex-shrink: 0;
}
.avatar_selected_outline {
width: 52px;
height: 52px;
Expand Down
36 changes: 17 additions & 19 deletions ui/components/Shared/SharedAddressAvatar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import React, { ReactElement } from "react"
import SharedAvatar from "./SharedAvatar"

type SharedAddressAvatarProps = {
address: string
url?: string
avatarType?: string
}

/*
@TODO Switch to using our own resolution service, especially
Expand All @@ -7,25 +14,16 @@ once we upgrade our service to support whatever else Effigy can do.
export default function SharedAddressAvatar({
address,
url,
}: {
address: string
url?: string
}): ReactElement {
avatarType,
}: SharedAddressAvatarProps): ReactElement {
return (
<div className="avatar">
<style jsx>{`
.avatar {
width: 40px;
height: 40px;
background-color: var(--castle-black);
background-image: url("${typeof url !== "undefined"
? url
: `https://effigy.im/a/${address}.png`}");
background-size: cover;
border-radius: 999px;
flex-shrink: 0;
}
`}</style>
</div>
<SharedAvatar
width="40px"
background="var(--castle-black)"
avatarURL={url}
avatarType={avatarType}
backupAvatar={`https://effigy.im/a/${address}.png`}
borderRadius="999px"
/>
)
}
58 changes: 58 additions & 0 deletions ui/components/Shared/SharedAvatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React, { CSSProperties } from "react"

type SharedAvatarProps = {
width: string
background?: string
borderRadius?: string
avatarURL?: string
avatarType?: string
backupAvatar: string
style?: CSSProperties
}

export default function SharedAvatar({
width,
background = "var(--green-40)",
borderRadius = "12px",
avatarURL,
avatarType,
backupAvatar,
style,
}: SharedAvatarProps) {
return (
<>
{avatarType === "video/mp4" ? (
<div className="video" style={style}>
<video src={avatarURL} autoPlay muted loop />
</div>
) : (
<div className="avatar" style={style} />
)}
<style jsx>{`
.avatar {
width: ${width};
height: ${width};
border-radius: ${borderRadius};
flex-shrink: 0;
background-color: ${background};
background: url("${avatarURL ?? backupAvatar}");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
.video {
width: ${width};
height: ${width};
border-radius: ${borderRadius};
background-color: ${background};
overflow: hidden;
}
.video > video {
width: 100%;
height: 100%;
object-fit: cover;
}
`}</style>
</>
)
}
21 changes: 10 additions & 11 deletions ui/components/Shared/SharedCurrentAccountInformation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import React, { ReactElement } from "react"
import classNames from "classnames"
import SharedIcon from "./SharedIcon"
import { useAreInternalSignersUnlocked } from "../../hooks/signing-hooks"
import SharedAvatar from "./SharedAvatar"

type Props = {
shortenedAddress: string
name: string | undefined
avatarURL: string | undefined
avatarType: string | undefined
showHoverStyle: boolean
showLockStatus?: boolean
}
Expand All @@ -15,6 +17,7 @@ export default function SharedCurrentAccountInformation({
shortenedAddress,
name,
avatarURL,
avatarType,
showHoverStyle,
showLockStatus,
}: Props): ReactElement {
Expand All @@ -25,7 +28,13 @@ export default function SharedCurrentAccountInformation({
<span className="account_info_label ellipsis">
{name ?? shortenedAddress}
</span>
<div className="avatar" />
<SharedAvatar
avatarURL={avatarURL}
avatarType={avatarType}
backupAvatar="./images/portrait.png"
width="32px"
style={{ marginLeft: 8 }}
/>
{showLockStatus && (
<div data-testid="lock" className="lock_icon_wrap">
<SharedIcon
Expand All @@ -45,16 +54,6 @@ export default function SharedCurrentAccountInformation({
position: relative;
min-width: 0; // Allow the account address/name to collapse to an ellipsis.
}
.avatar {
border-radius: 12px;
width: 32px;
height: 32px;
margin-left: 8px;
background: url("${avatarURL ?? "./images/portrait.png"}");
background-color: var(--green-40);
background-size: cover;
flex-shrink: 0;
}
.hover:hover .account_info_label {
color: var(--trophy-gold);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe("SharedCurrentAccountInformation", () => {
name={name}
shortenedAddress=""
avatarURL={undefined}
avatarType={undefined}
/>,
)

Expand All @@ -28,6 +29,7 @@ describe("SharedCurrentAccountInformation", () => {
name={undefined}
shortenedAddress={shortenedAddress}
avatarURL={undefined}
avatarType={undefined}
/>,
)

Expand All @@ -40,6 +42,7 @@ describe("SharedCurrentAccountInformation", () => {
name={name}
shortenedAddress=""
avatarURL={undefined}
avatarType={undefined}
showLockStatus={false}
/>,
)
Expand All @@ -54,6 +57,7 @@ describe("SharedCurrentAccountInformation", () => {
name={name}
shortenedAddress=""
avatarURL={undefined}
avatarType={undefined}
showLockStatus
/>,
)
Expand All @@ -70,6 +74,7 @@ describe("SharedCurrentAccountInformation", () => {
name={name}
shortenedAddress=""
avatarURL={undefined}
avatarType={undefined}
showLockStatus
/>,
{
Expand Down
4 changes: 3 additions & 1 deletion ui/components/Signing/SigningNetworkAccountInfoTopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default function SigningNetworkAccountInfoTopBar({
accountTotal,
}: Props): ReactElement {
const { t } = useTranslation()
const { network, shortenedAddress, name, avatarURL } = accountTotal
const { network, shortenedAddress, name, avatarURL, avatarType } =
accountTotal

return (
<div className="top_bar_wrap standard_width">
Expand All @@ -29,6 +30,7 @@ export default function SigningNetworkAccountInfoTopBar({
shortenedAddress={shortenedAddress}
name={name}
avatarURL={avatarURL}
avatarType={avatarType}
/>
</div>
<style jsx>
Expand Down
3 changes: 2 additions & 1 deletion ui/components/TopMenu/TopMenuProfileButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function TopMenuProfileButton(props: {
}): ReactElement {
const { t } = useTranslation()
const dispatch = useDispatch()
const { name, avatarURL, address, accountType } =
const { name, avatarURL, avatarType, address, accountType } =
useBackgroundSelector(selectCurrentAccountTotal) ?? {}

const { truncatedAddress } = useBackgroundSelector(selectCurrentAccount) ?? {}
Expand Down Expand Up @@ -68,6 +68,7 @@ export default function TopMenuProfileButton(props: {
shortenedAddress={truncatedAddress}
name={name}
avatarURL={avatarURL}
avatarType={avatarType}
showHoverStyle
showLockStatus={accountType && isAccountWithSecrets(accountType)}
/>
Expand Down
Loading