1
0
Fork 0
mirror of https://github.com/cheeaun/phanpy.git synced 2025-03-08 17:35:39 +03:00

Improve comments rendering

- Don't render summary/details if not needed
- Delay-render collapsed summary/details
This commit is contained in:
Lim Chee Aun 2024-10-31 18:48:13 +08:00
parent cbfd4ef333
commit 2f53fd332f
2 changed files with 132 additions and 111 deletions
src
locales
pages

8
src/locales/en.po generated
View file

@ -186,7 +186,7 @@ msgstr ""
#: src/pages/catchup.jsx:1445 #: src/pages/catchup.jsx:1445
#: src/pages/catchup.jsx:2056 #: src/pages/catchup.jsx:2056
#: src/pages/status.jsx:921 #: src/pages/status.jsx:921
#: src/pages/status.jsx:1523 #: src/pages/status.jsx:1543
msgid "Replies" msgid "Replies"
msgstr "" msgstr ""
@ -3636,15 +3636,15 @@ msgstr ""
msgid "Unable to load post" msgid "Unable to load post"
msgstr "" msgstr ""
#: src/pages/status.jsx:1455 #: src/pages/status.jsx:1473
msgid "{0, plural, one {# reply} other {<0>{1}</0> replies}}" msgid "{0, plural, one {# reply} other {<0>{1}</0> replies}}"
msgstr "" msgstr ""
#: src/pages/status.jsx:1473 #: src/pages/status.jsx:1491
msgid "{totalComments, plural, one {# comment} other {<0>{0}</0> comments}}" msgid "{totalComments, plural, one {# comment} other {<0>{0}</0> comments}}"
msgstr "" msgstr ""
#: src/pages/status.jsx:1495 #: src/pages/status.jsx:1513
msgid "View post with its replies" msgid "View post with its replies"
msgstr "" msgstr ""

View file

@ -1423,22 +1423,40 @@ 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 ( return (
<details <Container
ref={detailsRef} ref={detailsRef}
class="replies" class="replies"
open={openBefore || open} open={isDetails ? openBefore || open : undefined}
onToggle={(e) => { onToggle={
isDetails
? (e) => {
const { open } = e.target; const { open } = e.target;
// use first reply as ID // use first reply as ID
cachedRepliesToggle[replies[0].id] = open; cachedRepliesToggle[replies[0].id] = open;
}} }
: undefined
}
style={{ style={{
'--comments-level': level, '--comments-level': level,
}} }}
data-comments-level={level} data-comments-level={level}
data-comments-level-overflow={level > 4} data-comments-level-overflow={level > 4}
> >
{!open && (
<summary class="replies-summary" hidden={open}> <summary class="replies-summary" hidden={open}>
<span class="avatars"> <span class="avatars">
{accounts.map((a) => ( {accounts.map((a) => (
@ -1498,6 +1516,8 @@ function SubComments({
</Link> </Link>
)} )}
</summary> </summary>
)}
{renderReplies && (
<ul> <ul>
{replies.map((r) => ( {replies.map((r) => (
<li key={r.id}> <li key={r.id}>
@ -1546,7 +1566,8 @@ function SubComments({
</li> </li>
))} ))}
</ul> </ul>
</details> )}
</Container>
); );
} }