Skip to content

Commit

Permalink
Highlight multiple comments
Browse files Browse the repository at this point in the history
When a user gets a notification for multiple replies for a post or a
comment, those comments are now highlighted after navigating to the
post page.
  • Loading branch information
previnder committed Jan 12, 2025
1 parent e45167f commit cc767c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 12 additions & 0 deletions core/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"log"
"net/url"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -332,6 +333,17 @@ func (n *Notification) MarshalJSON() ([]byte, error) {
view.SeenAt = n.SeenAt
view.CreatedAt = n.CreatedAt
view.UpdatedAt = n.updatedAt
if view.CreatedAt != view.UpdatedAt && (n.Type == NotificationTypeNewComment || n.Type == NotificationTypeCommentReply) && view.ToURL != "" {
url, err := url.Parse(view.ToURL)
if err != nil {
return nil, fmt.Errorf("error parsing notification view url: %w", err)
}
q := url.Query()
q.Add("highlightFrom", strconv.FormatInt(view.CreatedAt.Unix(), 10))
q.Add("highlightTo", strconv.FormatInt(view.UpdatedAt.Unix(), 10))
url.RawQuery = q.Encode()
view.ToURL = url.String()
}
return json.Marshal(view)
}

Expand Down
16 changes: 15 additions & 1 deletion ui/src/pages/Post/Comment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import PropTypes from 'prop-types';
import React, { useEffect, useRef, useState } from 'react';
import { useDispatch } from 'react-redux';
import { useLocation } from 'react-router-dom';
import Dropdown from '../../components/Dropdown';
import MarkdownBody from '../../components/MarkdownBody';
import ModalConfirm from '../../components/Modal/ModalConfirm';
Expand Down Expand Up @@ -233,6 +234,19 @@ const Comment = ({
}
}, [focused, focusId]);

const location = useLocation();
const params = new URLSearchParams(location.search);

const highlighted = (() => {
const ts = new Date(comment.createdAt).getTime() / 1000,
from = parseInt(params.get('highlightFrom')),
to = parseInt(params.get('highlightTo'));
if (isNaN(to)) {
return ts >= from;
}
return ts >= from && ts <= to;
})();

const [reportModalOpen, setReportModalOpen] = useState(false); // for mobile

const [mutedUserHidden, setMutedUserHidden] = useState(comment.isAuthorMuted);
Expand Down Expand Up @@ -521,7 +535,7 @@ const Comment = ({
<div
className={
'post-comment-text' +
(focused ? ' is-focused' : '') +
(focused || highlighted ? ' is-focused' : '') +
(deleted && !purged ? ' is-red' : '')
}
onClick={handleCommentTextClick}
Expand Down

0 comments on commit cc767c3

Please sign in to comment.