Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(core): runtime control of telemetry (#10663)

forehalo56b842f2e16e
diff --git a/packages/common/infra/src/atom/root-store.ts b/packages/common/infra/src/atom/root-store.ts
index e0241e2a9f..62b402cf72 100644
--- a/packages/common/infra/src/atom/root-store.ts
+++ b/packages/common/infra/src/atom/root-store.ts
@@ -1,15 +1,5 @@
-import { createStore } from 'jotai';
+import { getDefaultStore } from 'jotai';
-// global store
-let rootStore = createStore();
-
-export function getCurrentStore(): ReturnType<typeof createStore> {
- return rootStore;
-}
-
-/**
- * @internal do not use this function unless you know what you are doing
- */
-export function _setCurrentStore(store: ReturnType<typeof createStore>) {
- rootStore = store;
+export function getCurrentStore() {
+ return getDefaultStore();
}
diff --git a/packages/common/infra/src/atom/settings.ts b/packages/common/infra/src/atom/settings.ts
index ea908c69c5..ba1885806f 100644
--- a/packages/common/infra/src/atom/settings.ts
+++ b/packages/common/infra/src/atom/settings.ts
@@ -22,15 +22,23 @@ export const windowFrameStyleOptions: AppSetting['windowFrameStyle'][] = [
'NativeTitleBar',
];
-const appSettingBaseAtom = atomWithStorage<AppSetting>('affine-settings', {
- clientBorder: BUILD_CONFIG.isElectron && !environment.isWindows,
- windowFrameStyle: 'frameless',
- enableBlurBackground: true,
- enableNoisyBackground: true,
- autoCheckUpdate: true,
- autoDownloadUpdate: true,
- enableTelemetry: true,
-});
+export const APP_SETTINGS_STORAGE_KEY = 'affine-settings';
+const appSettingBaseAtom = atomWithStorage<AppSetting>(
+ APP_SETTINGS_STORAGE_KEY,
+ {
+ clientBorder: BUILD_CONFIG.isElectron && !environment.isWindows,
+ windowFrameStyle: 'frameless',
+ enableBlurBackground: true,
+ enableNoisyBackground: true,
+ autoCheckUpdate: true,
+ autoDownloadUpdate: true,
+ enableTelemetry: true,
+ },
+ undefined,
+ {
+ getOnInit: true,
+ }
+);
type SetStateAction<Value> = Value | ((prev: Value) => Value);
diff --git a/packages/frontend/core/src/bootstrap/telemetry.ts b/packages/frontend/core/src/bootstrap/telemetry.ts
index 5257d4022c..141ed572bc 100644
--- a/packages/frontend/core/src/bootstrap/telemetry.ts
+++ b/packages/frontend/core/src/bootstrap/telemetry.ts
@@ -1,4 +1,20 @@
import { mixpanel, sentry } from '@affine/track';
+import { APP_SETTINGS_STORAGE_KEY } from '@toeverything/infra';
mixpanel.init();
sentry.init();
+
+if (typeof localStorage !== 'undefined') {
+ let enabled = true;
+ const settingsStr = localStorage.getItem(APP_SETTINGS_STORAGE_KEY);
+
+ if (settingsStr) {
+ const parsed = JSON.parse(settingsStr);
+ enabled = parsed.enableTelemetry;
+ }
+
+ if (!enabled) {
+ mixpanel.opt_out_tracking();
+ sentry.disable();
+ }
+}
diff --git a/packages/frontend/core/src/components/telemetry/index.tsx b/packages/frontend/core/src/components/telemetry/index.tsx
index c930e9461c..3f4d29a004 100644
--- a/packages/frontend/core/src/components/telemetry/index.tsx
+++ b/packages/frontend/core/src/components/telemetry/index.tsx
@@ -1,11 +1,12 @@
import { enableAutoTrack, mixpanel, sentry } from '@affine/track';
import { appSettingAtom } from '@toeverything/infra';
import { useAtomValue } from 'jotai/react';
-import { useLayoutEffect } from 'react';
+import { useEffect } from 'react';
export function Telemetry() {
const settings = useAtomValue(appSettingAtom);
- useLayoutEffect(() => {
+
+ useEffect(() => {
if (settings.enableTelemetry === false) {
sentry.disable();
mixpanel.opt_out_tracking();
@@ -16,5 +17,6 @@ export function Telemetry() {
return enableAutoTrack(document.body, mixpanel.track);
}
}, [settings.enableTelemetry]);
+
return null;
}
diff --git a/packages/frontend/track/src/sentry.ts b/packages/frontend/track/src/sentry.ts
index 94358bb8b1..5f88418fd4 100644
--- a/packages/frontend/track/src/sentry.ts
+++ b/packages/frontend/track/src/sentry.ts
@@ -8,39 +8,42 @@ import {
} from 'react-router-dom';
function createSentry() {
- let enabled = true;
+ let client: Sentry.BrowserClient | undefined;
const wrapped = {
init() {
- // https://docs.sentry.io/platforms/javascript/guides/react/#configure
- Sentry.init({
- enabled: enabled,
- dsn: process.env.SENTRY_DSN,
- debug: BUILD_CONFIG.debug ?? false,
- environment: process.env.BUILD_TYPE ?? 'development',
- integrations: [
- Sentry.reactRouterV6BrowserTracingIntegration({
- useEffect,
- useLocation,
- useNavigationType,
- createRoutesFromChildren,
- matchRoutes,
- }),
- ],
- beforeSend(event) {
- return enabled ? event : null;
- },
- });
- Sentry.setTags({
- distribution: BUILD_CONFIG.distribution,
- appVersion: BUILD_CONFIG.appVersion,
- editorVersion: BUILD_CONFIG.editorVersion,
- });
+ if (!globalThis.SENTRY_RELEASE) {
+ // https://docs.sentry.io/platforms/javascript/guides/react/#configure
+ client = Sentry.init({
+ dsn: process.env.SENTRY_DSN,
+ debug: BUILD_CONFIG.debug ?? false,
+ environment: process.env.BUILD_TYPE ?? 'development',
+ integrations: [
+ Sentry.reactRouterV6BrowserTracingIntegration({
+ useEffect,
+ useLocation,
+ useNavigationType,
+ createRoutesFromChildren,
+ matchRoutes,
+ }),
+ ],
+ }) as Sentry.BrowserClient;
+
+ Sentry.setTags({
+ distribution: BUILD_CONFIG.distribution,
+ appVersion: BUILD_CONFIG.appVersion,
+ editorVersion: BUILD_CONFIG.editorVersion,
+ });
+ }
},
enable() {
- enabled = true;
+ if (client) {
+ client.getOptions().enabled = true;
+ }
},
disable() {
- enabled = false;
+ if (client) {
+ client.getOptions().enabled = false;
+ }
},
};