From 2f53fd332f91279d799349250b5031f4549de3c9 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Thu, 31 Oct 2024 18:48:13 +0800 Subject: [PATCH] Improve comments rendering - Don't render summary/details if not needed - Delay-render collapsed summary/details --- src/locales/en.po | 8 +- src/pages/status.jsx | 235 +++++++++++++++++++++++-------------------- 2 files changed, 132 insertions(+), 111 deletions(-) diff --git a/src/locales/en.po b/src/locales/en.po index 6131a53b..4b563894 100644 --- a/src/locales/en.po +++ b/src/locales/en.po @@ -186,7 +186,7 @@ msgstr "" #: src/pages/catchup.jsx:1445 #: src/pages/catchup.jsx:2056 #: src/pages/status.jsx:921 -#: src/pages/status.jsx:1523 +#: src/pages/status.jsx:1543 msgid "Replies" msgstr "" @@ -3636,15 +3636,15 @@ msgstr "" msgid "Unable to load post" msgstr "" -#: src/pages/status.jsx:1455 +#: src/pages/status.jsx:1473 msgid "{0, plural, one {# reply} other {<0>{1} replies}}" msgstr "" -#: src/pages/status.jsx:1473 +#: src/pages/status.jsx:1491 msgid "{totalComments, plural, one {# comment} other {<0>{0} comments}}" msgstr "" -#: src/pages/status.jsx:1495 +#: src/pages/status.jsx:1513 msgid "View post with its replies" msgstr "" diff --git a/src/pages/status.jsx b/src/pages/status.jsx index b7c90aef..a26a5f2e 100644 --- a/src/pages/status.jsx +++ b/src/pages/status.jsx @@ -1423,130 +1423,151 @@ function SubComments({ }; }, []); + // If not open, delay render replies + const [renderReplies, setRenderReplies] = useState(openBefore || open); + useEffect(() => { + let timer; + if (!openBefore && !open) { + timer = setTimeout(() => setRenderReplies(true), 100); + } + return () => clearTimeout(timer); + }, [openBefore, open]); + + const Container = open ? 'div' : 'details'; + const isDetails = Container === 'details'; + return ( -
{ - const { open } = e.target; - // use first reply as ID - cachedRepliesToggle[replies[0].id] = open; - }} + open={isDetails ? openBefore || open : undefined} + onToggle={ + isDetails + ? (e) => { + const { open } = e.target; + // use first reply as ID + cachedRepliesToggle[replies[0].id] = open; + } + : undefined + } style={{ '--comments-level': level, }} data-comments-level={level} data-comments-level-overflow={level > 4} > -
+ + ))} + + )} + ); }