-
-
Notifications
You must be signed in to change notification settings - Fork 42
feat(modal): implement support modal visibility logic based on localstorage #110
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,51 @@ export const SupportModal = ({ user }: { user: Profile | null }) => { | |
| const [copied, setCopied] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| const checkShouldShowModal = () => { | ||
| try { | ||
| const modalData = localStorage.getItem('devb-support-modal'); | ||
|
|
||
| if (!modalData) { | ||
| // First time visit - show modal | ||
| return true; | ||
| } | ||
|
|
||
| const { timestamp } = JSON.parse(modalData); | ||
| const oneDayInMs = 24 * 60 * 60 * 1000; // 1 day in milliseconds | ||
| const now = Date.now(); | ||
|
|
||
| // Show modal if more than 1 day has passed | ||
| return (now - timestamp) > oneDayInMs; | ||
| } catch (error) { | ||
| // If there's any error with localStorage, show modal | ||
| console.warn('Error checking modal state:', error); | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| const timer = setTimeout(() => { | ||
| setOpen(!user?.cached || false); | ||
| const shouldShow = checkShouldShowModal(); | ||
| setOpen(shouldShow && (!user?.cached || false)); | ||
| }, 4000); | ||
|
|
||
| return () => clearTimeout(timer); | ||
| }, [user]); | ||
|
|
||
| const handleModalClose = (isOpen: boolean) => { | ||
| setOpen(isOpen); | ||
| if (!isOpen) { | ||
| try { | ||
| // Save timestamp when modal is closed | ||
| const modalData = { | ||
| timestamp: Date.now(), | ||
| }; | ||
| localStorage.setItem('devb-support-modal', JSON.stringify(modalData)); | ||
| } catch (error) { | ||
| console.warn('Error saving modal state:', error); | ||
| } | ||
| } | ||
| }; | ||
|
Comment on lines
+53
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This handler function is recreated on every render. While it may not cause performance issues here, it's a good practice to memoize handlers with |
||
|
|
||
| const copyToClipboard = () => { | ||
| navigator.clipboard | ||
| .writeText(DEVB_INVITE_LINK) | ||
|
|
@@ -44,7 +82,7 @@ export const SupportModal = ({ user }: { user: Profile | null }) => { | |
| }; | ||
|
|
||
| return ( | ||
| <Dialog open={open} onOpenChange={setOpen}> | ||
| <Dialog open={open} onOpenChange={handleModalClose}> | ||
| <DialogContent className="sm:max-w-[700px] p-0 overflow-hidden border-black border-1 border-b-4 rounded-3xl"> | ||
| <div className="p-6 pt-8"> | ||
| <div className="bg-[#B9FF66] inline-block px-4 py-2 rounded-lg mb-4"> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function can be made more robust and maintainable.
JSON.parsewill yield an object with a numerictimestamp. IflocalStoragecontains malformed data (e.g.,"{}"or{"timestamp": "abc"}),now - timestampwill beNaN. This causes the check to fail silently, incorrectly preventing the modal from showing. You should validate the parsed data's structure.localStoragekey is hardcoded. Defining it as a constant improves readability and makes future changes easier, especially since it's used in another function.