mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Sentry
This commit is contained in:
16 files changed
+494
-37
No files matched your search
+31
-26
@@ -13,6 +13,7 @@ import { NostrLoginProvider } from '@nostrify/react/login';
|
||||
import { AppProvider } from '@/components/AppProvider';
|
||||
import { AppConfig } from '@/contexts/AppContext';
|
||||
import { useAppContext } from '@/hooks/useAppContext';
|
||||
import { SentryProvider } from '@/components/SentryProvider';
|
||||
import { AISettingsProvider } from '@/components/AISettingsProvider';
|
||||
import { GitSettingsProvider } from '@/components/GitSettingsProvider';
|
||||
import { DeploySettingsProvider } from '@/components/DeploySettingsProvider';
|
||||
@@ -99,6 +100,8 @@ const defaultConfig: AppConfig = {
|
||||
fsPathProjects: electronPaths.fsPathProjects,
|
||||
fsPathConfig: electronPaths.fsPathConfig,
|
||||
fsPathTmp: electronPaths.fsPathTmp,
|
||||
sentryDsn: import.meta.env.VITE_SENTRY_DSN || "",
|
||||
sentryEnabled: false,
|
||||
};
|
||||
|
||||
const presetRelays = [
|
||||
@@ -134,32 +137,34 @@ export function App() {
|
||||
<UnheadProvider head={head}>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<AppProvider storageKey="nostr:app-config" defaultConfig={defaultConfig} presetRelays={presetRelays}>
|
||||
<FSProvider fs={fs}>
|
||||
<ConsoleErrorProvider>
|
||||
<FSCleanupHandler />
|
||||
<NostrLoginProvider storageKey='nostr:login'>
|
||||
<NostrProvider>
|
||||
<AISettingsProvider>
|
||||
<GitSettingsProvider>
|
||||
<DeploySettingsProvider>
|
||||
<SessionManagerProvider>
|
||||
<TooltipProvider>
|
||||
<Toaster />
|
||||
<DynamicFavicon />
|
||||
<OfflineIndicator />
|
||||
<PWAUpdatePrompt />
|
||||
<Suspense>
|
||||
<AppRouter />
|
||||
</Suspense>
|
||||
</TooltipProvider>
|
||||
</SessionManagerProvider>
|
||||
</DeploySettingsProvider>
|
||||
</GitSettingsProvider>
|
||||
</AISettingsProvider>
|
||||
</NostrProvider>
|
||||
</NostrLoginProvider>
|
||||
</ConsoleErrorProvider>
|
||||
</FSProvider>
|
||||
<SentryProvider>
|
||||
<FSProvider fs={fs}>
|
||||
<ConsoleErrorProvider>
|
||||
<FSCleanupHandler />
|
||||
<NostrLoginProvider storageKey='nostr:login'>
|
||||
<NostrProvider>
|
||||
<AISettingsProvider>
|
||||
<GitSettingsProvider>
|
||||
<DeploySettingsProvider>
|
||||
<SessionManagerProvider>
|
||||
<TooltipProvider>
|
||||
<Toaster />
|
||||
<DynamicFavicon />
|
||||
<OfflineIndicator />
|
||||
<PWAUpdatePrompt />
|
||||
<Suspense>
|
||||
<AppRouter />
|
||||
</Suspense>
|
||||
</TooltipProvider>
|
||||
</SessionManagerProvider>
|
||||
</DeploySettingsProvider>
|
||||
</GitSettingsProvider>
|
||||
</AISettingsProvider>
|
||||
</NostrProvider>
|
||||
</NostrLoginProvider>
|
||||
</ConsoleErrorProvider>
|
||||
</FSProvider>
|
||||
</SentryProvider>
|
||||
</AppProvider>
|
||||
</QueryClientProvider>
|
||||
</UnheadProvider>
|
||||
|
||||
@@ -30,6 +30,8 @@ const AppConfigSchema = z.object({
|
||||
fsPathProjects: z.string().min(1),
|
||||
fsPathConfig: z.string().min(1),
|
||||
fsPathTmp: z.string().min(1),
|
||||
sentryDsn: z.string(),
|
||||
sentryEnabled: z.boolean(),
|
||||
}) satisfies z.ZodType<AppConfig>;
|
||||
|
||||
export function AppProvider(props: AppProviderProps) {
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, waitFor } from '@testing-library/react';
|
||||
import { SentryProvider } from './SentryProvider';
|
||||
import { AppContext } from '@/contexts/AppContext';
|
||||
import * as sentryModule from '@/lib/sentry';
|
||||
|
||||
// Mock the sentry module
|
||||
vi.mock('@/lib/sentry', () => ({
|
||||
initializeSentry: vi.fn(() => Promise.resolve()),
|
||||
disableSentry: vi.fn(() => Promise.resolve()),
|
||||
isSentryInitialized: vi.fn(() => false),
|
||||
}));
|
||||
|
||||
describe('SentryProvider', () => {
|
||||
const mockUpdateConfig = vi.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('initializes Sentry when both DSN and sentryEnabled are true', async () => {
|
||||
const config = {
|
||||
sentryDsn: 'https://[email protected]/123',
|
||||
sentryEnabled: true,
|
||||
theme: 'light' as const,
|
||||
relayUrl: 'wss://relay.test',
|
||||
projectTemplate: 'https://test.git',
|
||||
esmUrl: 'https://esm.test',
|
||||
corsProxy: 'https://proxy.test/{href}',
|
||||
faviconUrl: 'https://favicon.test/{href}',
|
||||
previewDomain: 'test.dev',
|
||||
showcaseEnabled: false,
|
||||
showcaseModerator: '',
|
||||
ngitServers: [],
|
||||
fsPathProjects: '/projects',
|
||||
fsPathConfig: '/config',
|
||||
fsPathTmp: '/tmp',
|
||||
};
|
||||
|
||||
render(
|
||||
<AppContext.Provider value={{ config, updateConfig: mockUpdateConfig }}>
|
||||
<SentryProvider>
|
||||
<div>Test</div>
|
||||
</SentryProvider>
|
||||
</AppContext.Provider>
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(sentryModule.initializeSentry).toHaveBeenCalledWith('https://[email protected]/123');
|
||||
});
|
||||
});
|
||||
|
||||
it('does not initialize Sentry when DSN is empty', async () => {
|
||||
const config = {
|
||||
sentryDsn: '',
|
||||
sentryEnabled: true,
|
||||
theme: 'light' as const,
|
||||
relayUrl: 'wss://relay.test',
|
||||
projectTemplate: 'https://test.git',
|
||||
esmUrl: 'https://esm.test',
|
||||
corsProxy: 'https://proxy.test/{href}',
|
||||
faviconUrl: 'https://favicon.test/{href}',
|
||||
previewDomain: 'test.dev',
|
||||
showcaseEnabled: false,
|
||||
showcaseModerator: '',
|
||||
ngitServers: [],
|
||||
fsPathProjects: '/projects',
|
||||
fsPathConfig: '/config',
|
||||
fsPathTmp: '/tmp',
|
||||
};
|
||||
|
||||
render(
|
||||
<AppContext.Provider value={{ config, updateConfig: mockUpdateConfig }}>
|
||||
<SentryProvider>
|
||||
<div>Test</div>
|
||||
</SentryProvider>
|
||||
</AppContext.Provider>
|
||||
);
|
||||
|
||||
// Wait a bit to ensure no initialization happens
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
|
||||
expect(sentryModule.initializeSentry).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not initialize Sentry when sentryEnabled is false', async () => {
|
||||
const config = {
|
||||
sentryDsn: 'https://[email protected]/123',
|
||||
sentryEnabled: false,
|
||||
theme: 'light' as const,
|
||||
relayUrl: 'wss://relay.test',
|
||||
projectTemplate: 'https://test.git',
|
||||
esmUrl: 'https://esm.test',
|
||||
corsProxy: 'https://proxy.test/{href}',
|
||||
faviconUrl: 'https://favicon.test/{href}',
|
||||
previewDomain: 'test.dev',
|
||||
showcaseEnabled: false,
|
||||
showcaseModerator: '',
|
||||
ngitServers: [],
|
||||
fsPathProjects: '/projects',
|
||||
fsPathConfig: '/config',
|
||||
fsPathTmp: '/tmp',
|
||||
};
|
||||
|
||||
render(
|
||||
<AppContext.Provider value={{ config, updateConfig: mockUpdateConfig }}>
|
||||
<SentryProvider>
|
||||
<div>Test</div>
|
||||
</SentryProvider>
|
||||
</AppContext.Provider>
|
||||
);
|
||||
|
||||
// Wait a bit to ensure no initialization happens
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
|
||||
expect(sentryModule.initializeSentry).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('disables Sentry when it was initialized but conditions change', async () => {
|
||||
// Mock that Sentry is initialized
|
||||
vi.mocked(sentryModule.isSentryInitialized).mockReturnValue(true);
|
||||
|
||||
const config = {
|
||||
sentryDsn: '',
|
||||
sentryEnabled: false,
|
||||
theme: 'light' as const,
|
||||
relayUrl: 'wss://relay.test',
|
||||
projectTemplate: 'https://test.git',
|
||||
esmUrl: 'https://esm.test',
|
||||
corsProxy: 'https://proxy.test/{href}',
|
||||
faviconUrl: 'https://favicon.test/{href}',
|
||||
previewDomain: 'test.dev',
|
||||
showcaseEnabled: false,
|
||||
showcaseModerator: '',
|
||||
ngitServers: [],
|
||||
fsPathProjects: '/projects',
|
||||
fsPathConfig: '/config',
|
||||
fsPathTmp: '/tmp',
|
||||
};
|
||||
|
||||
render(
|
||||
<AppContext.Provider value={{ config, updateConfig: mockUpdateConfig }}>
|
||||
<SentryProvider>
|
||||
<div>Test</div>
|
||||
</SentryProvider>
|
||||
</AppContext.Provider>
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(sentryModule.disableSentry).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { ReactNode, useEffect } from 'react';
|
||||
import { useAppContext } from '@/hooks/useAppContext';
|
||||
import { initializeSentry, disableSentry, isSentryInitialized } from '@/lib/sentry';
|
||||
|
||||
interface SentryProviderProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export function SentryProvider({ children }: SentryProviderProps) {
|
||||
const { config } = useAppContext();
|
||||
|
||||
useEffect(() => {
|
||||
const shouldEnableSentry = config.sentryDsn && config.sentryEnabled;
|
||||
|
||||
if (shouldEnableSentry && !isSentryInitialized()) {
|
||||
// Initialize Sentry
|
||||
initializeSentry(config.sentryDsn).catch(console.error);
|
||||
} else if (!shouldEnableSentry && isSentryInitialized()) {
|
||||
// Disable Sentry
|
||||
disableSentry().catch(console.error);
|
||||
}
|
||||
}, [config.sentryDsn, config.sentryEnabled]);
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
@@ -31,6 +31,10 @@ export interface AppConfig {
|
||||
fsPathConfig: string;
|
||||
/** VFS path for temporary files directory */
|
||||
fsPathTmp: string;
|
||||
/** Sentry DSN for error reporting (empty string = disabled) */
|
||||
sentryDsn: string;
|
||||
/** Whether the user has enabled Sentry error reporting */
|
||||
sentryEnabled: boolean;
|
||||
}
|
||||
|
||||
export interface AppContextType {
|
||||
|
||||
@@ -53,6 +53,8 @@ describe('useAIProjectId', () => {
|
||||
fsPathProjects: '/projects',
|
||||
fsPathConfig: '/config',
|
||||
fsPathTmp: '/tmp',
|
||||
sentryDsn: '',
|
||||
sentryEnabled: false,
|
||||
},
|
||||
updateConfig: vi.fn(),
|
||||
});
|
||||
|
||||
@@ -45,6 +45,8 @@ function createWrapper(corsProxy: string) {
|
||||
fsPathProjects: '/projects',
|
||||
fsPathConfig: '/config',
|
||||
fsPathTmp: '/tmp',
|
||||
sentryDsn: '',
|
||||
sentryEnabled: false,
|
||||
};
|
||||
|
||||
return createElement(
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
import type * as SentryTypes from '@sentry/react';
|
||||
|
||||
let sentryInstance: typeof SentryTypes | null = null;
|
||||
let isInitialized = false;
|
||||
let isEnabled = false;
|
||||
|
||||
/**
|
||||
* Dynamically imports and initializes Sentry
|
||||
* @param dsn - Sentry DSN
|
||||
* @returns Promise that resolves when Sentry is initialized
|
||||
*/
|
||||
export async function initializeSentry(dsn: string): Promise<void> {
|
||||
// Don't initialize if DSN is empty
|
||||
if (!dsn) {
|
||||
console.log('Sentry DSN is empty, skipping initialization');
|
||||
return;
|
||||
}
|
||||
|
||||
// If Sentry was already initialized once, just re-enable it
|
||||
if (isInitialized && sentryInstance) {
|
||||
console.log('Sentry already initialized, re-enabling');
|
||||
const client = sentryInstance.getClient();
|
||||
if (client) {
|
||||
client.getOptions().enabled = true;
|
||||
}
|
||||
isEnabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Dynamic import to avoid loading Sentry unless needed
|
||||
const Sentry = await import('@sentry/react');
|
||||
sentryInstance = Sentry;
|
||||
|
||||
// Initialize Sentry
|
||||
Sentry.init({
|
||||
dsn,
|
||||
integrations: [
|
||||
Sentry.browserTracingIntegration(),
|
||||
Sentry.replayIntegration({
|
||||
maskAllText: true,
|
||||
blockAllMedia: true,
|
||||
}),
|
||||
],
|
||||
// Performance Monitoring
|
||||
tracesSampleRate: 0.1, // Capture 10% of transactions for performance monitoring
|
||||
// Session Replay
|
||||
replaysSessionSampleRate: 0.1, // Sample 10% of sessions
|
||||
replaysOnErrorSampleRate: 1.0, // Sample 100% of sessions with errors
|
||||
// Environment
|
||||
environment: import.meta.env.MODE,
|
||||
// Release
|
||||
release: import.meta.env.VITE_APP_VERSION || 'development',
|
||||
});
|
||||
|
||||
isInitialized = true;
|
||||
isEnabled = true;
|
||||
console.log('Sentry initialized successfully');
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize Sentry:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables Sentry by setting enabled to false
|
||||
*/
|
||||
export async function disableSentry(): Promise<void> {
|
||||
if (!isInitialized || !sentryInstance || !isEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const client = sentryInstance.getClient();
|
||||
if (client) {
|
||||
// Just disable the client instead of closing it
|
||||
// This allows re-enabling without re-initializing
|
||||
client.getOptions().enabled = false;
|
||||
}
|
||||
isEnabled = false;
|
||||
console.log('Sentry disabled successfully');
|
||||
} catch (error) {
|
||||
console.error('Failed to disable Sentry:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if Sentry is currently initialized and enabled
|
||||
*/
|
||||
export function isSentryInitialized(): boolean {
|
||||
return isInitialized && isEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Sentry instance (if initialized)
|
||||
*/
|
||||
export function getSentryInstance(): typeof SentryTypes | null {
|
||||
return sentryInstance;
|
||||
}
|
||||
@@ -55,6 +55,10 @@
|
||||
"configDirectoryDescription": "Filesystem path where configuration files are stored",
|
||||
"temporaryDirectory": "Temporary Directory",
|
||||
"temporaryDirectoryDescription": "Filesystem path for temporary files",
|
||||
"sentryDsn": "Sentry DSN",
|
||||
"sentryDsnDescription": "Sentry Data Source Name (DSN) for error reporting. Leave empty to disable Sentry.",
|
||||
"shareErrorReports": "Share error reports",
|
||||
"shareErrorReportsDescription": "Help improve this app by automatically sending crash and error reports.",
|
||||
"english": "English",
|
||||
"portuguese": "Portuguese",
|
||||
"chinese": "Chinese",
|
||||
|
||||
@@ -3,15 +3,17 @@ import { useTranslation } from 'react-i18next';
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ThemePicker } from "@/components/ThemePicker";
|
||||
import { LanguagePicker } from "@/components/LanguagePicker";
|
||||
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { useIsMobile } from "@/hooks/useIsMobile";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useAppContext } from "@/hooks/useAppContext";
|
||||
|
||||
export function Preferences() {
|
||||
const { t } = useTranslation();
|
||||
const isMobile = useIsMobile();
|
||||
const navigate = useNavigate();
|
||||
const { config, updateConfig } = useAppContext();
|
||||
|
||||
return (
|
||||
<div className="p-6 space-y-6">
|
||||
@@ -70,6 +72,29 @@ export function Preferences() {
|
||||
{t('languageDescription')}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between max-w-xs">
|
||||
<div className="space-y-0.5">
|
||||
<Label htmlFor="sentry-enabled" className="text-sm font-medium">
|
||||
{t('shareErrorReports')}
|
||||
</Label>
|
||||
</div>
|
||||
<Switch
|
||||
id="sentry-enabled"
|
||||
checked={config.sentryEnabled}
|
||||
onCheckedChange={(checked) => {
|
||||
updateConfig((current) => ({
|
||||
...current,
|
||||
sentryEnabled: checked,
|
||||
}));
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground max-w-xs">
|
||||
{t('shareErrorReportsDescription')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -29,6 +29,7 @@ export function SystemSettings() {
|
||||
const [fsPathProjectsInput, setFsPathProjectsInput] = useState(config.fsPathProjects);
|
||||
const [fsPathConfigInput, setFsPathConfigInput] = useState(config.fsPathConfig);
|
||||
const [fsPathTmpInput, setFsPathTmpInput] = useState(config.fsPathTmp);
|
||||
const [sentryDsnInput, setSentryDsnInput] = useState(config.sentryDsn);
|
||||
|
||||
// Service Worker state
|
||||
const [swState, setSwState] = useState<string>('');
|
||||
@@ -464,6 +465,36 @@ export function SystemSettings() {
|
||||
</AccordionItem>
|
||||
</Accordion>
|
||||
|
||||
{/* Sentry DSN Configuration */}
|
||||
<Accordion type="single" collapsible className="w-full">
|
||||
<AccordionItem value="sentry-dsn" className="border rounded-lg">
|
||||
<AccordionTrigger className="px-4 py-3 hover:no-underline">
|
||||
<h4 className="text-sm font-medium">{t('sentryDsn')}</h4>
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
<div className="py-1 space-y-2">
|
||||
<Input
|
||||
id="sentry-dsn"
|
||||
type="text"
|
||||
placeholder="https://..."
|
||||
value={sentryDsnInput}
|
||||
onChange={(e) => {
|
||||
const value = e.target.value;
|
||||
setSentryDsnInput(value);
|
||||
updateConfig((current) => ({
|
||||
...current,
|
||||
sentryDsn: value,
|
||||
}));
|
||||
}}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{t('sentryDsnDescription')}
|
||||
</p>
|
||||
</div>
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
</Accordion>
|
||||
|
||||
{/* Filesystem Paths Configuration */}
|
||||
<Accordion type="single" collapsible className="w-full">
|
||||
<AccordionItem value="filesystem-paths" className="border rounded-lg">
|
||||
|
||||
@@ -42,6 +42,8 @@ export function TestApp({ children }: TestAppProps) {
|
||||
fsPathProjects: '/projects',
|
||||
fsPathConfig: '/config',
|
||||
fsPathTmp: '/tmp',
|
||||
sentryDsn: '',
|
||||
sentryEnabled: false,
|
||||
};
|
||||
|
||||
// Initialize LightningFS for testing
|
||||
|
||||
Vendored
+1
-10
@@ -1,11 +1,2 @@
|
||||
/// <reference types="vite/client" />
|
||||
/// <reference types="vite-plugin-pwa/client" />
|
||||
|
||||
interface ImportMetaEnv {
|
||||
readonly VITE_GITHUB_OAUTH_CLIENT_ID?: string;
|
||||
readonly VITE_GITHUB_OAUTH_CLIENT_SECRET?: string;
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv;
|
||||
}
|
||||
/// <reference types="vite-plugin-pwa/client" />
|
||||
Reference in new issue
Block a user