Skip to content

Instantly share code, notes, and snippets.

@t3dotgg
Created June 25, 2024 00:01
Show Gist options
  • Save t3dotgg/426212b45a94e2a523c7b85f307cab52 to your computer and use it in GitHub Desktop.
Save t3dotgg/426212b45a94e2a523c7b85f307cab52 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