diff --git a/src/feedbacks.ts b/src/feedbacks.ts index 1c64025..23de853 100644 --- a/src/feedbacks.ts +++ b/src/feedbacks.ts @@ -16,13 +16,34 @@ export function UpdateFeedbacks(self: TogglTrack): void { type: 'dropdown', label: 'Project', default: -1, - choices: [{ id: -1, label: 'None' }].concat(self.projects!), + choices: self.projects ?? [{ id: -1, label: 'None' }], }, ], callback: (feedback) => { - const projID = self.getVariableValue('timerProjectID') - self.log('debug', 'check if ' + feedback.options.project + '=' + projID) - return feedback.options.project == projID + //self.log('debug', 'check project counting ' + feedback.options.project) + return feedback.options.project == self.currentEntry?.project_id + }, + }, + ClientRunningState: { + name: 'Client Counting', + type: 'boolean', + defaultStyle: { + bgcolor: combineRgb(255, 0, 0), + color: combineRgb(0, 0, 0), + }, + options: [ + { + id: 'client', + type: 'dropdown', + label: 'Client', + default: -1, + choices: self.clients ?? [{ id: -1, label: 'None' }], + }, + ], + callback: (feedback) => { + //self.log('debug', 'check client counting ' + feedback.options.client) + // find the project that matches the project_id of the current entry and compare its client_id with the configured one + return feedback.options.client == self.projects?.find((p) => p.id == self.currentEntry?.project_id)?.clientID }, }, }) diff --git a/src/main.ts b/src/main.ts index 7e17403..e6bccae 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,7 +8,7 @@ import UpdatePresets from './presets.js' import UpdateVariableDefinitions from './variables.js' import UpgradeScripts from './upgrades.js' import { UpdateFeedbacks } from './feedbacks.js' -import { Toggl, ITimeEntry, IWorkspaceProject } from 'toggl-track' +import { Toggl, ITimeEntry, IWorkspaceProject, IClient } from 'toggl-track' import { togglGetWorkspaces } from './toggl-extend.js' import { timecodeSince } from './utils.js' @@ -19,7 +19,10 @@ export class TogglTrack extends InstanceBase { workspaceId?: number // current active workspace id workspaceName: string = '' // name of workspace - projects?: { id: number; label: string }[] + projects?: { id: number; label: string; clientID?: number }[] + clients?: { id: number; label: string }[] + currentEntry?: ITimeEntry + intervalId?: NodeJS.Timeout currentTimerUpdaterIntervalId?: NodeJS.Timeout @@ -163,13 +166,17 @@ export class TogglTrack extends InstanceBase { * @param entry running entry or undefined */ private setCurrentlyRunningTimeEntry(entry: ITimeEntry | undefined): void { + this.currentEntry = entry if (entry) { + const project = this.projects?.find((p) => p.id == entry.project_id) 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, + timerProject: project?.label, timerProjectID: entry.project_id, + timerClient: this.clients!.find((c) => c.id == project?.clientID)?.label, + timerClientID: project?.clientID, }) // in case there is on update thread running clear it @@ -192,9 +199,11 @@ export class TogglTrack extends InstanceBase { timerDuration: undefined, timerProject: undefined, timerProjectID: undefined, + timerClient: undefined, + timerClientID: undefined, }) } - this.checkFeedbacks('ProjectRunningState') + this.checkFeedbacks('ProjectRunningState', 'ClientRunningState') } async getCurrentTimer(): Promise { @@ -260,6 +269,7 @@ export class TogglTrack extends InstanceBase { }) await this.getProjects() + await this.getClients() } async getProjects(): Promise { @@ -287,6 +297,7 @@ export class TogglTrack extends InstanceBase { return { id: p.id, label: p.name, + clientID: p.client_id, } }) .sort((a, b) => { @@ -305,6 +316,47 @@ export class TogglTrack extends InstanceBase { this.log('debug', 'Projects: ' + JSON.stringify(this.projects)) } + private async getClients(): Promise { + this.log('debug', 'function: getClients ' + this.workspaceId) + + if (!this.workspaceId) { + this.log('warn', 'workspaceId undefined') + return + } + + const clients: IClient[] = await this.toggl!.me.clients() + + if (typeof clients === 'string' || clients.length == 0) { + this.log('debug', 'No clients found') + this.clients = undefined + this.log('debug', 'clients response' + JSON.stringify(clients)) + return + } + + this.clients = clients + .filter((c) => c.wid == this.workspaceId) + .map((c) => { + return { + id: c.id, + label: c.name, + } + }) + .sort((a, b) => { + const fa = a.label.toLowerCase() + const fb = b.label.toLowerCase() + + if (fa < fb) { + return -1 + } + if (fa > fb) { + return 1 + } + return 0 + }) + + this.log('debug', 'Clients: ' + JSON.stringify(this.clients)) + } + async startTimer(project: number, description: string): Promise { if (!this.toggl || !this.workspaceId) { this.log('error', 'toggle not initialized. Do not start time') diff --git a/src/variables.ts b/src/variables.ts index dd7ac63..3d88c24 100644 --- a/src/variables.ts +++ b/src/variables.ts @@ -30,5 +30,13 @@ export default function (self: TogglTrack): void { name: 'Current Timer Project', variableId: 'timerProject', }, + { + name: 'Current Timer Client ID', + variableId: 'timerClientID', + }, + { + name: 'Current Timer Client', + variableId: 'timerClient', + }, ]) }