From 8fbae2304250d0d858f331e1839215d4acbcef8b Mon Sep 17 00:00:00 2001 From: Gavin Kasdorf Date: Fri, 21 Jul 2023 05:32:14 -0700 Subject: [PATCH 1/2] cleanup --- ios/Memmy/Info.plist | 2 +- .../screens/Feed/components/FeedView.tsx | 20 +++++++----- src/components/screens/Post/PostScreen.tsx | 5 +++ .../Post/components/PostCommentItem.tsx | 31 ++++++++++++++++++- src/hooks/post/usePost.ts | 10 +++++- src/stores/posts/postsStore.ts | 18 +++++++++++ 6 files changed, 76 insertions(+), 10 deletions(-) diff --git a/ios/Memmy/Info.plist b/ios/Memmy/Info.plist index d45bd97b2..5fdbbe4ff 100644 --- a/ios/Memmy/Info.plist +++ b/ios/Memmy/Info.plist @@ -173,7 +173,7 @@ CFBundleVersion 2 CodePushDeploymentKey - l3pg50FJ4Bgt5LrFzkh0d10d8ukjpoAptMP2g + f3fsdf ITSAppUsesNonExemptEncryption LSApplicationCategoryType diff --git a/src/components/screens/Feed/components/FeedView.tsx b/src/components/screens/Feed/components/FeedView.tsx index acd9b206f..c2f76432e 100644 --- a/src/components/screens/Feed/components/FeedView.tsx +++ b/src/components/screens/Feed/components/FeedView.tsx @@ -36,12 +36,18 @@ import { } from "../../../../stores/feeds/feedsStore"; import { useCommunity } from "../../../../stores/communities/communitiesStore"; import loadFeedPosts from "../../../../stores/feeds/actions/loadFeedPosts"; +import HideReadFAB from "../../../common/Buttons/HideReadFAB"; +import setFeedPosts from "../../../../stores/feeds/actions/setFeedPosts"; +import { removeReadPosts } from "../../../../helpers/LemmyHelpers"; interface FeedViewProps { header?: () => React.ReactNode; } function FeedView({ header }: FeedViewProps) { + const { hideReadPostsOnFeed, showHideReadButton } = + useAppSelector(selectSettings); + const { key } = useRoute(); // Global state props @@ -161,13 +167,13 @@ function FeedView({ header }: FeedViewProps) { getItemType={getItemType} /> )} - {/* {hideReadPostsOnFeed && showHideReadButton && showFab && ( */} - {/* { */} - {/* feed.setPosts(removeReadPosts(feed.posts)); */} - {/* }} */} - {/* /> */} - {/* )} */} + {hideReadPostsOnFeed && showHideReadButton && ( + { + setFeedPosts(key, removeReadPosts(posts)); + }} + /> + )} ); } diff --git a/src/components/screens/Post/PostScreen.tsx b/src/components/screens/Post/PostScreen.tsx index 3c4fb22f5..b424b3ded 100644 --- a/src/components/screens/Post/PostScreen.tsx +++ b/src/components/screens/Post/PostScreen.tsx @@ -29,6 +29,8 @@ import { clearNewComment } from "../../../slices/comments/newCommentSlice"; import PostCommentItem from "./components/PostCommentItem"; import usePost from "../../../hooks/post/usePost"; import CommentSortButton from "./components/CommentSortButton"; +import { useCommunity } from "../../../stores/communities/communitiesStore"; +import { getBaseUrl } from "../../../helpers/LinkHelper"; interface IProps { navigation: NativeStackNavigationProp; @@ -44,6 +46,9 @@ function PostScreen({ navigation }: IProps) { const commentsSort = usePostCommentsSort(postKey); const comments = usePostComments(postKey); const commentsStatus = usePostCommentsStatus(postKey); + const community = useCommunity( + `${currentPost.community}@${getBaseUrl(currentPost.community.actor_id)}` + ); const { t } = useTranslation(); const theme = useTheme(); diff --git a/src/components/screens/Post/components/PostCommentItem.tsx b/src/components/screens/Post/components/PostCommentItem.tsx index 5b56f8681..16b692f23 100644 --- a/src/components/screens/Post/components/PostCommentItem.tsx +++ b/src/components/screens/Post/components/PostCommentItem.tsx @@ -1,11 +1,14 @@ -import React, { useCallback } from "react"; +import React, { useCallback, useEffect } from "react"; import { useRoute } from "@react-navigation/core"; +import { LayoutAnimation } from "react-native"; import CommentItem from "../../../common/Comments/CommentItem"; import setPostCommentVote from "../../../../stores/posts/actions/setPostCommentVote"; import { ILemmyVote } from "../../../../types/lemmy/ILemmyVote"; import { determineVotes } from "../../../../helpers/VoteHelper"; import { usePostComment, + usePostCommentCollapsed, + usePostCommentHidden, usePostsStore, } from "../../../../stores/posts/postsStore"; @@ -39,6 +42,32 @@ function PostCommentItem({ commentId }: IProps) { ); prevComment.collapsed = !prevComment.collapsed; prev.rerenderComments = !prev.rerenderComments; + + const prevToHide = prev.commentsState.comments.filter( + (c) => + c.comment.comment.path.includes(prevComment.comment.comment.path) && + c.comment.comment.id !== commentId + ); + + if (!prevComment.collapsed) { + prevToHide.forEach((c) => { + const shouldUnhide = + prevToHide.findIndex( + (cc) => + cc.collapsed && + c.comment.comment.path.includes(cc.comment.comment.path) && + c.comment.comment.id !== cc.comment.comment.id + ) === -1; + + if (shouldUnhide) { + c.hidden = false; + } + }); + } else { + prevToHide.forEach((c) => { + c.hidden = true; + }); + } }); }, [comment.comment.comment.id]); diff --git a/src/hooks/post/usePost.ts b/src/hooks/post/usePost.ts index eabefa104..bf3aa532d 100644 --- a/src/hooks/post/usePost.ts +++ b/src/hooks/post/usePost.ts @@ -1,6 +1,7 @@ import { useCallback } from "react"; import { useRoute } from "@react-navigation/core"; import { CommentSortType } from "lemmy-js-client"; +import { LayoutAnimation } from "react-native"; import { useCurrentPost, usePostCommentsSort, @@ -8,6 +9,8 @@ import { } from "../../stores/posts/postsStore"; import { setPostCollapsed } from "../../stores/posts/actions"; import loadPostComments from "../../stores/posts/actions/loadPostComments"; +import loadCommunity from "../../stores/communities/actions/loadCommunity"; +import { getBaseUrl } from "../../helpers/LinkHelper"; export interface UsePost { doLoad: () => void; @@ -22,8 +25,13 @@ const usePost = (): UsePost => { const doLoad = useCallback(() => { loadPostComments(postKey, { - sortType: commentsSortType, // TODO Use default here + sortType: commentsSortType, }).then(); + loadCommunity( + `${currentPost.community.name}@${getBaseUrl( + currentPost.community.actor_id + )}` + ).then(); }, [currentPost.post.id, commentsSortType]); const onPostPress = useCallback(() => { diff --git a/src/stores/posts/postsStore.ts b/src/stores/posts/postsStore.ts index e038c8f13..5a52318b2 100644 --- a/src/stores/posts/postsStore.ts +++ b/src/stores/posts/postsStore.ts @@ -60,3 +60,21 @@ export const usePostComment = (postKey: string, commentId: number) => .get(postKey) .commentsState.comments.find((c) => c.comment.comment.id === commentId) ); + +export const usePostCommentCollapsed = (postKey: string, commentId: number) => + usePostsStore( + (state) => + state.posts + .get(postKey) + .commentsState.comments.find((c) => c.comment.comment.id === commentId) + .collapsed + ); + +export const usePostCommentHidden = (postKey: string, commentId: number) => + usePostsStore( + (state) => + state.posts + .get(postKey) + .commentsState.comments.find((c) => c.comment.comment.id === commentId) + .hidden + ); From 42da65f05039d04701e8d69559e2bd376861c912 Mon Sep 17 00:00:00 2001 From: Gavin Kasdorf Date: Fri, 21 Jul 2023 05:43:52 -0700 Subject: [PATCH 2/2] cleanup --- .../common/ImageViewer/ImageViewFooter.tsx | 11 +++++++++++ src/components/common/ImageViewer/ImageViewer.tsx | 2 ++ src/components/screens/Post/PostScreen.tsx | 5 ----- .../screens/Post/components/PostCommentItem.tsx | 5 +---- .../Settings/Account/ViewAccountsScreen.tsx | 5 ++++- src/hooks/comments/useNewComment.ts | 3 ++- src/hooks/post/usePost.ts | 4 ++-- src/stores/communities/actions/loadCommunity.ts | 15 ++++++++++++++- 8 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/components/common/ImageViewer/ImageViewFooter.tsx b/src/components/common/ImageViewer/ImageViewFooter.tsx index 0ab6c54f7..38fc2691d 100644 --- a/src/components/common/ImageViewer/ImageViewFooter.tsx +++ b/src/components/common/ImageViewer/ImageViewFooter.tsx @@ -6,6 +6,8 @@ import { saveImage } from "../../../helpers/ImageHelper"; import { shareLink } from "../../../helpers/ShareHelper"; import IconButtonWithText from "../IconButtonWithText"; import SFIcon from "../icons/SFIcon"; +import { useAppDispatch } from "../../../../store"; +import { showToast } from "../../../slices/toast/toastSlice"; interface ImageViewFooterProps { source: string; @@ -13,11 +15,20 @@ interface ImageViewFooterProps { function ImageViewFooter({ source }: ImageViewFooterProps) { const theme = useTheme(); + const dispatch = useAppDispatch(); const onSave = async () => { onGenericHapticFeedback(); await saveImage(source); + + dispatch( + showToast({ + message: "Image saved.", + duration: 1500, + variant: "success", + }) + ); }; const onShare = async () => { diff --git a/src/components/common/ImageViewer/ImageViewer.tsx b/src/components/common/ImageViewer/ImageViewer.tsx index 4c04cf7af..5a1690263 100644 --- a/src/components/common/ImageViewer/ImageViewer.tsx +++ b/src/components/common/ImageViewer/ImageViewer.tsx @@ -34,6 +34,7 @@ import { useAppSelector } from "../../../../store"; import { selectSettings } from "../../../slices/settings/settingsSlice"; import ImageButton from "../Buttons/ImageButton"; import { onGenericHapticFeedback } from "../../../helpers/HapticFeedbackHelpers"; +import Toast from "../Toast"; interface IProps { source: string; @@ -618,6 +619,7 @@ function ImageViewer({ )} + {/* eslint-disable-next-line react/style-prop-object */} diff --git a/src/components/screens/Post/PostScreen.tsx b/src/components/screens/Post/PostScreen.tsx index b424b3ded..3c4fb22f5 100644 --- a/src/components/screens/Post/PostScreen.tsx +++ b/src/components/screens/Post/PostScreen.tsx @@ -29,8 +29,6 @@ import { clearNewComment } from "../../../slices/comments/newCommentSlice"; import PostCommentItem from "./components/PostCommentItem"; import usePost from "../../../hooks/post/usePost"; import CommentSortButton from "./components/CommentSortButton"; -import { useCommunity } from "../../../stores/communities/communitiesStore"; -import { getBaseUrl } from "../../../helpers/LinkHelper"; interface IProps { navigation: NativeStackNavigationProp; @@ -46,9 +44,6 @@ function PostScreen({ navigation }: IProps) { const commentsSort = usePostCommentsSort(postKey); const comments = usePostComments(postKey); const commentsStatus = usePostCommentsStatus(postKey); - const community = useCommunity( - `${currentPost.community}@${getBaseUrl(currentPost.community.actor_id)}` - ); const { t } = useTranslation(); const theme = useTheme(); diff --git a/src/components/screens/Post/components/PostCommentItem.tsx b/src/components/screens/Post/components/PostCommentItem.tsx index 16b692f23..970c3b8af 100644 --- a/src/components/screens/Post/components/PostCommentItem.tsx +++ b/src/components/screens/Post/components/PostCommentItem.tsx @@ -1,14 +1,11 @@ -import React, { useCallback, useEffect } from "react"; +import React, { useCallback } from "react"; import { useRoute } from "@react-navigation/core"; -import { LayoutAnimation } from "react-native"; import CommentItem from "../../../common/Comments/CommentItem"; import setPostCommentVote from "../../../../stores/posts/actions/setPostCommentVote"; import { ILemmyVote } from "../../../../types/lemmy/ILemmyVote"; import { determineVotes } from "../../../../helpers/VoteHelper"; import { usePostComment, - usePostCommentCollapsed, - usePostCommentHidden, usePostsStore, } from "../../../../stores/posts/postsStore"; diff --git a/src/components/screens/Settings/Account/ViewAccountsScreen.tsx b/src/components/screens/Settings/Account/ViewAccountsScreen.tsx index 0f214d581..076cceaa6 100644 --- a/src/components/screens/Settings/Account/ViewAccountsScreen.tsx +++ b/src/components/screens/Settings/Account/ViewAccountsScreen.tsx @@ -160,7 +160,10 @@ function ViewAccountsScreen({ navigation }: ViewAccountsScreenProps) { - + { parent_id: responseTo.comment ? responseTo.comment.comment.id : undefined, - language_id: responseTo.languageId, + language_id: + responseTo.languageId === 0 ? undefined : responseTo.languageId, }); useUpdatesStore diff --git a/src/hooks/post/usePost.ts b/src/hooks/post/usePost.ts index bf3aa532d..d879630c6 100644 --- a/src/hooks/post/usePost.ts +++ b/src/hooks/post/usePost.ts @@ -1,7 +1,6 @@ import { useCallback } from "react"; import { useRoute } from "@react-navigation/core"; import { CommentSortType } from "lemmy-js-client"; -import { LayoutAnimation } from "react-native"; import { useCurrentPost, usePostCommentsSort, @@ -30,7 +29,8 @@ const usePost = (): UsePost => { loadCommunity( `${currentPost.community.name}@${getBaseUrl( currentPost.community.actor_id - )}` + )}`, + true ).then(); }, [currentPost.post.id, commentsSortType]); diff --git a/src/stores/communities/actions/loadCommunity.ts b/src/stores/communities/actions/loadCommunity.ts index 4922f3a31..dde77a51c 100644 --- a/src/stores/communities/actions/loadCommunity.ts +++ b/src/stores/communities/actions/loadCommunity.ts @@ -2,7 +2,20 @@ import { useCommunitiesStore } from "../communitiesStore"; import { lemmyAuthToken, lemmyInstance } from "../../../LemmyInstance"; import { handleLemmyError } from "../../../helpers/LemmyErrorHelper"; -const loadCommunity = async (communityName: string) => { +const loadCommunity = async (communityName: string, noRefresh = false) => { + const current = useCommunitiesStore + .getState() + .communityStates.get(communityName); + + if ( + noRefresh && + current && + !current.status.loading && + !current.status.error + ) { + return; + } + useCommunitiesStore.setState((state) => { if (!state.communityStates.has(communityName)) { state.communityStates.set(communityName, {