Something went wrong. Try again.
TimesUp – timetracking for ClickUp made easy
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437import { useState, useEffect } from "react";import { startTimer, stopTimer, updateTimeEntry, getTimeEntries, getMyTasks, getTask,} from "../lib/clickup.js";import { formatDuration, formatDurationShort, startOfDay, endOfDay } from "../lib/time.js";import "./TimerPanel.css";
export default function TimerPanel({ teamId, userId, currentEntry, onBrowse, onChange,}) { const [elapsed, setElapsed] = useState(0); const [description, setDescription] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(""); const [suggestedTasks, setSuggestedTasks] = useState([]); const [suggestionsLoading, setSuggestionsLoading] = useState(false); const [lastEntry, setLastEntry] = useState(null); const [showSuggestions, setShowSuggestions] = useState(false); const [taskDetail, setTaskDetail] = useState(null); const [capacity, setCapacity] = useState(0); const [completedToday, setCompletedToday] = useState(0); const [editingStart, setEditingStart] = useState(false); const [startEdit, setStartEdit] = useState("");
const isRunning = !!currentEntry;
// Tick elapsed time useEffect(() => { if (!isRunning) { setElapsed(0); return; } const startMs = parseInt(currentEntry.start); const update = () => setElapsed(Date.now() - startMs); update(); const interval = setInterval(update, 1000); return () => clearInterval(interval); }, [isRunning, currentEntry]);
// Read daily goal from local settings useEffect(() => { window.api.store.get('daily_goal_hours').then(h => setCapacity((h || 0) * 3600000)); }, []);
// Fetch today's completed total whenever the running entry changes useEffect(() => { getTimeEntries(teamId, startOfDay(), endOfDay()) .then(data => { const ms = (data || []) .filter(e => !currentEntry || e.id !== currentEntry.id) .reduce((sum, e) => sum + parseInt(e.duration || 0), 0); setCompletedToday(ms); }) .catch(() => {}); }, [teamId, currentEntry?.id]);
// Sync description from running entry useEffect(() => { if (currentEntry?.id) setDescription(currentEntry.description || ""); }, [currentEntry?.id]);
// Fetch full task details (includes list) when tracking a task useEffect(() => { if (!currentEntry?.task?.id) { setTaskDetail(null); return; } getTask(currentEntry.task.id).then(setTaskDetail).catch(() => setTaskDetail(null)); }, [currentEntry?.task?.id]);
const isRunningUnassigned = isRunning && !currentEntry?.task;
// Fetch suggestions when idle or running unassigned useEffect(() => { if (isRunning && !isRunningUnassigned) return; if (userId) { setSuggestionsLoading(true); getMyTasks(teamId, userId) .then((tasks) => setSuggestedTasks(tasks.slice(0, 5))) .catch(() => {}) .finally(() => setSuggestionsLoading(false)); } if (!isRunning) { const sevenDaysAgo = Date.now() - 7 * 24 * 60 * 60 * 1000; getTimeEntries(teamId, sevenDaysAgo, endOfDay()) .then((data) => { const sorted = (data || []).sort( (a, b) => parseInt(b.start) - parseInt(a.start) ); setLastEntry(sorted[0] || null); }) .catch(() => {}); } }, [teamId, userId, isRunning, isRunningUnassigned]);
function openStartEdit() { const d = new Date(parseInt(currentEntry.start)); const pad = (n) => String(n).padStart(2, "0"); setStartEdit( `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}` ); setEditingStart(true); }
async function saveStartEdit() { const newMs = new Date(startEdit).getTime(); if (!newMs || newMs >= Date.now()) { setEditingStart(false); return; } setBusy(true); setError(""); try { await updateTimeEntry(teamId, currentEntry.id, { start: newMs }); setEditingStart(false); onChange(); } catch (e) { setError(e.message); } finally { setBusy(false); } }
async function handleStop() { setBusy(true); setError(""); try { await stopTimer(teamId); onChange(); } catch (e) { setError(e.message); } finally { setBusy(false); } }
async function startWithTask(taskId, desc = "") { setBusy(true); setError(""); try { await startTimer(teamId, taskId, desc); setShowSuggestions(false); onChange(); } catch (e) { setError(e.message); } finally { setBusy(false); } }
async function assignToRunning(taskId) { setBusy(true); setError(""); try { await updateTimeEntry(teamId, currentEntry.id, { tid: taskId }); setShowSuggestions(false); onChange(); } catch (e) { setError(e.message); } finally { setBusy(false); } }
return ( <div className="timer-panel"> {/* Start / Stop */} <button className={`timer-action ${ isRunning ? "timer-action-stop" : "timer-action-start" }`} onClick={ isRunning ? handleStop : () => startWithTask(null, description) } disabled={busy} > {busy ? ( "..." ) : isRunning ? ( <> <svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor"> <rect x="6" y="6" width="12" height="12" rx="1" /> </svg> Stop </> ) : ( <> <svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor"> <path d="M8 5v14l11-7z" /> </svg> Start unassigned </> )} </button>
{/* Clock */} <div className="timer-display"> <div className={`timer-time ${isRunning ? "timer-time-running" : ""}`}> {formatDuration(elapsed)} </div> <div className="timer-status"> {isRunning ? ( <div className="timer-task-info"> <span className="timer-task-name-row"> <span className="status-dot status-running" /> <span>{currentEntry.task?.name || "unassigned"}</span> {currentEntry.task?.id && ( <button className="timer-task-link" title="Open in ClickUp" onClick={() => window.api.shell.openExternal( taskDetail?.url || `https://app.clickup.com/t/${currentEntry.task.id}` )} > <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"> <path d="M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6M15 3h6v6M10 14L21 3" /> </svg> </button> )} </span> {taskDetail?.list?.name && ( <span className="timer-task-list">{taskDetail.list.name}</span> )} </div> ) : ( <> <span className="status-dot" /> <span>stopped</span> </> )} </div> </div>
{/* Start time editor */} {isRunning && ( <div className="timer-start-edit"> {editingStart ? ( <div className="timer-start-input-row"> <input type="datetime-local" className="timer-start-input" value={startEdit} onChange={(e) => setStartEdit(e.target.value)} max={new Date().toISOString().slice(0, 16)} onKeyDown={(e) => { if (e.key === "Enter") saveStartEdit(); if (e.key === "Escape") setEditingStart(false); }} autoFocus /> <button className="timer-start-confirm" onClick={saveStartEdit} disabled={busy}> <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5"> <path d="M20 6L9 17l-5-5" /> </svg> </button> <button className="timer-start-cancel" onClick={() => setEditingStart(false)}> <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5"> <path d="M18 6L6 18M6 6l12 12" /> </svg> </button> </div> ) : ( <button className="timer-start-show" onClick={openStartEdit}> since {new Date(parseInt(currentEntry.start)).toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" })} <svg width="9" height="9" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"> <path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7" /> <path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z" /> </svg> </button> )} </div> )}
{/* Daily progress */} {capacity > 0 && (() => { const total = completedToday + (isRunning ? elapsed : 0); const pct = Math.min(total / capacity, 1); const done = pct >= 1; return ( <div className="daily-progress"> <div className="daily-progress-labels"> <span className="daily-progress-today">{formatDurationShort(total)}</span> <span className="daily-progress-goal">{formatDurationShort(capacity)}</span> </div> <div className="daily-progress-track"> <div className={`daily-progress-fill${done ? ' daily-progress-fill-done' : ''}`} style={{ width: `${pct * 100}%` }} /> </div> </div> ); })()}
{/* Notes */} <input className="timer-description" placeholder="Notes..." value={description} onChange={(e) => setDescription(e.target.value)} onBlur={async () => { if (isRunning && currentEntry?.id) { try { await updateTimeEntry(teamId, currentEntry.id, { description }); } catch {} } }} maxLength={200} />
{error && <div className="timer-error">{error}</div>}
{/* Assign task button + collapsible suggestions */} {(!isRunning || isRunningUnassigned) && ( <> <button className={`task-assign-btn ${ showSuggestions ? "task-assign-btn-open" : "" }`} onClick={() => setShowSuggestions((v) => !v)} > <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" > <path d="M9 11l3 3 8-8M20 12v7a2 2 0 01-2 2H5a2 2 0 01-2-2V6a2 2 0 012-2h11" /> </svg> Assign a task <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" className="task-assign-chev" > <path d={showSuggestions ? "M18 15l-6-6-6 6" : "M6 9l6 6 6-6"} /> </svg> </button>
{showSuggestions && ( <div className="suggestions"> {suggestionsLoading && ( <div className="suggestions-loading">Loading tasks…</div> )} {!suggestionsLoading && suggestedTasks.map((task) => ( <button key={task.id} className="suggestion-item" onClick={() => isRunningUnassigned ? assignToRunning(task.id) : startWithTask(task.id)} > <div className="suggestion-info"> <span className="suggestion-name">{task.name}</span> <span className="suggestion-meta"> {task.list?.name && ( <span className="suggestion-list">{task.list.name}</span> )} {task.list?.name && task.status?.status && ( <span className="suggestion-meta-sep">·</span> )} {task.status?.status && ( <span className="suggestion-status" style={{ color: task.status.color || "var(--text-muted)" }} > {task.status.status} </span> )} </span> </div> <svg width="10" height="10" viewBox="0 0 24 24" fill="currentColor" className="suggestion-play"> <path d="M8 5v14l11-7z" /> </svg> </button> ))} <button className="suggestions-browse" onClick={() => { setShowSuggestions(false); onBrowse(); }} > Browse all tasks → </button> </div> )} </> )}
{/* Last entry quick-start */} {!isRunning && lastEntry && ( <button className="last-entry" onClick={() => startWithTask( lastEntry.task?.id || null, lastEntry.description || "" ) } > <div className="last-entry-info"> <span className="last-entry-label">last</span> <span className="last-entry-name"> {lastEntry.task?.name || lastEntry.description || "Unassigned"} </span> <span className="last-entry-meta"> {new Date(parseInt(lastEntry.start)).toLocaleTimeString( undefined, { hour: "numeric", minute: "2-digit" } )} {" · "} {formatDurationShort(parseInt(lastEntry.duration))} tracked </span> </div> <svg width="10" height="10" viewBox="0 0 24 24" fill="currentColor" className="last-entry-play" > <path d="M8 5v14l11-7z" /> </svg> </button> )} </div> );}