Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Refactor expansion #559

Merged
merged 2 commits into from
Mar 22, 2022
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
3 changes: 0 additions & 3 deletions src/App.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ export const LoggedOutHiddenPicks = () => (
expanded={false}
onPermalinkClick={() => {}}
apiKey=""
onExpanded={(expandedTime) => {
console.log(expandedTime);
}}
/>
</div>
);
Expand Down
33 changes: 9 additions & 24 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ type Props = {
parentCommentId: number,
) => Promise<CommentResponse>;
onPreview?: (body: string) => Promise<string>;
onExpanded?: (expandedTime: number) => void;
};

const footerStyles = css`
Expand Down Expand Up @@ -210,7 +209,7 @@ const writeMutes = (mutes: string[]) => {
// capture these errors
}
};
let expandedStartTime: number;

export const App = ({
baseUrl,
shortUrl,
Expand All @@ -229,7 +228,6 @@ export const App = ({
onComment,
onReply,
onPreview,
onExpanded,
}: Props) => {
const [filters, setFilters] = useState<FilterOptions>(
initialiseFilters({
Expand Down Expand Up @@ -258,17 +256,6 @@ export const App = ({
const [commentCount, setCommentCount] = useState<number>(0);
const [mutes, setMutes] = useState<string[]>(readMutes());

useEffect(() => {
if (
isExpanded &&
onExpanded &&
window.performance &&
window.performance.now
) {
onExpanded(window.performance.now() - expandedStartTime);
}
}, [isExpanded, onExpanded]);

useEffect(() => {
if (isExpanded) {
// We want react to complete the current work and render, without trying to batch this update
Expand All @@ -284,6 +271,12 @@ export const App = ({
}
}, [isExpanded, comments.length]);

useEffect(() => {
// We need this use effect to capture any changes in the expanded prop. This is typicallly
// seen when clicking permalinks
setIsExpanded(expanded);
}, [expanded]);

useEffect(() => {
setLoading(true);
getDiscussion(shortUrl, { ...filters, page }).then((json) => {
Expand Down Expand Up @@ -367,14 +360,6 @@ export const App = ({
setPage(page);
};

const expandView = () => {
if (window.performance && window.performance.now) {
expandedStartTime = window.performance.now();
}

setIsExpanded(true);
};

const toggleMuteStatus = (userId: string) => {
let updatedMutes;
if (mutes.includes(userId)) {
Expand All @@ -397,7 +382,7 @@ export const App = ({

if (!isExpanded) {
// It's possible to post a comment without the view being expanded
expandView();
setIsExpanded(true);
}

const commentElement = document.getElementById(`comment-${comment.id}`);
Expand Down Expand Up @@ -490,7 +475,7 @@ export const App = ({
>
<PillarButton
pillar={pillar}
onClick={() => expandView()}
onClick={() => setIsExpanded(true)}
icon={<SvgPlus />}
iconSide="left"
linkName="more-comments"
Expand Down