Skip to content

Commit

Permalink
Fix remove block button when long-pressing your own handle (#1594)
Browse files Browse the repository at this point in the history
Resolves #1592
  • Loading branch information
sharunkumar authored Oct 3, 2024
1 parent 4b31323 commit cc31185
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions src/features/labels/links/PersonLink.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getHandle } from "../../../helpers/lemmy";
import { getHandle, getRemoteHandle } from "../../../helpers/lemmy";
import { useBuildGeneralBrowseLink } from "../../../helpers/routes";
import { Person } from "lemmy-js-client";
import { renderHandle } from "../Handle";
Expand All @@ -15,12 +15,14 @@ import { styled } from "@linaria/react";
import { LinkContainer, StyledLink, hideCss } from "./shared";
import { cx } from "@linaria/core";
import { LongPressOptions, useLongPress } from "use-long-press";
import { useIonActionSheet } from "@ionic/react";
import { ActionSheetOptions, useIonActionSheet } from "@ionic/react";
import { removeCircleOutline } from "ionicons/icons";
import { blockUser } from "../../user/userSlice";
import useAppToast from "../../../helpers/useAppToast";
import { buildBlocked } from "../../../helpers/toastMessages";
import { getBlockUserErrorMessage } from "../../../helpers/lemmyErrors";
import { userHandleSelector } from "../../auth/authSelectors";
import { compact } from "lodash";

const Prefix = styled.span`
font-weight: normal;
Expand All @@ -38,6 +40,8 @@ interface PersonLinkProps {
className?: string;
}

type Button = ActionSheetOptions["buttons"][number];

export default function PersonLink({
person,
opId,
Expand All @@ -59,7 +63,7 @@ export default function PersonLink({

const onCommunityLinkLongPress = useCallback(() => {
const state = store.getState();

const currentUserHandle = userHandleSelector(state);
const blocks = state.site.response?.my_user?.person_blocks;
const isBlocked = blocks?.some(
(b) =>
Expand All @@ -70,36 +74,36 @@ export default function PersonLink({

stopIonicTapClick();

presentActionSheet({
cssClass: "left-align-buttons",
buttons: [
{
text: `${isBlocked ? "Unblock" : "Block"} User`,
icon: removeCircleOutline,
role: "destructive",
handler: () => {
(async () => {
try {
await dispatch(blockUser(!isBlocked, person.id));
} catch (error) {
presentToast({
color: "danger",
message: getBlockUserErrorMessage(error, person),
});
throw error;
}

presentToast(buildBlocked(!isBlocked, getHandle(person)));
})();
},
},
{
text: "Cancel",
role: "cancel",
const isCurrentUser = currentUserHandle === getRemoteHandle(person);

const buttons = compact<Button>([
!isCurrentUser && {
text: `${isBlocked ? "Unblock" : "Block"} User`,
icon: removeCircleOutline,
role: "destructive",
handler: () => {
(async () => {
try {
await dispatch(blockUser(!isBlocked, person.id));
} catch (error) {
presentToast({
color: "danger",
message: getBlockUserErrorMessage(error, person),
});
throw error;
}

presentToast(buildBlocked(!isBlocked, getHandle(person)));
})();
},
],
});
}, [presentActionSheet, presentToast, dispatch, person]);
},
{
text: "Cancel",
role: "cancel",
},
]);
presentActionSheet({ cssClass: "left-align-buttons", buttons });
}, [person, presentActionSheet, presentToast, dispatch]);

const bind = useLongPress(onCommunityLinkLongPress, {
cancelOnMovement: 15,
Expand Down

0 comments on commit cc31185

Please sign in to comment.