From cb9947e4f246d3b1b85d728ecfa84884fc7d3bc9 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sun, 24 Nov 2024 00:59:59 +0100 Subject: [PATCH] refactor: Check darkmode in useWallets (#1640) * Check darkmode in useWallets * Check darkmode in useWalletImage --- components/wallet-card.js | 17 ++++------------- components/wallet-image.js | 14 ++++++++++++++ pages/settings/wallets/[wallet].js | 21 ++++++--------------- 3 files changed, 24 insertions(+), 28 deletions(-) create mode 100644 components/wallet-image.js diff --git a/components/wallet-card.js b/components/wallet-card.js index a964f8d6..14ae570c 100644 --- a/components/wallet-card.js +++ b/components/wallet-card.js @@ -7,8 +7,7 @@ import { Status, isConfigured } from '@/wallets/common' import DraggableIcon from '@/svgs/draggable.svg' import RecvIcon from '@/svgs/arrow-left-down-line.svg' import SendIcon from '@/svgs/arrow-right-up-line.svg' -import useDarkMode from './dark-mode' -import { useEffect, useState } from 'react' +import { useWalletImage } from '@/components/wallet-image' const statusToClass = status => { switch (status) { @@ -20,15 +19,7 @@ const statusToClass = status => { } export default function WalletCard ({ wallet, draggable, onDragStart, onDragEnter, onDragEnd, onTouchStart, sourceIndex, targetIndex, index }) { - const [dark] = useDarkMode() - const { card: { title, image } } = wallet.def - const [imgSrc, setImgSrc] = useState(image?.src) - - useEffect(() => { - if (!imgSrc) return - // wallet.png <-> wallet-dark.png - setImgSrc(dark ? image?.src.replace(/\.([a-z]{3})$/, '-dark.$1') : image?.src) - }, [dark]) + const image = useWalletImage(wallet) return (
{image - ? {title} - : {title}} + ? + : {wallet.def.card.title}}
diff --git a/components/wallet-image.js b/components/wallet-image.js new file mode 100644 index 00000000..f0d7a27c --- /dev/null +++ b/components/wallet-image.js @@ -0,0 +1,14 @@ +import useDarkMode from '@/components/dark-mode' + +export function useWalletImage (wallet) { + const [darkMode] = useDarkMode() + + const { title, image } = wallet.def.card + + if (!image) return null + + // wallet.png <-> wallet-dark.png + const src = darkMode ? image?.src.replace(/\.([a-z]{3})$/, '-dark.$1') : image?.src + + return { ...image, alt: title, src } +} diff --git a/pages/settings/wallets/[wallet].js b/pages/settings/wallets/[wallet].js index f69e93d5..b3bcca62 100644 --- a/pages/settings/wallets/[wallet].js +++ b/pages/settings/wallets/[wallet].js @@ -13,12 +13,12 @@ import { canReceive, canSend, isConfigured } from '@/wallets/common' import { SSR } from '@/lib/constants' import WalletButtonBar from '@/components/wallet-buttonbar' import { useWalletConfigurator } from '@/wallets/config' -import { useCallback, useEffect, useMemo, useState } from 'react' +import { useCallback, useMemo } from 'react' import { useMe } from '@/components/me' import validateWallet from '@/wallets/validate' import { ValidationError } from 'yup' import { useFormikContext } from 'formik' -import useDarkMode from '@/components/dark-mode' +import { useWalletImage } from '@/components/wallet-image' export const getServerSideProps = getGetServerSideProps({ authRequired: true }) @@ -29,8 +29,7 @@ export default function WalletSettings () { const wallet = useWallet(name) const { me } = useMe() const { save, detach } = useWalletConfigurator(wallet) - const [dark] = useDarkMode() - const [imgSrc, setImgSrc] = useState(wallet?.def.card?.image?.src) + const image = useWalletImage(wallet) const initial = useMemo(() => { const initial = wallet?.def.fields.reduce((acc, field) => { @@ -70,20 +69,12 @@ export default function WalletSettings () { } }, [wallet.def]) - const { card: { image, title, subtitle } } = wallet?.def || { card: {} } - - useEffect(() => { - if (!imgSrc) return - // wallet.png <-> wallet-dark.png - setImgSrc(dark ? image?.src.replace(/\.([a-z]{3})$/, '-dark.$1') : image?.src) - }, [dark]) - return ( {image - ? {title} - :

{title}

} -
{subtitle}
+ ? + :

{wallet.def.card.title}

} +
{wallet.def.card.subtitle}