Skip to content

Commit

Permalink
TW-1148 Refactoring according to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
keshan3262 committed Nov 14, 2023
1 parent d238871 commit 6c6ca64
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 38 deletions.
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const config = {
'.+\\.tsx$': 'ts-jest'
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
setupFiles: ['dotenv/config', '@serh11p/jest-webextension-mock'],
setupFiles: ['dotenv/config', '@temple-wallet/jest-webextension-mock'],
setupFilesAfterEnv: ['./jest.setup.js']
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"@redux-devtools/remote": "^0.8.0",
"@reduxjs/toolkit": "^1.8.5",
"@rnw-community/shared": "^0.48.0",
"@serh11p/jest-webextension-mock": "4.0.0",
"@svgr/webpack": "6.4.0",
"@taquito/ledger-signer": "17.0.0",
"@taquito/local-forging": "17.0.0",
Expand All @@ -58,6 +57,7 @@
"@taquito/tzip16": "17.0.0",
"@taquito/utils": "17.0.0",
"@temple-wallet/dapp": "5.0.2",
"@temple-wallet/jest-webextension-mock": "^4.1.0",
"@temple-wallet/wallet-address-validator": "^0.4.3",
"@tezos-domains/core": "1.26.0",
"@tezos-domains/taquito-client": "1.26.0",
Expand Down
3 changes: 3 additions & 0 deletions public/_locales/uk/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@
"lock": {
"message": "Заблокувати"
},
"lockUpSettingsDescription": {
"message": "Розширення буде заблоковане після 5 хвилин відсутності активності."
},
"importedAccount": {
"message": "Імпортовані"
},
Expand Down
11 changes: 7 additions & 4 deletions src/app/templates/DelegateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { useTezosAddressByDomainName } from 'lib/temple/front/tzdns';
import { hasManager, isAddressValid, isKTAddress, mutezToTz, tzToMutez } from 'lib/temple/helpers';
import { TempleAccountType } from 'lib/temple/types';
import { useSafeState } from 'lib/ui/hooks';
import { delay } from 'lib/utils';
import { delay, fifoResolve } from 'lib/utils';
import { Link, useLocation } from 'lib/woozie';

import { useUserTestingGroupNameSelector } from '../store/ab-testing/selectors';
Expand Down Expand Up @@ -200,6 +200,11 @@ const DelegateForm: FC = () => {
return undefined;
}, [balanceNum, baseFee]);

const fifoValidateDelegate = useMemo(
() => fifoResolve((value: any) => validateDelegate(value, domainsClient, validateAddress)),
[domainsClient]
);

