Skip to content

Commit

Permalink
Refactor error toast messages (#1352)
Browse files Browse the repository at this point in the history
Resolves #1349
  • Loading branch information
aeharding authored Mar 21, 2024
1 parent aefe74d commit 7bec240
Show file tree
Hide file tree
Showing 20 changed files with 124 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/core/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getInboxCounts, syncMessages } from "../features/inbox/inboxSlice";
import { useInterval } from "usehooks-ts";
import usePageVisibility from "../helpers/usePageVisibility";
import { getDefaultServer } from "../services/app";
import { isLemmyError } from "../helpers/lemmy";
import { isLemmyError } from "../helpers/lemmyErrors";
import useAppToast from "../helpers/useAppToast";
import BackgroundReportSync from "../features/moderation/BackgroundReportSync";
import { getSiteIfNeeded, isAdminSelector } from "../features/auth/siteSlice";
Expand Down
5 changes: 4 additions & 1 deletion src/features/auth/login/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import {
import useAppToast from "../../../../helpers/useAppToast";
import { useAppDispatch, useAppSelector } from "../../../../store";
import { addGuestInstance, login } from "../../authSlice";
import { getLoginErrorMessage, isLemmyError } from "../../../../helpers/lemmy";
import {
getLoginErrorMessage,
isLemmyError,
} from "../../../../helpers/lemmyErrors";
import Totp from "./Totp";
import { DynamicDismissableModalContext } from "../../../shared/DynamicDismissableModal";
import InAppExternalLink from "../../../shared/InAppExternalLink";
Expand Down
5 changes: 4 additions & 1 deletion src/features/auth/login/login/Totp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import {
import useAppToast from "../../../../helpers/useAppToast";
import { useAppDispatch } from "../../../../store";
import { login } from "../../authSlice";
import { getLoginErrorMessage, isLemmyError } from "../../../../helpers/lemmy";
import {
getLoginErrorMessage,
isLemmyError,
} from "../../../../helpers/lemmyErrors";
import { DynamicDismissableModalContext } from "../../../shared/DynamicDismissableModal";
import { loginSuccess } from "../../../../helpers/toastMessages";
import AppHeader from "../../../shared/AppHeader";
Expand Down
2 changes: 1 addition & 1 deletion src/features/comment/compose/reply/CommentReply.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import { receivedComments } from "../../commentSlice";
import CommentEditorContent from "../CommentEditorContent";
import useAppToast from "../../../../helpers/useAppToast";
import { isLemmyError } from "../../../../helpers/lemmy";
import { isLemmyError } from "../../../../helpers/lemmyErrors";
import AccountSwitcher from "../../../auth/AccountSwitcher";
import { getClient } from "../../../../services/lemmy";
import AppHeader from "../../../shared/AppHeader";
Expand Down
17 changes: 14 additions & 3 deletions src/features/comment/useCommentActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
postLocked,
saveError,
saveSuccess,
voteError,
} from "../../helpers/toastMessages";
import store, { useAppDispatch } from "../../store";
import { PageContext } from "../auth/PageContext";
Expand All @@ -45,6 +44,7 @@ import { useOptimizedIonRouter } from "../../helpers/useOptimizedIonRouter";
import { isDownvoteEnabledSelector } from "../auth/siteSlice";
import { compact } from "lodash";
import { isStubComment } from "./CommentHeader";
import { getVoteErrorMessage } from "../../helpers/lemmyErrors";

export interface CommentActionsProps {
comment: CommentView | PersonMentionView | CommentReplyView;
Expand Down Expand Up @@ -125,7 +125,12 @@ export default function useCommentActions({
try {
await dispatch(voteOnComment(comment.id, myVote === 1 ? 0 : 1));
} catch (error) {
presentToast(voteError);
presentToast({
color: "danger",
message: getVoteErrorMessage(error),
});

throw error;
}
})();
},
Expand All @@ -143,7 +148,12 @@ export default function useCommentActions({
voteOnComment(comment.id, myVote === -1 ? 0 : -1),
);
} catch (error) {
presentToast(voteError);
presentToast({
color: "danger",
message: getVoteErrorMessage(error),
});

throw error;
}
})();
},
Expand All @@ -162,6 +172,7 @@ export default function useCommentActions({
if (!mySaved) presentToast(saveSuccess);
} catch (error) {
presentToast(saveError);
throw error;
}
})();
},
Expand Down
2 changes: 1 addition & 1 deletion src/features/inbox/inboxSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { logoutAccount } from "../auth/authSlice";
import { InboxItemView } from "./InboxItem";
import { differenceBy, uniqBy } from "lodash";
import { receivedUsers } from "../user/userSlice";
import { isLemmyError } from "../../helpers/lemmy";
import { isLemmyError } from "../../helpers/lemmyErrors";
import {
clientSelector,
userHandleSelector,
Expand Down
9 changes: 7 additions & 2 deletions src/features/labels/Vote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { arrowDownSharp, arrowUpSharp } from "ionicons/icons";
import { voteOnPost } from "../post/postSlice";
import React, { useContext } from "react";
import { voteOnComment } from "../comment/commentSlice";
import { downvotesDisabled, voteError } from "../../helpers/toastMessages";
import { downvotesDisabled } from "../../helpers/toastMessages";
import { PageContext } from "../auth/PageContext";
import {
calculateTotalScore,
Expand All @@ -18,6 +18,7 @@ import useHapticFeedback from "../../helpers/useHapticFeedback";
import useAppToast from "../../helpers/useAppToast";
import { formatNumber } from "../../helpers/number";
import { styled } from "@linaria/react";
import { getVoteErrorMessage } from "../../helpers/lemmyErrors";

const Container = styled.div<{
vote?: 1 | -1 | 0;
Expand Down Expand Up @@ -91,7 +92,11 @@ export default function Vote({
try {
await dispatch(dispatcherFn(id, vote));
} catch (error) {
presentToast(voteError);
presentToast({
color: "danger",
message: getVoteErrorMessage(error),
});

throw error;
}
}
Expand Down
27 changes: 6 additions & 21 deletions src/features/post/postSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
jwtSelector,
} from "../auth/authSelectors";
import { IPostMetadata, db } from "../../services/db";
import { isLemmyError } from "../../helpers/lemmy";
import { isLemmyError } from "../../helpers/lemmyErrors";
import { resolvePostReport } from "../moderation/modSlice";

interface PostHiddenData {
Expand Down Expand Up @@ -324,10 +324,9 @@ export const getPost =
id,
});
} catch (error) {
// I think there is a bug in lemmy-js-client where it tries to parse 404 with non-json body
if (
isLemmyError(error, "couldnt_find_post") ||
error instanceof SyntaxError
isLemmyError(error, "unknown")
) {
dispatch(receivedPostNotFound(id));
}
Expand All @@ -341,24 +340,10 @@ export const getPost =

export const deletePost =
(id: number) => async (dispatch: AppDispatch, getState: () => RootState) => {
try {
await clientSelector(getState()).deletePost({
post_id: id,
deleted: true,
});
} catch (error) {
// I think there is a bug in lemmy-js-client where it tries to parse 404 with non-json body
if (
isLemmyError(error, "couldnt_find_post") ||
error instanceof SyntaxError
) {
dispatch(receivedPostNotFound(id));

return;
}

throw error;
}
await clientSelector(getState()).deletePost({
post_id: id,
deleted: true,
});

dispatch(postDeleted(id));
};
Expand Down
7 changes: 5 additions & 2 deletions src/features/post/shared/VoteButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useAppDispatch, useAppSelector } from "../../../store";
import { voteOnPost } from "../postSlice";
import { arrowDownSharp, arrowUpSharp } from "ionicons/icons";
import { ActionButton } from "../actions/ActionButton";
import { voteError } from "../../../helpers/toastMessages";
import { PageContext } from "../../auth/PageContext";
import { isDownvoteEnabledSelector } from "../../auth/siteSlice";
import { bounceAnimationOnTransition, bounceMs } from "../../shared/animations";
Expand All @@ -13,6 +12,7 @@ import { ImpactStyle } from "@capacitor/haptics";
import useHapticFeedback from "../../../helpers/useHapticFeedback";
import useAppToast from "../../../helpers/useAppToast";
import { styled } from "@linaria/react";
import { getVoteErrorMessage } from "../../../helpers/lemmyErrors";

const InactiveItem = styled(ActionButton)`
${bounceAnimationOnTransition}
Expand Down Expand Up @@ -103,7 +103,10 @@ export function VoteButton({ type, postId }: VoteButtonProps) {
voteOnPost(postId, myVote === selectedVote ? 0 : selectedVote),
);
} catch (error) {
presentToast(voteError);
presentToast({
color: "danger",
message: getVoteErrorMessage(error),
});

throw error;
}
Expand Down
12 changes: 9 additions & 3 deletions src/features/post/shared/usePostActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {
postLocked,
saveError,
saveSuccess,
voteError,
} from "../../../helpers/toastMessages";
import { userHandleSelector } from "../../auth/authSelectors";
import useAppToast from "../../../helpers/useAppToast";
Expand All @@ -50,6 +49,7 @@ import { isDownvoteEnabledSelector } from "../../auth/siteSlice";
import { resolveObject } from "../../resolve/resolveSlice";
import { compact } from "lodash";
import { InFeedContext } from "../../feed/Feed";
import { getVoteErrorMessage } from "../../../helpers/lemmyErrors";

export default function usePostActions(post: PostView) {
const inFeed = useContext(InFeedContext);
Expand Down Expand Up @@ -105,7 +105,10 @@ export default function usePostActions(post: PostView) {
try {
await dispatch(voteOnPost(post.post.id, myVote === 1 ? 0 : 1));
} catch (error) {
presentToast(voteError);
presentToast({
color: "danger",
message: getVoteErrorMessage(error),
});

throw error;
}
Expand All @@ -124,7 +127,10 @@ export default function usePostActions(post: PostView) {
voteOnPost(post.post.id, myVote === -1 ? 0 : -1),
);
} catch (error) {
presentToast(voteError);
presentToast({
color: "danger",
message: getVoteErrorMessage(error),
});

throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/features/report/Report.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { forwardRef, useImperativeHandle, useState } from "react";
import useClient from "../../helpers/useClient";
import { IonAlertCustomEvent, OverlayEventDetail } from "@ionic/core";
import useAppToast from "../../helpers/useAppToast";
import { isLemmyError } from "../../helpers/lemmy";
import { isLemmyError } from "../../helpers/lemmyErrors";

export type ReportableItem = CommentView | PostView | PrivateMessageView;

Expand Down
2 changes: 1 addition & 1 deletion src/features/resolve/resolveSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { receivedCommunity } from "../community/communitySlice";
import { receivedPosts } from "../post/postSlice";
import { receivedUsers } from "../user/userSlice";
import { PayloadAction, createSlice } from "@reduxjs/toolkit";
import { isLemmyError } from "../../helpers/lemmy";
import { isLemmyError } from "../../helpers/lemmyErrors";
import { getClient } from "../../services/lemmy";
import {
COMMENT_PATH,
Expand Down
9 changes: 7 additions & 2 deletions src/features/shared/sliding/BaseSlidingVote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
postLocked,
replyStubError,
saveSuccess,
voteError,
} from "../../../helpers/toastMessages";
import {
saveComment,
Expand All @@ -46,6 +45,7 @@ import { scrollCommentIntoViewIfNeeded } from "../../comment/inTree/CommentTree"
import { AppContext } from "../../auth/AppContext";
import { getCanModerate } from "../../moderation/useCanModerate";
import { isStubComment } from "../../comment/CommentHeader";
import { getVoteErrorMessage } from "../../../helpers/lemmyErrors";

const StyledItemContainer = styled.div`
--ion-item-border-color: transparent;
Expand Down Expand Up @@ -151,7 +151,12 @@ function BaseSlidingVoteInternal({
if (isPost) await dispatch(voteOnPost(item.post.id, score));
else await dispatch(voteOnComment(item.comment.id, score));
} catch (error) {
presentToast(voteError);
presentToast({
color: "danger",
message: getVoteErrorMessage(error),
});

throw error;
}
},
[presentLoginIfNeeded, isPost, dispatch, item, presentToast],
Expand Down
2 changes: 1 addition & 1 deletion src/features/shared/useLemmyUrlHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useBuildGeneralBrowseLink } from "../../helpers/routes";
import { normalizeObjectUrl, resolveObject } from "../resolve/resolveSlice";
import { MouseEvent } from "react";
import useAppToast from "../../helpers/useAppToast";
import { isLemmyError } from "../../helpers/lemmy";
import { isLemmyError } from "../../helpers/lemmyErrors";
import { useOptimizedIonRouter } from "../../helpers/useOptimizedIonRouter";

