Skip to content

Commit

Permalink
Improve undefined createdTime handling in time-ago helper (#626)
Browse files Browse the repository at this point in the history
Handle undefined `createdTime`s
  • Loading branch information
jeffdaley authored Mar 5, 2024
1 parent 8dd4e56 commit 333a6cd
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion web/app/components/dashboard/recently-viewed/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class DashboardRecentlyViewedItemComponent extends Component<Dash
/**
* The modified time of the item, whether it's a document or a project.
*/
protected get modifiedTime(): number {
protected get modifiedTime(): number | undefined {
if (this.itemIsDoc) {
return this.doc.modifiedTime || this.doc.createdTime;
} else {
Expand Down
2 changes: 1 addition & 1 deletion web/app/components/related-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface RelatedHermesDocument {
title: string;
documentType: string;
documentNumber: string;
createdTime: number;
createdTime?: number;
modifiedTime: number;
sortOrder: number;
product: string;
Expand Down
2 changes: 1 addition & 1 deletion web/app/helpers/time-ago.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import timeAgo from "hermes/utils/time-ago";

export interface TimeAgoHelperSignature {
Args: {
Positional: [time: number];
Positional: [time?: number];
Named: {
limitTo24Hours?: boolean;
};
Expand Down
3 changes: 2 additions & 1 deletion web/app/types/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ export interface HermesDocument {

/**
* A timestamp in seconds. Used for sorting.
* Undefined when the doc was created off-app.
*/
createdTime: number;
createdTime?: number;

/**
* A timestamp in seconds. Used Translated by the `time-ago` helper
Expand Down
4 changes: 3 additions & 1 deletion web/app/utils/time-ago.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import parseDate from "./parse-date";
* Used throughout the app to format document metadata.
*/
export default function timeAgo(
timeInSeconds: number,
timeInSeconds?: number,
options?: {
limitTo24Hours?: boolean;
},
) {
if (!timeInSeconds) return "Unknown date";

const now = Date.now();
const before = new Date(timeInSeconds * 1000).getTime();
const elapsed = now - before;
Expand Down
1 change: 1 addition & 0 deletions web/tests/unit/utils/time-ago-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module("Unit | Utility | time-ago", function () {
assert.equal("1 minute ago", timeAgo(now - 60));
assert.equal("5 minutes ago", timeAgo(now - 300));
assert.equal("6 hours ago", timeAgo(now - 21600));
assert.equal("Unknown date", timeAgo(undefined));

assert.equal("2 months ago", timeAgo(now - 5184000));

Expand Down

0 comments on commit 333a6cd

Please sign in to comment.