2023-03-23 18:49:28 +00:00
|
|
|
import { noop } from "lodash";
|
2023-03-22 14:29:14 +00:00
|
|
|
import { useIntercom as useIntercomLib } from "react-use-intercom";
|
|
|
|
import { z } from "zod";
|
2022-05-17 19:31:49 +00:00
|
|
|
|
2023-03-22 14:29:14 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
|
|
|
import { CAL_URL } from "@calcom/lib/constants";
|
|
|
|
import { useHasTeamPlan, useHasPaidPlan } from "@calcom/lib/hooks/useHasPaidPlan";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
|
|
|
|
|
|
|
// eslint-disable-next-line turbo/no-undeclared-env-vars
|
|
|
|
export const isInterComEnabled = z.string().min(1).safeParse(process.env.NEXT_PUBLIC_INTERCOM_APP_ID).success;
|
|
|
|
|
|
|
|
const useIntercomHook = isInterComEnabled
|
|
|
|
? useIntercomLib
|
|
|
|
: () => {
|
|
|
|
return {
|
2022-05-18 16:05:25 +00:00
|
|
|
boot: noop,
|
|
|
|
show: noop,
|
2023-03-22 14:29:14 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useIntercom = () => {
|
|
|
|
const hookData = useIntercomHook();
|
|
|
|
const { data } = trpc.viewer.me.useQuery();
|
|
|
|
const { hasPaidPlan } = useHasPaidPlan();
|
|
|
|
const { hasTeamPlan } = useHasTeamPlan();
|
|
|
|
|
2023-04-19 20:22:07 +00:00
|
|
|
const open = async () => {
|
|
|
|
let userHash;
|
|
|
|
|
|
|
|
const req = await fetch(`/api/intercom-hash`);
|
|
|
|
const res = await req.json();
|
|
|
|
if (res?.hash) {
|
|
|
|
userHash = res.hash;
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:29:14 +00:00
|
|
|
hookData.boot({
|
|
|
|
name: data?.name ?? "",
|
|
|
|
email: data?.email,
|
|
|
|
userId: String(data?.id),
|
|
|
|
createdAt: String(dayjs(data?.createdDate).unix()),
|
2023-04-19 20:22:07 +00:00
|
|
|
...(userHash && { userHash }),
|
2023-03-22 14:29:14 +00:00
|
|
|
customAttributes: {
|
|
|
|
//keys should be snake cased
|
|
|
|
user_name: data?.username,
|
|
|
|
link: CAL_URL + "/" + data?.username,
|
|
|
|
identity_provider: data?.identityProvider,
|
|
|
|
timezone: data?.timeZone,
|
|
|
|
locale: data?.locale,
|
|
|
|
has_paid_plan: hasPaidPlan,
|
|
|
|
has_team_plan: hasTeamPlan,
|
|
|
|
metadata: data?.metadata,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
hookData.show();
|
|
|
|
};
|
|
|
|
return { ...hookData, open };
|
|
|
|
};
|
2022-05-17 19:31:49 +00:00
|
|
|
|
|
|
|
export default useIntercom;
|