Skip to content

Commit b3c3b42

Browse files
authored
feat: use cache (calcom#27627)
1 parent 31c6bbb commit b3c3b42

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

apps/web/modules/ee/support/lib/intercom/useIntercom.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,20 @@ export const useIntercom = () => {
2929
const hookData = useIntercomHook();
3030
const isMobile = useMediaQuery("(max-width: 768px)");
3131
const { data } = trpc.viewer.me.get.useQuery();
32-
const utils = trpc.useUtils();
32+
const { data: statsData } = trpc.viewer.me.myStats.useQuery(undefined, {
33+
staleTime: 30 * 60 * 1000, // 30 minutes
34+
gcTime: 30 * 60 * 1000, // 30 minutes
35+
trpc: {
36+
context: {
37+
skipBatch: true,
38+
},
39+
},
40+
});
3341
const { hasPaidPlan, plan } = useHasPaidPlan();
3442
const { hasTeamPlan } = useHasTeamPlan();
3543

36-
const fetchStatsAndBoot = async () => {
37-
if (!data) return;
38-
39-
const statsData = await utils.viewer.me.myStats.fetch();
40-
44+
const boot = async () => {
45+
if (!data || !statsData) return;
4146
let userHash;
4247
const req = await fetch(`/api/support/hash`);
4348
const res = await req.json();
@@ -83,11 +88,8 @@ export const useIntercom = () => {
8388
};
8489

8590
const open = async () => {
86-
if (!data) return;
87-
88-
const statsData = await utils.viewer.me.myStats.fetch();
89-
9091
let userHash;
92+
9193
const req = await fetch(`/api/support/hash`);
9294
const res = await req.json();
9395
if (res?.hash) {
@@ -131,7 +133,7 @@ export const useIntercom = () => {
131133
});
132134
hookData.show();
133135
};
134-
return { ...hookData, open, boot: fetchStatsAndBoot };
136+
return { ...hookData, open, boot };
135137
};
136138

137139
declare global {
@@ -150,13 +152,23 @@ export const useBootIntercom = () => {
150152

151153
const { data: user } = trpc.viewer.me.get.useQuery();
152154
const isTieredSupportEnabled = flagMap["tiered-support-chat"];
155+
const { data: statsData } = trpc.viewer.me.myStats.useQuery(undefined, {
156+
staleTime: 30 * 60 * 1000, // 30 minutes
157+
gcTime: 30 * 60 * 1000, // 30 minutes
158+
trpc: {
159+
context: {
160+
skipBatch: true,
161+
},
162+
},
163+
});
153164
useEffect(() => {
154165
// not using useMediaQuery as it toggles between true and false
155166
const showIntercom = localStorage.getItem("showIntercom");
156167
if (
157168
!isInterComEnabled ||
158169
showIntercom === "false" ||
159170
!user ||
171+
!statsData ||
160172
(!hasPaidPlan && isTieredSupportEnabled)
161173
)
162174
return;
@@ -174,7 +186,7 @@ export const useBootIntercom = () => {
174186
window.dispatchEvent(new Event("support:ready"));
175187
}
176188
// eslint-disable-next-line react-hooks/exhaustive-deps
177-
}, [user, hasPaidPlan, isTieredSupportEnabled]);
189+
}, [user, statsData, hasPaidPlan, isTieredSupportEnabled]);
178190
};
179191

180192
export default useIntercom;

0 commit comments

Comments
 (0)