diff --git a/backend/services/connection_service.go b/backend/services/connection_service.go index e5d38e7..843ebd1 100644 --- a/backend/services/connection_service.go +++ b/backend/services/connection_service.go @@ -119,6 +119,17 @@ func (c *connectionService) RemoveConnection(name string) (resp types.JSResp) { return } +// SaveSortedConnection save sorted connection after drag +func (c *connectionService) SaveSortedConnection(sortedConns types.Connections) (resp types.JSResp) { + err := c.conns.SaveSortedConnection(sortedConns) + if err != nil { + resp.Msg = err.Error() + return + } + resp.Success = true + return +} + // CreateGroup create new group func (c *connectionService) CreateGroup(name string) (resp types.JSResp) { err := c.conns.CreateGroup(name) diff --git a/backend/storage/connections.go b/backend/storage/connections.go index d4a16fc..55d282c 100644 --- a/backend/storage/connections.go +++ b/backend/storage/connections.go @@ -5,6 +5,7 @@ import ( "gopkg.in/yaml.v3" "sync" "tinyrdm/backend/types" + sliceutil "tinyrdm/backend/utils/slice" ) type ConnectionsStorage struct { @@ -240,6 +241,47 @@ func (c *ConnectionsStorage) RemoveConnection(name string) error { return c.saveConnections(conns) } +// SaveSortedConnection save connection after sort +func (c *ConnectionsStorage) SaveSortedConnection(sortedConns types.Connections) error { + c.mutex.Lock() + defer c.mutex.Unlock() + + conns := c.GetConnectionsFlat() + takeConn := func(name string) (types.Connection, bool) { + idx, ok := sliceutil.Find(conns, func(i int) bool { + return conns[i].Name == name + }) + if ok { + ret := conns[idx] + conns = append(conns[:idx], conns[idx+1:]...) + return ret, true + } + return types.Connection{}, false + } + var replaceConn func(connections types.Connections) types.Connections + replaceConn = func(cons types.Connections) types.Connections { + var newConns types.Connections + for _, conn := range cons { + if conn.Type == "group" { + newConns = append(newConns, types.Connection{ + ConnectionConfig: types.ConnectionConfig{ + Name: conn.Name, + }, + Type: "group", + Connections: replaceConn(conn.Connections), + }) + } else { + if foundConn, ok := takeConn(conn.Name); ok { + newConns = append(newConns, foundConn) + } + } + } + return newConns + } + conns = replaceConn(sortedConns) + return c.saveConnections(conns) +} + // CreateGroup create new group func (c *ConnectionsStorage) CreateGroup(name string) error { c.mutex.Lock() diff --git a/backend/types/connection.go b/backend/types/connection.go index 06cdedd..2999629 100644 --- a/backend/types/connection.go +++ b/backend/types/connection.go @@ -4,7 +4,7 @@ type ConnectionCategory int type ConnectionConfig struct { Name string `json:"name" yaml:"name"` - Group string `json:"group" yaml:"-"` + Group string `json:"group,omitempty" yaml:"-"` Addr string `json:"addr,omitempty" yaml:"addr,omitempty"` Port int `json:"port,omitempty" yaml:"port,omitempty"` Username string `json:"username,omitempty" yaml:"username,omitempty"` diff --git a/frontend/src/components/sidebar/ConnectionPane.vue b/frontend/src/components/sidebar/ConnectionPane.vue index f5580f2..dab7d54 100644 --- a/frontend/src/components/sidebar/ConnectionPane.vue +++ b/frontend/src/components/sidebar/ConnectionPane.vue @@ -9,13 +9,11 @@ import Filter from '../icons/Filter.vue' import ConnectionTree from './ConnectionTree.vue' import Unlink from '../icons/Unlink.vue' import useConnectionStore from '../../stores/connections.js' +import { ref } from 'vue' const dialogStore = useDialogStore() const connectionStore = useConnectionStore() - -const onSort = () => { - dialogStore.openPreferencesDialog() -} +const filterPattern = ref('') const onDisconnectAll = () => { connectionStore.closeAllConnection() @@ -24,7 +22,7 @@ const onDisconnectAll = () => {