const handleFeeFieldChange = useCallback<BakerFormProps['handleFeeFieldChange']>(
([v]) => (maxAddFee && v > maxAddFee ? maxAddFee : v),
[maxAddFee]
Expand Down Expand Up @@ -312,9 +317,7 @@ const DelegateForm: FC = () => {
name="to"
as={<NoSpaceField ref={toFieldRef} />}
control={control}
rules={{
validate: (value: any) => validateDelegate(value, domainsClient, validateAddress)
}}
rules={{ validate: fifoValidateDelegate }}
onChange={([v]) => v}
onFocus={() => toFieldRef.current?.focus()}
textarea
Expand Down
36 changes: 17 additions & 19 deletions src/lib/i18n/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,32 @@ let fetchedLocaleMessages: FetchedLocaleMessages = {
let cldrLocale = cldrjsLocales.en;

export async function init() {
const refetched: FetchedLocaleMessages = {
target: null,
fallback: null
};

const saved = getSavedLocale();
const deflt = getDefaultLocale();
const native = getNativeLocale();

if (saved) {
const deflt = getDefaultLocale();
const [newTargetLocale, newFallbackLocale] = await Promise.all([
!areLocalesEqual(saved, deflt) ? fetchLocaleMessages(saved) : null,
fetchLocaleMessages(deflt)
]);

refetched.target = newTargetLocale;
refetched.fallback = newFallbackLocale;
}
const [target, fallback] = await Promise.all([
!saved || areLocalesEqual(saved, native) ? null : fetchLocaleMessages(saved),
areLocalesEqual(deflt, native) || (saved && areLocalesEqual(deflt, saved)) ? null : fetchLocaleMessages(deflt)
]);

fetchedLocaleMessages = refetched;
fetchedLocaleMessages = { target, fallback };
cldrLocale = (cldrjsLocales as Record<string, any>)[getCurrentLocale()] || cldrjsLocales.en;
}

export function getMessage(messageName: string, substitutions?: Substitutions) {
const val = fetchedLocaleMessages.target?.[messageName] ?? fetchedLocaleMessages.fallback?.[messageName];
const { target, fallback } = fetchedLocaleMessages;
const targetVal = target?.[messageName];

if (targetVal) return applySubstitutions(targetVal, substitutions);

const nativeVal = browser.i18n.getMessage(messageName, substitutions);

if (nativeVal && !target) return nativeVal;

if (val) return applySubstitutions(val, substitutions);
const fallbackVal = fallback?.[messageName];

return browser.i18n.getMessage(messageName, substitutions);
return fallbackVal ? applySubstitutions(fallbackVal, substitutions) : '';
}

export function getDateFnsLocale() {
Expand Down
9 changes: 1 addition & 8 deletions src/lib/temple/front/validate-delegate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { TaquitoTezosDomainsClient } from '@tezos-domains/taquito-client';
import memoize from 'micro-memoize';

import { t } from 'lib/i18n';
import { isDomainNameValid } from 'lib/temple/front';
import { isAddressValid } from 'lib/temple/helpers';
import { fifoResolve } from 'lib/utils';

function validateAnyAddress(value: string) {
switch (false) {
Expand All @@ -19,11 +17,6 @@ function validateAnyAddress(value: string) {
}
}

const fifoResolveNameToAddress = memoize(
(domainsClient: TaquitoTezosDomainsClient): ((name: string) => Promise<string | null>) =>
fifoResolve(domainsClient.resolver.resolveNameToAddress)
);

export const validateDelegate = async (
value: string | null | undefined,
domainsClient: TaquitoTezosDomainsClient,
Expand All @@ -34,7 +27,7 @@ export const validateDelegate = async (
if (!domainsClient.isSupported) return validateAddress(value);

if (isDomainNameValid(value, domainsClient)) {
const resolved = await fifoResolveNameToAddress(domainsClient)(value);
const resolved = await domainsClient.resolver.resolveNameToAddress(value);
if (!resolved) {
return validateAddress(value) || t('domainDoesntResolveToAddress', value);
}
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2310,11 +2310,6 @@
resolved "https://registry.yarnpkg.com/@rushstack/eslint-patch/-/eslint-patch-1.2.0.tgz#8be36a1f66f3265389e90b5f9c9962146758f728"
integrity sha512-sXo/qW2/pAcmT43VoRKOJbDOfV3cYpq3szSVfIThQXNt+E4DfKj361vaAt3c88U5tPUxzEswam7GW48PJqtKAg==

"@serh11p/[email protected]":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@serh11p/jest-webextension-mock/-/jest-webextension-mock-4.0.0.tgz#c3b2a00e8c758e156a4a922718b35183e9023af9"
integrity sha512-SQDFFOJGwQpaPopzYkFgzdDdi8tL8US/XM2UjBu4rmLLztTI/wOSecwXj/i3lhOUwOs3bHO2Zz9eRu2rPy5p9A==

"@sinclair/typebox@^0.24.1":
version "0.24.44"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.44.tgz#0a0aa3bf4a155a678418527342a3ee84bd8caa5c"
Expand Down Expand Up @@ -2796,6 +2791,11 @@
dependencies:
nanoid "^3.1.25"

"@temple-wallet/jest-webextension-mock@^4.1.0":
version "4.1.0"
resolved "https://registry.yarnpkg.com/@temple-wallet/jest-webextension-mock/-/jest-webextension-mock-4.1.0.tgz#6834acd98297eb1c76b85a2585a0584ff5578e8f"
integrity sha512-1D/3WpaJS9rk5SENnFB1RZ3oeYkrfDvCV4qyIYjAD6uWe6RHVzcGxQ8UPtSQ/MRrcqjT95q2VwDNxek+9z6+yw==

"@temple-wallet/wallet-address-validator@^0.4.3":
version "0.4.3"
resolved "https://registry.yarnpkg.com/@temple-wallet/wallet-address-validator/-/wallet-address-validator-0.4.3.tgz#4bb7905a824e290872b7a7fc449df2e8c332e9e1"
Expand Down

0 comments on commit 6c6ca64

Please sign in to comment.