Skip to content

Commit 0d94626

Browse files
author
Philip Aston
committed
Visually separate each calendar day in Logbook.
1 parent 27f542f commit 0d94626

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"@mapbox/geo-viewport": "^0.5.0",
6262
"circular-buffer": "^1.0.3",
6363
"jsonschema": "^1.4.1",
64+
"luxon": "^3.3.0",
6465
"ordinal": "^1.0.3",
6566
"timezones-list": "^3.0.2",
6667
"where": "^0.4.1",

src/components/Logbook.jsx

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
Button,
55
} from 'reactstrap';
66
import { Point } from 'where';
7+
import { DateTime } from 'luxon';
8+
import styles from './styles.module.css';
79

810
function getWeather(entry) {
911
const weather = [];
@@ -43,6 +45,29 @@ function getCourse(entry) {
4345
return '';
4446
}
4547

48+
function toDateTime(entry, props) {
49+
return DateTime.fromJSDate(entry.date).setZone(props.displayTimeZone);
50+
}
51+
52+
function entriesForSameDay(entry, previous, props) {
53+
function sameDay(d1, d2) {
54+
return d1.hasSame(d2, 'year') && d1.hasSame(d2, 'month') && d1.hasSame(d2, 'day');
55+
}
56+
57+
return previous && sameDay(toDateTime(previous, props), toDateTime(entry, props));
58+
}
59+
60+
function toLocaleString(entry, previous, props) {
61+
const dt = toDateTime(entry, props);
62+
63+
if (entriesForSameDay(entry, previous, props)) {
64+
return dt.toLocaleString(DateTime.DATETIME_SHORT_WITH_SECONDS);
65+
}
66+
else {
67+
return `${dt.toLocaleString(DateTime.DATETIME_SHORT_WITH_SECONDS)} (${dt.toLocaleString({ weekday: 'long' })})`;
68+
}
69+
}
70+
4671
function Logbook(props) {
4772
const entries = props.entries.map((entry) => ({
4873
...entry,
@@ -68,11 +93,10 @@ function Logbook(props) {
6893
</tr>
6994
</thead>
7095
<tbody>
71-
{entries.map((entry) => (
72-
<tr key={entry.datetime} onClick={() => props.editEntry(entry)}>
73-
<td>{entry.date.toLocaleString('en-GB', {
74-
timeZone: props.displayTimeZone,
75-
})}</td>
96+
{entries.map((entry, index) => (
97+
<tr className={!entriesForSameDay(entry, entries[index-1], props) && styles["new-day"]}
98+
key={entry.datetime} onClick={() => props.editEntry(entry)}>
99+
<td>{toLocaleString(entry, entries[index-1], props)}</td>
76100
<td>{getCourse(entry)}</td>
77101
<td>{entry.speed && !Number.isNaN(Number(entry.speed.sog)) ? `${entry.speed.sog}kt` : ''}</td>
78102
<td>{getWeather(entry)}</td>

src/components/styles.module.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.new-day {
2+
border-top: double 4px;
3+
}

0 commit comments

Comments
 (0)