-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9759923
commit a974fc8
Showing
3 changed files
with
97 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.