export const POST_PATH = /^\/post\/(\d+)$/;
Expand Down
2 changes: 1 addition & 1 deletion src/features/user/AsyncProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { GetPersonDetailsResponse } from "lemmy-js-client";
import { useAppDispatch } from "../../store";
import { getUser } from "../../features/user/userSlice";
import { useBuildGeneralBrowseLink } from "../../helpers/routes";
import { isLemmyError } from "../../helpers/lemmy";
import { isLemmyError } from "../../helpers/lemmyErrors";
import { useOptimizedIonRouter } from "../../helpers/useOptimizedIonRouter";
import { styled } from "@linaria/react";

Expand Down
33 changes: 0 additions & 33 deletions src/helpers/lemmy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Community,
CommunityModeratorView,
GetSiteResponse,
LemmyErrorType,
Post,
PostView,
} from "lemmy-js-client";
Expand Down Expand Up @@ -279,13 +278,6 @@ export function keywordFoundInSentence(
return pattern.test(sentence);
}

export type LemmyErrorValue = LemmyErrorType["error"];

export function isLemmyError(error: unknown, lemmyErrorValue: LemmyErrorValue) {
if (!(error instanceof Error)) return;
return error.message === lemmyErrorValue;
}

export function canModerateCommunity(
communityId: number | undefined,
moderates: CommunityModeratorView[] | undefined,
Expand Down Expand Up @@ -320,31 +312,6 @@ export function buildCrosspostBody(post: Post): string {
return `${header}\n>\n${quote(post.body)}`;
}

export function getLoginErrorMessage(
error: unknown,
instanceActorId: string,
): string {
if (!(error instanceof Error))
return "Unknown error occurred, please try again.";

switch (error.message as LemmyErrorValue) {
case "incorrect_totp_token":
return "Incorrect 2nd factor code. Please try again.";
case "couldnt_find_person":
return `User not found. Is your account on ${instanceActorId}?`;
case "incorrect_login":
return `Incorrect login credentials for ${instanceActorId}. Please try again.`;
case "email_not_verified":
return `Email not verified. Please check your inbox. Request a new verification email from https://${instanceActorId}.`;
case "site_ban":
return "You have been banned.";
case "deleted":
return "Account deleted.";
default:
return "Connection error, please try again.";
}
}

export function isPost(item: PostView | CommentView): item is PostView {
return !isComment(item);
}
Expand Down
Loading

0 comments on commit 7bec240

Please sign in to comment.