Skip to content

Commit b56eca8

Browse files
authored
refactor: move utils/helpers around, split into files (#1020)
1 parent a095161 commit b56eca8

37 files changed

+499
-496
lines changed

src/background/services/background.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import type { Browser, Runtime } from 'webextension-polyfill';
2-
import type { ToBackgroundMessage } from '@/shared/messages';
2+
import { failure, success, type ToBackgroundMessage } from '@/shared/messages';
33
import {
44
errorWithKeyToJSON,
5-
failure,
65
getNextOccurrence,
76
getWalletInformation,
87
isErrorWithKey,
9-
success,
108
} from '@/shared/helpers';
119
import { KeyAutoAddService } from '@/background/services/keyAutoAdd';
1210
import { OpenPaymentsClientError } from '@interledger/open-payments/dist/client/error';

src/background/services/paymentSession.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ import {
55
type OutgoingPaymentWithSpentAmounts as OutgoingPayment,
66
type WalletAddress,
77
} from '@interledger/open-payments';
8-
import {
9-
bigIntMax,
10-
convert,
11-
ErrorWithKey,
12-
sleep,
13-
transformBalance,
14-
} from '@/shared/helpers';
8+
import { ErrorWithKey, sleep, transformBalance } from '@/shared/helpers';
159
import {
1610
isInternalServerError,
1711
isInvalidReceiverError,
@@ -21,7 +15,7 @@ import {
2115
isOutOfBalanceError,
2216
isTokenExpiredError,
2317
} from '@/background/services/openPayments';
24-
import { getNextSendableAmount } from '@/background/utils';
18+
import { bigIntMax, convert, getNextSendableAmount } from '@/background/utils';
2519
import type {
2620
EventsService,
2721
OpenPaymentsService,

src/background/services/storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import type {
77
StorageKey,
88
WalletAmount,
99
} from '@/shared/types';
10-
import { bigIntMax, objectEquals, ThrottleBatch } from '@/shared/helpers';
11-
import { computeBalance } from '../utils';
10+
import { objectEquals, ThrottleBatch } from '@/shared/helpers';
11+
import { bigIntMax, computeBalance } from '../utils';
1212
import type { Cradle } from '../container';
1313

1414
const defaultStorage = {

src/background/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ export const toAmount = ({
6666
};
6767
};
6868

69+
export function bigIntMax<T extends bigint | AmountValue>(a: T, b: T): T {
70+
return BigInt(a) > BigInt(b) ? a : b;
71+
}
72+
6973
interface ExchangeRates {
7074
base: string;
7175
rates: Record<string, number>;
@@ -124,6 +128,14 @@ export const convertWithExchangeRate = <T extends AmountValue | bigint>(
124128
: (converted as T);
125129
};
126130

131+
export function convert(value: bigint, source: number, target: number) {
132+
const scaleDiff = target - source;
133+
if (scaleDiff > 0) {
134+
return value * BigInt(10 ** scaleDiff);
135+
}
136+
return value / BigInt(10 ** -scaleDiff);
137+
}
138+
127139
export const getTabId = (sender: Runtime.MessageSender): number => {
128140
return notNullOrUndef(notNullOrUndef(sender.tab, 'sender.tab').id, 'tab.id');
129141
};

src/content/__tests__/monetizationLinkManager.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { promisify } from 'node:util';
22
import { JSDOM } from 'jsdom';
33
import { MonetizationLinkManager } from '@/content/services/monetizationLinkManager';
4-
import { success, failure } from '@/shared/helpers';
4+
import { success, failure } from '@/shared/messages';
55
import type {
66
ContentToBackgroundMessage as Msg,
77
MessageManager,

src/content/keyAutoAdd/testWallet.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// cSpell:ignore nextjs
2-
import { errorWithKey, ErrorWithKey, sleep } from '@/shared/helpers';
2+
import {
3+
errorWithKey,
4+
ErrorWithKey,
5+
sleep,
6+
toWalletAddressUrl,
7+
} from '@/shared/helpers';
38
import {
49
KeyAutoAdd,
510
LOGIN_WAIT_TIMEOUT,
611
type StepRun as Run,
712
} from './lib/keyAutoAdd';
813
import { isTimedOut, waitForURL } from './lib/helpers';
914
import { revokeKey } from './lib/helpers/testWallet';
10-
import { toWalletAddressUrl } from '@/pages/shared/lib/utils';
1115

1216
// region: Steps
1317
type Account = {

src/content/services/contentScript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ToContentMessage } from '@/shared/messages';
22
import type { Cradle } from '@/content/container';
3-
import { failure, success } from '@/shared/helpers';
3+
import { failure, success } from '@/shared/messages';
44

55
export class ContentScript {
66
private browser: Cradle['browser'];

src/pages/app/pages/PostInstall.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import {
55
ExternalIcon,
66
} from '@/pages/shared/components/Icons';
77
import {
8-
cn,
98
getBrowserName,
109
getWalletInformation,
1110
type BrowserName,
1211
} from '@/shared/helpers';
1312
import { useBrowser, useTranslation } from '@/app/lib/context';
1413
import { ConnectWalletForm } from '@/popup/components/ConnectWalletForm';
14+
import { cn } from '@/pages/shared/lib/utils';
1515
import { useMessage } from '@/app/lib/context';
1616
import { useAppState } from '@/app/lib/store';
1717

src/pages/popup/components/ConnectWalletForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import {
1010
InputAmount,
1111
validateAmount,
1212
} from '@/pages/shared/components/InputAmount';
13-
import { toWalletAddressUrl } from '@/pages/shared/lib/utils';
13+
import { cn } from '@/pages/shared/lib/utils';
1414
import { useTranslation } from '@/popup/lib/context';
1515
import { deepClone } from 'valtio/utils';
1616
import {
17-
cn,
1817
errorWithKey,
1918
isErrorWithKey,
2019
sleep,
20+
toWalletAddressUrl,
2121
type ErrorWithKeyLike,
2222
} from '@/shared/helpers';
2323
import type { WalletAddress } from '@interledger/open-payments';

src/pages/popup/components/PayWebsiteForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import React from 'react';
22
import { Button } from '@/pages/shared/components/ui/Button';
33
import { InputAmount } from '@/pages/shared/components/InputAmount';
44
import { FadeInOut } from '@/pages/shared/components/FadeInOut';
5-
import { roundWithPrecision } from '@/pages/shared/lib/utils';
6-
import { cn, type ErrorWithKeyLike } from '@/shared/helpers';
5+
import { roundWithPrecision, cn } from '@/pages/shared/lib/utils';
6+
import type { ErrorWithKeyLike } from '@/shared/helpers';
77
import { useMessage, useTranslation } from '@/popup/lib/context';
88
import { usePopupState } from '@/popup/lib/store';
99

0 commit comments

Comments
 (0)