mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {useEffect, useState} from 'react'
2
3import {isWeb} from '#/platform/detection'
4import {useSession} from '#/state/session'
5
6export function useWelcomeModal() {
7 const {hasSession} = useSession()
8 const [isOpen, setIsOpen] = useState(false)
9
10 const open = () => setIsOpen(true)
11 const close = () => {
12 setIsOpen(false)
13 // Mark that user has actively closed the modal, don't show again this session
14 if (typeof window !== 'undefined') {
15 sessionStorage.setItem('welcomeModalClosed', 'true')
16 }
17 }
18
19 useEffect(() => {
20 // Only show modal if:
21 // 1. User is not logged in
22 // 2. We're on the web (this is a web-only feature)
23 // 3. We're on the homepage (path is '/' or '/home')
24 // 4. User hasn't actively closed the modal in this session
25 if (isWeb && !hasSession && typeof window !== 'undefined') {
26 const currentPath = window.location.pathname
27 const isHomePage = currentPath === '/'
28 const hasUserClosedModal =
29 sessionStorage.getItem('welcomeModalClosed') === 'true'
30
31 if (isHomePage && !hasUserClosedModal) {
32 // Small delay to ensure the page has loaded
33 const timer = setTimeout(() => {
34 open()
35 }, 1000)
36
37 return () => clearTimeout(timer)
38 }
39 }
40 }, [hasSession])
41
42 return {isOpen, open, close}
43}