Skip to content

Commit d2f7bd8

Browse files
committed
refactor to use shared logic for determining if a timer is running
1 parent 7a734cc commit d2f7bd8

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/main/tray.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ let timerInterval: NodeJS.Timeout | undefined = undefined
1212

1313
let currentTrayTimeEntry: TimeEntry | null = null
1414

15+
function isTimerRunning(timeEntry: TimeEntry | null): boolean {
16+
// empty timers have a empty string as start time because of the init state in timeEntries.ts
17+
return !!(timeEntry && timeEntry.start && timeEntry.start !== '')
18+
}
19+
1520
function getIconPath(active: boolean) {
1621
// On macOS, template images (with 'Template' in filename) are automatically inverted by the OS
1722
// So we should always use the non-inverted version on macOS
@@ -28,13 +33,13 @@ function getIconPath(active: boolean) {
2833
}
2934

3035
function buildMenu(mainWindow: BrowserWindow, timeEntry: TimeEntry | null) {
31-
const isRunning = !!(timeEntry && timeEntry.start && timeEntry.start !== '')
36+
const isRunning = isTimerRunning(timeEntry)
3237

3338
return Menu.buildFromTemplate([
3439
{
3540
label: timeEntry?.description ?? '',
3641
enabled: false,
37-
visible: !!(isRunning && timeEntry.description && timeEntry.description !== ''),
42+
visible: !!(isRunning && timeEntry?.description && timeEntry.description !== ''),
3843
},
3944
{ label: isRunning ? 'Timer is running' : 'Timer is stopped', enabled: false },
4045
{ type: 'separator' },
@@ -75,19 +80,15 @@ export function initializeTray(mainWindow: Electron.BrowserWindow) {
7580
tray.setContextMenu(buildMenu(mainWindow, null))
7681

7782
nativeTheme.on('updated', () => {
78-
const isRunning = !!(
79-
currentTrayTimeEntry &&
80-
currentTrayTimeEntry.start &&
81-
currentTrayTimeEntry.start !== ''
82-
)
83+
const isRunning = isTimerRunning(currentTrayTimeEntry)
8384
tray.setImage(nativeImage.createFromPath(getIconPath(isRunning)))
8485
})
8586

8687
return tray
8788
}
8889

8990
function updateTimerInterval(timeEntry: TimeEntry, tray: Tray) {
90-
if (timeEntry && timeEntry.start && timeEntry.start !== '') {
91+
if (isTimerRunning(timeEntry)) {
9192
const duration = dayjs.duration(dayjs().diff(dayjs(timeEntry.start), 'second'), 's')
9293
// duration formatted to HH:MM
9394
const hours = Math.floor(duration.asHours()).toString().padStart(2, '0')
@@ -102,7 +103,7 @@ export function registerTrayListeners(tray: Tray, mainWindow: BrowserWindow) {
102103
if (serializedTimeEntry) {
103104
const timeEntry = JSON.parse(serializedTimeEntry) as TimeEntry
104105
currentTrayTimeEntry = timeEntry
105-
const isRunning = !!timeEntry?.start
106+
const isRunning = isTimerRunning(timeEntry)
106107
tray.setImage(nativeImage.createFromPath(getIconPath(isRunning)))
107108
tray.setToolTip(
108109
isRunning ? 'solidtime - Timer is running' : 'solidtime - Timer is stopped'

0 commit comments

Comments
 (0)