2022-05-28 18:43:56 +02:00
|
|
|
import { ConnectionParams } from 'common/interfaces/antares';
|
2022-11-21 20:19:02 +01:00
|
|
|
import { uidGen } from 'common/libs/uidGen';
|
2023-08-18 15:57:31 +02:00
|
|
|
import * as crypto from 'crypto';
|
2023-11-12 18:05:11 +01:00
|
|
|
import { ipcRenderer } from 'electron';
|
2023-08-18 15:57:31 +02:00
|
|
|
import * as Store from 'electron-store';
|
|
|
|
import { defineStore } from 'pinia';
|
2023-09-11 18:02:19 +02:00
|
|
|
|
|
|
|
import { useWorkspacesStore } from '@/stores/workspaces';
|
2023-11-13 18:08:29 +01:00
|
|
|
let key = localStorage.getItem('key');
|
2022-04-30 00:47:37 +02:00
|
|
|
|
2022-11-21 20:19:02 +01:00
|
|
|
export interface SidebarElement {
|
|
|
|
isFolder: boolean;
|
|
|
|
uid: string;
|
|
|
|
client?: string;
|
|
|
|
connections?: string[];
|
|
|
|
color?: string;
|
|
|
|
name?: string;
|
2022-11-26 11:21:47 +01:00
|
|
|
icon?: null | string;
|
2022-11-21 20:19:02 +01:00
|
|
|
}
|
|
|
|
|
2023-11-13 18:08:29 +01:00
|
|
|
if (!key) { // If no key in local storace
|
|
|
|
const storedKey = ipcRenderer.sendSync('get-key');// Ask for key stored on disk
|
2023-11-12 18:05:11 +01:00
|
|
|
|
2023-11-13 18:08:29 +01:00
|
|
|
if (!storedKey) { // Of nop stored key on disk
|
2023-11-12 18:05:11 +01:00
|
|
|
const newKey = crypto.randomBytes(16).toString('hex');
|
|
|
|
localStorage.setItem('key', newKey);
|
2023-11-13 18:08:29 +01:00
|
|
|
key = newKey;
|
2023-11-12 18:05:11 +01:00
|
|
|
}
|
2023-11-13 18:08:29 +01:00
|
|
|
else {
|
2023-11-12 18:05:11 +01:00
|
|
|
localStorage.setItem('key', storedKey);
|
2023-11-13 18:08:29 +01:00
|
|
|
key = storedKey;
|
|
|
|
}
|
2023-11-12 18:05:11 +01:00
|
|
|
}
|
2023-11-13 18:08:29 +01:00
|
|
|
else
|
|
|
|
ipcRenderer.send('set-key', key);
|
2022-04-30 00:47:37 +02:00
|
|
|
|
|
|
|
const persistentStore = new Store({
|
|
|
|
name: 'connections',
|
|
|
|
encryptionKey: key,
|
|
|
|
clearInvalidConfig: true
|
|
|
|
});
|
|
|
|
|
|
|
|
export const useConnectionsStore = defineStore('connections', {
|
|
|
|
state: () => ({
|
2022-06-29 19:05:45 +02:00
|
|
|
connections: persistentStore.get('connections', []) as ConnectionParams[],
|
2022-11-21 20:19:02 +01:00
|
|
|
lastConnections: persistentStore.get('lastConnections', []) as {uid: string; time: number}[],
|
|
|
|
connectionsOrder: persistentStore.get('connectionsOrder', []) as SidebarElement[]
|
2022-04-30 00:47:37 +02:00
|
|
|
}),
|
|
|
|
getters: {
|
2022-11-21 20:19:02 +01:00
|
|
|
getConnectionByUid: state => (uid:string) => state.connections.find(connection => connection.uid === uid),
|
2022-05-28 18:43:56 +02:00
|
|
|
getConnectionName: state => (uid: string) => {
|
2022-11-21 20:19:02 +01:00
|
|
|
const connection = state.connections.find(connection => connection.uid === uid);
|
2022-04-30 00:47:37 +02:00
|
|
|
let connectionName = '';
|
2022-11-21 20:19:02 +01:00
|
|
|
if (connection) {
|
|
|
|
if (connection.name)
|
|
|
|
connectionName = connection.name;
|
|
|
|
else if (connection.ask)
|
|
|
|
connectionName = `${connection.host}:${connection.port}`;
|
|
|
|
else if (connection.databasePath) {
|
|
|
|
let string = connection.databasePath.split(/[/\\]+/).pop();
|
2022-04-30 00:47:37 +02:00
|
|
|
|
2022-11-21 20:19:02 +01:00
|
|
|
if (string.length >= 30)
|
|
|
|
string = `...${string.slice(-30)}`;
|
2022-04-30 00:47:37 +02:00
|
|
|
|
2022-11-21 20:19:02 +01:00
|
|
|
connectionName = string;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
connectionName = `${connection.user + '@'}${connection.host}:${connection.port}`;
|
2022-04-30 00:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return connectionName;
|
2022-11-21 20:19:02 +01:00
|
|
|
},
|
2022-11-28 11:56:02 +01:00
|
|
|
getConnectionOrderByUid: state => (uid:string) => state.connectionsOrder
|
|
|
|
.find(connection => connection.uid === uid),
|
|
|
|
getFolders: state => state.connectionsOrder.filter(conn => conn.isFolder),
|
|
|
|
getConnectionFolder: state => (uid:string) => state.connectionsOrder
|
|
|
|
.find(folder => folder.isFolder && folder.connections.includes(uid))
|
2022-04-30 00:47:37 +02:00
|
|
|
},
|
|
|
|
actions: {
|
2022-05-28 18:43:56 +02:00
|
|
|
addConnection (connection: ConnectionParams) {
|
2022-04-30 00:47:37 +02:00
|
|
|
this.connections.push(connection);
|
|
|
|
persistentStore.set('connections', this.connections);
|
2022-11-21 20:19:02 +01:00
|
|
|
|
|
|
|
this.connectionsOrder.push({
|
|
|
|
isFolder: false,
|
|
|
|
uid: connection.uid,
|
2022-11-26 11:21:47 +01:00
|
|
|
client: connection.client,
|
2022-11-29 18:18:56 +01:00
|
|
|
icon: null,
|
|
|
|
name: null
|
2022-11-21 20:19:02 +01:00
|
|
|
});
|
|
|
|
persistentStore.set('connectionsOrder', this.connectionsOrder);
|
|
|
|
},
|
|
|
|
addFolder (params: {after: string; connections: [string, string]}) {
|
|
|
|
const index = this.connectionsOrder.findIndex((conn: SidebarElement) => conn.uid === params.after);
|
2022-11-29 18:18:56 +01:00
|
|
|
|
2022-11-21 20:19:02 +01:00
|
|
|
this.connectionsOrder.splice(index, 0, {
|
|
|
|
isFolder: true,
|
|
|
|
uid: uidGen('F'),
|
|
|
|
name: '',
|
2022-11-26 11:21:47 +01:00
|
|
|
color: '#E36929',
|
2022-11-21 20:19:02 +01:00
|
|
|
connections: params.connections
|
|
|
|
});
|
|
|
|
persistentStore.set('connectionsOrder', this.connectionsOrder);
|
2022-04-30 00:47:37 +02:00
|
|
|
},
|
2022-11-23 17:52:08 +01:00
|
|
|
addToFolder (params: {folder: string; connection: string}) {
|
|
|
|
this.connectionsOrder = this.connectionsOrder.map((conn: SidebarElement) => {
|
|
|
|
if (conn.uid === params.folder)
|
|
|
|
conn.connections.push(params.connection);
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
});
|
|
|
|
persistentStore.set('connectionsOrder', this.connectionsOrder);
|
2022-11-29 13:19:41 +01:00
|
|
|
this.clearEmptyFolders();
|
2022-11-23 17:52:08 +01:00
|
|
|
},
|
2022-11-21 20:19:02 +01:00
|
|
|
deleteConnection (connection: SidebarElement | ConnectionParams) {
|
2022-11-26 11:21:47 +01:00
|
|
|
this.connectionsOrder = (this.connectionsOrder as SidebarElement[]).map(el => { // Removes connection from folders
|
|
|
|
if (el.isFolder && el.connections.includes(connection.uid))
|
|
|
|
el.connections = el.connections.filter(uid => uid !== connection.uid);
|
|
|
|
return el;
|
|
|
|
});
|
2022-11-28 13:06:01 +01:00
|
|
|
this.connectionsOrder = (this.connectionsOrder as SidebarElement[]).filter(el => el.uid !== connection.uid);
|
2023-09-11 18:02:19 +02:00
|
|
|
this.lastConnections = (this.lastConnections as SidebarElement[]).filter(el => el.uid !== connection.uid);
|
2022-11-26 11:21:47 +01:00
|
|
|
|
2022-11-28 13:06:01 +01:00
|
|
|
this.connections = (this.connections as SidebarElement[]).filter(el => el.uid !== connection.uid);
|
|
|
|
persistentStore.set('connections', this.connections);
|
2022-11-29 13:19:41 +01:00
|
|
|
this.clearEmptyFolders();
|
2023-09-11 18:02:19 +02:00
|
|
|
useWorkspacesStore().removeWorkspace(connection.uid);
|
2022-04-30 00:47:37 +02:00
|
|
|
},
|
2022-05-28 18:43:56 +02:00
|
|
|
editConnection (connection: ConnectionParams) {
|
|
|
|
const editedConnections = (this.connections as ConnectionParams[]).map(conn => {
|
2022-04-30 00:47:37 +02:00
|
|
|
if (conn.uid === connection.uid) return connection;
|
|
|
|
return conn;
|
|
|
|
});
|
2022-12-03 17:53:00 +01:00
|
|
|
|
2022-04-30 00:47:37 +02:00
|
|
|
this.connections = editedConnections;
|
|
|
|
persistentStore.set('connections', this.connections);
|
2022-12-03 17:53:00 +01:00
|
|
|
|
|
|
|
const editedConnectionsOrder = (this.connectionsOrder as SidebarElement[]).map(conn => {
|
|
|
|
if (conn.uid === connection.uid) {
|
|
|
|
return {
|
|
|
|
isFolder: false,
|
|
|
|
uid: connection.uid,
|
|
|
|
client: connection.client,
|
|
|
|
icon: conn.icon,
|
|
|
|
name: conn.name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return conn;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.connectionsOrder = editedConnectionsOrder;
|
|
|
|
persistentStore.set('connectionsOrder', this.connectionsOrder);
|
2022-04-30 00:47:37 +02:00
|
|
|
},
|
2022-06-04 18:37:16 +02:00
|
|
|
updateConnections (connections: ConnectionParams[]) {
|
2022-04-30 00:47:37 +02:00
|
|
|
this.connections = connections;
|
|
|
|
persistentStore.set('connections', this.connections);
|
2022-06-29 19:05:45 +02:00
|
|
|
},
|
2022-11-29 18:18:56 +01:00
|
|
|
initConnectionsOrder () {
|
|
|
|
this.connectionsOrder = (this.connections as ConnectionParams[]).map<SidebarElement>(conn => {
|
|
|
|
return {
|
|
|
|
isFolder: false,
|
|
|
|
uid: conn.uid,
|
|
|
|
client: conn.client,
|
|
|
|
icon: null,
|
|
|
|
name: null
|
|
|
|
};
|
|
|
|
});
|
|
|
|
persistentStore.set('connectionsOrder', this.connectionsOrder);
|
|
|
|
},
|
2022-11-21 20:19:02 +01:00
|
|
|
updateConnectionsOrder (connections: SidebarElement[]) {
|
2022-11-23 17:52:08 +01:00
|
|
|
const invalidElements = connections.reduce<{index: number; uid: string}[]>((acc, curr, i) => {
|
|
|
|
if (typeof curr === 'string')
|
|
|
|
acc.push({ index: i, uid: curr });
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
2022-11-24 16:40:04 +01:00
|
|
|
if (invalidElements.length) {
|
|
|
|
invalidElements.forEach(el => {
|
|
|
|
let connIndex = connections.findIndex(conn => conn.uid === el.uid);
|
|
|
|
const conn = connections[connIndex];
|
2022-11-23 17:52:08 +01:00
|
|
|
|
2022-11-24 16:40:04 +01:00
|
|
|
if (connIndex === -1) return;
|
2022-11-23 17:52:08 +01:00
|
|
|
|
2022-11-24 16:40:04 +01:00
|
|
|
connections.splice(el.index, 1, { // Move to new position
|
|
|
|
isFolder: false,
|
|
|
|
client: conn.client,
|
2022-11-27 17:52:32 +01:00
|
|
|
uid: conn.uid,
|
|
|
|
icon: conn.icon,
|
|
|
|
name: conn.name
|
2022-11-24 16:40:04 +01:00
|
|
|
});
|
2022-11-23 17:52:08 +01:00
|
|
|
|
2022-11-28 09:42:18 +01:00
|
|
|
connIndex = connections.findIndex((conn, i) => conn.uid === el.uid && i !== el.index);
|
2022-11-24 16:40:04 +01:00
|
|
|
connections.splice(connIndex, 1);// Delete old object
|
2022-11-23 17:52:08 +01:00
|
|
|
});
|
2022-11-24 16:40:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear empty folders
|
|
|
|
const emptyFolders = connections.reduce<string[]>((acc, curr) => {
|
|
|
|
if (curr.connections && curr.connections.length === 0)
|
|
|
|
acc.push(curr.uid);
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
connections = connections.filter(el => !emptyFolders.includes(el.uid));
|
2022-11-23 17:52:08 +01:00
|
|
|
|
2022-11-21 20:19:02 +01:00
|
|
|
this.connectionsOrder = connections;
|
|
|
|
persistentStore.set('connectionsOrder', this.connectionsOrder);
|
2022-06-29 19:05:45 +02:00
|
|
|
},
|
2022-11-26 11:21:47 +01:00
|
|
|
updateConnectionOrder (element: SidebarElement) {
|
|
|
|
this.connectionsOrder = (this.connectionsOrder as SidebarElement[]).map(el => {
|
|
|
|
if (el.uid === element.uid)
|
|
|
|
el = element;
|
|
|
|
return el;
|
|
|
|
});
|
2022-11-27 17:52:32 +01:00
|
|
|
persistentStore.set('connectionsOrder', this.connectionsOrder);
|
2022-11-26 11:21:47 +01:00
|
|
|
},
|
2022-06-29 19:05:45 +02:00
|
|
|
updateLastConnection (uid: string) {
|
2022-06-30 18:20:14 +02:00
|
|
|
const cIndex = (this.lastConnections as {uid: string; time: number}[]).findIndex((c) => c.uid === uid);
|
|
|
|
|
|
|
|
if (cIndex >= 0)
|
|
|
|
this.lastConnections[cIndex].time = new Date().getTime();
|
|
|
|
else
|
|
|
|
this.lastConnections.push({ uid, time: new Date().getTime() });
|
|
|
|
|
2022-06-29 19:05:45 +02:00
|
|
|
persistentStore.set('lastConnections', this.lastConnections);
|
2022-11-29 13:19:41 +01:00
|
|
|
},
|
|
|
|
clearEmptyFolders () {
|
|
|
|
// Clear empty folders
|
|
|
|
const emptyFolders = (this.connectionsOrder as SidebarElement[]).reduce<string[]>((acc, curr) => {
|
|
|
|
if (curr.connections && curr.connections.length === 0)
|
|
|
|
acc.push(curr.uid);
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
this.connectionsOrder = (this.connectionsOrder as SidebarElement[]).filter(el => !emptyFolders.includes(el.uid));
|
|
|
|
persistentStore.set('connectionsOrder', this.connectionsOrder);
|
2023-08-18 12:11:08 +02:00
|
|
|
},
|
|
|
|
importConnections (importObj: {
|
|
|
|
connections: ConnectionParams[];
|
|
|
|
connectionsOrder: SidebarElement[];
|
|
|
|
}) {
|
|
|
|
this.connections = [...this.connections, ...importObj.connections];
|
|
|
|
this.connectionsOrder = [...this.connectionsOrder, ...importObj.connectionsOrder];
|
|
|
|
|
|
|
|
persistentStore.set('connections', this.connections);
|
|
|
|
persistentStore.set('connectionsOrder', this.connectionsOrder);
|
2022-04-30 00:47:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|