From 78210d558faae01322742527d414955d771a59e0 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 7 May 2024 11:51:17 +0300 Subject: [PATCH] seen-usecontext --- src/components/Article/Comment/Comment.tsx | 2 +- src/components/Article/CommentsTree.tsx | 14 +++++++------- src/components/Article/FullArticle.tsx | 3 +++ src/components/Feed/Sidebar/Sidebar.tsx | 5 +++-- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/components/Article/Comment/Comment.tsx b/src/components/Article/Comment/Comment.tsx index d66e1767..5e94c4a8 100644 --- a/src/components/Article/Comment/Comment.tsx +++ b/src/components/Article/Comment/Comment.tsx @@ -127,7 +127,7 @@ export const Comment = (props: Props) => {
  • props.lastSeen, + [styles.isNew]: props.lastSeen > (props.comment.updated_at || props.comment.created_at), })} > diff --git a/src/components/Article/CommentsTree.tsx b/src/components/Article/CommentsTree.tsx index e8acc1fa..d899be8b 100644 --- a/src/components/Article/CommentsTree.tsx +++ b/src/components/Article/CommentsTree.tsx @@ -11,6 +11,7 @@ import { ShowIfAuthenticated } from '../_shared/ShowIfAuthenticated' import { Comment } from './Comment' +import { useSeen } from '../../context/seen' import styles from './Article.module.scss' const SimplifiedEditor = lazy(() => import('../Editor/SimplifiedEditor')) @@ -48,21 +49,20 @@ export const CommentsTree = (props: Props) => { } return newSortedComments }) - - const dateFromLocalStorage = Number.parseInt(localStorage.getItem(`${props.shoutSlug}`)) + const { seen } = useSeen() + const shoutLastSeen = createMemo(() => seen()[props.shoutSlug] ?? 0) const currentDate = new Date() const setCookie = () => localStorage.setItem(`${props.shoutSlug}`, `${currentDate}`) onMount(() => { - if (!dateFromLocalStorage) { + if (!shoutLastSeen()) { setCookie() - } else if (currentDate.getTime() > dateFromLocalStorage) { + } else if (currentDate.getTime() > shoutLastSeen()) { const newComments = comments().filter((c) => { if (c.reply_to || c.created_by.slug === author()?.slug) { return } - const created = c.created_at - return created > dateFromLocalStorage + return (c.updated_at || c.created_at) > shoutLastSeen() }) setNewReactions(newComments) setCookie() @@ -134,7 +134,7 @@ export const CommentsTree = (props: Props) => { comment={reaction} clickedReply={(id) => setClickedReplyId(id)} clickedReplyId={clickedReplyId()} - lastSeen={dateFromLocalStorage} + lastSeen={shoutLastSeen()} /> )} diff --git a/src/components/Article/FullArticle.tsx b/src/components/Article/FullArticle.tsx index 5d878580..94f4eff9 100644 --- a/src/components/Article/FullArticle.tsx +++ b/src/components/Article/FullArticle.tsx @@ -38,6 +38,7 @@ import { CommentsTree } from './CommentsTree' import { SharePopup, getShareUrl } from './SharePopup' import { ShoutRatingControl } from './ShoutRatingControl' +import { useSeen } from '../../context/seen' import stylesHeader from '../Nav/Header/Header.module.scss' import styles from './Article.module.scss' @@ -76,6 +77,7 @@ export const FullArticle = (props: Props) => { const [isActionPopupActive, setIsActionPopupActive] = createSignal(false) const { t, formatDate, lang } = useLocalize() const { author, session, requireAuthentication } = useSession() + const { addSeen } = useSeen() const formattedDate = createMemo(() => formatDate(new Date(props.article.published_at * 1000))) @@ -302,6 +304,7 @@ export const FullArticle = (props: Props) => { onMount(async () => { install('G-LQ4B87H8C2') await loadReactionsBy({ by: { shout: props.article.slug } }) + addSeen(props.article.slug) setIsReactionsLoaded(true) document.title = props.article.title window?.addEventListener('resize', updateIframeSizes) diff --git a/src/components/Feed/Sidebar/Sidebar.tsx b/src/components/Feed/Sidebar/Sidebar.tsx index 6fdbee40..a116bc4b 100644 --- a/src/components/Feed/Sidebar/Sidebar.tsx +++ b/src/components/Feed/Sidebar/Sidebar.tsx @@ -17,7 +17,7 @@ export const Sidebar = () => { const { seen } = useSeen() const { subscriptions } = useFollowing() const { page } = useRouter() - const { articlesByTopic } = useArticlesStore() + const { articlesByTopic, articlesByAuthor } = useArticlesStore() const [isSubscriptionsVisible, setSubscriptionsVisible] = createSignal(true) const checkTopicIsSeen = (topicSlug: string) => { @@ -25,8 +25,9 @@ export const Sidebar = () => { } const checkAuthorIsSeen = (authorSlug: string) => { - return Boolean(seen()[authorSlug]) + return articlesByAuthor()[authorSlug]?.every((article) => Boolean(seen()[article.slug])) } + return (