From 4e5422ec613aa1b81b6f95bace6ddebc751489b4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 28 Mar 2022 15:29:39 -0500 Subject: [PATCH] SoapboxConfig: normalize cryptoAddresses --- .../crypto_donate/components/site_wallet.tsx | 29 +++--------- .../soapbox/__tests__/soapbox_config-test.js | Bin 349 -> 947 bytes .../normalizers/soapbox/soapbox_config.ts | 42 ++++++++++++++++-- app/soapbox/types/soapbox.ts | 17 ++++++- 4 files changed, 61 insertions(+), 27 deletions(-) diff --git a/app/soapbox/features/crypto_donate/components/site_wallet.tsx b/app/soapbox/features/crypto_donate/components/site_wallet.tsx index f0b6accd7d..1e09292bd3 100644 --- a/app/soapbox/features/crypto_donate/components/site_wallet.tsx +++ b/app/soapbox/features/crypto_donate/components/site_wallet.tsx @@ -1,4 +1,3 @@ -import { trimStart } from 'lodash'; import React from 'react'; import { Stack } from 'soapbox/components/ui'; @@ -6,36 +5,22 @@ import { useSoapboxConfig } from 'soapbox/hooks'; import CryptoAddress from './crypto_address'; -import type { Map as ImmutableMap, List as ImmutableList } from 'immutable'; - -type Address = ImmutableMap; - -// Address example: -// {"ticker": "btc", "address": "bc1q9cx35adpm73aq2fw40ye6ts8hfxqzjr5unwg0n", "note": "This is our main address"} -const normalizeAddress = (address: Address): Address => { - return address.update('ticker', '', ticker => { - return trimStart(ticker, '$').toLowerCase(); - }); -}; - interface ISiteWallet { limit?: number, } const SiteWallet: React.FC = ({ limit }): JSX.Element => { - const addresses: ImmutableList
= - useSoapboxConfig().cryptoAddresses.map(normalizeAddress); - - const coinList = typeof limit === 'number' ? addresses.take(limit) : addresses; + const { cryptoAddresses } = useSoapboxConfig(); + const addresses = typeof limit === 'number' ? cryptoAddresses.take(limit) : cryptoAddresses; return ( - {coinList.map(coin => ( + {addresses.map(address => ( ))} diff --git a/app/soapbox/normalizers/soapbox/__tests__/soapbox_config-test.js b/app/soapbox/normalizers/soapbox/__tests__/soapbox_config-test.js index f564fd17dd1c1315925a941281ce27ca4c6e8061..f0a021586c4806fb5113595a3bb14a267efa3c65 100644 GIT binary patch literal 947 zcmbVK%WA_g5WMFr_E4}5!A+Vz6H;h$YM~UG-byL5v`&=TmLn+Z zVm^fm6o%)jEPT?UV!;d*l{`YKdQ9$J;*W!UOvsU481JiY0W#YMcj>}>E_tJ_(;UTo*`YMHI3oy>!M+l~AvfA)oj%G%N~WX4>3 z68rs)Q>5vCDYeZVH5wVG$ep@-&DHv8T?86hLS+K&uG*~wPGli$PMV_jJHd~LG&88h X)5I^jo{6TjmuPMSTDg8ml}*++&zmv( delta 12 TcmdnYewS%OBqK|$rZpD;9BBiS diff --git a/app/soapbox/normalizers/soapbox/soapbox_config.ts b/app/soapbox/normalizers/soapbox/soapbox_config.ts index 0696eb314c..f866bdac3e 100644 --- a/app/soapbox/normalizers/soapbox/soapbox_config.ts +++ b/app/soapbox/normalizers/soapbox/soapbox_config.ts @@ -4,6 +4,13 @@ import { Record as ImmutableRecord, fromJS, } from 'immutable'; +import { trimStart } from 'lodash'; + +import type { + PromoPanelItem, + FooterItem, + CryptoAddress, +} from 'soapbox/types/soapbox'; const DEFAULT_COLORS = ImmutableMap({ gray: ImmutableMap({ @@ -47,6 +54,23 @@ const DEFAULT_COLORS = ImmutableMap({ 'sea-blue': '#2feecc', }); +export const PromoPanelItemRecord = ImmutableRecord({ + icon: '', + text: '', + url: '', +}); + +export const FooterItemRecord = ImmutableRecord({ + title: '', + url: '', +}); + +export const CryptoAddressRecord = ImmutableRecord({ + address: '', + note: '', + ticker: '', +}); + export const SoapboxConfigRecord = ImmutableRecord({ logo: '', banner: '', @@ -59,10 +83,10 @@ export const SoapboxConfigRecord = ImmutableRecord({ extensions: ImmutableMap(), greentext: false, promoPanel: ImmutableMap({ - items: ImmutableList(), + items: ImmutableList(), }), navlinks: ImmutableMap({ - homeFooter: ImmutableList(), + homeFooter: ImmutableList(), }), allowedEmoji: ImmutableList([ '👍', @@ -75,7 +99,7 @@ export const SoapboxConfigRecord = ImmutableRecord({ verifiedIcon: '', verifiedCanEditName: false, displayFqn: true, - cryptoAddresses: ImmutableList>(), + cryptoAddresses: ImmutableList(), cryptoDonatePanel: ImmutableMap({ limit: 1, }), @@ -89,6 +113,17 @@ export const SoapboxConfigRecord = ImmutableRecord({ type SoapboxConfigMap = ImmutableMap; +const normalizeCryptoAddress = (address: unknown): CryptoAddress => { + return CryptoAddressRecord(ImmutableMap(fromJS(address))).update('ticker', ticker => { + return trimStart(ticker, '$').toLowerCase(); + }); +}; + +const normalizeCryptoAddresses = (soapboxConfig: SoapboxConfigMap): SoapboxConfigMap => { + const addresses = ImmutableList(soapboxConfig.get('cryptoAddresses')); + return soapboxConfig.set('cryptoAddresses', addresses.map(normalizeCryptoAddress)); +}; + const normalizeColors = (soapboxConfig: SoapboxConfigMap): SoapboxConfigMap => { const colors = DEFAULT_COLORS.mergeDeep(soapboxConfig.get('colors')); return soapboxConfig.set('colors', colors); @@ -98,6 +133,7 @@ export const normalizeSoapboxConfig = (soapboxConfig: Record) => { return SoapboxConfigRecord( ImmutableMap(fromJS(soapboxConfig)).withMutations(soapboxConfig => { normalizeColors(soapboxConfig); + normalizeCryptoAddresses(soapboxConfig); }), ); }; diff --git a/app/soapbox/types/soapbox.ts b/app/soapbox/types/soapbox.ts index 865a6567c9..386734d3b5 100644 --- a/app/soapbox/types/soapbox.ts +++ b/app/soapbox/types/soapbox.ts @@ -1,5 +1,18 @@ -import { SoapboxConfigRecord } from 'soapbox/normalizers'; +import { + PromoPanelItemRecord, + FooterItemRecord, + CryptoAddressRecord, + SoapboxConfigRecord, +} from 'soapbox/normalizers/soapbox/soapbox_config'; +type PromoPanelItem = ReturnType; +type FooterItem = ReturnType; +type CryptoAddress = ReturnType; type SoapboxConfig = ReturnType; -export { SoapboxConfig }; +export { + PromoPanelItem, + FooterItem, + CryptoAddress, + SoapboxConfig, +};