|
| 1 | +import { useEffect } from 'react'; |
| 2 | +import { useHistory, useLocation } from 'react-router-dom'; |
| 3 | + |
| 4 | +import { c } from 'ttag'; |
| 5 | + |
| 6 | +import { useUserKeys } from '@proton/account/userKeys/hooks'; |
| 7 | +import type { ContactEditProps } from '@proton/components/containers/contacts/edit/ContactEditModal'; |
| 8 | +import type { ContactGroupEditProps } from '@proton/components/containers/contacts/group/ContactGroupEditModal'; |
| 9 | +import useConfig from '@proton/components/hooks/useConfig'; |
| 10 | +import useNotifications from '@proton/components/hooks/useNotifications'; |
| 11 | +import type { PrivateKeyReference } from '@proton/crypto'; |
| 12 | +import { CONTACT_SEARCH_PARAMS, isContactSearchParams } from '@proton/mail/hooks/autoOpenContacts/helper'; |
| 13 | +import { useGetContact } from '@proton/mail/store/contacts/contactHooks'; |
| 14 | +import { APPS } from '@proton/shared/lib/constants'; |
| 15 | +import { prepareVCardContact } from '@proton/shared/lib/contacts/decrypt'; |
| 16 | +import { getSearchParams } from '@proton/shared/lib/helpers/url'; |
| 17 | +import type { DecryptedKey } from '@proton/shared/lib/interfaces'; |
| 18 | +import { splitKeys } from '@proton/shared/lib/keys'; |
| 19 | + |
| 20 | +interface Props { |
| 21 | + onEdit: (props: ContactEditProps) => void; |
| 22 | + onGroupEdit: (props: ContactGroupEditProps) => void; |
| 23 | + onSelectGroupTab: () => void; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * This hook allows ET app to open the contact drawer on the mail app using a deeplink |
| 28 | + */ |
| 29 | +const useContactsDrawerFromURL = ({ onEdit, onGroupEdit, onSelectGroupTab }: Props) => { |
| 30 | + const { APP_NAME } = useConfig(); |
| 31 | + const history = useHistory(); |
| 32 | + const location = useLocation(); |
| 33 | + const [userKeysList] = useUserKeys(); |
| 34 | + const getContact = useGetContact(); |
| 35 | + const { createNotification } = useNotifications(); |
| 36 | + |
| 37 | + const handleEditContact = async (contactID: string, userKeysList: DecryptedKey<PrivateKeyReference>[]) => { |
| 38 | + // To edit a contact we first need to get the contact vCard |
| 39 | + const contact = await getContact(contactID); |
| 40 | + const { publicKeys, privateKeys } = splitKeys(userKeysList); |
| 41 | + |
| 42 | + const { vCardContact, errors } = await prepareVCardContact(contact, { |
| 43 | + publicKeys, |
| 44 | + privateKeys, |
| 45 | + }); |
| 46 | + |
| 47 | + if (errors.length > 0) { |
| 48 | + createNotification({ type: 'error', text: c('Error').t`Failed to open to contact` }); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + onEdit({ contactID: contactID, vCardContact }); |
| 53 | + }; |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + if (APP_NAME !== APPS.PROTONMAIL || !isContactSearchParams(location) || !userKeysList) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const params = getSearchParams(location.hash); |
| 61 | + |
| 62 | + const contactCreateMatch = CONTACT_SEARCH_PARAMS.CREATE_CONTACT in params; |
| 63 | + const contactEditMatch = CONTACT_SEARCH_PARAMS.EDIT_CONTACT in params; |
| 64 | + const contactGroupCreateMatch = CONTACT_SEARCH_PARAMS.CREATE_CONTACT_GROUP in params; |
| 65 | + const contactGroupEditMatch = CONTACT_SEARCH_PARAMS.EDIT_CONTACT_GROUP in params; |
| 66 | + |
| 67 | + // Check for group-related routes first to set the correct tab |
| 68 | + if (contactGroupCreateMatch || contactGroupEditMatch) { |
| 69 | + onSelectGroupTab(); |
| 70 | + } |
| 71 | + |
| 72 | + if (contactCreateMatch) { |
| 73 | + onEdit({}); |
| 74 | + } else if (contactGroupCreateMatch) { |
| 75 | + onGroupEdit({}); |
| 76 | + } else if (contactEditMatch && params[CONTACT_SEARCH_PARAMS.EDIT_CONTACT]) { |
| 77 | + void handleEditContact(params[CONTACT_SEARCH_PARAMS.EDIT_CONTACT], userKeysList); |
| 78 | + } else if (contactGroupEditMatch && params[CONTACT_SEARCH_PARAMS.EDIT_CONTACT_GROUP]) { |
| 79 | + onGroupEdit({ contactGroupID: params[CONTACT_SEARCH_PARAMS.EDIT_CONTACT_GROUP] }); |
| 80 | + } |
| 81 | + |
| 82 | + // remove hash params so that closing the modal will not trigger a modal reopen |
| 83 | + history.replace({ pathname: location.pathname, search: location.search }); |
| 84 | + }, [location.hash, handleEditContact, userKeysList, APP_NAME]); |
| 85 | +}; |
| 86 | + |
| 87 | +export default useContactsDrawerFromURL; |
0 commit comments