Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine Modal Display Logic to Only Show on First Visit (SnapshotNotifyModal) #1644

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/components/_modals/SnapshotNotifyModal..tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ModalProps, Text, Link } from "@chakra-ui/react"
import { BaseModal } from "./BaseModal"
import { useEffect } from "react"
export const SnapshotNotifyModal = ({
isOpen,
onClose,
}: Pick<ModalProps, "isOpen" | "onClose">) => {
useEffect(() => {
// Logic here if needed, e.g., analytics
}, [isOpen])

return (
<BaseModal
isOpen={isOpen}
onClose={onClose}
heading="Connect Wallet on Snapshot"
>
<Link
href="/snapshot"
color="white.500"
textDecoration="underline"
isExternal
>
Complete campaigns to earn bonus SOMM rewards and/or airdrop
points.
</Link>
</BaseModal>
)
}
26 changes: 25 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import type { NextPage } from "next"
import { PageHome } from "components/_pages/PageHome"
import { useState, useEffect } from "react"
import { SnapshotNotifyModal } from "components/_modals/SnapshotNotifyModal."

const Home: NextPage = () => {
return <PageHome />
const [isModalOpen, setIsModalOpen] = useState(false)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider initializing isModalOpen state based on the localStorage value to avoid unnecessary re-renders.

-  const [isModalOpen, setIsModalOpen] = useState(false)
+  const [isModalOpen, setIsModalOpen] = useState(() => typeof window !== "undefined" && !localStorage.getItem("modalShown"))

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const [isModalOpen, setIsModalOpen] = useState(false)
const [isModalOpen, setIsModalOpen] = useState(() => typeof window !== "undefined" && !localStorage.getItem("modalShown"))


useEffect(() => {
const modalShown = localStorage.getItem("modalShown")
if (!modalShown) {
setIsModalOpen(true)
localStorage.setItem("modalShown", "true")
}
}, [])
Comment on lines +9 to +15
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that accessing localStorage directly in the useEffect hook does not cause any issues during server-side rendering (SSR) in Next.js applications. Consider checking if the window object is defined before accessing localStorage.

  useEffect(() => {
+   if (typeof window !== "undefined") {
      const modalShown = localStorage.getItem("modalShown")
      if (!modalShown) {
        setIsModalOpen(true)
        localStorage.setItem("modalShown", "true")
      }
+   }
  }, [])

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
useEffect(() => {
const modalShown = localStorage.getItem("modalShown")
if (!modalShown) {
setIsModalOpen(true)
localStorage.setItem("modalShown", "true")
}
}, [])
useEffect(() => {
if (typeof window !== "undefined") {
const modalShown = localStorage.getItem("modalShown")
if (!modalShown) {
setIsModalOpen(true)
localStorage.setItem("modalShown", "true")
}
}
}, [])


const handleCloseModal = () => setIsModalOpen(false)

return (
<>
<PageHome />
{isModalOpen && (
<SnapshotNotifyModal
isOpen={isModalOpen}
onClose={handleCloseModal}
/>
)}
</>
)
}

export default Home