antares/src/renderer/store/modules/workspaces.store.js

622 lines
22 KiB
JavaScript
Raw Normal View History

'use strict';
2020-05-18 18:06:32 +02:00
import Connection from '@/ipc-api/Connection';
2021-03-17 11:15:14 +01:00
import Schema from '@/ipc-api/Schema';
import Users from '@/ipc-api/Users';
import { uidGen } from 'common/libs/uidGen';
const tabIndex = [];
2020-12-03 13:00:54 +01:00
let lastBreadcrumbs = {};
2020-06-05 21:00:15 +02:00
export default {
namespaced: true,
strict: true,
state: {
workspaces: [],
2020-12-03 13:00:54 +01:00
selected_workspace: null,
2020-12-04 11:19:16 +01:00
has_unsaved_changes: false,
is_unsaved_discard_modal: false,
pending_breadcrumbs: {}
},
getters: {
getSelected: state => {
2020-05-18 18:06:32 +02:00
if (state.selected_workspace) return state.selected_workspace;
if (state.workspaces.length) return state.workspaces[0].uid;
2021-07-08 17:43:33 +02:00
return 'NEW';
},
2020-05-20 18:00:14 +02:00
getWorkspace: state => uid => {
return state.workspaces.find(workspace => workspace.uid === uid);
},
2020-09-25 12:39:58 +02:00
getDatabaseVariable: state => (uid, name) => {
return state.workspaces.find(workspace => workspace.uid === uid).variables.find(variable => variable.name === name);
},
getWorkspaceTab: (state, getters) => tUid => {
if (!getters.getSelected) return;
const workspace = state.workspaces.find(workspace => workspace.uid === getters.getSelected);
if ('tabs' in workspace)
return workspace.tabs.find(tab => tab.uid === tUid);
return {};
2020-05-20 18:00:14 +02:00
},
getConnected: state => {
return state.workspaces
.filter(workspace => workspace.connection_status === 'connected')
2020-05-20 18:00:14 +02:00
.map(workspace => workspace.uid);
2020-12-04 11:19:16 +01:00
},
getLoadedSchemas: state => uid => {
return state.workspaces.find(workspace => workspace.uid === uid).loaded_schemas;
},
2021-02-20 11:55:34 +01:00
getSearchTerm: state => uid => {
return state.workspaces.find(workspace => workspace.uid === uid).search_term;
},
2020-12-04 11:19:16 +01:00
isUnsavedDiscardModal: state => {
return state.is_unsaved_discard_modal;
2020-05-20 18:00:14 +02:00
}
},
mutations: {
SELECT_WORKSPACE (state, uid) {
2021-07-08 17:43:33 +02:00
if (!uid)
state.selected_workspace = state.workspaces.length ? state.workspaces[0].uid : 'NEW';
else
state.selected_workspace = uid;
},
SET_CONNECTED (state, payload) {
const { uid, client, dataTypes, indexTypes, customizations, structure, version } = payload;
2021-02-06 12:37:37 +01:00
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
client,
dataTypes,
2020-12-01 16:48:20 +01:00
indexTypes,
customizations,
2020-11-13 12:39:40 +01:00
structure,
connection_status: 'connected',
version
2020-11-13 12:39:40 +01:00
}
: workspace);
},
SET_CONNECTING (state, uid) {
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
structure: {},
breadcrumbs: {},
loaded_schemas: new Set(),
connection_status: 'connecting'
}
: workspace);
},
SET_FAILED (state, uid) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
structure: {},
breadcrumbs: {},
loaded_schemas: new Set(),
connection_status: 'failed'
}
: workspace);
},
SET_DISCONNECTED (state, uid) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
structure: {},
breadcrumbs: {},
loaded_schemas: new Set(),
connection_status: 'disconnected'
2020-11-13 12:39:40 +01:00
}
: workspace);
2020-05-20 18:00:14 +02:00
},
2020-05-31 17:56:33 +02:00
REFRESH_STRUCTURE (state, { uid, structure }) {
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
structure
}
: workspace);
2020-05-18 18:06:32 +02:00
},
REFRESH_SCHEMA (state, { uid, schema, schemaElements }) {
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === uid) {
const schemaIndex = workspace.structure.findIndex(s => s.name === schema);
if (schemaIndex !== -1)
workspace.structure[schemaIndex] = schemaElements;
else
workspace.structure.push(schemaElements);
}
return workspace;
});
},
2020-09-25 12:39:58 +02:00
REFRESH_COLLATIONS (state, { uid, collations }) {
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
collations
}
: workspace);
2020-09-25 12:39:58 +02:00
},
REFRESH_VARIABLES (state, { uid, variables }) {
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
variables
}
: workspace);
},
2020-11-16 17:16:39 +01:00
REFRESH_ENGINES (state, { uid, engines }) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
engines
}
: workspace);
},
REFRESH_USERS (state, { uid, users }) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
users
}
: workspace);
},
2020-05-18 18:06:32 +02:00
ADD_WORKSPACE (state, workspace) {
state.workspaces.push(workspace);
2020-06-05 21:00:15 +02:00
},
CHANGE_BREADCRUMBS (state, { uid, breadcrumbs }) {
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
breadcrumbs
}
: workspace);
2020-06-05 21:00:15 +02:00
},
2021-02-20 11:55:34 +01:00
SET_SEARCH_TERM (state, { uid, term }) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
search_term: term
}
: workspace);
},
2021-07-14 12:33:26 +02:00
NEW_TAB (state, { uid, tab, content, type, autorun, schema, table, element }) {
2021-07-12 19:18:29 +02:00
if (type === 'query')
tabIndex[uid] = tabIndex[uid] ? ++tabIndex[uid] : 1;
2020-06-05 21:00:15 +02:00
const newTab = {
2020-12-11 15:55:18 +01:00
uid: tab,
2021-07-12 19:18:29 +02:00
index: type === 'query' ? tabIndex[uid] : null,
2020-06-12 18:10:45 +02:00
selected: false,
2021-07-12 19:18:29 +02:00
type,
schema,
table,
2021-07-14 12:33:26 +02:00
element,
fields: [],
keyUsage: [],
content: content || '',
autorun: !!autorun
2020-06-05 21:00:15 +02:00
};
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
tabs: [...workspace.tabs, newTab]
};
}
else
return workspace;
});
2020-06-12 18:10:45 +02:00
},
2020-08-19 18:20:57 +02:00
REMOVE_TAB (state, { uid, tab: tUid }) {
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
tabs: workspace.tabs.filter(tab => tab.uid !== tUid)
};
}
else
return workspace;
});
},
2021-07-14 12:33:26 +02:00
REPLACE_TAB (state, { uid, tab: tUid, type, schema, table, element }) {
2021-07-13 16:53:47 +02:00
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
tabs: workspace.tabs.map(tab => {
if (tab.uid === tUid)
2021-07-14 12:33:26 +02:00
return { ...tab, type, schema, table, element };
2021-07-13 16:53:47 +02:00
return tab;
})
};
}
else
return workspace;
});
},
RENAME_TABS (state, { uid, schema, elementName, elementType, elementNewName }) {
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
tabs: workspace.tabs.map(tab => {
if (tab[elementType] === elementName && tab.schema === schema) {
return {
...tab,
[elementType]: elementNewName
};
}
return tab;
})
};
}
else
return workspace;
});
},
2020-06-12 18:10:45 +02:00
SELECT_TAB (state, { uid, tab }) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid ? { ...workspace, selected_tab: tab } : workspace);
},
2021-07-14 20:30:54 +02:00
UPDATE_TABS (state, { uid, tabs }) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid ? { ...workspace, tabs } : workspace);
2021-07-14 20:30:54 +02:00
},
SET_TAB_FIELDS (state, { cUid, tUid, fields }) {
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === cUid) {
return {
...workspace,
tabs: workspace.tabs.map(tab => {
if (tab.uid === tUid)
return { ...tab, fields };
else
return tab;
})
};
}
else
return workspace;
});
},
SET_TAB_KEY_USAGE (state, { cUid, tUid, keyUsage }) {
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === cUid) {
return {
...workspace,
tabs: workspace.tabs.map(tab => {
if (tab.uid === tUid)
return { ...tab, keyUsage };
else
return tab;
})
};
}
else
return workspace;
});
2020-12-04 11:19:16 +01:00
},
SET_UNSAVED_CHANGES (state, val) {
state.has_unsaved_changes = !!val;
},
SET_UNSAVED_DISCARD_MODAL (state, val) {
state.is_unsaved_discard_modal = !!val;
},
SET_PENDING_BREADCRUMBS (state, payload) {
state.pending_breadcrumbs = payload;
},
ADD_LOADED_SCHEMA (state, payload) {
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === payload.uid)
workspace.loaded_schemas.add(payload.schema);
return workspace;
});
}
},
actions: {
2020-11-13 12:39:40 +01:00
selectWorkspace ({ commit }, uid) {
commit('SELECT_WORKSPACE', uid);
},
2020-05-19 18:06:05 +02:00
async connectWorkspace ({ dispatch, commit }, connection) {
commit('SET_CONNECTING', connection.uid);
2020-05-19 18:06:05 +02:00
try {
const { status, response } = await Connection.connect(connection);
if (status === 'error') {
2020-05-19 18:06:05 +02:00
dispatch('notifications/addNotification', { status, message: response }, { root: true });
commit('SET_FAILED', connection.uid);
}
else {
2020-11-13 12:39:40 +01:00
let dataTypes = [];
2020-12-01 16:48:20 +01:00
let indexTypes = [];
let customizations = {};
2020-11-13 12:39:40 +01:00
switch (connection.client) {
case 'mysql':
case 'maria':
dataTypes = require('common/data-types/mysql');
2020-12-01 16:48:20 +01:00
indexTypes = require('common/index-types/mysql');
customizations = require('common/customizations/mysql');
break;
case 'pg':
dataTypes = require('common/data-types/postgresql');
indexTypes = require('common/index-types/postgresql');
customizations = require('common/customizations/postgresql');
2020-11-13 12:39:40 +01:00
break;
}
2021-03-17 11:15:14 +01:00
const { status, response: version } = await Schema.getVersion(connection.uid);
if (status === 'error')
dispatch('notifications/addNotification', { status, message: version }, { root: true });
2021-02-06 12:37:37 +01:00
const isMySQL = version.name.includes('MySQL');
if (isMySQL && connection.client !== 'mysql') {
const connProxy = Object.assign({}, connection);
connProxy.client = 'mysql';
dispatch('connections/editConnection', connProxy, { root: true });
}
else if (!isMySQL && connection.client === 'mysql') {
const connProxy = Object.assign({}, connection);
connProxy.client = 'maria';
dispatch('connections/editConnection', connProxy, { root: true });
}
commit('SET_CONNECTED', {
2020-11-13 12:39:40 +01:00
uid: connection.uid,
client: connection.client,
dataTypes,
2020-12-01 16:48:20 +01:00
indexTypes,
customizations,
structure: response,
version
2020-11-13 12:39:40 +01:00
});
dispatch('refreshCollations', connection.uid);
2020-09-25 12:39:58 +02:00
dispatch('refreshVariables', connection.uid);
2020-11-16 17:16:39 +01:00
dispatch('refreshEngines', connection.uid);
dispatch('refreshUsers', connection.uid);
}
2020-05-19 18:06:05 +02:00
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
async refreshStructure ({ dispatch, commit, getters }, uid) {
2020-05-20 18:00:14 +02:00
try {
2021-03-17 11:15:14 +01:00
const { status, response } = await Schema.getStructure({ uid, schemas: getters.getLoadedSchemas(uid) });
2020-05-20 18:00:14 +02:00
if (status === 'error')
dispatch('notifications/addNotification', { status, message: response }, { root: true });
else
commit('REFRESH_STRUCTURE', { uid, structure: response });
2020-05-20 18:00:14 +02:00
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
async refreshSchema ({ dispatch, commit }, { uid, schema }) {
try {
2021-03-17 11:15:14 +01:00
const { status, response } = await Schema.getStructure({ uid, schemas: new Set([schema]) });
if (status === 'error')
dispatch('notifications/addNotification', { status, message: response }, { root: true });
else
commit('REFRESH_SCHEMA', { uid, schema, schemaElements: response.find(_schema => _schema.name === schema) });
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
async refreshCollations ({ dispatch, commit }, uid) {
try {
2021-03-17 11:15:14 +01:00
const { status, response } = await Schema.getCollations(uid);
if (status === 'error')
dispatch('notifications/addNotification', { status, message: response }, { root: true });
else
commit('REFRESH_COLLATIONS', { uid, collations: response });
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
2020-09-25 12:39:58 +02:00
async refreshVariables ({ dispatch, commit }, uid) {
try {
2021-03-17 11:15:14 +01:00
const { status, response } = await Schema.getVariables(uid);
2020-09-25 12:39:58 +02:00
if (status === 'error')
dispatch('notifications/addNotification', { status, message: response }, { root: true });
else
commit('REFRESH_VARIABLES', { uid, variables: response });
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
2020-11-16 17:16:39 +01:00
async refreshEngines ({ dispatch, commit }, uid) {
try {
2021-03-17 11:15:14 +01:00
const { status, response } = await Schema.getEngines(uid);
2020-11-16 17:16:39 +01:00
if (status === 'error')
dispatch('notifications/addNotification', { status, message: response }, { root: true });
else
commit('REFRESH_ENGINES', { uid, engines: response });
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
async refreshUsers ({ dispatch, commit }, uid) {
try {
const { status, response } = await Users.getUsers(uid);
if (status === 'error')
dispatch('notifications/addNotification', { status, message: response }, { root: true });
else
commit('REFRESH_USERS', { uid, users: response });
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
2020-06-05 21:00:15 +02:00
removeConnected ({ commit }, uid) {
2020-05-18 18:06:32 +02:00
Connection.disconnect(uid);
commit('SET_DISCONNECTED', uid);
2020-06-13 18:14:32 +02:00
commit('SELECT_TAB', { uid, tab: 0 });
2020-05-18 18:06:32 +02:00
},
2020-06-05 21:00:15 +02:00
addWorkspace ({ commit, dispatch, getters }, uid) {
2020-05-18 18:06:32 +02:00
const workspace = {
uid,
connection_status: 'disconnected',
2020-06-13 18:14:32 +02:00
selected_tab: 0,
2021-02-20 11:55:34 +01:00
search_term: '',
tabs: [],
2020-06-05 21:00:15 +02:00
structure: {},
2020-09-25 12:39:58 +02:00
variables: [],
collations: [],
users: [],
breadcrumbs: {},
loaded_schemas: new Set()
2020-05-18 18:06:32 +02:00
};
2020-06-05 21:00:15 +02:00
2020-05-18 18:06:32 +02:00
commit('ADD_WORKSPACE', workspace);
2020-06-05 21:00:15 +02:00
2020-08-19 18:20:57 +02:00
if (getters.getWorkspace(uid).tabs.length < 3)
2021-07-12 19:18:29 +02:00
dispatch('newTab', { uid, type: 'query' });
2020-12-04 11:19:16 +01:00
dispatch('setUnsavedChanges', false);
2020-06-05 21:00:15 +02:00
},
2020-12-04 11:19:16 +01:00
changeBreadcrumbs ({ state, commit, getters }, payload) {
if (state.has_unsaved_changes) {
commit('SET_UNSAVED_DISCARD_MODAL', true);
commit('SET_PENDING_BREADCRUMBS', payload);
return;
}
2020-12-03 13:00:54 +01:00
const breadcrumbsObj = {
schema: null,
table: null,
trigger: null,
procedure: null,
2021-01-05 17:25:18 +01:00
function: null,
2020-12-26 14:47:15 +01:00
scheduler: null,
view: null,
query: null
2020-12-03 13:00:54 +01:00
};
const hasLastChildren = Object.keys(lastBreadcrumbs).filter(b => b !== 'schema').some(b => lastBreadcrumbs[b]);
const hasChildren = Object.keys(payload).filter(b => b !== 'schema').some(b => payload[b]);
if (lastBreadcrumbs.schema === payload.schema && hasLastChildren && !hasChildren) return;
2020-12-03 13:00:54 +01:00
if (lastBreadcrumbs.schema !== payload.schema)
2021-03-17 11:15:14 +01:00
Schema.useSchema({ uid: getters.getSelected, schema: payload.schema });
2020-12-03 13:00:54 +01:00
commit('CHANGE_BREADCRUMBS', { uid: getters.getSelected, breadcrumbs: { ...breadcrumbsObj, ...payload } });
lastBreadcrumbs = { ...breadcrumbsObj, ...payload };
if (payload.schema)
commit('ADD_LOADED_SCHEMA', { uid: getters.getSelected, schema: payload.schema });
2020-06-05 21:00:15 +02:00
},
2021-02-20 11:55:34 +01:00
setSearchTerm ({ commit, getters }, term) {
commit('SET_SEARCH_TERM', { uid: getters.getSelected, term });
},
2021-07-14 12:33:26 +02:00
newTab ({ state, commit }, { uid, content, type, autorun, schema, table, element }) {
2021-07-13 16:53:47 +02:00
let tabUid;
2021-07-13 19:23:02 +02:00
const workspaceTabs = state.workspaces.find(workspace => workspace.uid === uid);
2021-07-12 19:18:29 +02:00
if (type === 'temp-data') {
2021-07-13 19:23:02 +02:00
const existentTab = workspaceTabs
? workspaceTabs.tabs.find(tab =>
tab.schema === schema &&
tab.table === table &&
['temp-data', 'data'].includes(tab.type))
: false;
if (existentTab) { // if data tab exists
tabUid = existentTab.uid;
}
else {
const tempTabs = workspaceTabs ? workspaceTabs.tabs.filter(tab => tab.type === 'temp-data') : false;
if (tempTabs && tempTabs.length) { // if temp table already opened
for (const tab of tempTabs) {
2021-07-14 12:33:26 +02:00
commit('REPLACE_TAB', { uid, tab: tab.uid, type, schema, table, element });
2021-07-13 19:23:02 +02:00
tabUid = tab.uid;
}
2021-07-13 16:53:47 +02:00
}
2021-07-13 19:23:02 +02:00
else {
tabUid = uidGen('T');
2021-07-14 12:33:26 +02:00
commit('NEW_TAB', { uid, tab: tabUid, content, type, autorun, schema, table, element });
2021-07-13 19:23:02 +02:00
}
}
}
else if (type === 'data') {
const existentTab = workspaceTabs
? workspaceTabs.tabs.find(tab =>
tab.schema === schema &&
tab.table === table &&
['temp-data', 'data'].includes(tab.type))
: false;
if (existentTab) {
2021-07-14 12:33:26 +02:00
commit('REPLACE_TAB', { uid, tab: existentTab.uid, type, schema, table, element });
2021-07-13 19:23:02 +02:00
tabUid = existentTab.uid;
2021-07-13 16:53:47 +02:00
}
else {
tabUid = uidGen('T');
commit('NEW_TAB', { uid, tab: tabUid, content, type, autorun, schema, table });
2021-07-12 19:18:29 +02:00
}
}
2021-07-15 19:51:18 +02:00
else if (type === 'table-props') {
const existentTab = workspaceTabs
? workspaceTabs.tabs.find(tab =>
tab.schema === schema &&
tab.table === table &&
tab.type === type)
: false;
if (existentTab) {
commit('REPLACE_TAB', { uid, tab: existentTab.uid, type, schema, table, element });
tabUid = existentTab.uid;
}
else {
tabUid = uidGen('T');
commit('NEW_TAB', { uid, tab: tabUid, content, type, autorun, schema, table });
}
}
2021-07-13 16:53:47 +02:00
else {
tabUid = uidGen('T');
commit('NEW_TAB', { uid, tab: tabUid, content, type, autorun, schema, table });
}
2020-12-11 15:55:18 +01:00
2021-07-13 16:53:47 +02:00
commit('SELECT_TAB', { uid, tab: tabUid });
2020-06-12 18:10:45 +02:00
},
renameTabs ({ commit }, payload) {
commit('RENAME_TABS', payload);
},
2020-08-19 18:20:57 +02:00
removeTab ({ commit }, payload) {
commit('REMOVE_TAB', payload);
},
2020-06-12 18:10:45 +02:00
selectTab ({ commit }, payload) {
commit('SELECT_TAB', payload);
},
2021-07-14 20:30:54 +02:00
updateTabs ({ commit }, payload) {
commit('UPDATE_TABS', payload);
},
setTabFields ({ commit }, payload) {
commit('SET_TAB_FIELDS', payload);
},
setTabKeyUsage ({ commit }, payload) {
commit('SET_TAB_KEY_USAGE', payload);
2020-12-04 11:19:16 +01:00
},
setUnsavedChanges ({ commit }, val) {
commit('SET_UNSAVED_CHANGES', val);
},
discardUnsavedChanges ({ state, commit, dispatch }) {
dispatch('setUnsavedChanges', false);
dispatch('changeBreadcrumbs', state.pending_breadcrumbs);
commit('SET_UNSAVED_DISCARD_MODAL', false);
commit('SET_PENDING_BREADCRUMBS', {});
},
closeUnsavedChangesModal ({ commit }) {
commit('SET_UNSAVED_DISCARD_MODAL', false);
}
}
};