1 import { useCallback, useEffect } from 'react';
3 import { type Action, type ThunkDispatch, createSelector } from '@reduxjs/toolkit';
5 import { baseUseDispatch, baseUseSelector } from '@proton/react-redux-store';
6 import type { ProtonThunkArguments } from '@proton/redux-shared-store-types';
7 import type { Contact } from '@proton/shared/lib/interfaces/contacts';
9 import { type ContactState, contactThunk, selectContact } from './contactSlice';
11 export const useGetContact = () => {
12 const dispatch = baseUseDispatch<ThunkDispatch<ContactState, ProtonThunkArguments, Action>>();
13 return useCallback((contactID: string) => dispatch(contactThunk({ contactID })), [dispatch]);
16 type Result = { value: Contact | undefined; loading: boolean };
18 const selector = createSelector(
19 [(state: ContactState) => selectContact(state), (_, id: string | undefined) => id],
20 (contactState, id): Result => {
21 const contact = contactState[id || ''];
23 return { value: undefined, loading: false };
27 loading: !contact.value,
32 export const useContact = (contactID?: string) => {
33 const dispatch = baseUseDispatch<ThunkDispatch<ContactState, ProtonThunkArguments, Action>>();
34 const selectedValue = baseUseSelector<ContactState, Result>((state) => selector(state, contactID));
39 dispatch(contactThunk({ contactID }));
41 return [selectedValue.value, selectedValue.loading] as const;