fix(dashboard): normalize recoverable layout preferences on save
This commit is contained in:
1 parent
667776e43f
commit
38b0fe2a94
4 files changed
+43
-4
No files matched your search
@@ -69,12 +69,22 @@ func TestOrganizerDashboardLayoutIsolationAndCAS(t *testing.T) {
|
||||
if err = json.Unmarshal(w.Body.Bytes(), &layout); err != nil || len(layout.Widgets) != 0 {
|
||||
t.Fatal("empty layout reset", layout, err)
|
||||
}
|
||||
for _, invalid := range [][]store.DashboardWidget{{{ID: "iframe", Width: 4, Height: 3}}, {{ID: "tasks", Width: 13, Height: 3, Tab: "inbox"}}, {{ID: "tasks", Width: 4, Height: 3, Tab: "secret"}}, {{ID: "progress", Width: 4, Height: 3}, {ID: "progress", Width: 4, Height: 3}}} {
|
||||
for _, invalid := range [][]store.DashboardWidget{{{ID: "iframe", Width: 4, Height: 3}}} {
|
||||
w = taskRequest(s, token, "PUT", "dashboard", map[string]any{"version": layout.Version, "widgets": invalid}, "")
|
||||
if w.Code != 422 {
|
||||
t.Fatal("invalid accepted", w.Code, w.Body)
|
||||
}
|
||||
}
|
||||
for _, id := range []string{"commands", "tomorrow-schedule", "feeds"} {
|
||||
input := []store.DashboardWidget{{ID: "tasks", Width: 13, Height: 0}, {ID: id, Width: 6, Height: 4, Tab: "obsolete"}, {ID: id, Width: 6, Height: 4}}
|
||||
w = taskRequest(s, token, "PUT", "dashboard", map[string]any{"version": layout.Version, "widgets": input}, "")
|
||||
if w.Code != 200 {
|
||||
t.Fatal("normalizable layout rejected", w.Code, w.Body)
|
||||
}
|
||||
if err = json.Unmarshal(w.Body.Bytes(), &layout); err != nil || len(layout.Widgets) != 2 || layout.Widgets[0].Width != 12 || layout.Widgets[0].Height != 2 || layout.Widgets[0].Tab != "inbox" || layout.Widgets[1].Tab != "" {
|
||||
t.Fatal("normalization", layout, err)
|
||||
}
|
||||
}
|
||||
other, err := s.store.CreatePasswordAccount(domain, "dashboardother", "long test password", "Other", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -53,6 +53,33 @@ type DashboardLayout struct {
|
||||
Widgets []DashboardWidget `json:"widgets"`
|
||||
}
|
||||
|
||||
func normalizeDashboard(items []DashboardWidget) ([]DashboardWidget, error) {
|
||||
if len(items) > 100 {
|
||||
return nil, ErrTaskInvalid
|
||||
}
|
||||
out := make([]DashboardWidget, 0, len(items))
|
||||
seen := map[string]bool{}
|
||||
for _, item := range items {
|
||||
if seen[item.ID] {
|
||||
continue
|
||||
}
|
||||
seen[item.ID] = true
|
||||
item.Width = max(3, min(12, item.Width))
|
||||
item.Height = max(2, min(10, item.Height))
|
||||
if item.ID == "tasks" {
|
||||
switch item.Tab {
|
||||
case "inbox", "backlog", "someday", "review", "all", "done":
|
||||
default:
|
||||
item.Tab = "inbox"
|
||||
}
|
||||
} else {
|
||||
item.Tab = ""
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out, ValidateDashboard(out)
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -79,9 +106,11 @@ func (s *Store) SaveDashboard(ctx context.Context, domain, actor string, version
|
||||
if version < 0 {
|
||||
return p, ErrTaskInvalid
|
||||
}
|
||||
if err := ValidateDashboard(widgets); err != nil {
|
||||
normalized, err := normalizeDashboard(widgets)
|
||||
if err != nil {
|
||||
return p, err
|
||||
}
|
||||
widgets = normalized
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return p, err
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
}
|
||||
const save=button(t('Save layout','配置を保存'),t('Save layout','配置を保存'),async()=>{
|
||||
if(saving||!loaded)return;saving=true;updateControls();
|
||||
try{const data=await request(api+'dashboard',{method:'PUT',signal:AbortSignal.timeout(15000),body:JSON.stringify({version,widgets})});version=data.version;dirty=false;status.textContent=t('Layout saved','配置を保存しました');}
|
||||
try{const data=await request(api+'dashboard',{method:'PUT',signal:AbortSignal.timeout(15000),body:JSON.stringify({version,widgets:widgets.map(({id,width,height,tab})=>({id,width,height,...(id==='tasks'?{tab:tab||'inbox'}:{})}))})});version=data.version;widgets=data.widgets;dirty=false;render();status.textContent=t('Layout saved','配置を保存しました');}
|
||||
catch(e){status.textContent=e.message;}finally{saving=false;updateControls();}
|
||||
});
|
||||
const edit=button('⚙',t('Arrange widgets','ウィジェットを配置'),()=>{editing=!editing;root.classList.toggle('is-arranging',editing);edit.setAttribute('aria-pressed',String(editing));});edit.setAttribute('aria-pressed','false');
|
||||
|
||||
@@ -4,4 +4,4 @@
|
||||
{{ template "organizer-nav" . }}
|
||||
<main id="organizer-dashboard" data-writable="{{ .TaskWritable }}" data-calendar="{{ .CalendarEnabled }}" data-today="{{ .Today }}" data-timezone="{{ .Timezone }}">
|
||||
<header class="dashboard-toolbar"><div><h1>{{ T "organizer.dashboard" }}</h1><small>{{ .Today }} · {{ .Timezone }}</small></div><div id="dashboard-controls"></div></header><p id="dashboard-status" role="status" aria-live="polite"></p><div id="dashboard-grid"></div>
|
||||
</main><script src="/static/js/task-presentation.js?v=1" defer></script><script src="/static/js/dashboard-commands.js?v=1" defer></script><script src="/static/js/organizer-dashboard.js?v=3" defer></script><script src="/static/js/task-productivity.js?v=8" defer></script></body></html>{{ end }}
|
||||
</main><script src="/static/js/task-presentation.js?v=1" defer></script><script src="/static/js/dashboard-commands.js?v=1" defer></script><script src="/static/js/organizer-dashboard.js?v=4" defer></script><script src="/static/js/task-productivity.js?v=8" defer></script></body></html>{{ end }}
|
||||
Reference in new issue
Block a user