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

This commit is contained in:
forehalo
2025-03-06 09:56:13 +00:00
parent 93920f9895
commit 56b842f2e1
5 changed files with 70 additions and 51 deletions

View File

@@ -1,15 +1,5 @@
import { createStore } from 'jotai'; import { getDefaultStore } from 'jotai';
// global store export function getCurrentStore() {
let rootStore = createStore(); return getDefaultStore();
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;
} }

View File

@@ -22,7 +22,10 @@ export const windowFrameStyleOptions: AppSetting['windowFrameStyle'][] = [
'NativeTitleBar', 'NativeTitleBar',
]; ];
const appSettingBaseAtom = atomWithStorage<AppSetting>('affine-settings', { export const APP_SETTINGS_STORAGE_KEY = 'affine-settings';
const appSettingBaseAtom = atomWithStorage<AppSetting>(
APP_SETTINGS_STORAGE_KEY,
{
clientBorder: BUILD_CONFIG.isElectron && !environment.isWindows, clientBorder: BUILD_CONFIG.isElectron && !environment.isWindows,
windowFrameStyle: 'frameless', windowFrameStyle: 'frameless',
enableBlurBackground: true, enableBlurBackground: true,
@@ -30,7 +33,12 @@ const appSettingBaseAtom = atomWithStorage<AppSetting>('affine-settings', {
autoCheckUpdate: true, autoCheckUpdate: true,
autoDownloadUpdate: true, autoDownloadUpdate: true,
enableTelemetry: true, enableTelemetry: true,
}); },
undefined,
{
getOnInit: true,
}
);
type SetStateAction<Value> = Value | ((prev: Value) => Value); type SetStateAction<Value> = Value | ((prev: Value) => Value);

View File

@@ -1,4 +1,20 @@
import { mixpanel, sentry } from '@affine/track'; import { mixpanel, sentry } from '@affine/track';
import { APP_SETTINGS_STORAGE_KEY } from '@toeverything/infra';
mixpanel.init(); mixpanel.init();
sentry.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();
}
}

View File

@@ -1,11 +1,12 @@
import { enableAutoTrack, mixpanel, sentry } from '@affine/track'; import { enableAutoTrack, mixpanel, sentry } from '@affine/track';
import { appSettingAtom } from '@toeverything/infra'; import { appSettingAtom } from '@toeverything/infra';
import { useAtomValue } from 'jotai/react'; import { useAtomValue } from 'jotai/react';
import { useLayoutEffect } from 'react'; import { useEffect } from 'react';
export function Telemetry() { export function Telemetry() {
const settings = useAtomValue(appSettingAtom); const settings = useAtomValue(appSettingAtom);
useLayoutEffect(() => {
useEffect(() => {
if (settings.enableTelemetry === false) { if (settings.enableTelemetry === false) {
sentry.disable(); sentry.disable();
mixpanel.opt_out_tracking(); mixpanel.opt_out_tracking();
@@ -16,5 +17,6 @@ export function Telemetry() {
return enableAutoTrack(document.body, mixpanel.track); return enableAutoTrack(document.body, mixpanel.track);
} }
}, [settings.enableTelemetry]); }, [settings.enableTelemetry]);
return null; return null;
} }

View File

@@ -8,12 +8,12 @@ import {
} from 'react-router-dom'; } from 'react-router-dom';
function createSentry() { function createSentry() {
let enabled = true; let client: Sentry.BrowserClient | undefined;
const wrapped = { const wrapped = {
init() { init() {
if (!globalThis.SENTRY_RELEASE) {
// https://docs.sentry.io/platforms/javascript/guides/react/#configure // https://docs.sentry.io/platforms/javascript/guides/react/#configure
Sentry.init({ client = Sentry.init({
enabled: enabled,
dsn: process.env.SENTRY_DSN, dsn: process.env.SENTRY_DSN,
debug: BUILD_CONFIG.debug ?? false, debug: BUILD_CONFIG.debug ?? false,
environment: process.env.BUILD_TYPE ?? 'development', environment: process.env.BUILD_TYPE ?? 'development',
@@ -26,21 +26,24 @@ function createSentry() {
matchRoutes, matchRoutes,
}), }),
], ],
beforeSend(event) { }) as Sentry.BrowserClient;
return enabled ? event : null;
},
});
Sentry.setTags({ Sentry.setTags({
distribution: BUILD_CONFIG.distribution, distribution: BUILD_CONFIG.distribution,
appVersion: BUILD_CONFIG.appVersion, appVersion: BUILD_CONFIG.appVersion,
editorVersion: BUILD_CONFIG.editorVersion, editorVersion: BUILD_CONFIG.editorVersion,
}); });
}
}, },
enable() { enable() {
enabled = true; if (client) {
client.getOptions().enabled = true;
}
}, },
disable() { disable() {
enabled = false; if (client) {
client.getOptions().enabled = false;
}
}, },
}; };