From f8c8688e786e29361db65a19e3e0c78f32d8d979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Truite=20S=C3=A9ch=C3=A9e?= Date: Fri, 13 Sep 2024 22:24:45 +0200 Subject: [PATCH] feat(Messaging): now support anchor links --- src/components/app/Messaging/Inbox.css | 1 + .../app/Messaging/MessageReader.jsx | 2 +- src/components/app/Messaging/Messaging.jsx | 41 ++++++++++++++++++- src/components/graphics/Arrow.jsx | 2 +- src/components/graphics/AttachmentIcon.jsx | 2 +- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/components/app/Messaging/Inbox.css b/src/components/app/Messaging/Inbox.css index bf3c17b..b5d3328 100644 --- a/src/components/app/Messaging/Inbox.css +++ b/src/components/app/Messaging/Inbox.css @@ -75,6 +75,7 @@ flex-flow: row nowrap; justify-content: space-between; width: 100%; + gap: 5px; } #inbox .message-container .message-subject .author-name { diff --git a/src/components/app/Messaging/MessageReader.jsx b/src/components/app/Messaging/MessageReader.jsx index e819b8d..5da9e06 100644 --- a/src/components/app/Messaging/MessageReader.jsx +++ b/src/components/app/Messaging/MessageReader.jsx @@ -44,7 +44,7 @@ export default function MessageReader({ selectedMessage }) {
diff --git a/src/components/app/Messaging/Messaging.jsx b/src/components/app/Messaging/Messaging.jsx index 98c971a..452ac20 100644 --- a/src/components/app/Messaging/Messaging.jsx +++ b/src/components/app/Messaging/Messaging.jsx @@ -1,5 +1,7 @@ -import { useState, useEffect, useContext } from "react"; +import { useState, useRef, useEffect, useContext } from "react"; +import { useNavigate, useLocation, Navigate, Link } from "react-router-dom"; + import { WindowsContainer, WindowsLayout, @@ -17,9 +19,14 @@ import MessageReader from "./MessageReader"; export default function Messaging({ isLoggedIn, activeAccount, fetchMessages, fetchMessageContent, fetchMessageMarkAsUnread }) { // States + const navigate = useNavigate(); + const location = useLocation(); + const { useUserData } = useContext(AppContext); - const [selectedMessage, setSelectedMessage] = useState(null); + const [selectedMessage, setSelectedMessage] = useState(isNaN(parseInt(location.hash.slice(1))) ? null : parseInt(location.hash.slice(1))); + const oldSelectedMessage = useRef(selectedMessage); const messages = useUserData("sortedMessages"); + // behavior useEffect(() => { @@ -44,11 +51,41 @@ export default function Messaging({ isLoggedIn, activeAccount, fetchMessages, fe const controller = new AbortController(); if (selectedMessage !== null) { fetchMessageContent(selectedMessage, controller); + const parsedHash = parseInt(location.hash.slice(1)); + if (parsedHash !== selectedMessage) { + const newHash = "#" + selectedMessage; + navigate(newHash); + } + } else { + if (location.hash) { + navigate("#"); + } } return () => { controller.abort(); } + }, [location, selectedMessage]); + + useEffect(() => { + if (oldSelectedMessage.current !== selectedMessage) { + return; + } + const parsedHash = parseInt(location.hash.slice(1)); + if (!isNaN(parsedHash) && parsedHash !== selectedMessage) { + if (messages.get()) { + const doesMessageExist = messages.get()?.findIndex((item) => item.id === parsedHash) !== -1; + if (doesMessageExist) { + setSelectedMessage(parsedHash); + } else { + navigate("#"); + } + } + } + }, [location, messages.get(), oldSelectedMessage.current, selectedMessage]); + + useEffect(() => { + oldSelectedMessage.current = selectedMessage; }, [selectedMessage]); // JSX diff --git a/src/components/graphics/Arrow.jsx b/src/components/graphics/Arrow.jsx index a9894d5..39e4848 100644 --- a/src/components/graphics/Arrow.jsx +++ b/src/components/graphics/Arrow.jsx @@ -2,6 +2,6 @@ import "./graphics.css" export default function Arrow({ className = "", id = "", alt, ...props }) { return ( - + ) } \ No newline at end of file diff --git a/src/components/graphics/AttachmentIcon.jsx b/src/components/graphics/AttachmentIcon.jsx index 78c722f..9d0acc9 100644 --- a/src/components/graphics/AttachmentIcon.jsx +++ b/src/components/graphics/AttachmentIcon.jsx @@ -4,7 +4,7 @@ import "./graphics.css" export default function AttachmentIcon({ className = "", id = "", alt, ...props }) { return ( - + ) } \ No newline at end of file