Skip to content

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

Shift notice banner logic to context (#20337)

Joshen Lim83285301a1d1
diff --git a/apps/studio/components/interfaces/App/AppBannerWrapperContext.tsx b/apps/studio/components/interfaces/App/AppBannerWrapperContext.tsx
new file mode 100644
index 00000000000..221a8f9c376
--- /dev/null
+++ b/apps/studio/components/interfaces/App/AppBannerWrapperContext.tsx
@@ -0,0 +1,47 @@
+import { noop } from 'lodash'
+import { PropsWithChildren, createContext, useContext, useEffect, useState } from 'react'
+
+import { LOCAL_STORAGE_KEYS } from 'lib/constants'
+
+const NOTICE_BANNER_KEY = LOCAL_STORAGE_KEYS.PGBOUNCER_DEPRECATION_WARNING
+
+// [Joshen] Update this as and when we need to use the NoticeBanner
+
+type AppBannerContextType = {
+ acknowledged: boolean
+ onUpdateAcknowledged: (value: boolean) => void
+}
+
+const AppBannerContext = createContext<AppBannerContextType>({
+ acknowledged: false,
+ onUpdateAcknowledged: noop,
+})
+
+export const useAppBannerContext = () => useContext(AppBannerContext)
+
+export const AppBannerContextProvider = ({ children }: PropsWithChildren<{}>) => {
+ const [acknowledged, setAcknowledged] = useState(false)
+
+ useEffect(() => {
+ if (typeof window !== 'undefined') {
+ setAcknowledged(localStorage.getItem(NOTICE_BANNER_KEY) === 'true')
+ }
+ }, [])
+
+ const value = {
+ acknowledged,
+ onUpdateAcknowledged: (value: boolean) => {
+ if (typeof window !== 'undefined') {
+ window.localStorage.setItem(NOTICE_BANNER_KEY, value.toString())
+ }
+ setAcknowledged(value)
+ },
+ }
+
+ return <AppBannerContext.Provider value={value}>{children}</AppBannerContext.Provider>
+}
+
+export const useIsNoticeBannerShown = () => {
+ const { acknowledged } = useAppBannerContext()
+ return acknowledged
+}
diff --git a/apps/studio/components/layouts/AppLayout/AppLayout.tsx b/apps/studio/components/layouts/AppLayout/AppLayout.tsx
index d8687edfc42..7ed065414ee 100644
--- a/apps/studio/components/layouts/AppLayout/AppLayout.tsx
+++ b/apps/studio/components/layouts/AppLayout/AppLayout.tsx
@@ -1,5 +1,6 @@
import { PropsWithChildren } from 'react'
+import { useIsNoticeBannerShown } from 'components/interfaces/App/AppBannerWrapperContext'
import { useFlag } from 'hooks'
import AppHeader from './AppHeader'
@@ -8,8 +9,11 @@ const AppLayout = ({ children }: PropsWithChildren<{}>) => {
const ongoingIncident = useFlag('ongoingIncident')
const showNoticeBanner = useFlag('showNoticeBanner')
+ const noticeAcknowledged = useIsNoticeBannerShown()
+
const appHeaderHeight = navLayoutV2 ? 49 : 0
- const bannerHeight = [ongoingIncident, showNoticeBanner].filter(Boolean).length * 44
+ const bannerHeight =
+ [ongoingIncident, showNoticeBanner && !noticeAcknowledged].filter(Boolean).length * 44
const maxHeight = `calc(100vh - ${bannerHeight}px - ${appHeaderHeight}px)`
return (
diff --git a/apps/studio/components/layouts/AppLayout/NoticeBanner.tsx b/apps/studio/components/layouts/AppLayout/NoticeBanner.tsx
index e438b4c22d1..2752f2b4568 100644
--- a/apps/studio/components/layouts/AppLayout/NoticeBanner.tsx
+++ b/apps/studio/components/layouts/AppLayout/NoticeBanner.tsx
@@ -1,8 +1,7 @@
-import { Button, IconExternalLink } from 'ui'
-import { useRouter } from 'next/router'
+import { useAppBannerContext } from 'components/interfaces/App/AppBannerWrapperContext'
import { useProfile } from 'lib/profile'
-import { LOCAL_STORAGE_KEYS } from 'lib/constants'
-import { useLocalStorage } from 'hooks'
+import { useRouter } from 'next/router'
+import { Button, IconExternalLink } from 'ui'
// [Joshen] For this notice specifically, just FYI
// 1 month after 26th Jan we'll need to add some contextual information about this deprecation
@@ -12,12 +11,10 @@ export const NoticeBanner = () => {
const router = useRouter()
const { isLoading } = useProfile()
- const [isDismissed, setIsDismissed] = useLocalStorage(
- LOCAL_STORAGE_KEYS.PGBOUNCER_DEPRECATION_WARNING,
- false
- )
+ const appBannerContext = useAppBannerContext()
+ const { acknowledged, onUpdateAcknowledged } = appBannerContext
- if (isLoading || router.pathname.includes('sign-in') || isDismissed) return null
+ if (isLoading || router.pathname.includes('sign-in') || acknowledged) return null
return (
<div
@@ -35,7 +32,7 @@ export const NoticeBanner = () => {
Learn more
</a>
</Button>
- <Button type="text" className="opacity-75" onClick={() => setIsDismissed(true)}>
+ <Button type="text" className="opacity-75" onClick={() => onUpdateAcknowledged(true)}>
Dismiss
</Button>
</div>
diff --git a/apps/studio/components/layouts/LayoutWrapper.tsx b/apps/studio/components/layouts/LayoutWrapper.tsx
index 76e2100eeeb..29f9c820395 100644
--- a/apps/studio/components/layouts/LayoutWrapper.tsx
+++ b/apps/studio/components/layouts/LayoutWrapper.tsx
@@ -1,3 +1,4 @@
+import { useIsNoticeBannerShown } from 'components/interfaces/App/AppBannerWrapperContext'
import { useFlag } from 'hooks'
import { PropsWithChildren } from 'react'
@@ -13,7 +14,12 @@ export const LayoutWrapper = ({
}: PropsWithChildren<LayoutWrapperProps>) => {
const ongoingIncident = useFlag('ongoingIncident')
const showNoticeBanner = useFlag('showNoticeBanner')
- const numBanners = [ongoingIncident, showNoticeBanner].filter(Boolean).length
+
+ const noticeAcknowledged = useIsNoticeBannerShown()
+
+ const numBanners = [ongoingIncident, showNoticeBanner && !noticeAcknowledged].filter(
+ Boolean
+ ).length
const maxHeight = `calc(100vh - ${numBanners * 44}px)`
return (
diff --git a/apps/studio/pages/_app.tsx b/apps/studio/pages/_app.tsx
index a9dea5faaa0..9495cfc665f 100644
--- a/apps/studio/pages/_app.tsx
+++ b/apps/studio/pages/_app.tsx
@@ -57,6 +57,7 @@ import { useAppStateSnapshot } from 'state/app-state'
import { RootStore } from 'stores'
import HCaptchaLoadedStore from 'stores/hcaptcha-loaded-store'
import { AppPropsWithLayout } from 'types'
+import { AppBannerContextProvider } from 'components/interfaces/App/AppBannerWrapperContext'
dayjs.extend(customParseFormat)
dayjs.extend(utc)
@@ -162,14 +163,16 @@ function CustomApp({ Component, pageProps }: AppPropsWithLayout) {
<TooltipProvider>
<RouteValidationWrapper>
<ThemeProvider defaultTheme="system" enableSystem disableTransitionOnChange>
- <CommandMenuWrapper>
- <AppBannerWrapper>
- <FeaturePreviewContextProvider>
- {getLayout(<Component {...pageProps} />)}
- <FeaturePreviewModal />
- </FeaturePreviewContextProvider>
- </AppBannerWrapper>
- </CommandMenuWrapper>
+ <AppBannerContextProvider>
+ <CommandMenuWrapper>
+ <AppBannerWrapper>
+ <FeaturePreviewContextProvider>
+ {getLayout(<Component {...pageProps} />)}
+ <FeaturePreviewModal />
+ </FeaturePreviewContextProvider>
+ </AppBannerWrapper>
+ </CommandMenuWrapper>
+ </AppBannerContextProvider>
</ThemeProvider>
</RouteValidationWrapper>
</TooltipProvider>