Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow showing the content of collapsed comments #472

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions src/features/comment/Comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ function Comment({
depth,
absoluteDepth,
onClick,
collapsed,
collapsed: _collapsed,
context,
routerLink,
className,
rootIndex,
}: CommentProps) {
const showCollapsedComment = useAppSelector(
(state) => state.settings.general.comments.showCollapsed,
);
const commentFromStore = useAppSelector(
(state) => state.comment.commentById[commentView.comment.id],
);
Expand All @@ -128,6 +131,11 @@ function Comment({

const commentEllipsisHandleRef = useRef<CommentEllipsisHandle>(null);

const collapsed =
showCollapsedComment && !commentView.counts.child_count
? false
: _collapsed;

const onCommentLongPress = useCallback(() => {
commentEllipsisHandleRef.current?.present();
}, []);
Expand Down Expand Up @@ -183,27 +191,32 @@ function Comment({
<CommentVote item={commentView} />
<Edited item={commentView} />
<div style={{ flex: 1 }} />
{!collapsed ? (
<ActionsContainer>
{renderActions()}
<CommentEllipsis
comment={commentView}
rootIndex={rootIndex}
ref={commentEllipsisHandleRef}
/>
<Ago date={comment.published} />
</ActionsContainer>
) : (
<ActionsContainer
className={collapsed ? "ion-hide" : undefined}
>
{renderActions()}
<CommentEllipsis
comment={commentView}
rootIndex={rootIndex}
ref={commentEllipsisHandleRef}
/>
<Ago date={comment.published} />
</ActionsContainer>
{collapsed && (
<>
<AmountCollapsed>
{commentView.counts.child_count + 1}
{commentView.counts.child_count +
(showCollapsedComment ? 0 : 1)}
</AmountCollapsed>
<CollapsedIcon icon={chevronDownOutline} />
</>
)}
</Header>

<AnimateHeight duration={200} height={collapsed ? 0 : "auto"}>
<AnimateHeight
duration={200}
height={!showCollapsedComment && collapsed ? 0 : "auto"}
>
<Content
className="collapse-md-margins"
onClick={(e) => {
Expand Down
9 changes: 0 additions & 9 deletions src/features/comment/commentSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ export const commentSlice = createSlice({
if (comment.my_vote)
state.commentVotesById[comment.comment.id] = comment.my_vote as 1 | -1;
},

updateCommentCollapseState: (
state,
action: PayloadAction<{ commentId: number; collapsed: boolean }>,
) => {
state.commentCollapsedById[action.payload.commentId] =
action.payload.collapsed;
},
toggleCommentCollapseState: (state, action: PayloadAction<number>) => {
state.commentCollapsedById[action.payload] =
!state.commentCollapsedById[action.payload];
Expand Down Expand Up @@ -86,7 +78,6 @@ export const commentSlice = createSlice({
export const {
receivedComments,
mutatedComment,
updateCommentCollapseState,
toggleCommentCollapseState,
updateCommentVote,
updateCommentSaved,
Expand Down
17 changes: 6 additions & 11 deletions src/features/comment/inTree/CommentTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CommentNodeI } from "../../../helpers/lemmy";
import React, { RefObject, memo, useContext, useMemo } from "react";
import CommentHr from "./CommentHr";
import { useAppDispatch, useAppSelector } from "../../../store";
import { updateCommentCollapseState } from "../commentSlice";
import { toggleCommentCollapseState } from "../commentSlice";
import CommentExpander from "./CommentExpander";
import { OTapToCollapseType } from "../../../services/db";
import { getOffsetTop, scrollIntoView } from "../../../helpers/dom";
Expand Down Expand Up @@ -34,8 +34,8 @@ function CommentTree({
(state) =>
state.comment.commentCollapsedById[comment.comment_view.comment.id],
);
const { tapToCollapse } = useAppSelector(
(state) => state.settings.general.comments,
const tapToCollapse = useAppSelector(
(state) => state.settings.general.comments.tapToCollapse,
);
const { activePageRef } = useContext(AppContext);

Expand All @@ -53,13 +53,8 @@ function CommentTree({
return false;
}, [comment.comment_view.comment.path, highlightedCommentId]);

function setCollapsed(collapsed: boolean) {
dispatch(
updateCommentCollapseState({
commentId: comment.comment_view.comment.id,
collapsed,
}),
);
function toggleCollapsed() {
dispatch(toggleCommentCollapseState(comment.comment_view.comment.id));
}

if (
Expand Down Expand Up @@ -101,7 +96,7 @@ function CommentTree({
)
return;

setCollapsed(!collapsed);
toggleCollapsed();

scrollCommentIntoViewIfNeeded(e.target, activePageRef);
}}
Expand Down
2 changes: 2 additions & 0 deletions src/features/settings/general/comments/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import HighlightNewAccount from "./HighlightNewAccount";
import TouchFriendlyLinks from "./TouchFriendlyLinks";
import TapToCollapse from "./TapToCollapse";
import ShowCommentImages from "./ShowCommentImages";
import ShowCollapsed from "./ShowCollapsed";

export default function Comments() {
return (
Expand All @@ -24,6 +25,7 @@ export default function Comments() {
<HighlightNewAccount />
<TouchFriendlyLinks />
<ShowCommentImages />
<ShowCollapsed />
</IonList>
</>
);
Expand Down
22 changes: 22 additions & 0 deletions src/features/settings/general/comments/ShowCollapsed.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IonToggle } from "@ionic/react";
import { useAppDispatch, useAppSelector } from "../../../../store";
import { InsetIonItem } from "../../shared/formatting";
import { setShowCollapsedComment } from "../../settingsSlice";

export default function ShowCollapsed() {
const dispatch = useAppDispatch();
const showCollapsedComment = useAppSelector(
(state) => state.settings.general.comments.showCollapsed,
);

return (
<InsetIonItem>
<IonToggle
checked={showCollapsedComment}
onIonChange={(e) => dispatch(setShowCollapsedComment(e.detail.checked))}
>
Show Collapsed Comment
</IonToggle>
</InsetIonItem>
);
}
13 changes: 13 additions & 0 deletions src/features/settings/settingsSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ interface SettingsState {
highlightNewAccount: boolean;
touchFriendlyLinks: boolean;
showCommentImages: boolean;
showCollapsed: boolean;
};
posts: {
sort: SortType;
Expand Down Expand Up @@ -191,6 +192,7 @@ export const initialState: SettingsState = {
highlightNewAccount: true,
touchFriendlyLinks: true,
showCommentImages: true,
showCollapsed: false,
},
posts: {
sort: "Active",
Expand Down Expand Up @@ -325,6 +327,10 @@ export const appearanceSlice = createSlice({
state.general.comments.showCommentImages = action.payload;
db.setSetting("show_comment_images", action.payload);
},
setShowCollapsedComment(state, action: PayloadAction<boolean>) {
state.general.comments.showCollapsed = action.payload;
db.setSetting("show_collapsed_comment", action.payload);
},
setPostAppearance(state, action: PayloadAction<PostAppearanceType>) {
state.appearance.posts.type = action.payload;
db.setSetting("post_appearance_type", action.payload);
Expand Down Expand Up @@ -637,6 +643,9 @@ export const fetchSettingsFromDatabase = createAsyncThunk<SettingsState>(
const filtered_keywords = await db.getSetting("filtered_keywords");
const touch_friendly_links = await db.getSetting("touch_friendly_links");
const show_comment_images = await db.getSetting("show_comment_images");
const show_collapsed_comment = await db.getSetting(
"show_collapsed_comment",
);
const no_subscribed_in_feed = await db.getSetting(
"no_subscribed_in_feed",
);
Expand Down Expand Up @@ -713,6 +722,9 @@ export const fetchSettingsFromDatabase = createAsyncThunk<SettingsState>(
showCommentImages:
show_comment_images ??
initialState.general.comments.showCommentImages,
showCollapsed:
show_collapsed_comment ??
initialState.general.comments.showCollapsed,
},
posts: {
disableMarkingRead:
Expand Down Expand Up @@ -824,6 +836,7 @@ export const {
setDefaultFeed,
setNoSubscribedInFeed,
setAlwaysUseReaderMode,
setShowCollapsedComment,
} = appearanceSlice.actions;

export default appearanceSlice.reducer;
Expand Down
11 changes: 3 additions & 8 deletions src/features/shared/sliding/BaseSlidingVote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
} from "../../../helpers/toastMessages";
import {
saveComment,
updateCommentCollapseState,
toggleCommentCollapseState,
voteOnComment,
} from "../../comment/commentSlice";
import { PageContext } from "../../auth/PageContext";
Expand Down Expand Up @@ -247,16 +247,11 @@ function BaseSlidingVoteInternal({
(e: TouchEvent | MouseEvent) => {
if (isPost) return;

dispatch(
updateCommentCollapseState({
commentId: item.comment.id,
collapsed: !collapsed,
}),
);
dispatch(toggleCommentCollapseState(item.comment.id));

if (e.target) scrollCommentIntoViewIfNeeded(e.target, activePageRef);
},
[collapsed, dispatch, isPost, item, activePageRef],
[dispatch, isPost, item, activePageRef],
);
const collapseAction = useMemo(() => {
return {
Expand Down
1 change: 1 addition & 0 deletions src/services/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ export type SettingValueTypes = {
embed_crossposts: boolean;
show_community_icons: boolean;
autoplay_media: AutoplayMediaType;
show_collapsed_comment: boolean;
};

export interface ISettingItem<T extends keyof SettingValueTypes> {
Expand Down
Loading