From d2861dfb19bf7210c0ab12935de8a9ceca2fdae8 Mon Sep 17 00:00:00 2001 From: Leonardo Dourado Date: Mon, 17 Jun 2024 15:17:09 -0300 Subject: [PATCH 1/3] feat: add useSentryException hook --- package.json | 1 + src/hooks/useSentryException.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/hooks/useSentryException.ts diff --git a/package.json b/package.json index 44c42e2..f4b4ada 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "coverage": "TZ=UTC+3 jest --coverage" }, "dependencies": { + "@sentry/react": "^8.9.2", "react": "^18.2.0" }, "devDependencies": { diff --git a/src/hooks/useSentryException.ts b/src/hooks/useSentryException.ts new file mode 100644 index 0000000..508715e --- /dev/null +++ b/src/hooks/useSentryException.ts @@ -0,0 +1,27 @@ +import { useCallback } from 'react'; +import * as Sentry from '@sentry/react'; + +interface CaptureExceptionOptions { + component: string; + clientId: string; +} + +const useSentryException = (): (( + error: Error, + options: CaptureExceptionOptions, +) => void) => { + const captureException = useCallback( + (error: Error, { component, clientId }: CaptureExceptionOptions) => { + Sentry.withScope((scope: Sentry.Scope) => { + scope.setTag('component', component); + scope.setTag('clientId', clientId); + Sentry.captureException(error); + }); + }, + [], + ); + + return captureException; +}; + +export default useSentryException; From e911e572a7e98de16be40bedd5053ea484ed4b8a Mon Sep 17 00:00:00 2001 From: Leonardo Dourado Date: Mon, 17 Jun 2024 16:51:06 -0300 Subject: [PATCH 2/3] fix: hook removing unnecessary usecallback --- src/hooks/useSentryException.ts | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/hooks/useSentryException.ts b/src/hooks/useSentryException.ts index 508715e..9f630ee 100644 --- a/src/hooks/useSentryException.ts +++ b/src/hooks/useSentryException.ts @@ -1,4 +1,3 @@ -import { useCallback } from 'react'; import * as Sentry from '@sentry/react'; interface CaptureExceptionOptions { @@ -6,22 +5,15 @@ interface CaptureExceptionOptions { clientId: string; } -const useSentryException = (): (( +const useSentryException = ( error: Error, - options: CaptureExceptionOptions, -) => void) => { - const captureException = useCallback( - (error: Error, { component, clientId }: CaptureExceptionOptions) => { - Sentry.withScope((scope: Sentry.Scope) => { - scope.setTag('component', component); - scope.setTag('clientId', clientId); - Sentry.captureException(error); - }); - }, - [], - ); - - return captureException; + { component, clientId }: CaptureExceptionOptions, +): void => { + Sentry.withScope((scope: Sentry.Scope) => { + scope.setTag('component', component); + scope.setTag('clientId', clientId); + Sentry.captureException(error); + }); }; export default useSentryException; From e2d3a8b85658fea86d0d399c850f855575883650 Mon Sep 17 00:00:00 2001 From: Leonardo Dourado Date: Mon, 17 Jun 2024 19:13:39 -0300 Subject: [PATCH 3/3] feat: add import on index --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index ddf2469..72f9afb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ // hooks export { default as useOnScreen } from './hooks/useOnScreen'; +export { default as useSentryException } from './hooks/useSentryException'; // normalizers export { default as normalizeMoney } from './normalizers/normalizeMoney';