* refactor: move trackEvents to a utilities folder so that it can be re-used * feat: add a function called trackCommentClicks and use it on the articlePage * feat: update the auth widget to take the correct properties * feat: add create account tracking method for hamburger * feat: add create account tracking method for sidebar left * feat: whoops, sidebar tracking info added * feat: add the feed card create account tracking * feat: whoops add the id to the dom * feat: add top nav create account tracking * feat: create account tracking for the registration modal * fix: callback instead of invoking + rename class * fix: js-prepend * feat: some showLogin event tracking * use the tracking propert * x - feed.jsx * fix: oops I removed hidden * track from the search login * feat: add tracking for follow button * fix: main_stories is only being called in view/articles and both are including the FollowButtons pack * comments track * feat: remove punc (.) * chore: cahnge working * chore: remove tracking-id from modal * feat: remove ecmascript features * refactor: rename some events * feat: referrer page * feat: add a secondary source for the follow buttons * feat: remove file that is not used * chore: rename key * feat: use @utilities * feat: add spec for create account clicks * feat: add key referrer * fix: tests * fix: oops * chore: add commented test * test for tracking * feat: update the change by 1 * feat: aggregate_failures * feat: add data-no-instant back * feat: try a different way of testing * refactor: change secondary_source to referring_souce * feat: change ahoy event test * feat: add a version number to ahoy tracking * feat: change the href to pathname * feat: reply comment tracking * fix: to be present syntax * feat: use href instead of pathname * chore: do not track tags and podcasts follow and remove referrer * feat: add readinglist * fix: remove referrer from test * feat: fail gracefully in instances where there is no tracking data
205 lines
6 KiB
JavaScript
205 lines
6 KiB
JavaScript
import { h } from 'preact';
|
|
import { useEffect, useState } from 'preact/hooks';
|
|
import PropTypes from 'prop-types';
|
|
import { useListNavigation } from '../shared/components/useListNavigation';
|
|
import { useKeyboardShortcuts } from '../shared/components/useKeyboardShortcuts';
|
|
|
|
/* global userData sendHapticMessage showLoginModal buttonFormData renderNewSidebarCount */
|
|
|
|
export const Feed = ({ timeFrame, renderFeed }) => {
|
|
const { reading_list_ids = [] } = userData(); // eslint-disable-line camelcase
|
|
const [bookmarkedFeedItems, setBookmarkedFeedItems] = useState(
|
|
new Set(reading_list_ids),
|
|
);
|
|
const [pinnedArticle, setPinnedArticle] = useState(null);
|
|
const [feedItems, setFeedItems] = useState([]);
|
|
const [podcastEpisodes, setPodcastEpisodes] = useState([]);
|
|
const [onError, setOnError] = useState(false);
|
|
|
|
useEffect(() => {
|
|
setPodcastEpisodes(getPodcastEpisodes());
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
const fetchFeedItems = async () => {
|
|
try {
|
|
if (onError) setOnError(false);
|
|
|
|
let feedItems = await getFeedItems(timeFrame);
|
|
|
|
// Here we extract from the feed two special items: pinned and featured
|
|
|
|
const pinnedArticle = feedItems.find((story) => story.pinned === true);
|
|
|
|
// Ensure first article is one with a main_image
|
|
// This is important because the featuredStory will
|
|
// appear at the top of the feed, with a larger
|
|
// main_image than any of the stories or feed elements.
|
|
const featuredStory = feedItems.find(
|
|
(story) => story.main_image !== null,
|
|
);
|
|
|
|
// If pinned and featured article aren't the same,
|
|
// (either because featuredStory is missing or because they represent two different articles),
|
|
// we set the pinnedArticle and remove it from feedItems.
|
|
// If pinned and featured are the same, we just remove it from feedItems without setting it as state.
|
|
// NB: We only show the pinned post on the "Relevant" feed (when there is no 'timeFrame' selected)
|
|
if (pinnedArticle && timeFrame === '') {
|
|
feedItems = feedItems.filter((item) => item.id !== pinnedArticle.id);
|
|
|
|
if (pinnedArticle.id !== featuredStory?.id) {
|
|
setPinnedArticle(pinnedArticle);
|
|
}
|
|
}
|
|
|
|
// Remove that first story from the array to
|
|
// prevent it from rendering twice in the feed.
|
|
const featuredIndex = feedItems.indexOf(featuredStory);
|
|
if (featuredStory) {
|
|
feedItems.splice(featuredIndex, 1);
|
|
}
|
|
const organizedFeedItems = [featuredStory, feedItems].flat();
|
|
|
|
setFeedItems(organizedFeedItems);
|
|
} catch {
|
|
if (!onError) setOnError(true);
|
|
}
|
|
};
|
|
|
|
fetchFeedItems();
|
|
}, [timeFrame, onError]);
|
|
|
|
/**
|
|
* Retrieves feed data.
|
|
*
|
|
* @param {number} [page=1] Page of feed data to retrieve
|
|
*
|
|
* @returns {Promise} A promise containing the JSON response for the feed data.
|
|
*/
|
|
async function getFeedItems(timeFrame = '', page = 1) {
|
|
const response = await fetch(`/stories/feed/${timeFrame}?page=${page}`, {
|
|
method: 'GET',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'X-CSRF-Token': window.csrfToken,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
credentials: 'same-origin',
|
|
});
|
|
return await response.json();
|
|
}
|
|
|
|
function getPodcastEpisodes() {
|
|
const el = document.getElementById('followed-podcasts');
|
|
const user = userData(); // Global
|
|
const episodes = [];
|
|
if (
|
|
user &&
|
|
user.followed_podcast_ids &&
|
|
user.followed_podcast_ids.length > 0
|
|
) {
|
|
const data = JSON.parse(el.dataset.episodes);
|
|
data.forEach((episode) => {
|
|
if (user.followed_podcast_ids.indexOf(episode.podcast.id) > -1) {
|
|
episodes.push(episode);
|
|
}
|
|
});
|
|
}
|
|
return episodes;
|
|
}
|
|
|
|
/**
|
|
* Dispatches a click event to bookmark/unbookmark an article.
|
|
*
|
|
* @param {Event} event
|
|
*/
|
|
async function bookmarkClick(event) {
|
|
// The assumption is that the user is logged on at this point.
|
|
const { userStatus } = document.body;
|
|
event.preventDefault();
|
|
sendHapticMessage('medium');
|
|
|
|
if (userStatus === 'logged-out') {
|
|
showLoginModal({
|
|
referring_source: 'post_index_toolbar',
|
|
trigger: 'readinglist',
|
|
});
|
|
return;
|
|
}
|
|
|
|
const { currentTarget: button } = event;
|
|
const data = buttonFormData(button);
|
|
|
|
const csrfToken = await getCsrfToken();
|
|
if (!csrfToken) return;
|
|
|
|
const fetchCallback = sendFetch('reaction-creation', data);
|
|
const response = await fetchCallback(csrfToken);
|
|
if (response.status === 200) {
|
|
const json = await response.json();
|
|
const articleId = Number(button.dataset.reactableId);
|
|
|
|
const { result } = json;
|
|
const updatedBookmarkedFeedItems = new Set([
|
|
...bookmarkedFeedItems.values(),
|
|
]);
|
|
|
|
if (result === 'create') {
|
|
updatedBookmarkedFeedItems.add(articleId);
|
|
}
|
|
|
|
if (result === 'destroy') {
|
|
updatedBookmarkedFeedItems.delete(articleId);
|
|
}
|
|
|
|
renderNewSidebarCount(button, json);
|
|
|
|
setBookmarkedFeedItems(updatedBookmarkedFeedItems);
|
|
}
|
|
}
|
|
|
|
useListNavigation(
|
|
'article.crayons-story',
|
|
'a.crayons-story__hidden-navigation-link',
|
|
'div.paged-stories',
|
|
);
|
|
|
|
useKeyboardShortcuts({
|
|
b: (event) => {
|
|
const article = event.target?.closest('article.crayons-story');
|
|
|
|
if (!article) return;
|
|
|
|
article.querySelector('button[id^=article-save-button]')?.click();
|
|
},
|
|
});
|
|
|
|
return (
|
|
<div id="rendered-article-feed">
|
|
{onError ? (
|
|
<div class="crayons-notice crayons-notice--danger">
|
|
There was a problem fetching your feed.
|
|
</div>
|
|
) : (
|
|
renderFeed({
|
|
pinnedArticle,
|
|
feedItems,
|
|
podcastEpisodes,
|
|
bookmarkedFeedItems,
|
|
bookmarkClick,
|
|
})
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
Feed.defaultProps = {
|
|
timeFrame: '',
|
|
};
|
|
|
|
Feed.propTypes = {
|
|
timeFrame: PropTypes.string,
|
|
renderFeed: PropTypes.func.isRequired,
|
|
};
|
|
|
|
Feed.displayName = 'Feed';
|