mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 06:28:51 +00:00
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.
32 lines
895 B
TypeScript
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 };
|
|
};
|