Skip to content

Instantly share code, notes, and snippets.

@Ebrahim-Ramadan
Forked from t3dotgg/sentry-feedback.tsx
Created July 4, 2024 20:23
Show Gist options
  • Save Ebrahim-Ramadan/67b80aa96bcaf29be6907baf0b4d24bc to your computer and use it in GitHub Desktop.
Save Ebrahim-Ramadan/67b80aa96bcaf29be6907baf0b4d24bc to your computer and use it in GitHub Desktop.
"use client";
import { useAuth } from "@clerk/nextjs";
import * as Sentry from "@sentry/nextjs";
import { useEffect, useState } from "react";
function createWidget() {
return Sentry.getFeedback()?.createWidget();
}
function useFeedbackWidget(shouldMount: boolean) {
const [widget, setWidget] = useState<ReturnType<typeof createWidget> | null>(
null,
);
useEffect(() => {
// Mount if true and no widget exists
if (shouldMount && !widget) {
const newWidget = createWidget();
setWidget(newWidget);
}
// Unmount if false and widget exists
if (!shouldMount && widget) {
widget.removeFromDom();
setWidget(null);
}
}, [shouldMount, widget]);
}
export default function SentryFeedbackWidget() {
const auth = useAuth();
useFeedbackWidget(!!auth?.isSignedIn);
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment