WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 8262028

Browse files
authored
refactor: extract hot path handlers (#6092)
1 parent 7b92ae3 commit 8262028

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

apps/web/src/components/Account/Menu/Block.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ import { MenuItem } from "@headlessui/react";
22
import { NoSymbolIcon } from "@heroicons/react/24/outline";
33
import getAccount from "@hey/helpers/getAccount";
44
import type { AccountFragment } from "@hey/indexer";
5+
import { type MouseEvent, useCallback } from "react";
56
import cn from "@/helpers/cn";
67
import stopEventPropagation from "@/helpers/stopEventPropagation";
78
import { useBlockAlertStore } from "@/store/non-persisted/alert/useBlockAlertStore";
89

10+
const menuItemClassName = ({ focus }: { focus: boolean }) =>
11+
cn(
12+
{ "dropdown-active": focus },
13+
"m-2 flex cursor-pointer items-center space-x-2 rounded-lg px-2 py-1.5 text-sm"
14+
);
15+
916
interface BlockProps {
1017
account: AccountFragment;
1118
}
@@ -14,20 +21,16 @@ const Block = ({ account }: BlockProps) => {
1421
const { setShowBlockOrUnblockAlert } = useBlockAlertStore();
1522
const isBlockedByMe = account.operations?.isBlockedByMe;
1623

24+
const handleClick = useCallback(
25+
(event: MouseEvent) => {
26+
stopEventPropagation(event);
27+
setShowBlockOrUnblockAlert(true, account);
28+
},
29+
[account, setShowBlockOrUnblockAlert]
30+
);
31+
1732
return (
18-
<MenuItem
19-
as="div"
20-
className={({ focus }) =>
21-
cn(
22-
{ "dropdown-active": focus },
23-
"m-2 flex cursor-pointer items-center space-x-2 rounded-lg px-2 py-1.5 text-sm"
24-
)
25-
}
26-
onClick={(event) => {
27-
stopEventPropagation(event);
28-
setShowBlockOrUnblockAlert(true, account);
29-
}}
30-
>
33+
<MenuItem as="div" className={menuItemClassName} onClick={handleClick}>
3134
<NoSymbolIcon className="size-4" />
3235
<div>
3336
{isBlockedByMe ? "Unblock" : "Block"}{" "}

apps/web/src/components/Post/PostStats.tsx

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { PostFragment } from "@hey/indexer";
22
import { AnimateNumber } from "motion-plus-react";
33
import plur from "plur";
4-
import { memo, useState } from "react";
4+
import { memo, useCallback, useState } from "react";
55
import { Link } from "react-router";
66
import Likes from "@/components/Shared/Modal/Likes";
77
import PostExecutors from "@/components/Shared/Modal/PostExecutors";
@@ -44,6 +44,34 @@ const PostStats = ({ post }: PostStatsProps) => {
4444
const [showPostExecutorsModal, setShowPostExecutorsModal] =
4545
useState<PostExecutorsType | null>(null);
4646

47+
const handleOpenRepostsModal = useCallback(() => {
48+
setShowRepostsModal(true);
49+
}, []);
50+
51+
const handleOpenLikesModal = useCallback(() => {
52+
setShowLikesModal(true);
53+
}, []);
54+
55+
const handleOpenTippersModal = useCallback(() => {
56+
setShowPostExecutorsModal("Tippers");
57+
}, []);
58+
59+
const handleOpenCollectorsModal = useCallback(() => {
60+
setShowPostExecutorsModal("Collectors");
61+
}, []);
62+
63+
const handleCloseLikesModal = useCallback(() => {
64+
setShowLikesModal(false);
65+
}, []);
66+
67+
const handleCloseRepostsModal = useCallback(() => {
68+
setShowRepostsModal(false);
69+
}, []);
70+
71+
const handleClosePostExecutorsModal = useCallback(() => {
72+
setShowPostExecutorsModal(null);
73+
}, []);
74+
4775
const { bookmarks, comments, reposts, quotes, reactions, collects, tips } =
4876
post.stats;
4977

@@ -74,7 +102,7 @@ const PostStats = ({ post }: PostStatsProps) => {
74102
{reposts > 0 ? (
75103
<button
76104
className="outline-offset-2"
77-
onClick={() => setShowRepostsModal(true)}
105+
onClick={handleOpenRepostsModal}
78106
type="button"
79107
>
80108
<AnimatedNumber
@@ -96,7 +124,7 @@ const PostStats = ({ post }: PostStatsProps) => {
96124
{reactions > 0 ? (
97125
<button
98126
className="outline-offset-2"
99-
onClick={() => setShowLikesModal(true)}
127+
onClick={handleOpenLikesModal}
100128
type="button"
101129
>
102130
<AnimatedNumber
@@ -109,7 +137,7 @@ const PostStats = ({ post }: PostStatsProps) => {
109137
{tips > 0 ? (
110138
<button
111139
className="outline-offset-2"
112-
onClick={() => setShowPostExecutorsModal("Tippers")}
140+
onClick={handleOpenTippersModal}
113141
type="button"
114142
>
115143
<AnimatedNumber
@@ -122,7 +150,7 @@ const PostStats = ({ post }: PostStatsProps) => {
122150
{collects > 0 ? (
123151
<button
124152
className="outline-offset-2"
125-
onClick={() => setShowPostExecutorsModal("Collectors")}
153+
onClick={handleOpenCollectorsModal}
126154
type="button"
127155
>
128156
<AnimatedNumber
@@ -141,21 +169,21 @@ const PostStats = ({ post }: PostStatsProps) => {
141169
) : null}
142170
</div>
143171
<Modal
144-
onClose={() => setShowLikesModal(false)}
172+
onClose={handleCloseLikesModal}
145173
show={showLikesModal}
146174
title="Likes"
147175
>
148176
<Likes postId={post.id} />
149177
</Modal>
150178
<Modal
151-
onClose={() => setShowRepostsModal(false)}
179+
onClose={handleCloseRepostsModal}
152180
show={showRepostsModal}
153181
title="Reposts"
154182
>
155183
<Reposts postId={post.id} />
156184
</Modal>
157185
<Modal
158-
onClose={() => setShowPostExecutorsModal(null)}
186+
onClose={handleClosePostExecutorsModal}
159187
show={showPostExecutorsModal !== null}
160188
title={showPostExecutorsModal === "Tippers" ? "Tippers" : "Collectors"}
161189
>

0 commit comments

Comments
 (0)