Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Click to chat button for WhatsApp #7396

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 62 additions & 35 deletions src/Components/Facility/DoctorVideoSlideover.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import React, { useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import SlideOver from "../../CAREUI/interactive/SlideOver";
import { getFacilityUsers } from "../../Redux/actions";
Expand All @@ -8,6 +8,7 @@ import CareIcon, { IconName } from "../../CAREUI/icons/CareIcon";
import { relativeTime } from "../../Utils/utils";
import useAuthUser from "../../Common/hooks/useAuthUser";
import { triggerGoal } from "../../Integrations/Plausible";
import { Warn } from "../../Utils/Notifications";

export default function DoctorVideoSlideover(props: {
show: boolean;
Expand Down Expand Up @@ -112,6 +113,64 @@ function UserListItem(props: { user: UserAssignedModel }) {
user.user_type === "Doctor" ? "l-user-md" : "l-user-nurse";
const authUser = useAuthUser();

function connectOnWhatsApp(e: React.MouseEvent<HTMLAnchorElement>) {
e.stopPropagation();
if (!user.alt_phone_number) return;
const phoneNumber = user.alt_phone_number;
const message = `Hey ${user.first_name} ${user.last_name}, I have a query regarding a patient.\n\nPatient Link: ${window.location.href}`;
const encodedMessage = encodeURIComponent(message);
const whatsappAppURL = `whatsapp://send?phone=${phoneNumber}&text=${encodedMessage}`;
const whatsappWebURL = `https://web.whatsapp.com/send?phone=${phoneNumber}&text=${encodedMessage}`;

const userAgent = navigator.userAgent;
const isEdge = /edge\/\d+/i.test(userAgent);
const isMobileFirefoxOrSafari =
/iPhone|iPad|iPod|Android/i.test(userAgent) &&
(/Firefox/i.test(userAgent) ||
(/Safari/i.test(userAgent) && !/Chrome/i.test(userAgent)));
const isSafari = /Safari/i.test(userAgent) && !/Chrome/i.test(userAgent);
const isMobile = /iPhone|iPad|iPod|Android/i.test(userAgent);

const openWhatsAppWebFallback = () => {
if (isMobile) {
Warn({
msg: "Please install WhatsApp to connect with the doctor",
});
}
window.open(whatsappWebURL, "_blank");
};

if (isEdge) {
if (navigator.msLaunchUri) {
navigator.msLaunchUri(whatsappAppURL, null, openWhatsAppWebFallback);
} else {
openWhatsAppWebFallback();
}
} else {
const attemptOpenWhatsApp = (url: string) => {
if (isMobileFirefoxOrSafari || isSafari) {
const iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.src = url;
document.body.appendChild(iframe);
} else {
window.location.href = url;
}
};

attemptOpenWhatsApp(whatsappAppURL);

const fallbackTimeout = setTimeout(() => {
openWhatsAppWebFallback();
}, 1250);

// Listen for when the window loses focus, indicating app launch success
window.addEventListener("blur", () => {
clearTimeout(fallbackTimeout);
});
}
}

return (
<li>
<li
Expand All @@ -126,20 +185,7 @@ function UserListItem(props: { user: UserAssignedModel }) {
role="option"
tabIndex={-1}
>
<a
href={
user.alt_phone_number
? `https://api.whatsapp.com/send/?phone=${encodeURIComponent(
user.alt_phone_number
)}&text=${encodeURIComponent(
`Hey ${user.first_name} ${user.last_name}, I have a query regarding a patient.\n\nPatient Link: ${window.location.href}`
)}`
: "#"
}
target="_blank"
rel="noopener noreferrer"
className="flex"
>
<a className="flex" onClick={connectOnWhatsApp}>
<div className="flex h-10 w-10 flex-none items-center justify-center">
{
// Show online icon based on last_login
Expand Down Expand Up @@ -178,26 +224,7 @@ function UserListItem(props: { user: UserAssignedModel }) {
</div>
</a>
)}
<a
href={
user.alt_phone_number
? `https://api.whatsapp.com/send/?phone=${encodeURIComponent(
user.alt_phone_number
)}&text=${encodeURIComponent(
`Hey ${user.first_name} ${user.last_name}, I have a query regarding a patient.\n\nPatient Link: ${window.location.href}`
)}`
: "#"
}
onClick={() => {
triggerGoal("Doctor Connect Click", {
medium: "WhatsApp",
userId: authUser?.id,
targetUserType: user.user_type,
});
}}
target="_blank"
rel="noopener noreferrer"
>
<a onClick={connectOnWhatsApp}>
<div className="tooltip">
<span className="tooltip-text tooltip-left">
Connect on WhatsApp
Expand Down
Loading