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

Make integrations toggle-able during build #9160

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
6 changes: 2 additions & 4 deletions care.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@ const careConfig = {
},

sentry: {
dsn:
env.REACT_SENTRY_DSN ||
"https://[email protected]/5183632",
environment: env.REACT_SENTRY_ENVIRONMENT || "staging",
dsn: env.REACT_SENTRY_DSN,
environment: env.REACT_SENTRY_ENVIRONMENT || "unknown",
sainak marked this conversation as resolved.
Show resolved Hide resolved
},

hcx: {
Expand Down
1 change: 0 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const App = () => {
</AuthUserProvider>

{/* Integrations */}
<Integrations.Sentry disabled={!import.meta.env.PROD} />
<Integrations.Plausible />
</HistoryAPIProvider>
<Toaster />
Expand Down
5 changes: 5 additions & 0 deletions src/Integrations/Plausible-disabled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function Plausible() {
return null;
}

export const triggerGoal = (_: string, __: object): void => {};
3 changes: 3 additions & 0 deletions src/Integrations/Sentry-disabled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function initSentry(): void {}

export function captureException(_: any): void {}
34 changes: 16 additions & 18 deletions src/Integrations/Sentry.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import careConfig from "@careConfig";
import { useEffect } from "react";
import * as Sentry from "@sentry/browser";

interface Props {
disabled?: boolean;
export function initSentry() {
if (!careConfig.sentry.dsn || !careConfig.sentry.environment) {
console.error(
"Sentry is not configured correctly. Please check your environment variables.",
);
return;
}
Sentry.init(careConfig.sentry);
sainak marked this conversation as resolved.
Show resolved Hide resolved
}

export default function Sentry({ disabled }: Props) {
useEffect(() => {
if (disabled || !careConfig.sentry.dsn || !careConfig.sentry.environment) {
console.error(
"Sentry is not configured correctly. Please check your environment variables.",
);
return;
}

import("@sentry/browser").then((Sentry) => {
Sentry.init(careConfig.sentry);
});
}, [disabled]);

return null;
/**
* Captures an exception and sends it to Sentry for monitoring and logging.
*
* @param {any} error - The error object that needs to be captured and sent to Sentry.
*/
export function captureException(error: any) {
Sentry.captureException(error);
sainak marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 1 addition & 2 deletions src/Integrations/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Plausible from "@/Integrations/Plausible";
import Sentry from "@/Integrations/Sentry";

const Integrations = { Sentry, Plausible };
const Integrations = { Plausible };

export default Integrations;
9 changes: 9 additions & 0 deletions src/Integrations/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "@/Integrations/Sentry" {
export function initSentry(): void;
export function captureException(error: any): void;
}
sainak marked this conversation as resolved.
Show resolved Hide resolved

declare module "@/Integrations/Plausible" {
export default function Plausible(): JSX.Element | null;
export function triggerGoal(goal: string, props: object): void;
}
sainak marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 3 additions & 3 deletions src/components/Notifications/NotificationsList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as Sentry from "@sentry/browser";
import { navigate } from "raviger";
import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
Expand All @@ -19,6 +18,7 @@ import useAuthUser from "@/hooks/useAuthUser";

import { NOTIFICATION_EVENTS } from "@/common/constants";

import { captureException } from "@/Integrations/Sentry";
import { Error, Success, Warn } from "@/Utils/Notifications";
import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
Expand Down Expand Up @@ -238,7 +238,7 @@ export default function NotificationsList({
setIsSubscribed("SubscribedOnAnotherDevice");
}
} catch (error) {
Sentry.captureException(error);
captureException(error);
}
};

Expand Down Expand Up @@ -325,7 +325,7 @@ export default function NotificationsList({
});
})
.catch(function (_e) {
Sentry.captureException(_e);
captureException(_e);
});
sainak marked this conversation as resolved.
Show resolved Hide resolved
};

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useNotificationSubscriptionState.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as Sentry from "@sentry/browser";
import { useEffect, useState } from "react";

import useAuthUser from "@/hooks/useAuthUser";

import { captureException } from "@/Integrations/Sentry";
import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";

Expand Down Expand Up @@ -45,7 +45,7 @@ export default function useNotificationSubscriptionState(
}
} catch (error) {
setSubscriptionState("error");
Sentry.captureException(error);
captureException(error);
}
};

Expand Down
13 changes: 5 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import * as Sentry from "@sentry/browser";
import { createRoot } from "react-dom/client";
import { registerSW } from "virtual:pwa-register";

import App from "@/App";
import { initSentry } from "@/Integrations/Sentry";
import "@/i18n";
import "@/style/index.css";

if ("serviceWorker" in navigator) {
registerSW({ immediate: false });
if (import.meta.env.PROD) {
initSentry();
}

if (import.meta.env.PROD) {
Sentry.init({
environment: import.meta.env.MODE,
dsn: "https://[email protected]/5183632",
});
if ("serviceWorker" in navigator) {
registerSW({ immediate: false });
}

const root = createRoot(document.getElementById("root") as HTMLElement);
Expand Down
2 changes: 2 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ interface ImportMetaEnv {
readonly REACT_ENABLED_APPS?: string;

// Plugins related envs...
readonly REACT_PLAUSIBLE_ENABLED?: string;
readonly REACT_PLAUSIBLE_SERVER_URL?: string;
readonly REACT_PLAUSIBLE_SITE_DOMAIN?: string;
readonly REACT_SENTRY_ENABLED?: string;
readonly REACT_SENTRY_DSN?: string;
readonly REACT_SENTRY_ENVIRONMENT?: string;
readonly REACT_ENABLE_HCX?: string;
Expand Down
22 changes: 22 additions & 0 deletions vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ function getPluginAliases() {
return aliases;
}

function getIntegrationsAlias(env: Record<string, string>) {
const integrationsImportAlias = "@/Integrations";
const integrationsImportPath = "./src/Integrations";
const integrations = {
Sentry: env.REACT_SENTRY_ENABLED === "true",
Plausible: env.REACT_PLAUSIBLE_ENABLED === "true",
};
const importMap: Record<string, string> = {};

Object.entries(integrations).forEach(([name, enabled]) => {
importMap[`${integrationsImportAlias}/${name}`] = path.resolve(
__dirname,
enabled
? `${integrationsImportPath}/${name}`
: `${integrationsImportPath}/${name}-disabled`,
);
});

return importMap;
}
sainak marked this conversation as resolved.
Show resolved Hide resolved

function getPluginDependencies(): string[] {
const pluginsDir = path.resolve(__dirname, "apps");
// Make sure the `apps` folder exists
Expand Down Expand Up @@ -181,6 +202,7 @@ export default defineConfig(({ mode }) => {
],
resolve: {
alias: {
...getIntegrationsAlias(env),
...getPluginAliases(),
"@": path.resolve(__dirname, "./src"),
"@careConfig": path.resolve(__dirname, "./care.config.ts"),
Expand Down
Loading