@@ -0,0 +1,164 @@
<!doctype html>
< html lang = "en" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< title > Activity Tracker< / title >
< style >
: root { --bg : #fafafa ; --fg : #1a1a1a ; --muted : #6b6b6b ; --line : #e2e2e2 ; --bar : #4a7bd0 ; --ok : #2e8b57 ; --bad : #c0392b ; }
@ media ( prefers-color-scheme : dark ) {
: root { --bg : #151515 ; --fg : #e6e6e6 ; --muted : #999 ; --line : #2c2c2c ; --bar : #6f9be6 ; --ok : #5cc28a ; --bad : #e2685a ; }
}
* { box-sizing : border-box ; }
body { margin : 0 ; background : var ( - - bg ) ; color : var ( - - fg ) ; font : 15 px / 1.45 system-ui , sans-serif ; }
main { max-width : 760 px ; margin : 0 auto ; padding : 24 px 16 px 48 px ; }
header { display : flex ; flex-wrap : wrap ; gap : 8 px ; align-items : center ; justify-content : space-between ; }
h1 { font-size : 20 px ; margin : 0 ; }
h2 { font-size : 13 px ; text-transform : uppercase ; letter-spacing : .05 em ; color : var ( - - muted ) ; margin : 28 px 0 8 px ; }
input , button { font : inherit ; color : inherit ; background : transparent ; border : 1 px solid var ( - - line ) ; border-radius : 6 px ; padding : 4 px 8 px ; }
button { cursor : pointer ; }
. nav { display : flex ; gap : 6 px ; align-items : center ; }
. muted { color : var ( - - muted ) ; }
. err { color : var ( - - bad ) ; }
table { width : 100 % ; border-collapse : collapse ; }
td { padding : 5 px 0 ; border-bottom : 1 px solid var ( - - line ) ; vertical-align : top ; }
td . num { text-align : right ; white-space : nowrap ; font-variant-numeric : tabular-nums ; padding-left : 12 px ; }
td . label { overflow-wrap : anywhere ; }
. bar { height : 4 px ; background : var ( - - bar ) ; border-radius : 2 px ; margin-top : 3 px ; }
. ok { color : var ( - - ok ) ; } . bad { color : var ( - - bad ) ; }
# login { margin-top : 40 px ; display : flex ; gap : 8 px ; }
# login input { flex : 1 ; }
< / style >
< / head >
< body >
< main >
< header >
< h1 > Activity Tracker< / h1 >
< div class = "nav" id = "nav" hidden >
< button id = "prev" aria-label = "Previous day" > ‹ < / button >
< input type = "date" id = "date" >
< button id = "next" aria-label = "Next day" > › < / button >
< button id = "logout" title = "Forget API key" > Sign out< / button >
< / div >
< / header >
< form id = "login" hidden >
< input type = "password" id = "key" placeholder = "API key (trk_…)" autocomplete = "off" required >
< button > Open< / button >
< / form >
< p id = "status" class = "muted" > < / p >
< div id = "content" hidden >
< h2 > Goals< / h2 > < table id = "goals" > < / table >
< h2 > By kind< / h2 > < table id = "kinds" > < / table >
< h2 > Top labels< / h2 > < table id = "labels" > < / table >
< h2 > Timeline< / h2 > < table id = "timeline" > < / table >
< / div >
< / main >
< script >
const $ = id => document . getElementById ( id ) ;
const KEY = "tracker_api_key" ;
const store = {
get ( ) { try { return localStorage . getItem ( KEY ) || "" ; } catch { return "" ; } } ,
set ( v ) { try { v ? localStorage . setItem ( KEY , v ) : localStorage . removeItem ( KEY ) ; } catch { } } ,
} ;
let apiKey = store . get ( ) ;
function dur ( s ) {
s = Math . round ( s ) ;
const h = Math . floor ( s / 3600 ) , m = Math . floor ( s % 3600 / 60 ) ;
if ( h ) return ` ${ h } h ${ String ( m ) . padStart ( 2 , "0" ) } m ` ;
return m ? ` ${ m } m ` : ` ${ s } s ` ;
}
const time = t => new Date ( t ) . toLocaleTimeString ( [ ] , { hour : "2-digit" , minute : "2-digit" } ) ;
const esc = s => String ( s ? ? "" ) . replace ( /[&<>"]/g , c => ( { "&" : "&" , "<" : "<" , ">" : ">" , '"' : """ } [ c ] ) ) ;
const today = ( ) => new Date ( ) . toLocaleDateString ( "en-CA" ) ; // YYYY-MM-DD in local time
async function api ( path ) {
const r = await fetch ( "api/v1/" + path , { headers : { Authorization : "Bearer " + apiKey } } ) ;
const body = await r . json ( ) . catch ( ( ) => ( { } ) ) ;
if ( r . status === 401 ) { signOut ( ) ; throw new Error ( body . error || "unauthorized" ) ; }
if ( ! r . ok ) throw new Error ( body . error || r . statusText ) ;
return body ;
}
function rows ( el , items , render , empty ) {
el . innerHTML = items . length ? items . map ( render ) . join ( "" ) : ` <tr><td class="muted"> ${ empty } </td></tr> ` ;
}
async function load ( ) {
const date = $ ( "date" ) . value ;
$ ( "status" ) . textContent = "Loading…" ;
try {
const sum = await api ( "summary/daily?date=" + date ) ;
const q = new URLSearchParams ( { from : sum . from , to : sum . to , limit : "500" } ) ;
const { activities } = await api ( "activities?" + q ) ;
rows ( $ ( "goals" ) , sum . goals , g => ` <tr>
<td class="label"> ${ esc ( g . kind ) } ${ g . label ? " · " + esc ( g . label ) : "" }
<span class="muted"> ${ esc ( g . op ) } ${ dur ( g . target _s ) } </span></td>
<td class="num"> ${ dur ( g . actual _s ) } </td>
<td class="num ${ g . met ? "ok" : "bad" } "> ${ g . met ? "✓ met" : "✗ missed" } </td></tr> ` ,
"No goals. Add one with <code>tracker goal add</code>." ) ;
const kinds = Object . entries ( sum . by _kind ) . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] ) ;
const maxK = kinds [ 0 ] ? . [ 1 ] || 1 ;
rows ( $ ( "kinds" ) , kinds , ( [ k , s ] ) => ` <tr>
<td class="label"> ${ esc ( k ) } <div class="bar" style="width: ${ s / maxK * 100 } %"></div></td>
<td class="num"> ${ dur ( s ) } </td></tr> ` , "Nothing recorded." ) ;
const labels = [ ... sum . totals ] . sort ( ( a , b ) => b . seconds - a . seconds ) . slice ( 0 , 15 ) ;
const maxL = labels [ 0 ] ? . seconds || 1 ;
rows ( $ ( "labels" ) , labels , t => ` <tr>
<td class="label"> ${ esc ( t . label || "—" ) } <span class="muted"> ${ esc ( t . kind ) } </span>
<div class="bar" style="width: ${ t . seconds / maxL * 100 } %"></div></td>
<td class="num"> ${ dur ( t . seconds ) } </td></tr> ` , "Nothing recorded." ) ;
const acts = activities . filter ( a => a . duration _s >= 60 )
. sort ( ( a , b ) => new Date ( b . started _at ) - new Date ( a . started _at ) ) ;
rows ( $ ( "timeline" ) , acts , a => ` <tr>
<td class="num muted" style="padding:5px 12px 5px 0"> ${ time ( a . started _at ) } – ${ time ( a . ended _at ) } </td>
<td class="label"> ${ esc ( a . label || a . kind ) } <span class="muted"> ${ esc ( a . kind ) } ${ a . attrs ? . title ? " · " + esc ( a . attrs . title ) : "" } </span></td>
<td class="num"> ${ dur ( a . duration _s ) } </td></tr> ` , "No activities of a minute or longer." ) ;
$ ( "content" ) . hidden = false ;
$ ( "status" ) . textContent = activities . length >= 500 ? "Timeline shows the first 500 activities." : "" ;
} catch ( e ) {
$ ( "status" ) . innerHTML = ` <span class="err"> ${ esc ( e . message ) } </span> ` ;
}
}
function shift ( days ) {
const d = new Date ( $ ( "date" ) . value + "T12:00:00" ) ;
d . setDate ( d . getDate ( ) + days ) ;
$ ( "date" ) . value = d . toLocaleDateString ( "en-CA" ) ;
load ( ) ;
}
function signOut ( ) {
apiKey = "" ; store . set ( "" ) ;
show ( ) ;
}
function show ( ) {
$ ( "login" ) . hidden = ! ! apiKey ;
$ ( "nav" ) . hidden = ! apiKey ;
$ ( "content" ) . hidden = true ;
$ ( "status" ) . textContent = "" ;
if ( apiKey ) load ( ) ;
}
$ ( "login" ) . onsubmit = e => {
e . preventDefault ( ) ;
apiKey = $ ( "key" ) . value . trim ( ) ; store . set ( apiKey ) ; $ ( "key" ) . value = "" ;
show ( ) ;
} ;
$ ( "logout" ) . onclick = signOut ;
$ ( "prev" ) . onclick = ( ) => shift ( - 1 ) ;
$ ( "next" ) . onclick = ( ) => shift ( 1 ) ;
$ ( "date" ) . onchange = load ;
$ ( "date" ) . value = today ( ) ;
show ( ) ;
< / script >
< / body >
< / html >