From 2cdfaf487101711b9244e42db6865e0a24770b2b Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 12 Oct 2023 19:52:28 -0500 Subject: [PATCH 1/3] Remove useSentry hook, reorganize in SoapboxHead --- src/hooks/index.ts | 1 - src/init/soapbox-head.tsx | 11 ++++++++--- src/{hooks/useSentry.ts => sentry.ts} | 13 +------------ 3 files changed, 9 insertions(+), 16 deletions(-) rename src/{hooks/useSentry.ts => sentry.ts} (81%) diff --git a/src/hooks/index.ts b/src/hooks/index.ts index ef77478df..1da5fb9e4 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -19,7 +19,6 @@ export { useOwnAccount } from './useOwnAccount'; export { usePrevious } from './usePrevious'; export { useRefEventHandler } from './useRefEventHandler'; export { useRegistrationStatus } from './useRegistrationStatus'; -export { useSentry } from './useSentry'; export { useSettings } from './useSettings'; export { useSoapboxConfig } from './useSoapboxConfig'; export { useSystemTheme } from './useSystemTheme'; diff --git a/src/init/soapbox-head.tsx b/src/init/soapbox-head.tsx index 0039404e0..735e258f1 100644 --- a/src/init/soapbox-head.tsx +++ b/src/init/soapbox-head.tsx @@ -1,14 +1,14 @@ import clsx from 'clsx'; -import React from 'react'; +import React, { useEffect } from 'react'; import { - useSentry, useSettings, useSoapboxConfig, useTheme, useLocale, } from 'soapbox/hooks'; import { normalizeSoapboxConfig } from 'soapbox/normalizers'; +import { startSentry } from 'soapbox/sentry'; import { generateThemeCss } from 'soapbox/utils/theme'; const Helmet = React.lazy(() => import('soapbox/components/helmet')); @@ -26,6 +26,7 @@ const SoapboxHead: React.FC = ({ children }) => { const demo = !!settings.get('demo'); const darkMode = useTheme() === 'dark'; const themeCss = generateThemeCss(demo ? normalizeSoapboxConfig({ brandColor: '#0482d8' }) : soapboxConfig); + const dsn = soapboxConfig.sentryDsn; const bodyClass = clsx('h-full bg-white text-base dark:bg-gray-800', { 'no-reduce-motion': !settings.get('reduceMotion'), @@ -33,7 +34,11 @@ const SoapboxHead: React.FC = ({ children }) => { 'demetricator': settings.get('demetricator'), }); - useSentry(soapboxConfig.sentryDsn); + useEffect(() => { + if (dsn) { + startSentry(dsn).catch(console.error); + } + }, [dsn]); return ( <> diff --git a/src/hooks/useSentry.ts b/src/sentry.ts similarity index 81% rename from src/hooks/useSentry.ts rename to src/sentry.ts index 2a13b2645..a76d432c0 100644 --- a/src/hooks/useSentry.ts +++ b/src/sentry.ts @@ -1,14 +1,3 @@ -import { useEffect } from 'react'; - -/** Hook to start Sentry. Should only be called once. */ -function useSentry(dsn: string | undefined) { - useEffect(() => { - if (dsn) { - startSentry(dsn).catch(console.error); - } - }, [dsn]); -} - /** Start Sentry. */ async function startSentry(dsn: string): Promise { const [Sentry, { Integrations: Integrations }] = await Promise.all([ @@ -47,4 +36,4 @@ async function startSentry(dsn: string): Promise { }); } -export { useSentry }; \ No newline at end of file +export { startSentry }; \ No newline at end of file From a98dfd8c11095c98a3787f9cb2983609548b14a3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 12 Oct 2023 20:07:48 -0500 Subject: [PATCH 2/3] Send username/id to Sentry --- src/actions/me.ts | 14 ++++++++++---- src/sentry.ts | 14 +++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/actions/me.ts b/src/actions/me.ts index 4e02a6e62..dc6c8fa2d 100644 --- a/src/actions/me.ts +++ b/src/actions/me.ts @@ -1,4 +1,5 @@ import { selectAccount } from 'soapbox/selectors'; +import { setSentryAccount } from 'soapbox/sentry'; import KVStore from 'soapbox/storage/kv-store'; import { getAuthUserId, getAuthUserUrl } from 'soapbox/utils/auth'; @@ -8,6 +9,7 @@ import { loadCredentials } from './auth'; import { importFetchedAccount } from './importer'; import type { AxiosError, RawAxiosRequestHeaders } from 'axios'; +import type { Account } from 'soapbox/schemas'; import type { AppDispatch, RootState } from 'soapbox/store'; import type { APIEntity } from 'soapbox/types/entities'; @@ -88,10 +90,14 @@ const fetchMeRequest = () => ({ type: ME_FETCH_REQUEST, }); -const fetchMeSuccess = (me: APIEntity) => ({ - type: ME_FETCH_SUCCESS, - me, -}); +const fetchMeSuccess = (account: Account) => { + setSentryAccount(account); + + return { + type: ME_FETCH_SUCCESS, + me: account, + }; +}; const fetchMeFail = (error: APIEntity) => ({ type: ME_FETCH_FAIL, diff --git a/src/sentry.ts b/src/sentry.ts index a76d432c0..4ef1ea77e 100644 --- a/src/sentry.ts +++ b/src/sentry.ts @@ -1,3 +1,5 @@ +import type { Account } from './schemas'; + /** Start Sentry. */ async function startSentry(dsn: string): Promise { const [Sentry, { Integrations: Integrations }] = await Promise.all([ @@ -36,4 +38,14 @@ async function startSentry(dsn: string): Promise { }); } -export { startSentry }; \ No newline at end of file +/** Associate the account with Sentry events. */ +async function setSentryAccount(account: Account) { + const Sentry = await import('@sentry/react'); + + Sentry.setUser({ + id: account.id, + username: account.acct, + }); +} + +export { startSentry, setSentryAccount }; \ No newline at end of file From 626b3773711c0617d0f85097051278a68f0b4d76 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 12 Oct 2023 20:09:58 -0500 Subject: [PATCH 3/3] Fix type in compose.test.ts --- src/reducers/compose.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reducers/compose.test.ts b/src/reducers/compose.test.ts index c13e0bd4a..181e3b335 100644 --- a/src/reducers/compose.test.ts +++ b/src/reducers/compose.test.ts @@ -167,7 +167,7 @@ describe('compose reducer', () => { type: ME_FETCH_SUCCESS, me: { pleroma: { settings_store: { soapbox_fe: { defaultPrivacy: 'unlisted' } } } }, }; - expect(reducer(state, action).toJS().default).toMatchObject({ + expect(reducer(state, action as any).toJS().default).toMatchObject({ privacy: 'unlisted', }); });