Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit aac54bf

Browse files
committed
webui: Move time period format function to JSX code
1 parent 6b2e460 commit aac54bf

12 files changed

+69
-57
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ webui/js/db-header.js
6363
webui/js/discussion-comments.js
6464
webui/js/discussion-create-mr.js
6565
webui/js/discussion-list.js
66+
webui/js/format.js
6667
webui/js/markdown-editor.js
6768
webui/js/profile-page.js
6869
webui/js/user-page.js

webui/js/local.js

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,6 @@ function base64url(input) {
77
return encoded
88
}
99

10-
// Returns a string describing how long ago the given date was. eg "3 seconds ago", "2 weeks ago", etc
11-
function getTimePeriod(date1, includeOn) {
12-
let d1 = new Date(date1);
13-
let secondsElapsed = (new Date() - d1) / 1000;
14-
15-
// Check if the time elapsed is more then 1 minute
16-
if (secondsElapsed >= 60) {
17-
let minutesElapsed = secondsElapsed / 60;
18-
19-
// Check if the time elapsed is more then 1 hour
20-
if (minutesElapsed >= 60) {
21-
let hoursElapsed = minutesElapsed / 60;
22-
23-
// Check if the time elapsed is more then 1 day
24-
if (hoursElapsed >= 24) {
25-
let daysElapsed = hoursElapsed / 24;
26-
daysElapsed = Math.round(daysElapsed);
27-
28-
// Check if the time elapsed is more then 1 week
29-
if (daysElapsed >= 7) {
30-
let weeksElapsed = daysElapsed / 7;
31-
weeksElapsed = Math.round(weeksElapsed);
32-
33-
// If the time elapsed is more then 4 weeks ago, we return the date, nicely formatted
34-
if (weeksElapsed > 4) {
35-
let str = includeOn ? "on " : "";
36-
return str + d1.toLocaleTimeString(undefined, { year: "numeric", month: "short",
37-
day: "numeric", weekday: "short", hour: "2-digit", minute: "2-digit" });
38-
} else {
39-
let p0 = (weeksElapsed === 1) ? "" : "s";
40-
return weeksElapsed + " week" + p0 + " ago"
41-
}
42-
} else {
43-
let p1 = (daysElapsed === 1) ? "" : "s";
44-
return daysElapsed + " day" + p1 + " ago"
45-
}
46-
} else {
47-
hoursElapsed = Math.round(hoursElapsed);
48-
let p2 = (hoursElapsed === 1 ) ? "" : "s";
49-
return hoursElapsed + " hour" + p2 + " ago";
50-
}
51-
} else {
52-
minutesElapsed = Math.round(minutesElapsed);
53-
let p3 = (minutesElapsed === 1) ? "" : "s";
54-
return minutesElapsed + " minute" + p3 + " ago";
55-
}
56-
} else {
57-
secondsElapsed=Math.round(secondsElapsed);
58-
if (secondsElapsed === 0) {
59-
return "now"
60-
} else {
61-
let p4 = (secondsElapsed === 1) ? "" : "s";
62-
return secondsElapsed+" second" + p4 + " ago";
63-
}
64-
}
65-
}
66-
6710
// Construct a timestamp string for use in user messages
6811
function nowString() {
6912
// Construct a timestamp for the success message

webui/jsx/commit-list.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const React = require("react");
22
const ReactDOM = require("react-dom");
33

4+
import {getTimePeriod} from "./format";
5+
46
export default function CommitList({commits, owner, database}) {
57
// Prepare rendered rows for commit table
68
const commitRows = (commits === null ? null : commits.map(row => (

webui/jsx/database-commits.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const React = require("react");
22
const ReactDOM = require("react-dom");
33

44
import Select from "react-dropdown-select";
5+
import {getTimePeriod} from "./format";
56

67
function DatabaseCommitRow({data, index, branch, setStatusMessage, setStatusMessageColour}) {
78
const [commitIndex, setCommitIndex] = React.useState(Number(index));

webui/jsx/database-tags.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const React = require("react");
22
const ReactDOM = require("react-dom");
33

44
import MarkdownEditor from "./markdown-editor";
5+
import {getTimePeriod} from "./format";
56

67
function DatabaseTagRow({name, data, releases, setStatusMessage, setStatusMessageColour}) {
78
// This is the tag name currently shown in the front end

webui/jsx/database-watchers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const React = require("react");
22
const ReactDOM = require("react-dom");
33

4+
import {getTimePeriod} from "./format";
5+
46
export default function DatabaseWatchers({stars}) {
57
let data = stars ? starsData : watchersData;
68

webui/jsx/db-header.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const React = require("react");
22
const ReactDOM = require("react-dom");
33

4+
import {getTimePeriod} from "./format";
5+
46
function ToggleButton({icon, textSet, textUnset, redirectUrl, updateUrl, pageUrl, isSet, count, cyToggle, cyPage, disabled}) {
57
const [state, setState] = React.useState(isSet);
68
const [number, setNumber] = React.useState(count);

webui/jsx/discussion-comments.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const ReactDOM = require("react-dom");
33

44
import MarkdownEditor from "./markdown-editor";
55
import CommitList from "./commit-list";
6+
import {getTimePeriod} from "./format";
67
import { confirmAlert } from "react-confirm-alert";
78
import "react-confirm-alert/src/react-confirm-alert.css";
89

webui/jsx/discussion-list.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const React = require("react");
22
const ReactDOM = require("react-dom");
33

4+
import {getTimePeriod} from "./format";
5+
46
function DiscussionListRow({data, mergeRequests}) {
57
return (
68
<tr>

webui/jsx/format.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Returns a string describing how long ago the given date was. eg "3 seconds ago", "2 weeks ago", etc
2+
export function getTimePeriod(date1, includeOn) {
3+
const d1 = new Date(date1);
4+
let secondsElapsed = (new Date() - d1) / 1000;
5+
6+
// Check if the time elapsed is more then 1 minute
7+
if (secondsElapsed >= 60) {
8+
let minutesElapsed = secondsElapsed / 60;
9+
10+
// Check if the time elapsed is more then 1 hour
11+
if (minutesElapsed >= 60) {
12+
let hoursElapsed = minutesElapsed / 60;
13+
14+
// Check if the time elapsed is more then 1 day
15+
if (hoursElapsed >= 24) {
16+
const daysElapsed = Math.round(hoursElapsed / 24);
17+
18+
// Check if the time elapsed is more then 1 week
19+
if (daysElapsed >= 7) {
20+
const weeksElapsed = Math.round(daysElapsed / 7);
21+
22+
// If the time elapsed is more then 4 weeks ago, we return the date, nicely formatted
23+
if (weeksElapsed > 4) {
24+
const str = includeOn ? "on " : "";
25+
return str + d1.toLocaleTimeString(undefined, {year: "numeric", month: "short",
26+
day: "numeric", weekday: "short", hour: "2-digit", minute: "2-digit"});
27+
} else {
28+
const p0 = (weeksElapsed === 1) ? "" : "s";
29+
return weeksElapsed + " week" + p0 + " ago";
30+
}
31+
} else {
32+
const p1 = (daysElapsed === 1) ? "" : "s";
33+
return daysElapsed + " day" + p1 + " ago";
34+
}
35+
} else {
36+
hoursElapsed = Math.round(hoursElapsed);
37+
const p2 = (hoursElapsed === 1 ) ? "" : "s";
38+
return hoursElapsed + " hour" + p2 + " ago";
39+
}
40+
} else {
41+
minutesElapsed = Math.round(minutesElapsed);
42+
const p3 = (minutesElapsed === 1) ? "" : "s";
43+
return minutesElapsed + " minute" + p3 + " ago";
44+
}
45+
} else {
46+
secondsElapsed = Math.round(secondsElapsed);
47+
if (secondsElapsed === 0) {
48+
return "now";
49+
} else {
50+
const p4 = (secondsElapsed === 1) ? "" : "s";
51+
return secondsElapsed + " second" + p4 + " ago";
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)