Skip to content

Commit

Permalink
update 'timerDuration' to contain an updating timestamp of how long i…
Browse files Browse the repository at this point in the history
…t is running
  • Loading branch information
krombel committed Jan 17, 2025
1 parent 3aa54c4 commit 96e1d0f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
75 changes: 44 additions & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// toggltrack module
// Peter Daniel
// Peter Daniel, Matthias Kesler

import { InstanceBase, runEntrypoint, InstanceStatus, SomeCompanionConfigField } from '@companion-module/base'
import { GetConfigFields, type ModuleConfig } from './config.js'
Expand All @@ -10,6 +10,7 @@ import UpgradeScripts from './upgrades.js'
import { UpdateFeedbacks } from './feedbacks.js'
import { Toggl, ITimeEntry, IWorkspaceProject } from 'toggl-track'
import { togglGetWorkspaces } from './toggl-extend.js'
import { timecodeSince } from './utils.js'

export class TogglTrack extends InstanceBase<ModuleConfig> {
config!: ModuleConfig // Setup in init()
Expand All @@ -20,6 +21,7 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {
workspaceName: string = '' // name of workspace
projects?: { id: number; label: string }[]
intervalId?: NodeJS.Timeout
currentTimerUpdaterIntervalId?: NodeJS.Timeout

constructor(internal: unknown) {
super(internal)
Expand All @@ -34,6 +36,8 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {
if (this.config.startTimerPoller) {
this.stopTimeEntryPoller()
}

clearInterval(this.currentTimerUpdaterIntervalId)
}

async init(config: ModuleConfig): Promise<void> {
Expand Down Expand Up @@ -154,15 +158,42 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {
clearInterval(this.intervalId)
}

private setCurrentProject(projectID: number | undefined): void {
let pName: string | undefined
if (typeof projectID === 'number') {
pName = this.projects!.find((v) => v.id == projectID)?.label
/**
* Set variables to this time entry
* @param entry running entry or undefined
*/
private setCurrentlyRunningTimeEntry(entry: ITimeEntry | undefined): void {
if (entry) {
this.setVariableValues({
timerId: entry.id,
timerDescription: entry.description,
timerDuration: timecodeSince(new Date(entry.start)),
timerProject: this.projects!.find((v) => v.id == entry?.project_id)?.label,
timerProjectID: entry.project_id,
})

// in case there is on update thread running clear it
clearInterval(this.currentTimerUpdaterIntervalId)

// Update timerDuration once per second
this.currentTimerUpdaterIntervalId = setInterval(() => {
// this harms the linter (handle unawaited promise in an non-async context)
void (async () => {
this.setVariableValues({
timerDuration: timecodeSince(new Date(entry.start)),
})
})()
}, 1000) // update every second
} else {
clearInterval(this.currentTimerUpdaterIntervalId)
this.setVariableValues({
timerId: undefined,
timerDescription: undefined,
timerDuration: undefined,
timerProject: undefined,
timerProjectID: undefined,
})
}
this.setVariableValues({
timerProject: pName,
timerProjectID: projectID,
})
this.checkFeedbacks('ProjectRunningState')
}

Expand All @@ -179,22 +210,12 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {

if (entry) {
this.log('info', 'Current timer id: ' + entry.id)
this.setVariableValues({
timerId: entry.id,
timerDescription: entry.description,
timerDuration: entry.duration,
})
this.setCurrentProject(entry.project_id)
this.setCurrentlyRunningTimeEntry(entry)

return entry.id
} else {
this.log('info', 'No current timer')
this.setVariableValues({
timerId: undefined,
timerDescription: undefined,
timerDuration: undefined,
})
this.setCurrentProject(undefined)
this.setCurrentlyRunningTimeEntry(undefined)
return null
}
}
Expand Down Expand Up @@ -303,12 +324,7 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {
project_id: project != 0 ? project : undefined,
})
this.log('info', 'New timer started ' + newEntry.id + ' ' + newEntry.description)
this.setVariableValues({
timerId: newEntry.id,
timerDescription: newEntry.description,
timerDuration: newEntry.duration,
})
this.setCurrentProject(newEntry.project_id)
this.setCurrentlyRunningTimeEntry(newEntry)
} else {
this.log('info', 'A timer is already running ' + currentId + ' not starting a new one!')
}
Expand All @@ -328,13 +344,10 @@ export class TogglTrack extends InstanceBase<ModuleConfig> {
const updated: ITimeEntry = await this.toggl.timeEntry.stop(currentId, this.workspaceId)
this.log('info', 'Stopped ' + updated.id + ', duration ' + updated.duration)

this.setCurrentlyRunningTimeEntry(undefined)
this.setVariableValues({
timerId: undefined,
timerDescription: undefined,
timerDuration: undefined,
lastTimerDuration: updated.duration,
})
this.setCurrentProject(undefined)
} else {
this.log('warn', 'No running timer to stop or running timer id unknown')
}
Expand Down
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* returns formatted timecode from date until now
* @param date start date
* @returns formatted string 0:00:00 - where hours and minutes are hidden if 0
*/
export function timecodeSince(date: Date): string {
const dateObj = new Date(Date.now() - date.getTime())
const hours = dateObj.getUTCHours()
const minutes = `0${dateObj.getUTCMinutes()}`.slice(-2)
const seconds = `0${dateObj.getSeconds()}`.slice(-2)
return (hours > 0 ? hours + ':' : '') + (hours > 0 || minutes !== '00' ? minutes + ':' : '') + seconds
}

0 comments on commit 96e1d0f

Please sign in to comment.