Skip to content

Commit 28e787c

Browse files
committed
Turn toggleEnableFeedbackPanel async and check isIntercomAllowed
1 parent c1e8b8f commit 28e787c

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

packages/compass-intercom/src/setup-intercom.ts

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function setupIntercom(
1414
return;
1515
}
1616

17-
const { enableFeedbackPanel } = preferences.getPreferences();
17+
const { enableFeedbackPanel, networkTraffic } = preferences.getPreferences();
1818

1919
const intercomAppId = process.env.HADRON_METRICS_INTERCOM_APP_ID;
2020

@@ -25,11 +25,6 @@ export async function setupIntercom(
2525
return;
2626
}
2727

28-
if (!(await isIntercomAllowed())) {
29-
debug('skipping Intercom (not allowed)');
30-
return;
31-
}
32-
3328
const user = getActiveUser(preferences);
3429

3530
const metadata: IntercomMetadata = {
@@ -41,7 +36,19 @@ export async function setupIntercom(
4136
app_stage: process.env.NODE_ENV,
4237
};
4338

44-
if (enableFeedbackPanel) {
39+
async function toggleEnableFeedbackPanel(enableFeedbackPanel: boolean) {
40+
if (enableFeedbackPanel) {
41+
if (await isIntercomAllowed(networkTraffic)) {
42+
debug('loading intercom script');
43+
intercomScript.load(metadata);
44+
}
45+
} else {
46+
debug('unloading intercom script');
47+
intercomScript.unload();
48+
}
49+
}
50+
51+
if (enableFeedbackPanel && (await isIntercomAllowed(networkTraffic))) {
4552
// In some environment the network can be firewalled, this is a safeguard to avoid
4653
// uncaught errors when injecting the script.
4754
debug('testing intercom availability');
@@ -59,29 +66,18 @@ export async function setupIntercom(
5966
}
6067

6168
debug('intercom is reachable, proceeding with the setup');
69+
void toggleEnableFeedbackPanel(true);
6270
} else {
6371
debug(
64-
'not testing intercom connectivity because enableFeedbackPanel == false'
72+
'not testing intercom connectivity because enableFeedbackPanel == false || isAllowed == false'
6573
);
6674
}
6775

68-
const toggleEnableFeedbackPanel = (enableFeedbackPanel: boolean) => {
69-
if (enableFeedbackPanel) {
70-
debug('loading intercom script');
71-
intercomScript.load(metadata);
72-
} else {
73-
debug('unloading intercom script');
74-
intercomScript.unload();
75-
}
76-
};
77-
78-
toggleEnableFeedbackPanel(!!enableFeedbackPanel);
79-
8076
preferences.onPreferenceValueChanged(
8177
'enableFeedbackPanel',
8278
(enableFeedbackPanel) => {
8379
debug('enableFeedbackPanel changed');
84-
toggleEnableFeedbackPanel(enableFeedbackPanel);
80+
void toggleEnableFeedbackPanel(enableFeedbackPanel);
8581
}
8682
);
8783
}
@@ -92,18 +88,20 @@ export async function setupIntercom(
9288
*/
9389
let isIntercomAllowedResponse: null | Promise<boolean> = null;
9490

95-
function isIntercomAllowed(): Promise<boolean> {
91+
function isIntercomAllowed(allowNetworkTraffic = true): Promise<boolean> {
9692
if (!isIntercomAllowedResponse) {
97-
isIntercomAllowedResponse = fetchIntegrations().then(
98-
({ intercom }) => intercom,
99-
(error) => {
100-
debug(
101-
'Failed to fetch intercom integration status, defaulting to false',
102-
{ error: error instanceof Error && error.message }
103-
);
104-
return false;
105-
}
106-
);
93+
isIntercomAllowedResponse = allowNetworkTraffic
94+
? fetchIntegrations().then(
95+
({ intercom }) => intercom,
96+
(error) => {
97+
debug(
98+
'Failed to fetch intercom integration status, defaulting to false',
99+
{ error: error instanceof Error && error.message }
100+
);
101+
return false;
102+
}
103+
)
104+
: Promise.resolve(false);
107105
}
108106
return isIntercomAllowedResponse;
109107
}

0 commit comments

Comments
 (0)