Skip to content

Commit

Permalink
fix(hounddog): copy houndog logic to store getters (#3217)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmcg committed May 18, 2022
1 parent 91c0b02 commit 458b256
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
2 changes: 1 addition & 1 deletion store/friends/__snapshots__/state.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`init should return the initial settings state 1`] = `
Object {
"activeConversation": null,
"activeConversation": undefined,
"all": Array [],
"incomingRequests": Array [],
"outgoingRequests": Array [],
Expand Down
96 changes: 96 additions & 0 deletions store/friends/getters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { FriendsState } from './types'
import { Friend } from '~/types/ui/friends'
import { RootState } from '~/types/store/store'

const getters = {
/**
* @name findFriend
* @description Find friends based on identifiers {name,address,textilePubkey}
* @argument identifier Identifier for the search (query)
* @returns object containing an active friend in the form of the Friend interface if an active friend are found, returns undefined if no values satisfy the query
*/
findFriend:
(state: FriendsState) =>
(
identifier:
| Friend['name']
| Friend['address']
| Friend['textilePubkey']
| string,
): Friend | undefined => {
return state.all.find((f) => {
return (
f.name === identifier ||
f.address === identifier ||
f.account?.accountId === identifier ||
f.textilePubkey === identifier
)
})
},
/**
* @name findFriendByAddress
* @description Find friends who are currently active by address
* @argument {string} address Address as the identifier for the search (query)
* @returns object containing an active friend in the form of the Friend interface if an active friend are found, returns undefined if no values satisfy the query
*/
findFriendByAddress:
(state: FriendsState) =>
(address: string): Friend | undefined => {
return state.all.find((fr: Friend) => fr.address === address)
},

/**
* @name getActiveFriend
* @description Find friends who are currently active
* @returns object containing an active friend in the form of the Friend interface if an active friend are found, returns undefined if no values satisfy the query
*/
getActiveFriend: (state: FriendsState): Friend | undefined => {
return state.all.find((f) => f.state === 'online')
},

/** @function
* Determine the existence of a friend
* @name friendExists
* @argument address Address of the friend you are looking for
* @returns True if friend is found (exists), False if friend is not found (does not exist)
*/
friendExists:
(state: FriendsState) =>
(address: string): Boolean => {
return state.all.some((fr: Friend) => fr.address === address)
},

/**
* @name matchesActiveCall
* @description Find the first retrieved active call
* @returns object containing an active call in the form of the Friend interface if an active call are found, returns undefined if no active calls are found
*/
matchesActiveCall: (
state: FriendsState,
getters: any,
rootState: RootState,
): Friend | undefined => {
return state.all.find(
(friend: Friend) =>
friend.address === rootState.webrtc.activeCall?.peerId,
)
},

/**
* @name matchesSomeActiveCall
* @descriptionFind at minimum a single active call in the array (state), a single active call being found will return true; else it returns false
* @returns True if an active call are found, False if no active calls are found
*/
matchesSomeActiveCall: (
state: FriendsState,
getters: any,
rootState: RootState,
): Boolean => {
return state.all.some(
(friend: Friend) =>
friend.address === rootState.webrtc.activeCall?.peerId,
)
},
}

export default getters
2 changes: 1 addition & 1 deletion store/friends/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const InitialFriendsState: FriendsState = {
incomingRequests: [],
outgoingRequests: [],
all: [],
activeConversation: null,
activeConversation: undefined,
}

export default () => InitialFriendsState
4 changes: 4 additions & 0 deletions store/friends/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export interface FriendsState {
incomingRequests: IncomingRequest[]
outgoingRequests: OutgoingRequest[]
all: Friend[]
activeConversation?: {
type: string
target: Friend
}
}

export enum FriendsError {
Expand Down
1 change: 1 addition & 0 deletions types/ui/friends.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface Friend extends EncryptedFriend {
stored: Boolean
account: FriendAccount
address: string
name: string
// possibly break these out into different types. These optional fields come up in the friends list, add, request area
request?: IncomingRequest
photoHash?: string
Expand Down

0 comments on commit 458b256

Please sign in to comment.