Skip to content

Commit

Permalink
Merge branch 'bot_ui_pluralize_time' into 'master'
Browse files Browse the repository at this point in the history
Bot UI: Refactor timeAgo function to use existing pluralize utility

See merge request postgres-ai/database-lab!896
  • Loading branch information
Bogdan Tsechoev committed Aug 20, 2024
2 parents 2454f58 + 0c20d3b commit 3fa1ade
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions ui/packages/platform/src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import moment from 'moment'
import { pluralize } from "./time";

const Format = {
formatSeconds: function (seconds: number, decimal: number, separator = ' ') {
Expand Down Expand Up @@ -266,6 +267,7 @@ const Format = {
timeAgo: function (date: string | Date): string | null {
if (!date) return null


const now = new Date();
const past = new Date(date);
const diff = Math.abs(now.getTime() - past.getTime());
Expand All @@ -275,13 +277,13 @@ const Format = {
const days = Math.floor(hours / 24);

if (seconds < 60) {
return `${seconds} seconds ago`;
return `${seconds} ${pluralize('second', 'seconds')(seconds)} ago`;
} else if (minutes < 60) {
return `${minutes} minutes ago`;
return `${minutes} ${pluralize('minute', 'minutes')(minutes)} ago`;
} else if (hours < 24) {
return `${hours} hours ago`;
return `${hours} ${pluralize('hour', 'hours')(hours)} ago`;
} else {
return `${days} days ago`;
return `${days} ${pluralize('day', 'days')(seconds)} ago`;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ui/packages/platform/src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pluralize = (single: string, multiple: string) => (val: number) =>
export const pluralize = (single: string, multiple: string) => (val: number) =>
val === 1 ? single : multiple

const MS_IN_SECOND = 1000
Expand Down

0 comments on commit 3fa1ade

Please sign in to comment.