- JS: @sentry/browser + posthog-js with lazy init and path scrubbing - Rust: sentry crate with beforeSend path scrubber - useTelemetry hook reactively inits/tears down SDKs on settings change - reinit_telemetry Tauri command lets frontend trigger Rust-side reinit - Zero network requests without explicit user consent - All file paths redacted from error payloads via regex scrubber Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { useEffect, useRef } from 'react'
|
|
import { invoke } from '@tauri-apps/api/core'
|
|
import { isTauri, mockInvoke } from '../mock-tauri'
|
|
import { initSentry, teardownSentry, initPostHog, teardownPostHog } from '../lib/telemetry'
|
|
import type { Settings } from '../types'
|
|
|
|
function tauriCall(command: string): Promise<void> {
|
|
return isTauri() ? invoke<void>(command) : mockInvoke<void>(command)
|
|
}
|
|
|
|
/**
|
|
* Initializes / tears down Sentry and PostHog reactively based on user settings.
|
|
* Call once in App after settings are loaded.
|
|
*/
|
|
export function useTelemetry(settings: Settings, loaded: boolean): void {
|
|
const prevCrash = useRef(false)
|
|
const prevAnalytics = useRef(false)
|
|
|
|
useEffect(() => {
|
|
if (!loaded) return
|
|
const crashEnabled = settings.crash_reporting_enabled === true
|
|
const analyticsEnabled = settings.analytics_enabled === true
|
|
const id = settings.anonymous_id
|
|
|
|
if (crashEnabled && id && !prevCrash.current) {
|
|
initSentry(id)
|
|
} else if (!crashEnabled && prevCrash.current) {
|
|
teardownSentry()
|
|
tauriCall('reinit_telemetry').catch(() => {})
|
|
}
|
|
|
|
if (analyticsEnabled && id && !prevAnalytics.current) {
|
|
initPostHog(id)
|
|
} else if (!analyticsEnabled && prevAnalytics.current) {
|
|
teardownPostHog()
|
|
}
|
|
|
|
prevCrash.current = crashEnabled
|
|
prevAnalytics.current = analyticsEnabled
|
|
}, [loaded, settings.crash_reporting_enabled, settings.analytics_enabled, settings.anonymous_id])
|
|
}
|