Files
new-phone-who-dis/phone/src/apps/contacts/hooks/useContacts.ts
T
Taso c0509be2d1 Refactored Contacts to use Routes and not a Modal (#80)
The new route allows for other components to directly route to contacts/:id, where id is the contact id for a users given contacts. It also adds a getContact function accessible by the useConfig hook in case a contact needs to be retrieved using a contact id.
2021-02-09 09:59:54 -05:00

32 lines
895 B
TypeScript

import { useRecoilValue } from 'recoil';
import { contactsState } from './state';
import { Contact } from '../../../common/typings/contact';
import { useCallback } from 'react';
interface IUseContacts {
contacts: Contact[];
getDisplayByNumber: (number: string) => string;
getContact: (id: number) => Contact;
}
export const useContacts = (): IUseContacts => {
const contacts = useRecoilValue(contactsState.contacts);
const getDisplayByNumber = useCallback(
(number: string) => {
const found = contacts.find((contact) => contact.number === number);
return found ? found.display : number;
},
[contacts]
);
const getContact = useCallback(
(id: number): Contact | null => {
for (const contact of contacts) {
if (contact.id === id) return contact;
}
},
[contacts]
);
return { contacts, getDisplayByNumber, getContact };
};