Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(studio): cancel pending banner removal timeouts on unmount (#51491)

Alaister Youngf7b7969728e8
diff --git a/apps/studio/components/ui/BannerStack/BannerStackProvider.test.tsx b/apps/studio/components/ui/BannerStack/BannerStackProvider.test.tsx
index 0da73b822c9..23e791a858a 100644
--- a/apps/studio/components/ui/BannerStack/BannerStackProvider.test.tsx
+++ b/apps/studio/components/ui/BannerStack/BannerStackProvider.test.tsx
@@ -75,4 +75,26 @@ describe('BannerStackProvider', () => {
expect(result.current.banners).toEqual([])
})
+
+ it('cancels the pending removal when the provider unmounts', () => {
+ const { result, unmount } = renderHook(() => useBannerStack(), { wrapper })
+
+ act(() => {
+ result.current.addBanner({
+ id: BANNER_ID.LOGS_ALL_DEPRECATION,
+ isDismissed: false,
+ content: null,
+ })
+ })
+
+ act(() => {
+ result.current.dismissBanner(BANNER_ID.LOGS_ALL_DEPRECATION)
+ })
+
+ expect(vi.getTimerCount()).toBe(1)
+
+ unmount()
+
+ expect(vi.getTimerCount()).toBe(0)
+ })
})
diff --git a/apps/studio/components/ui/BannerStack/BannerStackProvider.tsx b/apps/studio/components/ui/BannerStack/BannerStackProvider.tsx
index e36f2d9fb6e..fdcf5f0a4db 100644
--- a/apps/studio/components/ui/BannerStack/BannerStackProvider.tsx
+++ b/apps/studio/components/ui/BannerStack/BannerStackProvider.tsx
@@ -1,4 +1,4 @@
-import { createContext, useCallback, useContext, useState } from 'react'
+import { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react'
export const BANNER_ID = {
INDEX_ADVISOR: 'index-advisor-banner',
@@ -30,6 +30,15 @@ const BannerStackContext = createContext<BannerStackContextType | undefined>(und
export const BannerStackProvider = ({ children }: { children: React.ReactNode }) => {
const [banners, setBanners] = useState<Banner[]>([])
+ const removalTimeouts = useRef(new Set<ReturnType<typeof setTimeout>>())
+
+ useEffect(() => {
+ const timeouts = removalTimeouts.current
+ return () => {
+ timeouts.forEach(clearTimeout)
+ timeouts.clear()
+ }
+ }, [])
const addBanner = useCallback((banner: Banner) => {
setBanners((prev) => {
@@ -47,11 +56,13 @@ export const BannerStackProvider = ({ children }: { children: React.ReactNode })
const dismissBanner = useCallback((id: string) => {
setBanners((prev) => prev.map((b) => (b.id === id ? { ...b, isDismissed: true } : b)))
- setTimeout(() => {
+ const timeout = setTimeout(() => {
+ removalTimeouts.current.delete(timeout)
// A later addBanner can revive this id before the exit animation
// finishes. Drop it only if it is still dismissed.
setBanners((prev) => prev.filter((b) => b.id !== id || !b.isDismissed))
}, 300)
+ removalTimeouts.current.add(timeout)
}, [])
return (