Skip to content

Commit

Permalink
refactor wallet avatar to have cache (#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanmino authored Sep 18, 2023
1 parent c6cf622 commit 2bf5f48
Show file tree
Hide file tree
Showing 39 changed files with 260 additions and 159 deletions.
70 changes: 35 additions & 35 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,40 +100,40 @@ jobs:
- name: Fail if any tests failed
if: steps.FFE2eParallel.outcome == 'failure'
run: exit 1
firefox-e2e-swap:
runs-on: swaps-runner-bx
timeout-minutes: 25
needs: [build]
env:
DISPLAY: :0
VITEST_SEGFAULT_RETRY: 4
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/firefoxTestsSetup
with:
gh-access-token: ${{ secrets.DOTENV_GITHUB_ACCESS_TOKEN }}
- name: Run e2e Swap (Firefox)
id: FFE2eSwap
continue-on-error: true
uses: nick-fields/retry@v2
with:
timeout_minutes: 25
max_attempts: 2
command: |
export BROWSER=firefox
export OS=linux
export FIREFOX_BIN=/opt/hostedtoolcache/firefox/latest-devedition/x64/firefox
yarn firefox:manifest && yarn firefox:zip
yarn vitest:swap --retry=5
- name: Upload deps artifacts
if: steps.FFE2eSwap.outcome == 'failure'
uses: actions/upload-artifact@v3
with:
name: screenshots
path: screenshots/
- name: Fail if any tests failed
if: steps.FFE2eSwap.outcome == 'failure'
run: exit 1
# firefox-e2e-swap:
# runs-on: swaps-runner-bx
# timeout-minutes: 25
# needs: [build]
# env:
# DISPLAY: :0
# VITEST_SEGFAULT_RETRY: 4
# steps:
# - uses: actions/checkout@v3
# - uses: ./.github/actions/firefoxTestsSetup
# with:
# gh-access-token: ${{ secrets.DOTENV_GITHUB_ACCESS_TOKEN }}
# - name: Run e2e Swap (Firefox)
# id: FFE2eSwap
# continue-on-error: true
# uses: nick-fields/retry@v2
# with:
# timeout_minutes: 25
# max_attempts: 2
# command: |
# export BROWSER=firefox
# export OS=linux
# export FIREFOX_BIN=/opt/hostedtoolcache/firefox/latest-devedition/x64/firefox
# yarn firefox:manifest && yarn firefox:zip
# yarn vitest:swap --retry=5
# - name: Upload deps artifacts
# if: steps.FFE2eSwap.outcome == 'failure'
# uses: actions/upload-artifact@v3
# with:
# name: screenshots
# path: screenshots/
# - name: Fail if any tests failed
# if: steps.FFE2eSwap.outcome == 'failure'
# run: exit 1
firefox-e2e-send:
runs-on: send-runner-bx
timeout-minutes: 16
Expand Down Expand Up @@ -457,7 +457,7 @@ jobs:
run: yarn typecheck
cleanup:
runs-on: ubuntu-latest
needs: [firefox-e2e-parallel, firefox-e2e-swap, firefox-e2e-send, firefox-e2e-dappInteractions, chrome-e2e-parallel, chrome-e2e-swap, chrome-e2e-send, chrome-e2e-dappInteractions, unit-tests, ci-checks]
needs: [firefox-e2e-parallel, firefox-e2e-send, firefox-e2e-dappInteractions, chrome-e2e-parallel, chrome-e2e-swap, chrome-e2e-send, chrome-e2e-dappInteractions, unit-tests, ci-checks]
steps:
- uses: geekyeggo/delete-artifact@v2
with:
Expand Down
11 changes: 10 additions & 1 deletion e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { erc20ABI } from 'wagmi';

import { RAINBOW_TEST_DAPP } from '~/core/references/links';

const browser = process.env.BROWSER || 'chrome';
const isFirefox = browser === 'firefox';

// consts

const waitUntilTime = 20_000;
Expand Down Expand Up @@ -387,6 +390,12 @@ export async function typeOnTextInput({
text: number | string;
driver: WebDriver;
}) {
if (isFirefox) {
await clearInput({
id,
driver,
});
}
const element = await findElementByTestId({ id, driver });
await element.sendKeys(text);
}
Expand All @@ -399,7 +408,7 @@ export async function clearInput({
driver: WebDriver;
}) {
const element = await findElementByTestId({ id, driver });
await element.clear();
return await element.clear();
}

export async function getTextFromTextInput({
Expand Down
14 changes: 11 additions & 3 deletions src/core/resources/metadata/ensAvatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,21 @@ export async function resolve(name: string) {
}
}

export async function resolveEnsProfileQueryFunction({
queryKey: [{ addressOrName }],
}: QueryFunctionArgs<typeof ResolveEnsProfileQueryKey>) {
export const resolveEnsAvatar = ({
addressOrName,
}: {
addressOrName?: string;
}) => {
if (!addressOrName) return null;
return isAddress(addressOrName)
? reverseResolve(addressOrName)
: resolve(addressOrName);
};

export async function resolveEnsProfileQueryFunction({
queryKey: [{ addressOrName }],
}: QueryFunctionArgs<typeof ResolveEnsProfileQueryKey>) {
return resolveEnsAvatar({ addressOrName });
}

// ///////////////////////////////////////////////
Expand Down
26 changes: 26 additions & 0 deletions src/core/state/dominantColor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import create from 'zustand';

import { createStore } from '~/core/state/internal/createStore';

type ColorCacheStore = {
colorCache: Record<string, string | null>;
setColorCache: (imageUrl: string, color: string | null) => void;
};

export const colorCacheStore = createStore<ColorCacheStore>(
(set) => ({
colorCache: {},
setColorCache: (imageUrl, color) =>
set((state) => ({
colorCache: { ...state.colorCache, [imageUrl]: color },
})),
}),
{
persist: {
name: 'dominantColorStore',
version: 0,
},
},
);

export const useColorCacheStore = create(colorCacheStore);
43 changes: 43 additions & 0 deletions src/core/state/walletAvatar/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import create from 'zustand';

import { createStore } from '~/core/state/internal/createStore';

export interface WalletAvatar {
color?: string;
imageUrl?: string;
emoji?: string;
}

type WalletAvatarStore = {
walletAvatar: Record<string, WalletAvatar>;
setWalletAvatar: ({
addressOrName,
walletAvatar,
}: {
addressOrName: string;
walletAvatar: WalletAvatar;
}) => void;
};

export const walletAvatarStore = createStore<WalletAvatarStore>(
(set, get) => ({
walletAvatar: {},
setWalletAvatar: ({ addressOrName, walletAvatar }) => {
const { walletAvatar: oldWalletAvatar } = get();
set({
walletAvatar: {
...oldWalletAvatar,
[addressOrName]: walletAvatar,
},
});
},
}),
{
persist: {
name: 'walletAvatarStore',
version: 0,
},
},
);

export const useWalletAvatarStore = create(walletAvatarStore);
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export const AnimatedRoute = forwardRef((props: AnimatedRouteProps, ref) => {
(navigationType === 'POP' && state?.isBack !== false) || state?.isBack;

const { currentAddress } = useCurrentAddressStore();
const { avatar } = useAvatar({ address: currentAddress });
const { data: avatar } = useAvatar({ addressOrName: currentAddress });
const [urlSearchParams] = useSearchParams();
const hideBackButton = urlSearchParams.get('hideBack') === 'true';

Expand Down
2 changes: 1 addition & 1 deletion src/design-system/components/Toggle/Toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Toggle = ({
testId,
}: ToggleProps) => {
const { currentAddress } = useCurrentAddressStore();
const { avatar } = useAvatar({ address: currentAddress });
const { data: avatar } = useAvatar({ addressOrName: currentAddress });
return (
<Box testId={testId}>
<Switch
Expand Down
2 changes: 1 addition & 1 deletion src/entries/popup/components/AccountItem/AccountItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default function AccountItem({
/>
</Box>
)}
<WalletAvatar address={account} size={36} emojiSize="20pt" />
<WalletAvatar addressOrName={account} size={36} emojiSize="20pt" />
</Box>
</Column>
<Column>
Expand Down
2 changes: 1 addition & 1 deletion src/entries/popup/components/AddressPill/AddressPill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useWalletName } from '../../hooks/useWalletName';
import { Avatar } from '../Avatar/Avatar';

export default function AddressPill({ address }: { address: Address }) {
const { avatar, isFetched } = useAvatar({ address });
const { data: avatar, isFetched } = useAvatar({ addressOrName: address });
const { displayName } = useWalletName({ address });
return (
<Box
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const AppConnectionNudgeSheet = ({
<Stack space="24px" alignHorizontal="center">
<Box>
<WalletAvatar
address={currentAddress}
addressOrName={currentAddress}
size={44}
background="transparent"
mask={appConnectionSheetImageMask}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const AppConnectionWalletItem = React.forwardRef(
mask={
showChainBadge ? appConnectionWalletItemImageMask : null
}
address={address}
addressOrName={address}
size={36}
emojiSize="20pt"
background="transparent"
Expand Down
2 changes: 1 addition & 1 deletion src/entries/popup/components/CommandK/CommandKModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function CommandKModal({
const { closeCommandK, isCommandKVisible, setFinishedExiting } =
useCommandKStatus();
const { currentAddress: address } = useCurrentAddressStore();
const { avatar } = useAvatar({ address });
const { data: avatar } = useAvatar({ addressOrName: address });
const { currentTheme } = useCurrentThemeStore();

return (
Expand Down
4 changes: 2 additions & 2 deletions src/entries/popup/components/CommandK/CommandRows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const ShortcutRow = ({
if (isAddAsWatchedWalletRow || isSwitchToWalletRow) {
return (
<WalletAvatar
address={command.address || ''}
addressOrName={command.address || ''}
boxShadow="12px accent"
emojiPaddingTop="1px"
emojiSize="10pt"
Expand Down Expand Up @@ -336,7 +336,7 @@ export const WalletRow = ({
const Avatar = React.useMemo(
() => (
<WalletAvatar
address={address}
addressOrName={address}
boxShadow="12px accent"
emojiPaddingTop="1px"
emojiSize="10pt"
Expand Down
4 changes: 2 additions & 2 deletions src/entries/popup/components/ContextMenu/ContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface ContextMenuTriggerProps {
export const ContextMenuTrigger = (props: ContextMenuTriggerProps) => {
const { children, accentColor, asChild, disabled } = props;
const { address } = useAccount();
const { avatar } = useAvatar({ address });
const { data: avatar } = useAvatar({ addressOrName: address });
const triggerRef = useRef<HTMLDivElement>(null);

return (
Expand Down Expand Up @@ -149,7 +149,7 @@ const ContextMenuContentBody = React.forwardRef<
} = props;
const { currentTheme } = useCurrentThemeStore();
const { address } = useAccount();
const { avatar } = useAvatar({ address });
const { data: avatar } = useAvatar({ addressOrName: address });
return (
<AccentColorProvider
color={accentColor || avatar?.color || globalColors.blue60}
Expand Down
4 changes: 2 additions & 2 deletions src/entries/popup/components/DropdownMenu/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface DropdownMenuTriggerProps {
export function DropdownMenuTrigger(props: DropdownMenuTriggerProps) {
const { children, accentColor, asChild } = props;
const { address } = useAccount();
const { avatar } = useAvatar({ address });
const { data: avatar } = useAvatar({ addressOrName: address });
const triggerRef = useRef<HTMLButtonElement>(null);

return (
Expand Down Expand Up @@ -123,7 +123,7 @@ export const DropdownMenuContentBody = React.forwardRef<
} = props;
const { currentTheme } = useCurrentThemeStore();
const { address } = useAccount();
const { avatar } = useAvatar({ address });
const { data: avatar } = useAvatar({ addressOrName: address });
return (
<AccentColorProvider
color={accentColor || avatar?.color || globalColors.blue60}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const AccountToImportRows = ({
>
<Column width="content">
<WalletAvatar
address={address}
addressOrName={address}
size={36}
emojiSize={'16pt'}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/entries/popup/components/SwitchMenu/SwitchMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const SelectContent = React.forwardRef<HTMLDivElement, SelectContentProps>(
) {
const { currentTheme } = useCurrentThemeStore();
const { currentAddress } = useCurrentAddressStore();
const { avatar } = useAvatar({ address: currentAddress });
const { data: avatar } = useAvatar({ addressOrName: currentAddress });
return (
<SelectPrimitive.Portal>
<SelectPrimitive.Content
Expand Down
8 changes: 4 additions & 4 deletions src/entries/popup/components/WalletAvatar/WalletAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Avatar } from '../../components/Avatar/Avatar';
import { useAvatar } from '../../hooks/useAvatar';

export function WalletAvatar({
address,
addressOrName,
size,
emojiSize,
mask,
Expand All @@ -15,7 +15,7 @@ export function WalletAvatar({
emojiPaddingTop,
boxShadow,
}: {
address?: string;
addressOrName?: string;
size: number;
emojiSize?: TextStyles['fontSize'];
mask?: string;
Expand All @@ -24,7 +24,7 @@ export function WalletAvatar({
emojiPaddingTop?: BoxStyles['paddingTop'];
boxShadow?: BoxStyles['boxShadow'];
}) {
const { avatar, isFetched } = useAvatar({ address });
const { data: avatar, isFetched } = useAvatar({ addressOrName });

return (
<AccentColorProvider color={avatar?.color || '#000000'}>
Expand All @@ -39,7 +39,7 @@ export function WalletAvatar({
overflow: 'hidden',
}}
>
{isFetched && address ? (
{isFetched && addressOrName ? (
<>
{avatar?.imageUrl ? (
<Avatar.Image
Expand Down
2 changes: 1 addition & 1 deletion src/entries/popup/components/WatchWallet/WatchWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function RecommendedAccountRow({
<Columns>
<Column>
<Inline space="8px" alignHorizontal="left" alignVertical="center">
<WalletAvatar address={name} size={32} emojiSize={'16pt'} />
<WalletAvatar addressOrName={name} size={32} emojiSize={'16pt'} />
<Box justifyContent="flex-start" width="fit">
<AddressOrEns
size="14pt"
Expand Down
Loading

0 comments on commit 2bf5f48

Please sign in to comment.