Skip to content

Commit

Permalink
Do not override user preferences in local storage (#10333)
Browse files Browse the repository at this point in the history
* do not restore user preferences

* Remove duplicate code

* Remove unused type
  • Loading branch information
abeddow91 authored Jan 26, 2024
1 parent 529bcfa commit 7675140
Showing 1 changed file with 14 additions and 38 deletions.
52 changes: 14 additions & 38 deletions dotcom-rendering/src/components/Discussion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {
CommentType,
FilterOptions,
SignedInUser,
ThreadsType,
} from '../types/discussion';
import { Comments } from './Discussion/Comments';
import { Hide } from './Hide';
Expand Down Expand Up @@ -71,16 +70,19 @@ const commentIdFromUrl = () => {
return parseInt(commentId, 10);
};

const filterByPermalinks = (
threads: ThreadsType,
const remapFilters = (
filters: FilterOptions,
hashCommentId: number | undefined,
) => {
const permalinkBeingUsed =
hashCommentId !== undefined && !Number.isNaN(hashCommentId);

return threads === 'collapsed' && permalinkBeingUsed
? 'expanded'
: undefined;
if (!permalinkBeingUsed) return filters;
if (filters.threads !== 'collapsed') return filters;
return {
...filters,
threads: 'expanded',
} satisfies FilterOptions;
};

export const Discussion = ({
Expand All @@ -93,10 +95,6 @@ export const Discussion = ({
idApiUrl,
}: Props) => {
const [commentPage, setCommentPage] = useState(1);
const [commentPageSize, setCommentPageSize] = useState<25 | 50 | 100>();
const [commentOrderBy, setCommentOrderBy] = useState<
'newest' | 'oldest' | 'recommendations'
>();
const [comments, setComments] = useState<CommentType[]>([]);
const [isClosedForComments, setIsClosedForComments] = useState(false);
const [isExpanded, setIsExpanded] = useState(false);
Expand Down Expand Up @@ -125,17 +123,7 @@ export const Discussion = ({
.catch((e) => console.error(`getDiscussion - error: ${String(e)}`));
}, [filters, commentPage, shortUrlId]);

useEffect(() => {
const orderByClosed = isClosedForComments ? 'oldest' : undefined;

setFilters((prevFilters) => ({
orderBy: commentOrderBy ?? orderByClosed ?? prevFilters.orderBy,
pageSize: commentPageSize ?? prevFilters.pageSize,
threads:
filterByPermalinks(prevFilters.threads, hashCommentId) ??
prevFilters.threads,
}));
}, [commentPageSize, commentOrderBy, isClosedForComments, hashCommentId]);
const validFilters = remapFilters(filters, hashCommentId);

useEffect(() => {
rememberFilters(filters);
Expand Down Expand Up @@ -163,8 +151,6 @@ export const Discussion = ({
getCommentContext(discussionApiUrl, hashCommentId)
.then((context) => {
setCommentPage(context.page);
setCommentPageSize(context.pageSize);
setCommentOrderBy(context.orderBy);
setIsExpanded(true);
})
.catch((e) =>
Expand All @@ -186,19 +172,6 @@ export const Discussion = ({
}
}, [commentCount]);

useEffect(() => {
if (window.location.hash === '#comments') {
setIsExpanded(true);
}
}, []);

useEffect(() => {
// There's no point showing the view more button if there isn't much more to view
if (commentCount === 0 || commentCount === 1 || commentCount === 2) {
setIsExpanded(true);
}
}, [commentCount]);

return (
<>
<div css={[positionRelative, !isExpanded && fixHeight]}>
Expand Down Expand Up @@ -238,8 +211,11 @@ export const Discussion = ({
idApiUrl={idApiUrl}
page={commentPage}
setPage={setCommentPage}
filters={filters}
setFilters={setFilters}
filters={validFilters}
setFilters={(newFilters) => {
setHashCommentId(undefined);
setFilters(newFilters);
}}
commentCount={commentCount ?? 0}
loading={loading}
totalPages={totalPages}
Expand Down

0 comments on commit 7675140

Please sign in to comment.