Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(recipient): enforce address for recent recipients #4691

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/recipients/recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ export function getRecipientVerificationStatus(
}
}

type PreparedRecipient = Recipient & {
type PreparedRecipient<T extends Recipient> = T & {
displayPrepared: Fuzzysort.Prepared | undefined
phonePrepared: Fuzzysort.Prepared | undefined
}

type FuzzyRecipient = Recipient | PreparedRecipient
type FuzzyRecipient<T extends Recipient> = T | PreparedRecipient<T>

const SCORE_THRESHOLD = -6000

Expand All @@ -226,19 +226,19 @@ const fuzzysortPreparedOptions = {
allowTypo: false,
}

function fuzzysortToRecipients(
fuzzyResults: Fuzzysort.KeysResults<FuzzyRecipient>
): FuzzyRecipient[] {
function fuzzysortToRecipients<T extends Recipient>(
fuzzyResults: Fuzzysort.KeysResults<FuzzyRecipient<T>>
): FuzzyRecipient<T>[] {
// This is the fastest way to map the 'obj' into a results array
// https://jsperf.com/set-iterator-vs-foreach/16
const result = []
const result: FuzzyRecipient<T>[] = []
for (let _len = fuzzyResults.length, _key = 0; _key < _len; _key++) {
result[_key] = fuzzyResults[_key].obj
}
return result
}

function nameCompare(a: FuzzyRecipient, b: FuzzyRecipient) {
function nameCompare<T extends Recipient>(a: FuzzyRecipient<T>, b: FuzzyRecipient<T>) {
const nameA = a.name?.toUpperCase() ?? ''
const nameB = b.name?.toUpperCase() ?? ''

Expand All @@ -250,16 +250,16 @@ function nameCompare(a: FuzzyRecipient, b: FuzzyRecipient) {
return 0
}

export function sortRecipients(recipients: Recipient[]) {
export function sortRecipients<T extends Recipient>(recipients: T[]) {
return recipients.sort(nameCompare)
}

function executeFuzzySearch(
recipients: FuzzyRecipient[],
function executeFuzzySearch<T extends Recipient>(
recipients: FuzzyRecipient<T>[],
query: string,
options: Fuzzysort.KeysOptions<FuzzyRecipient>,
options: Fuzzysort.KeysOptions<FuzzyRecipient<T>>,
shouldSort?: boolean
): FuzzyRecipient[] {
): FuzzyRecipient<T>[] {
const parsedQuery = query.replace(/[()-\s/\\]/g, '')
if (parsedQuery === '') {
// fuzzysort does not handle empty string query
Expand All @@ -277,7 +277,7 @@ export function filterRecipients(recipients: Recipient[], query: string, shouldS
return executeFuzzySearch(recipients, query, fuzzysortOptions, shouldSort)
}

export function filterRecipientFactory(recipients: Recipient[], shouldSort: boolean) {
export function filterRecipientFactory<T extends Recipient>(recipients: T[], shouldSort: boolean) {
const preparedRecipients = recipients.map((r) => ({
...r,
displayPrepared: fuzzysort.prepare(r.name!),
Expand Down
2 changes: 1 addition & 1 deletion src/send/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface PaymentInfo {

export interface State {
isSending: boolean
recentRecipients: Recipient[]
recentRecipients: (Recipient & { address: string })[]
// Keep a list of recent (last 24 hours) payments
recentPayments: PaymentInfo[]
inviteRewardsVersion: string
Expand Down
Loading