Unify organizer navigation and add personal widget dashboard
This commit is contained in:
1 parent
a9249009f0
commit
6ba30bc9f4
44 files changed
+843
-54
No files matched your search
@@ -0,0 +1,134 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Dashboard is owner-scoped presentation metadata. It never contains executable
|
||||
// widgets, URLs or private task content, and is versioned separately from mail.
|
||||
type DashboardWidget struct {
|
||||
ID string `json:"id"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
Tab string `json:"tab,omitempty"`
|
||||
}
|
||||
|
||||
func DefaultDashboard() []DashboardWidget {
|
||||
return []DashboardWidget{{ID: "progress", Width: 4, Height: 3}, {ID: "today", Width: 4, Height: 3}, {ID: "tomorrow", Width: 4, Height: 3}, {ID: "tasks", Width: 8, Height: 4, Tab: "inbox"}, {ID: "schedule", Width: 4, Height: 4}, {ID: "activity", Width: 12, Height: 5}}
|
||||
}
|
||||
|
||||
func ValidateDashboard(items []DashboardWidget) error {
|
||||
if len(items) > 6 {
|
||||
return ErrTaskInvalid
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, item := range items {
|
||||
switch item.ID {
|
||||
case "progress", "today", "tomorrow", "tasks", "schedule", "activity":
|
||||
default:
|
||||
return ErrTaskInvalid
|
||||
}
|
||||
if seen[item.ID] || item.Width < 3 || item.Width > 12 || item.Height < 2 || item.Height > 10 {
|
||||
return ErrTaskInvalid
|
||||
}
|
||||
seen[item.ID] = true
|
||||
if item.ID == "tasks" {
|
||||
switch item.Tab {
|
||||
case "inbox", "backlog", "someday", "review", "all", "done":
|
||||
default:
|
||||
return ErrTaskInvalid
|
||||
}
|
||||
} else if item.Tab != "" {
|
||||
return ErrTaskInvalid
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type DashboardLayout struct {
|
||||
Version int64 `json:"version"`
|
||||
Widgets []DashboardWidget `json:"widgets"`
|
||||
}
|
||||
|
||||
func (s *Store) Dashboard(ctx context.Context, domain, actor string) (DashboardLayout, error) {
|
||||
p := DashboardLayout{Widgets: DefaultDashboard()}
|
||||
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
||||
return p, err
|
||||
}
|
||||
var raw string
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT version,data FROM organizer_dashboard_layouts WHERE domain_uuid=? AND owner_uuid=?`, domain, actor).Scan(&p.Version, &raw)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return p, nil
|
||||
}
|
||||
if err != nil {
|
||||
return p, err
|
||||
}
|
||||
err = json.Unmarshal([]byte(raw), &p.Widgets)
|
||||
return p, err
|
||||
}
|
||||
|
||||
// Layout updates do not invalidate in-flight email delivery consent/version.
|
||||
func (s *Store) SaveDashboard(ctx context.Context, domain, actor string, version int64, widgets []DashboardWidget) (DashboardLayout, error) {
|
||||
p := DashboardLayout{}
|
||||
if s.ReadOnly {
|
||||
return p, ErrReadOnly
|
||||
}
|
||||
if version < 0 {
|
||||
return p, ErrTaskInvalid
|
||||
}
|
||||
if err := ValidateDashboard(widgets); err != nil {
|
||||
return p, err
|
||||
}
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return p, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err = taskProductivityWriter(ctx, tx, domain, actor); err != nil {
|
||||
return p, err
|
||||
}
|
||||
err = tx.QueryRowContext(ctx, `SELECT version FROM organizer_dashboard_layouts WHERE domain_uuid=? AND owner_uuid=?`, domain, actor).Scan(&p.Version)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return p, err
|
||||
}
|
||||
if p.Version != version {
|
||||
return p, ErrTaskConflict
|
||||
}
|
||||
p.Version++
|
||||
p.Widgets = widgets
|
||||
if p.Widgets == nil {
|
||||
p.Widgets = []DashboardWidget{}
|
||||
}
|
||||
data, err := json.Marshal(p.Widgets)
|
||||
if err != nil {
|
||||
return p, err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_dashboard_layouts(domain_uuid,owner_uuid,version,data) VALUES(?,?,?,?) ON CONFLICT(domain_uuid,owner_uuid) DO UPDATE SET version=excluded.version,data=excluded.data`, domain, actor, p.Version, string(data))
|
||||
if err != nil {
|
||||
return p, err
|
||||
}
|
||||
return p, tx.Commit()
|
||||
}
|
||||
|
||||
type DashboardProgress struct {
|
||||
Date string `json:"date"`
|
||||
Total int `json:"total"`
|
||||
Done int `json:"done"`
|
||||
}
|
||||
|
||||
// Deliberately count all rows, not a truncated list response. Cancelled and
|
||||
// archived items are excluded from both numerator and denominator.
|
||||
func (s *Store) DashboardProgress(ctx context.Context, domain, actor, day string) (DashboardProgress, error) {
|
||||
p := DashboardProgress{Date: day}
|
||||
if !validTaskDate(day) || day == "" {
|
||||
return p, ErrTaskInvalid
|
||||
}
|
||||
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
||||
return p, err
|
||||
}
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT count(*),COALESCE(sum(status='done'),0) FROM organizer_items WHERE domain_uuid=? AND owner_uuid=? AND kind='task' AND archived=0 AND status!='cancelled' AND planned_on=?`, domain, actor, day).Scan(&p.Total, &p.Done)
|
||||
return p, err
|
||||
}
|
||||
@@ -86,6 +86,30 @@ func TestTaskProductivityMigrationBackfillsOnce(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDashboardUpgradePreservesTaskPreferences(t *testing.T) {
|
||||
st := newSchemaTestStore(t)
|
||||
if err := db.ApplySQLiteMigrations(context.Background(), st.DB, "task-productivity", taskProductivityMigrations[:1]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := st.DB.Exec(`INSERT INTO organizer_task_preferences VALUES('domain','owner',7,'{"estimate_minutes":90}',123)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for range 2 {
|
||||
if err := st.ensureSchemaComponent("task-productivity"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var version, next int64
|
||||
var raw string
|
||||
if err := st.DB.QueryRow(`SELECT version,data,next_digest_ms FROM organizer_task_preferences`).Scan(&version, &raw, &next); err != nil || version != 7 || next != 123 || raw != `{"estimate_minutes":90}` {
|
||||
t.Fatal(version, raw, next, err)
|
||||
}
|
||||
var count int
|
||||
if err := st.DB.QueryRow(`SELECT count(*) FROM organizer_dashboard_layouts`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal(count, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedSchemasFreshRepeatAndReadOnly(t *testing.T) {
|
||||
for component := range managedStoreSchemas {
|
||||
t.Run(component, func(t *testing.T) {
|
||||
|
||||
@@ -26,7 +26,9 @@ var taskProductivityMigrations = []db.SQLMigration{{Version: 1, Name: "task_capt
|
||||
schemaColumns("organizer_task_preferences", "domain_uuid,owner_uuid,version,data,next_digest_ms"),
|
||||
schemaUnique("organizer_capture_receipts", "domain_uuid,owner_uuid,request_uuid"),
|
||||
schemaIndex("organizer_work_sessions", "organizer_work_period", "domain_uuid,owner_uuid,start_ms,uuid"),
|
||||
}}}
|
||||
}}, {Version: 2, Name: "personal_dashboard_layout", Statements: []string{
|
||||
`CREATE TABLE organizer_dashboard_layouts(domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,version INTEGER NOT NULL,data TEXT NOT NULL,PRIMARY KEY(domain_uuid,owner_uuid))`,
|
||||
}, Assertions: []string{schemaColumns("organizer_dashboard_layouts", "domain_uuid,owner_uuid,version,data"), schemaUnique("organizer_dashboard_layouts", "domain_uuid,owner_uuid")}}}
|
||||
|
||||
type TaskPreferences struct {
|
||||
Version int64 `json:"version"`
|
||||
|
||||
Reference in new issue
Block a user