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

Commit

Permalink
Merge pull request #464 from guardian/gtrufitt/onExpanded-event
Browse files Browse the repository at this point in the history
Add onExpanded prop to track expanding time
  • Loading branch information
gtrufitt authored Mar 8, 2021
2 parents 332c46f + 2da30e8 commit a247b9d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/App.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export const LoggedOutHiddenPicks = () => (
expanded={false}
onPermalinkClick={() => {}}
apiKey=""
onExpanded={(expandedTime) => {
console.log(expandedTime);
}}
/>
</div>
);
Expand Down
18 changes: 17 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Props = {
parentCommentId: number,
) => Promise<CommentResponse>;
onPreview?: (body: string) => Promise<string>;
onExpanded?: (expandedTime: number) => void;
};

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

let expandedStartTime: number;
export const App = ({
baseUrl,
shortUrl,
Expand All @@ -230,6 +231,7 @@ export const App = ({
onComment,
onReply,
onPreview,
onExpanded,
}: Props) => {
const [filters, setFilters] = useState<FilterOptions>(
initialiseFilters({
Expand All @@ -252,6 +254,16 @@ 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]);
useEffect(() => {
setLoading(true);
getDiscussion(shortUrl, { ...filters, page }).then((json) => {
Expand Down Expand Up @@ -335,6 +347,10 @@ export const App = ({
};

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

setIsExpanded(true);
};

Expand Down

0 comments on commit a247b9d

Please sign in to comment.