summaryrefslogtreecommitdiff
path: root/internal/handlers/timeline.go
blob: 37e688f3a020b6bfc6d334d7be5a1ab86752df9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package handlers

import (
	"net/http"
	"strconv"
	"time"

	"task-dashboard/internal/config"
	"task-dashboard/internal/models"
)

// TimelineData holds grouped timeline items for the template
type TimelineData struct {
	TodayItems    []models.TimelineItem
	TomorrowItems []models.TimelineItem
	LaterItems    []models.TimelineItem
	Start         time.Time
	Days          int

	// Section labels with day of week
	TodayLabel    string // e.g., "Today - Monday"
	TomorrowLabel string // e.g., "Tomorrow - Tuesday"
	LaterLabel    string // e.g., "Wednesday, Jan 29"
}

// HandleTimeline renders the timeline view
func (h *Handler) HandleTimeline(w http.ResponseWriter, r *http.Request) {
	// Parse query params
	startStr := r.URL.Query().Get("start")
	daysStr := r.URL.Query().Get("days")

	var start time.Time
	if startStr != "" {
		parsed, err := config.ParseDateInDisplayTZ(startStr)
		if err == nil {
			start = parsed
		} else {
			start = config.Today()
		}
	} else {
		start = config.Today()
	}

	days := 3 // Default
	if daysStr != "" {
		if d, err := strconv.Atoi(daysStr); err == nil && d > 0 {
			days = d
		}
	}

	end := start.AddDate(0, 0, days)

	// Call BuildTimeline
	items, err := BuildTimeline(r.Context(), h.store, h.googleCalendarClient, start, end)
	if err != nil {
		JSONError(w, http.StatusInternalServerError, "Failed to build timeline", err)
		return
	}

	// Compute section labels with day of week
	now := config.Now()
	today := config.Today()
	tomorrow := today.AddDate(0, 0, 1)
	dayAfterTomorrow := today.AddDate(0, 0, 2)

	// Group items by day section
	data := TimelineData{
		Start:         start,
		Days:          days,
		TodayLabel:    "Today - " + now.Format("Monday"),
		TomorrowLabel: "Tomorrow - " + tomorrow.Format("Monday"),
		LaterLabel:    dayAfterTomorrow.Format("Monday, Jan 2") + "+",
	}
	for _, item := range items {
		switch item.DaySection {
		case models.DaySectionToday:
			data.TodayItems = append(data.TodayItems, item)
		case models.DaySectionTomorrow:
			data.TomorrowItems = append(data.TomorrowItems, item)
		case models.DaySectionLater:
			data.LaterItems = append(data.LaterItems, item)
		}
	}

	HTMLResponse(w, h.templates, "timeline-tab", data)
}