-
Notifications
You must be signed in to change notification settings - Fork 9
/
ContactPicker.jsx
45 lines (39 loc) · 1.03 KB
/
ContactPicker.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { h } from 'preact';
const ContactPicker = ({ value, setValue, className }) => {
if (!('contacts' in navigator) || !('ContactsManager' in window)) {
return;
}
let text;
if (value.toLowerCase().indexOf('text') !== -1) {
text = ['Do you want to text someone?', 'Write message to {contact}'];
}
if (value.toLowerCase().indexOf('call') !== -1) {
text = ['Do you want to call someone?', 'Call {contact}'];
}
if (!text) {
return;
}
return (
<button
onClick={async () => {
try {
const contact = await navigator.contacts.select(['name', 'tel'], {
multiple: false,
});
setValue(
text[1].replace(
'{contact}',
contact[0].name[0] + ' - ' + contact[0].tel[0]
)
);
} catch (ex) {
alert('Contact Pick failed');
}
}}
className={`text-sm text-gray-500 hover:text-black mt-2 ${className}`}
>
{text[0]}
</button>
);
};
export default ContactPicker;