Skip to content

Commit

Permalink
add: refresh logic for tree
Browse files Browse the repository at this point in the history
- refactored tree node init
- new function for reset
  - reset filter
  - delete old nodes -> init new nodes
  - TODO: close table view if table was deleted
  • Loading branch information
gytic authored and [email protected] committed Nov 17, 2024
1 parent b08f1aa commit c0e5767
Showing 1 changed file with 65 additions and 32 deletions.
97 changes: 65 additions & 32 deletions components/Tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,7 @@ func NewTree(dbName string, dbdriver drivers.Driver) *Tree {
tree.SetCurrentNode(rootNode)

tree.SetFocusFunc(func() {
var databases []string

if dbName == "" {
dbs, err := tree.DBDriver.GetDatabases()
if err != nil {
panic(err.Error())
}
databases = dbs
} else {
databases = []string{dbName}
}

if tree.GetSelectedDatabase() == "" {
for _, database := range databases {
childNode := tview.NewTreeNode(database)
childNode.SetExpanded(false)
childNode.SetReference(database)
childNode.SetColor(app.Styles.PrimaryTextColor)
rootNode.AddChild(childNode)

go func(database string, node *tview.TreeNode) {
tables, err := tree.DBDriver.GetTables(database)
if err != nil {
logger.Error(err.Error(), nil)
return
}

tree.databasesToNodes(tables, node, true)
App.Draw()
}(database, childNode)
}
}
tree.InitializeNodes(dbName)
tree.SetFocusFunc(nil)
})

Expand Down Expand Up @@ -202,6 +171,8 @@ func NewTree(dbName string, dbdriver drivers.Driver) *Tree {
tree.CollapseAll()
case commands.ExpandAll:
tree.ExpandAll()
case commands.Refresh:
tree.Refresh(dbName)
}
return nil
})
Expand Down Expand Up @@ -557,3 +528,65 @@ func (tree *Tree) ExpandAll() {
return true
})
}

func (tree *Tree) InitializeNodes(dbName string) {
rootNode := tree.GetRoot()
if rootNode == nil {
panic("Internal Error: No tree root")
}

var databases []string

if dbName == "" {
dbs, err := tree.DBDriver.GetDatabases()
if err != nil {
panic(err.Error())
}
databases = dbs
} else {
databases = []string{dbName}
}

if tree.GetSelectedDatabase() == "" {
for _, database := range databases {
childNode := tview.NewTreeNode(database)
childNode.SetExpanded(false)
childNode.SetReference(database)
childNode.SetColor(app.Styles.PrimaryTextColor)
rootNode.AddChild(childNode)

go func(database string, node *tview.TreeNode) {
tables, err := tree.DBDriver.GetTables(database)
if err != nil {
logger.Error(err.Error(), nil)
return
}

tree.databasesToNodes(tables, node, true)
App.Draw()
}(database, childNode)
}
}
}

func (tree *Tree) Refresh(dbName string) {
rootNode := tree.GetRoot()
if rootNode == nil {
return
}
// remove existing Children
rootNode.ClearChildren()
// readd nodes
tree.InitializeNodes(dbName)
// reset search
tree.search("")

/** TODO:
check if the currently opened table was deleted
- if deleted
- selecedTable and selectedDatabase need to be reset
- close Table view
- if not
- do nothing
*/
}

0 comments on commit c0e5767

Please sign in to comment.