Use vault views for entry and recycle-bin state

This commit is contained in:
Joe Julian
2026-04-13 07:12:32 -07:00
parent ea30775eb7
commit 59cd01f8e7
7 changed files with 338 additions and 63 deletions
+31 -2
View File
@@ -28,7 +28,7 @@ func Vault(model vault.Model) View {
// VaultRoot returns the logical main-vault view rooted at the physical
// keepass storage group.
func VaultRoot(model vault.Model) View {
return rootView{model: model}
return rootView{model: model, rooted: usesKeepassRoot(model)}
}
// VaultRecycleBin returns the logical recycle-bin view.
@@ -69,7 +69,8 @@ func (v physicalView) FromPhysicalEntry(entry vault.Entry) vault.Entry {
}
type rootView struct {
model vault.Model
model vault.Model
rooted bool
}
func (v rootView) ChildGroups(path []string) []string {
@@ -85,6 +86,9 @@ func (v rootView) EntriesUnderPath(path []string) []vault.Entry {
}
func (v rootView) ToPhysicalPath(path []string) []string {
if !v.rooted {
return clonePath(path)
}
if len(path) == 0 {
return []string{KeepassRoot}
}
@@ -92,6 +96,9 @@ func (v rootView) ToPhysicalPath(path []string) []string {
}
func (v rootView) FromPhysicalPath(path []string) []string {
if !v.rooted {
return clonePath(path)
}
if len(path) == 0 {
return nil
}
@@ -267,3 +274,25 @@ func clonePath(path []string) []string {
}
return slices.Clone(path)
}
func usesKeepassRoot(model vault.Model) bool {
if len(model.Entries) == 0 && len(model.Groups) == 0 && len(model.RecycleBin) == 0 {
return true
}
for _, group := range model.Groups {
if len(group) > 0 && group[0] == KeepassRoot {
return true
}
}
for _, entry := range model.Entries {
if len(entry.Path) > 0 && entry.Path[0] == KeepassRoot {
return true
}
}
for _, entry := range model.RecycleBin {
if len(entry.Path) > 0 && entry.Path[0] == KeepassRoot {
return true
}
}
return false
}