Skip to content

Commit

Permalink
tracking changelog last visit + grouping by date
Browse files Browse the repository at this point in the history
  • Loading branch information
aorcsik committed Feb 9, 2024
1 parent bc23558 commit f43e5df
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const http = require('http');
const fs = require('fs');
const path = require('path');

const hostname = '127.0.0.1';
const hostname = process.argv[3] || '127.0.0.1';
const port = 3000;

const webflowDomain = process.argv[2] || 'webflow.bitrise.io';
Expand Down
30 changes: 28 additions & 2 deletions src/js/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,33 @@ const changelogList = new ChangelogList(document.getElementById("changelog-list"
/** @type {HTMLAnchorElement} */
const changelogLoadMoreButton = document.getElementById("changelog-load-more-button");

const now = new Date();
let lastVisitedDate = now;

/** @type {string} */
const cookieName = "changelogLastVisit";
const cookiePattern = new RegExp(`${cookieName}=([^;]+);?`);
const cookieMatch = document.cookie.match(cookiePattern);
if (cookieMatch) lastVisitedDate = new Date(cookieMatch[1]);

const expires = new Date();
expires.setMonth(expires.getMonth() + 12);
document.cookie = `${cookieName}=${now};expires=${expires};`;

const url = new URL(document.location.href);
const queryLAstVisit = url.searchParams.get("lastVisit");
if (queryLAstVisit) {
lastVisitedDate = new Date();
lastVisitedDate.setDate(lastVisitedDate.getDate() - parseInt(queryLAstVisit));
}

/* -- TEST -- */
// const testDate = new Date();
// testDate.setMonth(testDate.getMonth() - 1);
// lastVisitedDate = testDate;
/* -- TEST -- */

changelogLoadMoreButton.remove();
changelogService.loadTopics("/changelog.json").then(topics => {
changelogList.render(topics);
});
changelogList.render(topics, lastVisitedDate);
});
22 changes: 19 additions & 3 deletions src/js/changelog/ChangelogList.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class ChangelogList
this.readListItemTemplate.id = "";
this.readListItemTemplate.remove();

/** @type {HTMLLIElement} */
this.timestampListItemTemplate = this.list.querySelector("#changelog-group-timestamp-template");
this.timestampListItemTemplate.id = "";
this.timestampListItemTemplate.remove();

this.list.append(this.renderLoadingListItem());
this.list.append(this.renderLoadingListItem());
this.list.append(this.renderLoadingListItem());
Expand Down Expand Up @@ -47,13 +52,24 @@ class ChangelogList
}

/**
* @param {ChangelogTopic[]} topics
* @param {ChangelogTopic[]} topics
* @param {Date} lastVisitedDate
*/
render(topics) {
render(topics, lastVisitedDate) {
this.list.innerHTML = "";
let timestamp = null;
for (let topic of topics) {
if (!this.listItems[topic.id]) {
const listItem = this.renderListItem(topic);

if (timestamp != topic.createdAt.toLocaleDateString()) {
timestamp = topic.createdAt.toLocaleDateString();
const timestampListItem = this.timestampListItemTemplate.cloneNode(true);
timestampListItem.innerHTML = timestamp;
this.list.append(timestampListItem);
}

const isUnread = topic.createdAt.getTime() > lastVisitedDate.getTime();
const listItem = this.renderListItem(topic, isUnread);
this.listItems[topic.id] = listItem;
this.list.append(listItem);
}
Expand Down
1 change: 0 additions & 1 deletion src/js/changelog/ChangelogService.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ async loadTopics(changelogUrl) {
const url = this.apiBase + changelogUrl + "?_=" + chacheBuster;
const response = await fetch(url);
const json = await response.json();
console.log(json);
return json.topic_list.topics.map(data => new ChangelogTopic(data));
}

Expand Down

0 comments on commit f43e5df

Please sign in to comment.