docbrown/app/javascript/packs/homePage.jsx
Ridhwana 8003691b37
Restructure the way we render the feed + add billboard locations on signed in feed (#19652)
* feat: fix of there is no article

* feat: set the feed order in a data structure rather than in a view

* feat: update yarn.lock

* feat: push the items to the array

* feat: update feed to only show billboards if we have the correct length of items in the feed

* fix: organizedFeedItems.legth

* feat: rename 'featured' to 'image'

* feat: rename 'featured' to 'image'

* feat: rename 'featured' to 'image'

* feat: add some utilities for the feed that we can use in the FeedTest

* feat/WIP: the setup for the feed tests and a first working test

* test: imageItem

* fix: the podcasts can be passed through as an array of objects in the feedItems so that they can be grouped in one card

* feat: remove podcastEpisode state and the attribute in the object

* fix: check that the items exist before trying to slice them

* feat: setup the userdata and the podcast data in the test

* whoops - commit the podcast episodes

* feat: write soem more tests for the feed and including the podcasts

* feat: add some more tests

* feat: add more billboards tests to chcek the order of stuff

* feat: set the timeframe not empty

* feat: update the logic for organizaed feed by inserting the last on first

* doc: jsdoc for functions

* refactor: break the code up into smaller functions

* refactor: make the code more readable and easier to follow

* refactor: pull function out into a utlity

* feat: add specs to utility

* feat: chcek if pinned post

* test the latest timeframe correctly

* chore: update var name

* move the podcast items out of the object clause

* feat: update text

* feat: add a pack file that duplicates initializeDisplayAdVisibility

* feat: create callbacks that will help us to determine when the feed has been rendered so that we can observe the dsplay ads accordingly

* chore: rename to billboards instead of display ad

* feat: first pass at some error handling whilst maintaining the order of the items

* test: for feed error

* feat: abstract out some code

* update the feed items for errors

* chore: comment

* feat: update the variable names

* feat: update honeybadger message
2023-07-03 15:00:44 +02:00

148 lines
4.1 KiB
JavaScript

import { h, render } from 'preact';
import ahoy from 'ahoy.js';
import { TagsFollowed } from '../leftSidebar/TagsFollowed';
import {
observeDisplayAds,
initializeDisplayAdVisibility,
} from '../packs/billboardAfterRenderActions';
import { setupDisplayAdDropdown } from '@utilities/displayAdDropdown';
import { trackCreateAccountClicks } from '@utilities/ahoy/trackEvents';
/* global userData */
// This logic is similar to that in initScrolling.js.erb
const frontPageFeedPathNames = new Map([
['/', ''],
['/top/week', 'week'],
['/top/month', 'month'],
['/top/year', 'year'],
['/top/infinity', 'infinity'],
['/latest', 'latest'],
]);
/**
* Renders tags followed in the left side bar of the homepage.
*
* @param {HTMLElement} tagsFollowedContainer DOM element to render tags followed.
* @param {object} user The currently logged on user, null if not logged on.
*/
function renderTagsFollowed(user = userData()) {
const tagsFollowedContainer = document.getElementById(
'sidebar-nav-followed-tags',
);
if (user === null || !tagsFollowedContainer) {
// Return and do not render if the user is not logged in
// or if this is not the home page.
return false;
}
const { followed_tags } = user; // eslint-disable-line camelcase
const followedTags = JSON.parse(followed_tags);
// This should be done server-side potentially
// sort tags by descending weight, descending popularity and name
followedTags.sort((tagA, tagB) => {
return (
tagB.points - tagA.points ||
tagB.hotness_score - tagA.hotness_score ||
tagA.name.localeCompare(tagB.name)
);
});
render(<TagsFollowed tags={followedTags} />, tagsFollowedContainer);
trackTagCogIconClicks();
}
// Temporary Ahoy Stats for usage reports
function trackTagCogIconClicks() {
document
.getElementById('tag-priority-link')
?.addEventListener('click', () => {
ahoy.track('Tag settings cog icon click');
});
}
function renderSidebar() {
const sidebarContainer = document.getElementById('sidebar-wrapper-right');
const { pathname } = window.location;
// If the screen's width is less than 640 we don't need this extra data.
if (
sidebarContainer &&
screen.width >= 640 &&
(pathname === '/' || pathname === '/latest' || pathname.includes('/top/'))
) {
window
.fetch('/sidebars/home')
.then((res) => res.text())
.then((response) => {
sidebarContainer.innerHTML = response;
setupDisplayAdDropdown();
});
}
}
const feedTimeFrame = frontPageFeedPathNames.get(window.location.pathname);
if (!document.getElementById('featured-story-marker')) {
const waitingForDataLoad = setInterval(() => {
const { user = null, userStatus } = document.body.dataset;
if (userStatus === 'logged-out') {
return;
}
if (userStatus === 'logged-in' && user !== null) {
clearInterval(waitingForDataLoad);
if (document.getElementById('rendered-article-feed')) {
return;
}
import('./homePageFeed').then(({ renderFeed }) => {
const callback = () => {
initializeDisplayAdVisibility();
observeDisplayAds();
};
renderFeed(feedTimeFrame, callback);
InstantClick.on('change', () => {
const { userStatus: currentUserStatus } = document.body.dataset;
if (currentUserStatus === 'logged-out') {
return;
}
const url = new URL(window.location);
const changedFeedTimeFrame = frontPageFeedPathNames.get(url.pathname);
if (!frontPageFeedPathNames.has(url.pathname)) {
return;
}
const callback = () => {
initializeDisplayAdVisibility();
observeDisplayAds();
};
renderFeed(changedFeedTimeFrame, callback);
});
});
renderTagsFollowed();
renderSidebar();
}
}, 2);
}
InstantClick.on('change', () => {
if (document.body.dataset.userStatus !== 'logged-in') {
// Nothing to do, the user is not logged on.
return false;
}
renderTagsFollowed();
renderSidebar();
});
InstantClick.init();
setupDisplayAdDropdown();
trackCreateAccountClicks('sidebar-wrapper-left');