package store import ( "context" "encoding/json" "errors" "strconv" ) // View colours only change how the web UI shows a view, so they are kept // outside the configuration: setting one needs no changeset or deploy. const viewColorsKey = "view_colors" // ViewColors returns the colour of each view that has one, by view ID. func (s *Store) ViewColors(ctx context.Context) (map[int64]string, error) { out := map[int64]string{} raw, err := s.GetMeta(ctx, viewColorsKey) if errors.Is(err, ErrNotFound) { return out, nil } if err != nil { return nil, err } var m map[string]string if err := json.Unmarshal([]byte(raw), &m); err != nil { return nil, err } for k, v := range m { if id, err := strconv.ParseInt(k, 10, 64); err == nil { out[id] = v } } return out, nil } // SetViewColor sets a view's colour; "" removes it. View IDs are never // reused, so the colour of a deleted view is harmless and is left alone // (the view may still exist in a changeset). func (s *Store) SetViewColor(ctx context.Context, id int64, color string) error { m, err := s.ViewColors(ctx) if err != nil { return err } if color == "" { delete(m, id) } else { m[id] = color } out := map[string]string{} for k, v := range m { out[strconv.FormatInt(k, 10)] = v } b, _ := json.Marshal(out) return s.SetMeta(ctx, viewColorsKey, string(b)) }