Skip to content

Commit

Permalink
feat: timestamp as time ago
Browse files Browse the repository at this point in the history
  • Loading branch information
zugdev committed Dec 8, 2024
1 parent c303d49 commit 98f1463
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/home/rendering/render-github-issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GitHubAggregated, GitHubIssue, GitHubNotification, GitHubNotifications
import { renderErrorInModal } from "./display-popup-modal";
import { closeModal, modal, modalBodyInner, bottomBar, titleAnchor, titleHeader, bottomBarClearLabels } from "./render-preview-modal";
import { setupKeyboardNavigation } from "./setup-keyboard-navigation";
import { waitForElement } from "./utils";
import { getTimeAgo, waitForElement } from "./utils";
import { notificationsContainer } from "../home";

export function renderNotifications(notifications: GitHubAggregated[], skipAnimation: boolean) {
Expand Down Expand Up @@ -145,7 +145,8 @@ function parseAndGenerateLabels(notification: GitHubAggregated) {

// Add timestamp label
if (notification.notification.updated_at) {
labels.push(`<label class="timestamp">${new Date(notification.notification.updated_at).toLocaleString()}</label>`);
const timeAgo = getTimeAgo(new Date(notification.notification.updated_at));
labels.push(`<label class="timestamp">${timeAgo}</label>`);
}

return labels;
Expand Down
22 changes: 22 additions & 0 deletions src/home/rendering/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,25 @@ export function waitForElement(selector: string): Promise<Element> {
observer.observe(document.body, { childList: true, subtree: true });
});
}

export function getTimeAgo(date: Date): string {
const now = new Date();
const seconds = Math.floor((now.getTime() - date.getTime()) / 1000);
const intervals: { [key: string]: number } = {
year: 31536000,
month: 2592000,
week: 604800,
day: 86400,
hour: 3600,
minute: 60,
second: 1,
};

for (const [unit, value] of Object.entries(intervals)) {
const count = Math.floor(seconds / value);
if (count >= 1) {
return `${count} ${unit}${count > 1 ? 's' : ''} ago`;
}
}
return 'just now';
}

0 comments on commit 98f1463

Please sign in to comment.