-
-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(suite): refactor to new
@suite-common/contacts
package + use …
…Connect for verify
- Loading branch information
Showing
28 changed files
with
188 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "@suite-common/contacts", | ||
"version": "1.0.0", | ||
"private": true, | ||
"license": "See LICENSE.md in repo root", | ||
"sideEffects": false, | ||
"main": "src/index", | ||
"scripts": { | ||
"depcheck": "yarn g:depcheck", | ||
"type-check": "yarn g:tsc --build" | ||
}, | ||
"dependencies": { | ||
"@reduxjs/toolkit": "1.9.5", | ||
"@suite-common/suite-types": "workspace:*", | ||
"@trezor/connect": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const ADD = '@contacts/add'; | ||
export const REMOVE = '@contacts/remove'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export * as contactsActions from './redux/contactsActions'; | ||
export type { ContactsAction } from './redux/contactsActions'; | ||
export { prepareContactsReducer } from './redux/contactsReducer'; | ||
export * from './redux/contactsSelectors'; | ||
export * as CONTACTS from './constants'; | ||
export * from './types'; | ||
export * from './utils/findContact'; |
10 changes: 7 additions & 3 deletions
10
...uite/src/actions/suite/contactsActions.ts → ...mon/contacts/src/redux/contactsActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import { createAction } from '@reduxjs/toolkit'; | ||
|
||
import { Contact } from 'src/types/suite'; | ||
|
||
import { CONTACTS } from './constants'; | ||
import { Contact } from '../types'; | ||
import * as CONTACTS from '../constants'; | ||
|
||
export const addContact = createAction(CONTACTS.ADD, (payload: Contact) => ({ payload })); | ||
|
||
export const removeContact = createAction(CONTACTS.REMOVE, (payload: Contact) => ({ payload })); | ||
|
||
export const contactsActions = { | ||
addContact, | ||
removeContact, | ||
}; | ||
|
||
export type ContactsAction = ReturnType<typeof addContact> | ReturnType<typeof removeContact>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { TrezorDevice } from '@suite-common/suite-types'; | ||
import { createReducerWithExtraDeps } from '@suite-common/redux-utils'; | ||
|
||
import { Contact } from '../types'; | ||
import { contactsActions } from './contactsActions'; | ||
|
||
export type ContactsRootState = { | ||
contacts: ContactsState; | ||
}; | ||
|
||
type ContactsState = Contact[]; | ||
|
||
const initialState: ContactsState = []; | ||
|
||
const isEqual = (c1: Contact, c2: Contact) => | ||
c1.address === c2.address && c1.deviceState === c2.deviceState; | ||
|
||
export const getDeviceState = (device: TrezorDevice) => | ||
device.state?.staticSessionId?.split('@')?.[0]; | ||
|
||
export const prepareContactsReducer = createReducerWithExtraDeps(initialState, (builder, extra) => { | ||
builder | ||
.addCase(contactsActions.addContact, (state, { payload: contact }) => { | ||
const index = state.findIndex(c => isEqual(c, contact)); | ||
if (index >= 0) state[index] = contact; | ||
else state.push(contact); | ||
|
||
return state; | ||
}) | ||
.addCase(contactsActions.removeContact, (state, { payload }) => { | ||
return state.filter(c => !isEqual(c, payload)); | ||
}) | ||
.addMatcher( | ||
action => action.type === extra.actionTypes.storageLoad, | ||
extra.reducers.storageLoadContacts, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { TrezorDevice } from '@suite-common/suite-types'; | ||
|
||
import { ContactsRootState, getDeviceState } from './contactsReducer'; | ||
|
||
export const selectContacts = (state: ContactsRootState) => state.contacts; | ||
|
||
export const selectContactsForDevice = (device: TrezorDevice) => (state: ContactsRootState) => { | ||
const deviceState = getDeviceState(device); | ||
|
||
return deviceState ? state.contacts.filter(c => c.deviceState === deviceState) : []; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type Contact = { | ||
address: string; | ||
label: string; | ||
signature: string; | ||
deviceState: string; | ||
}; |
Oops, something went wrong.