Skip to content

Commit

Permalink
feat: add new notifications system
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCarames committed Oct 7, 2022
1 parent 9759923 commit a974fc8
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 27 deletions.
25 changes: 25 additions & 0 deletions src/components/Chat/useChat.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
import { useEffect } from "react"
import { useSelector } from "react-redux"
import { useNavigate } from "react-router-dom"
import { useNotifications } from "../../hooks/useNotifications"

interface User {
__typename: string
id: string
username: string
avatar: string
}

export const useChat = () => {
const navigate = useNavigate()
const chat = useSelector((state: any) => state.chat)
const loggedUser = useSelector((state: any) => state.loggedUser)
const { updateTitle, getNotificationsNumber, setDefaultTitle } = useNotifications()

useEffect(() => {
const auth = localStorage.auth
!auth && navigate("/login")
}, [])

useEffect(() => {
if (!chat.id) return
const possiblyAuthor = chat.users.find((user: User) => user.id !== loggedUser.id)
const notifications = JSON.parse(localStorage.getItem("notifications") as string)
const authorIndex = notifications.findIndex((notification: any) => notification.author === possiblyAuthor.id)
if (authorIndex === -1) return
notifications.splice(authorIndex, 1)
localStorage.setItem("notifications", JSON.stringify(notifications))
const notificationsNumber = getNotificationsNumber(notifications)
notificationsNumber === 0 && setDefaultTitle()
notificationsNumber >= 1 && updateTitle(notificationsNumber)
}, [chat])
}
66 changes: 66 additions & 0 deletions src/hooks/useNotifications.ts

Large diffs are not rendered by default.

33 changes: 6 additions & 27 deletions src/sections/ChatIndex/useChatIndex.tsx

Large diffs are not rendered by default.

0 comments on commit a974fc8

Please sign in to comment.