Skip to content

Commit

Permalink
Merge pull request #779 from hackclub/fix-hour-wraparound
Browse files Browse the repository at this point in the history
Fix hour count wraparound
  • Loading branch information
maxwofford authored Nov 15, 2024
2 parents 6edda6b + e913d0a commit 99204df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
10 changes: 10 additions & 0 deletions lib/pluralize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// pluralize(1, 'apple') => '1 apple'
// pluralize(2, 'apple') => '2 apples'
// pluralize(2, 'apple', false) => 'apples'

export default function pluralize(count, singular, includeCount = true) {
return (
(includeCount ? `${count} ` : '') +
(count === 1 ? singular : singular + 's')
)
}
26 changes: 14 additions & 12 deletions src/app/harbor/signpost/signpost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Cookies from 'js-cookie'
import FeedItems from './feed-items'
import { getWakaSessions } from '@/app/utils/waka'

import pluralize from '../../../../lib/pluralize.js'

export default function Signpost() {
let wakaKey: string | null = null
let hasHb: boolean | null = null
Expand Down Expand Up @@ -47,13 +49,13 @@ export default function Signpost() {
})
}, [])

const hms = wakaSessions
? new Date(wakaSessions.reduce((a, p) => (a += p.total), 0) * 1_000)
.toISOString()
.slice(11, 19)
.split(':')
.map((s) => Number(s))
: null
const wakaDuration = wakaSessions?.reduce((a, p) => (a += p.total), 0)
const hms = { hours: 0, minutes: 0, seconds: 0 }
if (wakaDuration) {
hms.hours = Math.floor(wakaDuration / 3600)
hms.minutes = Math.floor((wakaDuration % 3600) / 60)
hms.seconds = wakaDuration % 10
}

// Show or hide instructions for installing Hackatime
const [showInstructions, setShowInstructions] = useState(!hasHb)
Expand Down Expand Up @@ -91,16 +93,16 @@ export default function Signpost() {
<p className="text-md md:text-lg">
{hasHb ? (
<>
{hms ? (
{wakaDuration ? (
<p>
<span>
You've logged {hms[0]} hour{hms[0] !== 1 ? 's' : ''},{' '}
{hms[1]} minute{hms[1] !== 1 ? 's' : ''},{' '}
You've logged {pluralize(hms.hours, 'hour')},{' '}
{pluralize(hms.minutes, 'minute')},{' '}
</span>
<br className="sm:hidden"></br>
<span>
and {hms[2]} second{hms[2] !== 1 ? 's' : ''} of coding time
so far!
and {pluralize(hms.seconds, 'second')} of coding time so
far!
</span>
</p>
) : (
Expand Down

0 comments on commit 99204df

Please sign in to comment.