Time Tracking and Timer
Relevant source files
The following files were used as context for generating this wiki page: - [README.md](https://github.com/ever-co/ever-teams/blob/HEAD/README.md) - [apps/web/core/hooks/activities/use-timer.ts](https://github.com/ever-co/ever-teams/blob/HEAD/apps/web/core/hooks/activities/use-timer.ts) - [apps/mobile/app/services/hooks/use-timer.ts](https://github.com/ever-co/ever-teams/blob/HEAD/apps/mobile/app/services/hooks/use-timer.ts) - [apps/mobile/app/services/interfaces/ITimer.ts](https://github.com/ever-co/ever-teams/blob/HEAD/apps/mobile/app/services/interfaces/ITimer.ts) - [apps/web/core/components/timer/timer.tsx](https://github.com/ever-co/ever-teams/blob/HEAD/apps/web/core/components/timer/timer.tsx) - [apps/web/core/components/features/manual-time/add-manual-time-modal.tsx](https://github.com/ever-co/ever-teams/blob/HEAD/apps/web/core/components/features/manual-time/add-manual-time-modal.tsx) - [apps/web/core/components/tasks/task-times.tsx](https://github.com/ever-co/ever-teams/blob/HEAD/apps/web/core/components/tasks/task-times.tsx) - [apps/web/core/stores/README.md](https://github.com/ever-co/ever-teams/blob/HEAD/apps/web/core/stores/README.md)The Time Tracking and Timer feature is a core component of the Ever Teams platform, enabling users to track time spent on tasks across web, mobile, and desktop applications. This system provides real-time updates, synchronization across devices, and manual time entry capabilities. It is deeply integrated with tasks, projects, and teams to provide accurate productivity metrics.
The architecture relies on a combination of client-side state management with Jotai for immediate UI feedback and a robust set of backend services for persistence and synchronization. The system is designed to be resilient, handling side effects like switching tasks or teams, and preventing race conditions through debouncing and optimistic UI updates.
Sources: README.md, apps/web/core/hooks/activities/use-timer.ts
Core Concepts and Data Structures
The timer's functionality is built around a set of key data structures that define its state and behavior.
Sources: apps/mobile/app/services/interfaces/ITimer.ts
Data Model
The following diagram illustrates the primary interfaces used in the time tracking system.
This diagram shows the relationship between the server-provided ITimerStatus, the local ILocalTimerStatus for UI responsiveness, and the ETimeLogSource enum.
Sources: apps/mobile/app/services/interfaces/ITimer.ts:1-41
Key Interfaces
| Interface | Description | Fields |
|---|---|---|
ITimerStatus | Represents the global timer status fetched from the server. | duration, running, lastLog (optional ITimer object) |
ILocalTimerStatus | Manages the timer's state in the browser's local storage for immediate UI updates and persistence across tabs. | lastTaskId, runnedDateTime (timestamp), running |
ITimer | Represents a single time log entry. | id, taskId, startedAt, stoppedAt, duration, source, etc. |
Sources: apps/mobile/app/services/interfaces/ITimer.ts:24-36
Time Log Source (ETimeLogSource)
This enum is crucial for identifying the origin of a time tracking session, which helps in handling logic specific to different platforms (e.g., stopping a timer started on the web when a task is changed, but not if it was started from the desktop app).
| Enum Value | Description |
|---|---|
MOBILE | Timer started from the mobile application. |
BROWSER / TEAMS | Timer started from the web application. |
DESKTOP | Timer started from the desktop application. |
BROWSER_EXTENSION | Timer started from the browser extension. |
HUBSTAFF / UPWORK | Timer synced from an external integration. |
Sources: apps/mobile/app/services/interfaces/ITimer.ts:13-22, apps/web/core/hooks/activities/use-timer.ts:63-64
Client-Side Architecture
The client-side implementation uses Jotai for state management and a central useTimer hook to encapsulate all business logic.
State Management (Jotai)
Global state for the timer is managed using Jotai atoms, ensuring a reactive and predictable state flow. The stores are organized by feature domain.
This diagram shows how the useTimer hook consumes various Jotai atoms to manage and display the timer's state.
Sources: apps/web/core/stores/README.md, apps/web/core/hooks/activities/use-timer.ts:13-22
Key Atoms
| Atom | Description |
|---|---|
timerStatusState | Holds the ITimerStatus object fetched from the server. |
localTimerStatusState | Holds the ILocalTimerStatus object, synced with localStorage. |
timeCounterState | The running millisecond counter for the UI. |
timerSecondsState | The running seconds counter, used for progress bars and statistics. |
timerStatusFetchingState | A boolean flag indicating if a timer operation is in progress. |
Sources: apps/web/core/hooks/activities/use-timer.ts:13-22
The useTimer Hook
This hook is the brain of the time tracking feature. It is responsible for all logic related to starting, stopping, syncing, and managing the timer's state.
Sources: apps/web/core/hooks/activities/use-timer.ts:219-835
Local Time Counter (useLocalTimeCounter)
To provide an instant and smooth user experience, a local timer is managed separately from the server-synced state.
This flow shows how useLocalTimeCounter uses localStorage and a setInterval to manage the UI timer independently of server responses.
Sources: apps/web/core/hooks/activities/use-timer.ts:72-198
Starting the Timer
The startTimer function orchestrates several actions to begin a time tracking session.
This sequence shows the startTimer process, including local state updates, API calls, and cache invalidation.
Sources: apps/web/core/hooks/activities/use-timer.ts:442-555
Stopping the Timer
The stopTimer function includes debouncing logic to prevent race conditions and ensures the active task is cleared.
This sequence illustrates the stopTimer flow, highlighting the debounce mechanism and API calls to stop the timer and clear the active task.
Sources: apps/web/core/hooks/activities/use-timer.ts:558-622
Timer Synchronization
The timer state is periodically synchronized with the server to ensure data consistency and handle cases where the timer might be running on another device.
- A
syncTimerfunction is called via auseMutationhook. - This function is triggered automatically at a set interval (
SYNC_TIMER_INTERVAL) when the timer is running. - It is also called manually when the timer is stopped to send the final state.
- The sync process includes retry logic for network errors but skips retries on authentication failures (401).
Sources: apps/web/core/hooks/activities/use-timer.ts:311-334, 401-408, 624-633
UI Components
The user interacts with the timer through several key components.
Timer Component
This is the main display for the timer, showing the elapsed time and a start/stop button.
- It uses the
useTimerViewhook, a facade overuseTimer, to get formatted time and thetimerHanlderfunction. - It displays either the running time counter or the total time worked today, depending on the timer's status.
- It includes a
ProgressBarthat shows the task's progress based onactiveTaskEstimation. - It also renders an icon indicating the source of the running timer (e.g., mobile, desktop).
Sources: apps/web/core/components/timer/timer.tsx
TimerCard Component
A more compact and visual version of the timer, often used in dashboards. It displays the time and a large play/pause button.
Sources: apps/web/core/components/timer/timer-card.tsx
Manual Time Entry
The AddManualTimeModal component allows users to add time entries manually.
- The form collects
Date,Start Time,End Time,Team,Task,Description, andReason. - It performs client-side validation to ensure the end time is after the start time and all required fields are filled.
- On submission, it calls the
addManualTimefunction from theuseManualTimehook.
Sources: apps/web/core/components/features/manual-time/add-manual-time-modal.tsx
Backend Services
The frontend interacts with a set of backend services to manage the timer's state. These are accessed via timerService.
| Service Method | HTTP Endpoint (Inferred) | Description |
|---|---|---|
getTimerStatus() | GET /api/timer | Fetches the current timer status for the user. |
startTimer() | POST /api/timer/start | Starts the timer for the user's currently active task. |
stopTimer() | POST /api/timer/stop | Stops the currently running timer. Requires a source parameter. |
toggleTimer() | POST /api/timer/toggle | Toggles the timer state for a specific taskId. |
syncTimer() | POST /api/timer/sync | Synchronizes the local timer state with the server. Used for heartbeats and ensuring data consistency. |
addManualTime() | POST /api/timeslot | Adds a manual time entry with a specified start and end time. |
Sources: apps/web/core/hooks/activities/use-timer.ts:285-334, apps/web/core/components/features/manual-time/add-manual-time-modal.tsx:49-56
Cross-Platform Considerations
The time tracking feature is also implemented in the mobile application, sharing much of the same core logic.
- The mobile
useTimerhook inapps/mobile/app/services/hooks/use-timer.tsmirrors the web implementation. - It uses
AsyncStoragefor local state persistence, equivalent tolocalStorageon the web. - It calls the same backend API endpoints (
startTimerRequest,stopTimerRequest). - The
ETimeLogSourceenum is used to differentiate between sessions started on mobile (MOBILE) versus other platforms.
Sources: apps/mobile/app/services/hooks/use-timer.ts, apps/mobile/app/services/interfaces/ITimer.ts
Summary
The time tracking system in Ever Teams is a well-architected feature that balances immediate user feedback with robust server-side synchronization. By leveraging client-side state management with Jotai, optimistic UI updates, and careful handling of side effects, it provides a reliable and responsive experience. The clear separation of concerns between UI components, hooks, and services, along with a consistent data model, makes the system maintainable and scalable across web and mobile platforms.