-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds suspense boundary to useSearchParams() for fixing ssr issue
- Loading branch information
1 parent
8ae5933
commit b716b6c
Showing
2 changed files
with
56 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"use client"; | ||
|
||
import { useEffect } from "react"; | ||
import { useSearchParams } from "next/navigation"; | ||
import { useRouter } from "next/navigation"; | ||
import { useToast } from "./ui/use-toast"; | ||
import { useIsSubscribed } from "@/hooks/useIsSubscribed"; | ||
import { useClerk } from "@clerk/nextjs"; | ||
import { useIsFetching } from "@/providers/IsFetchingProvider"; | ||
|
||
export default function SearchParams({ setSuccess } : { setSuccess: (value: boolean) => void }) { | ||
const searchParams = useSearchParams(); | ||
const router = useRouter(); | ||
const { toast } = useToast(); | ||
const { user } = useClerk(); | ||
const isSubscribed = useIsSubscribed(user?.id ?? ""); | ||
const isFetching = useIsFetching(); | ||
|
||
// if the payment was successful, the user will be redirected to the plans page with a query parameter. | ||
// This block checks for the query parameter and displays a success message to the user. | ||
useEffect(() => { | ||
if (!isFetching && searchParams.get("session_id") && isSubscribed) { | ||
setSuccess(true); | ||
|
||
toast({ | ||
title: "Payment successful ✅", | ||
variant: "success", | ||
}); | ||
} | ||
// remove the query parameter from the URL and update the state after 5 seconds. | ||
setTimeout(() => { | ||
router.replace("/plans"); | ||
setSuccess(false); | ||
}, 5000); | ||
}, [isFetching, isSubscribed, router, searchParams, setSuccess, toast]); | ||
|
||
return null; | ||
} |