Skip to content

Commit

Permalink
feat: option to restore session on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Jul 22, 2021
1 parent 1e543aa commit adc5477
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 26 deletions.
18 changes: 18 additions & 0 deletions src/renderer/components/ModalSettings.vue
Expand Up @@ -104,6 +104,19 @@
</select>
</div>
</div>
<div class="form-group">
<div class="col-6 col-sm-12">
<label class="form-label">
{{ $t('message.restorePreviourSession') }}
</label>
</div>
<div class="col-6 col-sm-12">
<label class="form-switch d-inline-block" @click.prevent="toggleRestoreSession">
<input type="checkbox" :checked="restoreTabs">
<i class="form-icon" />
</label>
</div>
</div>
<div class="form-group">
<div class="col-6 col-sm-12">
<label class="form-label">
Expand Down Expand Up @@ -369,6 +382,7 @@ export default {
selectedAutoComplete: 'settings/getAutoComplete',
selectedLineWrap: 'settings/getLineWrap',
notificationsTimeout: 'settings/getNotificationsTimeout',
restoreTabs: 'settings/getRestoreTabs',
applicationTheme: 'settings/getApplicationTheme',
editorTheme: 'settings/getEditorTheme',
editorFontSize: 'settings/getEditorFontSize',
Expand Down Expand Up @@ -423,6 +437,7 @@ ORDER BY
closeModal: 'application/hideSettingModal',
changeLocale: 'settings/changeLocale',
changePageSize: 'settings/changePageSize',
changeRestoreTabs: 'settings/changeRestoreTabs',
changeAutoComplete: 'settings/changeAutoComplete',
changeLineWrap: 'settings/changeLineWrap',
changeApplicationTheme: 'settings/changeApplicationTheme',
Expand All @@ -447,6 +462,9 @@ ORDER BY
if (e.key === 'Escape')
this.closeModal();
},
toggleRestoreSession () {
this.changeRestoreTabs(!this.restoreTabs);
},
toggleAutoComplete () {
this.changeAutoComplete(!this.selectedAutoComplete);
},
Expand Down
12 changes: 6 additions & 6 deletions src/renderer/components/WorkspaceExploreBar.vue
Expand Up @@ -9,19 +9,19 @@
<div class="workspace-explorebar-header">
<span class="workspace-explorebar-title">{{ connectionName }}</span>
<span v-if="workspace.connection_status === 'connected'" class="workspace-explorebar-tools">
<i
class="mdi mdi-18px mdi-database-plus c-hand mr-2"
:title="$t('message.createNewSchema')"
@click="showNewDBModal"
/>
<i
class="mdi mdi-18px mdi-refresh c-hand mr-2"
:class="{'rotate':isRefreshing}"
:title="$t('word.refresh')"
@click="refresh"
/>
<i
class="mdi mdi-18px mdi-database-plus c-hand mr-2"
:title="$t('message.createNewSchema')"
@click="showNewDBModal"
/>
<i
class="mdi mdi-18px mdi-power-plug-off c-hand"
class="mdi mdi-18px mdi-power c-hand"
:title="$t('word.disconnect')"
@click="disconnectWorkspace(connection.uid)"
/>
Expand Down
10 changes: 5 additions & 5 deletions src/renderer/components/WorkspacePropsTabView.vue
Expand Up @@ -166,7 +166,7 @@
<BaseLoader v-if="isLoading" />
<label class="form-label ml-2">{{ $t('message.selectStatement') }}</label>
<QueryEditor
v-if="isSelected"
v-show="isSelected"
ref="queryEditor"
:value.sync="localView.sql"
:workspace="workspace"
Expand Down Expand Up @@ -225,16 +225,16 @@ export default {
}
},
watch: {
schema () {
async schema () {
if (this.isSelected) {
this.getViewData();
await this.getViewData();
this.$refs.queryEditor.editor.session.setValue(this.localView.sql);
this.lastView = this.view;
}
},
view () {
async view () {
if (this.isSelected) {
this.getViewData();
await this.getViewData();
this.$refs.queryEditor.editor.session.setValue(this.localView.sql);
this.lastView = this.view;
}
Expand Down
11 changes: 7 additions & 4 deletions src/renderer/components/WorkspaceQueryTab.vue
Expand Up @@ -162,7 +162,7 @@ export default {
return this.getWorkspace(this.connection.uid);
},
breadcrumbsSchema () {
return this.workspace.breadcrumbs.schema;
return this.workspace.breadcrumbs.schema || null;
},
databaseSchemas () {
return this.workspace.structure.reduce((acc, curr) => {
Expand All @@ -185,8 +185,8 @@ export default {
},
created () {
this.query = this.tab.content;
this.selectedSchema = this.breadcrumbsSchema;
this.changeBreadcrumbs({ schema: this.selectedSchema, query: `Query #${this.tab.index}` });
this.selectedSchema = this.tab.schema || this.breadcrumbsSchema;
// this.changeBreadcrumbs({ schema: this.selectedSchema, query: `Query #${this.tab.index}` });
window.addEventListener('keydown', this.onKey);
},
Expand All @@ -209,7 +209,8 @@ export default {
methods: {
...mapActions({
addNotification: 'notifications/addNotification',
changeBreadcrumbs: 'workspaces/changeBreadcrumbs'
changeBreadcrumbs: 'workspaces/changeBreadcrumbs',
updateTabContent: 'workspaces/updateTabContent'
}),
async runQuery (query) {
if (!query || this.isQuering) return;
Expand All @@ -231,6 +232,8 @@ export default {
this.resultsCount += this.results.reduce((acc, curr) => acc + (curr.rows ? curr.rows.length : 0), 0);
this.durationsCount += this.results.reduce((acc, curr) => acc + curr.duration, 0);
this.affectedCount += this.results.reduce((acc, curr) => acc + (curr.report ? curr.report.affectedRows : 0), 0);
this.updateTabContent({ uid: this.connection.uid, tab: this.tab.uid, type: 'query', schema: this.selectedSchema, content: query });
}
else
this.addNotification({ status: 'error', message: response });
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/i18n/en-US.js
Expand Up @@ -226,7 +226,8 @@ module.exports = {
pageNumber: 'Page number',
duplicateTable: 'Duplicate table',
noOpenTabs: 'There are no open tabs, navigate on the left bar or:',
noSchema: 'No schema'
noSchema: 'No schema',
restorePreviourSession: 'Restore previous session'
},
faker: {
address: 'Address',
Expand Down
13 changes: 11 additions & 2 deletions src/renderer/store/modules/settings.store.js
Expand Up @@ -16,7 +16,8 @@ export default {
line_wrap: persistentStore.get('line_wrap', true),
application_theme: persistentStore.get('application_theme', 'dark'),
editor_theme: persistentStore.get('editor_theme', 'twilight'),
editor_font_size: persistentStore.get('editor_font_size', 'medium')
editor_font_size: persistentStore.get('editor_font_size', 'medium'),
restore_tabs: persistentStore.get('restore_tabs', true)
},
getters: {
getLocale: state => state.locale,
Expand All @@ -28,7 +29,8 @@ export default {
getLineWrap: state => state.line_wrap,
getApplicationTheme: state => state.application_theme,
getEditorTheme: state => state.editor_theme,
getEditorFontSize: state => state.editor_font_size
getEditorFontSize: state => state.editor_font_size,
getRestoreTabs: state => state.restore_tabs
},
mutations: {
SET_LOCALE (state, locale) {
Expand Down Expand Up @@ -71,6 +73,10 @@ export default {
SET_EDITOR_FONT_SIZE (state, size) {
state.editor_font_size = size;
persistentStore.set('editor_font_size', state.editor_font_size);
},
SET_RESTORE_TABS (state, val) {
state.restore_tabs = val;
persistentStore.set('restore_tabs', state.restore_tabs);
}
},
actions: {
Expand Down Expand Up @@ -103,6 +109,9 @@ export default {
},
changeEditorFontSize ({ commit }, size) {
commit('SET_EDITOR_FONT_SIZE', size);
},
changeRestoreTabs ({ commit }, size) {
commit('SET_RESTORE_TABS', size);
}
}
};
47 changes: 39 additions & 8 deletions src/renderer/store/modules/workspaces.store.js
@@ -1,8 +1,10 @@
'use strict';
import Store from 'electron-store';
import Connection from '@/ipc-api/Connection';
import Schema from '@/ipc-api/Schema';
import Users from '@/ipc-api/Users';
import { uidGen } from 'common/libs/uidGen';
const persistentStore = new Store({ name: 'tabs' });
const tabIndex = [];

export default {
Expand Down Expand Up @@ -53,6 +55,15 @@ export default {
SET_CONNECTED (state, payload) {
const { uid, client, dataTypes, indexTypes, customizations, structure, version } = payload;

const cachedTabs = payload.restoreTabs ? persistentStore.get(uid, []) : [];

if (cachedTabs.length) {
tabIndex[uid] = cachedTabs.reduce((acc, curr) => {
if (curr.index > acc) acc = curr.index;
return acc;
}, null);
}

state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
Expand All @@ -62,6 +73,8 @@ export default {
customizations,
structure,
connection_status: 'connected',
tabs: cachedTabs,
selected_tab: cachedTabs.length ? cachedTabs[0].uid : null,
version
}
: workspace);
Expand Down Expand Up @@ -188,6 +201,7 @@ export default {
content: content || '',
autorun: !!autorun
};

state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === uid) {
return {
Expand All @@ -198,6 +212,8 @@ export default {
else
return workspace;
});

persistentStore.set(uid, state.workspaces.find(workspace => workspace.uid === uid).tabs);
},
REMOVE_TAB (state, { uid, tab: tUid }) {
state.workspaces = state.workspaces.map(workspace => {
Expand All @@ -210,6 +226,8 @@ export default {
else
return workspace;
});

persistentStore.set(uid, state.workspaces.find(workspace => workspace.uid === uid).tabs);
},
REMOVE_TABS (state, { uid, schema, elementName, elementType }) { // Multiple tabs based on schema and element name
if (elementType === 'procedure') elementType = 'routine'; // TODO: pass directly "routine"
Expand All @@ -228,15 +246,17 @@ export default {
else
return workspace;
});

persistentStore.set(uid, state.workspaces.find(workspace => workspace.uid === uid).tabs);
},
REPLACE_TAB (state, { uid, tab: tUid, type, schema, elementName, elementType }) {
REPLACE_TAB (state, { uid, tab: tUid, type, schema, content, elementName, elementType }) {
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
tabs: workspace.tabs.map(tab => {
if (tab.uid === tUid)
return { ...tab, type, schema, elementName, elementType };
return { ...tab, type, schema, content, elementName, elementType };

return tab;
})
Expand All @@ -245,6 +265,8 @@ export default {
else
return workspace;
});

persistentStore.set(uid, state.workspaces.find(workspace => workspace.uid === uid).tabs);
},
RENAME_TABS (state, { uid, schema, elementName, elementType, elementNewName }) {
state.workspaces = state.workspaces.map(workspace => {
Expand All @@ -266,12 +288,15 @@ export default {
else
return workspace;
});

persistentStore.set(uid, state.workspaces.find(workspace => workspace.uid === uid).tabs);
},
SELECT_TAB (state, { uid, tab }) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid ? { ...workspace, selected_tab: tab } : workspace);
},
UPDATE_TABS (state, { uid, tabs }) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid ? { ...workspace, tabs } : workspace);
persistentStore.set(uid, state.workspaces.find(workspace => workspace.uid === uid).tabs);
},
SET_TAB_FIELDS (state, { cUid, tUid, fields }) {
state.workspaces = state.workspaces.map(workspace => {
Expand All @@ -289,6 +314,8 @@ export default {
else
return workspace;
});

persistentStore.set(uid, state.workspaces.find(workspace => workspace.uid === uid).tabs);
},
SET_TAB_KEY_USAGE (state, { cUid, tUid, keyUsage }) {
state.workspaces = state.workspaces.map(workspace => {
Expand All @@ -306,6 +333,8 @@ export default {
else
return workspace;
});

persistentStore.set(uid, state.workspaces.find(workspace => workspace.uid === uid).tabs);
},
SET_UNSAVED_CHANGES (state, { uid, tUid, isChanged }) {
state.workspaces = state.workspaces.map(workspace => {
Expand All @@ -324,9 +353,6 @@ export default {
return workspace;
});
},
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)
Expand All @@ -339,7 +365,7 @@ export default {
selectWorkspace ({ commit }, uid) {
commit('SELECT_WORKSPACE', uid);
},
async connectWorkspace ({ dispatch, commit }, connection) {
async connectWorkspace ({ dispatch, commit, getters, rootGetters }, connection) {
commit('SET_CONNECTING', connection.uid);

try {
Expand Down Expand Up @@ -372,6 +398,7 @@ export default {
if (status === 'error')
dispatch('notifications/addNotification', { status, message: version }, { root: true });

// Check if Maria or MySQL
const isMySQL = version.name.includes('MySQL');

if (isMySQL && connection.client !== 'mysql') {
Expand All @@ -392,7 +419,8 @@ export default {
indexTypes,
customizations,
structure: response,
version
version,
restoreTabs: rootGetters['settings/getRestoreTabs']
});
dispatch('refreshCollations', connection.uid);
dispatch('refreshVariables', connection.uid);
Expand Down Expand Up @@ -482,7 +510,7 @@ export default {
commit('SET_DISCONNECTED', uid);
commit('SELECT_TAB', { uid, tab: 0 });
},
addWorkspace ({ commit, dispatch, getters }, uid) {
addWorkspace ({ commit }, uid) {
const workspace = {
uid,
connection_status: 'disconnected',
Expand Down Expand Up @@ -675,6 +703,9 @@ export default {
if (!isSelectedExistent && workspace.tabs.length)
commit('SELECT_TAB', { uid, tab: workspace.tabs[workspace.tabs.length - 1].uid });
},
updateTabContent ({ commit }, { uid, tab, type, schema, content }) {
commit('REPLACE_TAB', { uid, tab, type, schema, content });
},
renameTabs ({ commit }, payload) {
commit('RENAME_TABS', payload);
},
Expand Down

0 comments on commit adc5477

Please sign in to comment.