refactor: ts on pinia store

This commit is contained in:
Fabio Di Stasio 2022-05-28 18:43:56 +02:00
parent e97401e27d
commit 2007305ff0
9 changed files with 264 additions and 183 deletions

View File

@ -337,6 +337,15 @@ export interface SchemaInfos {
schedulers: EventInfos[];
}
export interface CollationInfos {
charset: string;
collation: string;
compiled: boolean;
default: boolean;
id: string | number;
sortLen: number;
}
// Query
export interface QueryBuilderObject {
schema: string;

View File

@ -1,12 +1,13 @@
import { defineStore, acceptHMRUpdate } from 'pinia';
import Store from 'electron-store';
import { defineStore } from 'pinia';
import * as Store from 'electron-store';
import { Ace } from 'ace-builds';
const persistentStore = new Store({ name: 'settings' });
export const useApplicationStore = defineStore('application', {
state: () => ({
appName: 'Antares - SQL Client',
appVersion: process.env.PACKAGE_VERSION || 0,
cachedVersion: persistentStore.get('cached_version', 0),
appVersion: process.env.PACKAGE_VERSION || '0',
cachedVersion: persistentStore.get('cached_version', '0') as string,
isLoading: false,
isNewModal: false,
isSettingModal: false,
@ -15,7 +16,7 @@ export const useApplicationStore = defineStore('application', {
selectedConection: {},
updateStatus: 'noupdate', // 'noupdate' | 'available' | 'checking' | 'nocheck' | 'downloading' | 'downloaded' | 'disabled'
downloadProgress: 0,
baseCompleter: [] // Needed to reset ace editor, due global-only ace completer
baseCompleter: [] as Ace.Completer[] // Needed to reset ace editor, due global-only ace completer
}),
getters: {
getBaseCompleter: state => state.baseCompleter,
@ -30,10 +31,10 @@ export const useApplicationStore = defineStore('application', {
persistentStore.set('cached_version', this.cachedVersion);
}
},
setLoadingStatus (payload) {
setLoadingStatus (payload: boolean) {
this.isLoading = payload;
},
setBaseCompleters (payload) {
setBaseCompleters (payload: boolean) {
this.baseCompleter = payload;
},
// Modals
@ -43,7 +44,7 @@ export const useApplicationStore = defineStore('application', {
hideNewConnModal () {
this.isNewModal = false;
},
showSettingModal (tab) {
showSettingModal (tab: string) {
this.selectedSettingTab = tab;
this.isSettingModal = true;
},
@ -58,6 +59,3 @@ export const useApplicationStore = defineStore('application', {
}
}
});
if (import.meta.webpackHot)
import.meta.webpackHot.accept(acceptHMRUpdate(useApplicationStore, import.meta.webpackHot));

View File

@ -1,6 +1,7 @@
import { defineStore, acceptHMRUpdate } from 'pinia';
import Store from 'electron-store';
import crypto from 'crypto';
import { defineStore } from 'pinia';
import * as Store from 'electron-store';
import * as crypto from 'crypto';
import { ConnectionParams } from 'common/interfaces/antares';
const key = localStorage.getItem('key');
if (!key)
@ -16,10 +17,10 @@ const persistentStore = new Store({
export const useConnectionsStore = defineStore('connections', {
state: () => ({
connections: persistentStore.get('connections', [])
connections: persistentStore.get('connections', []) as ConnectionParams[]
}),
getters: {
getConnectionName: state => uid => {
getConnectionName: state => (uid: string) => {
const connection = state.connections.filter(connection => connection.uid === uid)[0];
let connectionName = '';
@ -42,16 +43,16 @@ export const useConnectionsStore = defineStore('connections', {
}
},
actions: {
addConnection (connection) {
addConnection (connection: ConnectionParams) {
this.connections.push(connection);
persistentStore.set('connections', this.connections);
},
deleteConnection (connection) {
this.connections = this.connections.filter(el => el.uid !== connection.uid);
deleteConnection (connection: ConnectionParams) {
this.connections = (this.connections as ConnectionParams[]).filter(el => el.uid !== connection.uid);
persistentStore.set('connections', this.connections);
},
editConnection (connection) {
const editedConnections = this.connections.map(conn => {
editConnection (connection: ConnectionParams) {
const editedConnections = (this.connections as ConnectionParams[]).map(conn => {
if (conn.uid === connection.uid) return connection;
return conn;
});
@ -59,12 +60,9 @@ export const useConnectionsStore = defineStore('connections', {
this.selected_conection = {};
persistentStore.set('connections', this.connections);
},
updateConnections (connections) {
updateConnections (connections: ConnectionParams) {
this.connections = connections;
persistentStore.set('connections', this.connections);
}
}
});
if (import.meta.webpackHot)
import.meta.webpackHot.accept(acceptHMRUpdate(useConnectionsStore, import.meta.webpackHot));

View File

@ -1,19 +1,26 @@
import { defineStore, acceptHMRUpdate } from 'pinia';
import Store from 'electron-store';
import { defineStore } from 'pinia';
import * as Store from 'electron-store';
import { uidGen } from 'common/libs/uidGen';
const persistentStore = new Store({ name: 'history' });
const historySize = 1000;
export interface HistoryRecord {
uid: string;
sql: string;
date: Date;
schema?: string;
}
export const useHistoryStore = defineStore('history', {
state: () => ({
history: persistentStore.get('history', {}),
history: persistentStore.get('history', {}) as {[key: string]: HistoryRecord[]},
favorites: persistentStore.get('favorites', {})
}),
getters: {
getHistoryByWorkspace: state => uid => state.history[uid]
getHistoryByWorkspace: state => (uid: string) => state.history[uid]
},
actions: {
saveHistory (args) {
saveHistory (args: { uid: string; query: string; schema: string; tabUid: string }) {
if (this.getHistoryByWorkspace(args.uid) &&
this.getHistoryByWorkspace(args.uid).length &&
this.getHistoryByWorkspace(args.uid)[0].sql === args.query
@ -37,12 +44,9 @@ export const useHistoryStore = defineStore('history', {
persistentStore.set('history', this.history);
},
deleteQueryFromHistory (query) {
this.history[query.workspace] = this.history[query.workspace].filter(q => q.uid !== query.uid);
deleteQueryFromHistory (query: Partial<HistoryRecord> & { workspace: string}) {
this.history[query.workspace] = (this.history[query.workspace] as HistoryRecord[]).filter(q => q.uid !== query.uid);
persistentStore.set('history', this.history);
}
}
});
if (import.meta.webpackHot)
import.meta.webpackHot.accept(acceptHMRUpdate(useHistoryStore, import.meta.webpackHot));

View File

@ -1,20 +0,0 @@
import { defineStore, acceptHMRUpdate } from 'pinia';
import { uidGen } from 'common/libs/uidGen';
export const useNotificationsStore = defineStore('notifications', {
state: () => ({
notifications: []
}),
actions: {
addNotification (payload) {
payload.uid = uidGen('N');
this.notifications.unshift(payload);
},
removeNotification (uid) {
this.notifications = this.notifications.filter(item => item.uid !== uid);
}
}
});
if (import.meta.webpackHot)
import.meta.webpackHot.accept(acceptHMRUpdate(useNotificationsStore, import.meta.webpackHot));

View File

@ -0,0 +1,23 @@
import { defineStore } from 'pinia';
import { uidGen } from 'common/libs/uidGen';
export interface Notification {
uid: string;
status: string;
message: string;
}
export const useNotificationsStore = defineStore('notifications', {
state: () => ({
notifications: [] as Notification[]
}),
actions: {
addNotification (payload: { status: string; message: string }) {
const notification: Notification = { uid: uidGen('N'), ...payload };
this.notifications.unshift(notification);
},
removeNotification (uid: string) {
this.notifications = (this.notifications as Notification[]).filter(item => item.uid !== uid);
}
}
});

View File

@ -1,10 +1,10 @@
import { defineStore } from 'pinia';
import Store from 'electron-store';
import * as Store from 'electron-store';
const persistentStore = new Store({ name: 'notes' });
export const useScratchpadStore = defineStore('scratchpad', {
state: () => ({
notes: persistentStore.get('notes', '# HOW TO SUPPORT ANTARES\n\n- [ ] Leave a star to Antares [GitHub repo](https://github.com/antares-sql/antares)\n- [ ] Send feedbacks and advices\n- [ ] Report for bugs\n- [ ] If you enjoy, share Antares with friends\n\n# ABOUT SCRATCHPAD\n\nThis is a scratchpad where you can save your **personal notes**. It supports `markdown` format, but you are free to use plain text.\nThis content is just a placeholder, feel free to clear it to make space for your notes.\n')
notes: persistentStore.get('notes', '# HOW TO SUPPORT ANTARES\n\n- [ ] Leave a star to Antares [GitHub repo](https://github.com/antares-sql/antares)\n- [ ] Send feedbacks and advices\n- [ ] Report for bugs\n- [ ] If you enjoy, share Antares with friends\n\n# ABOUT SCRATCHPAD\n\nThis is a scratchpad where you can save your **personal notes**. It supports `markdown` format, but you are free to use plain text.\nThis content is just a placeholder, feel free to clear it to make space for your notes.\n') as string
}),
actions: {
changeNotes (notes: string) {

View File

@ -1,6 +1,6 @@
import { defineStore } from 'pinia';
import i18n from '@/i18n';
import Store from 'electron-store';
import * as Store from 'electron-store';
const persistentStore = new Store({ name: 'settings' });
const isDarkTheme = window.matchMedia('(prefers-color-scheme: dark)');
const defaultAppTheme = isDarkTheme.matches ? 'dark' : 'light';

View File

@ -1,5 +1,5 @@
import { defineStore, acceptHMRUpdate } from 'pinia';
import Store from 'electron-store';
import { defineStore } from 'pinia';
import * as Store from 'electron-store';
import Connection from '@/ipc-api/Connection';
import Schema from '@/ipc-api/Schema';
import Users from '@/ipc-api/Users';
@ -10,14 +10,88 @@ import customizations from 'common/customizations';
import { useConnectionsStore } from '@/stores/connections';
import { useNotificationsStore } from '@/stores/notifications';
import { useSettingsStore } from '@/stores/settings';
import {
CollationInfos,
ConnectionParams,
EventInfos,
FunctionInfos,
RoutineInfos,
TableInfos,
TriggerInfos,
TypesGroup
} from 'common/interfaces/antares';
import { Customizations } from 'common/interfaces/customizations';
export interface WorkspaceTab {
uid: string;
tab?: string;
index: number;
selected: boolean;
type: string;
schema?: string;
elementName?: string;
elementNewName?: string;
elementType?: string;
isChanged?: boolean;
content?: string;
autorun: boolean;
}
export interface WorkspaceStructure {
name: string;
functions: FunctionInfos[];
procedures: RoutineInfos[];
schedulers: EventInfos[];
tables: TableInfos[];
triggers: TriggerInfos[];
size: number;
}
export interface Breadcrumb {
function?: string;
procedure?: string;
query?: string;
scheduler?: string;
schema?: string;
table?: string;
trigger?: string;
triggerFunction?: string;
view?: string;
}
export interface Workspace {
uid: string;
client?: string;
connectionStatus: string;
selectedTab: string | number;
searchTerm: string;
tabs: WorkspaceTab[];
structure: WorkspaceStructure[];
variables: { name: string; value: string }[];
collations: CollationInfos[];
users: { host: string; name: string; password: string }[];
breadcrumbs: Breadcrumb[];
loadingElements: { name: string; schema: string; type: string }[];
loadedSchemas: Set<string>;
dataTypes?: { [key: string]: TypesGroup[] };
indexTypes?: string[];
customizations?: Customizations;
version?: {
number: string;
name: string;
arch: string;
os: string;
};
engines?: {[key: string]: string | boolean | number}[];
}
const persistentStore = new Store({ name: 'tabs' });
const tabIndex = [];
const tabIndex: {[key: string]: number} = {};
export const useWorkspacesStore = defineStore('workspaces', {
state: () => ({
workspaces: [],
selectedWorkspace: null
workspaces: [] as Workspace[],
selectedWorkspace: null as string
}),
getters: {
getSelected: state => {
@ -25,14 +99,14 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (state.selectedWorkspace) return state.selectedWorkspace;
return state.workspaces[0].uid;
},
getWorkspace: state => (uid) => {
getWorkspace: state => (uid: string) => {
return state.workspaces.find(workspace => workspace.uid === uid);
},
getDatabaseVariable: state => (uid, name) => {
getDatabaseVariable: state => (uid: string, name: string) => {
return state.workspaces.find(workspace => workspace.uid === uid).variables.find(variable => variable.name === name);
},
getWorkspaceTab (state) {
return (tUid) => {
return (tUid: string) => {
if (!this.getSelected) return;
const workspace = state.workspaces.find(workspace => workspace.uid === this.getSelected);
if ('tabs' in workspace)
@ -45,22 +119,22 @@ export const useWorkspacesStore = defineStore('workspaces', {
.filter(workspace => workspace.connectionStatus === 'connected')
.map(workspace => workspace.uid);
},
getLoadedSchemas: state => uid => {
getLoadedSchemas: state => (uid: string) => {
return state.workspaces.find(workspace => workspace.uid === uid).loadedSchemas;
},
getSearchTerm: state => uid => {
getSearchTerm: state => (uid: string) => {
return state.workspaces.find(workspace => workspace.uid === uid).searchTerm;
}
},
actions: {
selectWorkspace (uid) {
selectWorkspace (uid: string) {
if (!uid)
this.selectedWorkspace = this.workspaces.length ? this.workspaces[0].uid : 'NEW';
else
this.selectedWorkspace = uid;
},
async connectWorkspace (connection) {
this.workspaces = this.workspaces.map(workspace => workspace.uid === connection.uid
async connectWorkspace (connection: ConnectionParams) {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === connection.uid
? {
...workspace,
structure: {},
@ -79,7 +153,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error') {
notificationsStore.addNotification({ status, message: response });
this.workspaces = this.workspaces.map(workspace => workspace.uid === connection.uid
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === connection.uid
? {
...workspace,
structure: {},
@ -90,25 +164,25 @@ export const useWorkspacesStore = defineStore('workspaces', {
: workspace);
}
else {
let dataTypes = [];
let indexTypes = [];
let clientCustomizations;
let dataTypes: TypesGroup[] = [];
let indexTypes: string[] = [];
let clientCustomizations: Customizations;
switch (connection.client) {
case 'mysql':
case 'maria':
dataTypes = require('common/data-types/mysql').default;
indexTypes = require('common/index-types/mysql').default;
dataTypes = require('common/data-types/mysql');
indexTypes = require('common/index-types/mysql');
clientCustomizations = customizations.mysql;
break;
case 'pg':
dataTypes = require('common/data-types/postgresql').default;
indexTypes = require('common/index-types/postgresql').default;
dataTypes = require('common/data-types/postgresql');
indexTypes = require('common/index-types/postgresql');
clientCustomizations = customizations.pg;
break;
case 'sqlite':
dataTypes = require('common/data-types/sqlite').default;
indexTypes = require('common/index-types/sqlite').default;
dataTypes = require('common/data-types/sqlite');
indexTypes = require('common/index-types/sqlite');
clientCustomizations = customizations.sqlite;
break;
}
@ -132,16 +206,16 @@ export const useWorkspacesStore = defineStore('workspaces', {
connectionsStore.editConnection(connProxy);
}
const cachedTabs = settingsStore.restoreTabs ? persistentStore.get(connection.uid, []) : [];
const cachedTabs: WorkspaceTab[] = settingsStore.restoreTabs ? persistentStore.get(connection.uid, []) as WorkspaceTab[] : [];
if (cachedTabs.length) {
tabIndex[connection.uid] = cachedTabs.reduce((acc, curr) => {
tabIndex[connection.uid] = cachedTabs.reduce((acc: number, curr) => {
if (curr.index > acc) acc = curr.index;
return acc;
}, null);
}
this.workspaces = this.workspaces.map(workspace => workspace.uid === connection.uid
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === connection.uid
? {
...workspace,
client: connection.client,
@ -166,7 +240,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack });
}
},
async refreshStructure (uid) {
async refreshStructure (uid: string) {
const notificationsStore = useNotificationsStore();
try {
@ -175,7 +249,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error')
notificationsStore.addNotification({ status, message: response });
else {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? {
...workspace,
structure: response
@ -187,7 +261,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack });
}
},
async refreshSchema ({ uid, schema }) {
async refreshSchema ({ uid, schema }: {uid: string; schema: string}) {
const notificationsStore = useNotificationsStore();
try {
@ -195,8 +269,8 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error')
notificationsStore.addNotification({ status, message: response });
else {
const schemaElements =response.find(_schema => _schema.name === schema);
this.workspaces = this.workspaces.map(workspace => {
const schemaElements = (response as WorkspaceStructure[]).find(_schema => _schema.name === schema);
this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) {
const schemaIndex = workspace.structure.findIndex(s => s.name === schema);
@ -213,7 +287,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack });
}
},
async refreshCollations (uid) {
async refreshCollations (uid: string) {
const notificationsStore = useNotificationsStore();
try {
@ -221,7 +295,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error')
notificationsStore.addNotification({ status, message: response });
else {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? {
...workspace,
collations: response
@ -233,7 +307,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack });
}
},
async refreshVariables (uid) {
async refreshVariables (uid: string) {
const notificationsStore = useNotificationsStore();
try {
@ -241,7 +315,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error')
notificationsStore.addNotification({ status, message: response });
else {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? {
...workspace,
variables: response
@ -253,7 +327,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack });
}
},
async refreshEngines (uid) {
async refreshEngines (uid: string) {
const notificationsStore = useNotificationsStore();
try {
@ -261,7 +335,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error')
notificationsStore.addNotification({ status, message: response });
else {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? {
...workspace,
engines: response
@ -273,7 +347,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack });
}
},
async refreshUsers (uid) {
async refreshUsers (uid: string) {
const notificationsStore = useNotificationsStore();
try {
@ -281,7 +355,7 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (status === 'error')
notificationsStore.addNotification({ status, message: response });
else {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? {
...workspace,
users: response
@ -293,9 +367,9 @@ export const useWorkspacesStore = defineStore('workspaces', {
notificationsStore.addNotification({ status: 'error', message: err.stack });
}
},
removeConnected (uid) {
removeConnected (uid: string) {
Connection.disconnect(uid);
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? {
...workspace,
structure: {},
@ -307,26 +381,26 @@ export const useWorkspacesStore = defineStore('workspaces', {
this.selectTab({ uid, tab: 0 });
},
addWorkspace (uid) {
const workspace = {
addWorkspace (uid: string) {
const workspace: Workspace = {
uid,
connectionStatus: 'disconnected',
selectedTab: 0,
searchTerm: '',
tabs: [],
structure: {},
structure: [],
variables: [],
collations: [],
users: [],
breadcrumbs: {},
breadcrumbs: [],
loadingElements: [],
loadedSchemas: new Set()
};
this.workspaces.push(workspace);
},
changeBreadcrumbs (payload) {
const breadcrumbsObj = {
changeBreadcrumbs (payload: Breadcrumb) {
const breadcrumbsObj: Breadcrumb = {
schema: null,
table: null,
trigger: null,
@ -338,29 +412,29 @@ export const useWorkspacesStore = defineStore('workspaces', {
query: null
};
this.workspaces = this.workspaces.map(workspace => workspace.uid === this.getSelected
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === this.getSelected
? {
...workspace,
breadcrumbs: { ...breadcrumbsObj, ...payload }
}
: workspace);
},
addLoadedSchema (schema) {
this.workspaces = this.workspaces.map(workspace => {
addLoadedSchema (schema: string) {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === this.getSelected)
workspace.loadedSchemas.add(schema);
return workspace;
});
},
addLoadingElement (element) {
this.workspaces = this.workspaces.map(workspace => {
addLoadingElement (element: { name: string; schema: string; type: string }) {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === this.getSelected)
workspace.loadingElements.push(element);
return workspace;
});
},
removeLoadingElement (element) {
this.workspaces = this.workspaces.map(workspace => {
removeLoadingElement (element: { name: string; schema: string; type: string }) {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === this.getSelected) {
const loadingElements = workspace.loadingElements.filter(el =>
el.schema !== element.schema &&
@ -373,19 +447,19 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace;
});
},
setSearchTerm (term) {
this.workspaces = this.workspaces.map(workspace => workspace.uid === this.getSelected
setSearchTerm (term: string) {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === this.getSelected
? {
...workspace,
searchTerm: term
}
: workspace);
},
_addTab ({ uid, tab, content, type, autorun, schema, elementName, elementType }) {
_addTab ({ uid, tab, content, type, autorun, schema, elementName, elementType }: WorkspaceTab) {
if (type === 'query')
tabIndex[uid] = tabIndex[uid] ? ++tabIndex[uid] : 1;
const newTab = {
const newTab: WorkspaceTab = {
uid: tab,
index: type === 'query' ? tabIndex[uid] : null,
selected: false,
@ -393,13 +467,11 @@ export const useWorkspacesStore = defineStore('workspaces', {
schema,
elementName,
elementType,
fields: [],
keyUsage: [],
content: content || '',
autorun: !!autorun
};
this.workspaces = this.workspaces.map(workspace => {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
@ -410,10 +482,10 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace;
});
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs);
persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
},
_replaceTab ({ uid, tab: tUid, type, schema, content, elementName, elementType }) {
this.workspaces = this.workspaces.map(workspace => {
_replaceTab ({ uid, tab: tUid, type, schema, content, elementName, elementType }: WorkspaceTab) {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
@ -429,11 +501,11 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace;
});
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs);
persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
},
newTab ({ uid, content, type, autorun, schema, elementName, elementType }) {
newTab ({ uid, content, type, autorun, schema, elementName, elementType }: WorkspaceTab) {
let tabUid;
const workspaceTabs = this.workspaces.find(workspace => workspace.uid === uid);
const workspaceTabs = (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid);
switch (type) {
case 'new-table':
@ -535,8 +607,8 @@ export const useWorkspacesStore = defineStore('workspaces', {
this.selectTab({ uid, tab: tabUid });
},
checkSelectedTabExists (uid) {
const workspace = this.workspaces.find(workspace => workspace.uid === uid);
checkSelectedTabExists (uid: string) {
const workspace = (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid);
const isSelectedExistent = workspace
? workspace.tabs.some(tab => tab.uid === workspace.selectedTab)
: false;
@ -544,11 +616,11 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (!isSelectedExistent && workspace.tabs.length)
this.selectTab({ uid, tab: workspace.tabs[workspace.tabs.length - 1].uid });
},
updateTabContent ({ uid, tab, type, schema, content }) {
updateTabContent ({ uid, tab, type, schema, content }: WorkspaceTab) {
this._replaceTab({ uid, tab, type, schema, content });
},
renameTabs ({ uid, schema, elementName, elementNewName }) {
this.workspaces = this.workspaces.map(workspace => {
renameTabs ({ uid, schema, elementName, elementNewName }: WorkspaceTab) {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
@ -568,10 +640,10 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace;
});
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs);
persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
},
removeTab ({ uid, tab: tUid }) {
this.workspaces = this.workspaces.map(workspace => {
removeTab ({ uid, tab: tUid }: {uid: string; tab: string}) {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
@ -582,13 +654,13 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace;
});
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs);
persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
this.checkSelectedTabExists(uid);
},
removeTabs ({ uid, schema, elementName, elementType }) { // Multiple tabs based on schema and element name
removeTabs ({ uid, schema, elementName, elementType }: WorkspaceTab) { // Multiple tabs based on schema and element name
if (elementType === 'procedure') elementType = 'routine'; // TODO: pass directly "routine"
this.workspaces = this.workspaces.map(workspace => {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
@ -603,62 +675,62 @@ export const useWorkspacesStore = defineStore('workspaces', {
return workspace;
});
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs);
persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
this.checkSelectedTabExists(uid);
},
selectTab ({ uid, tab }) {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid
selectTab ({ uid, tab }: {uid: string; tab: string}) {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? { ...workspace, selectedTab: tab }
: workspace
);
},
updateTabs ({ uid, tabs }) {
this.workspaces = this.workspaces.map(workspace => workspace.uid === uid
updateTabs ({ uid, tabs }: {uid: string; tabs: string[]}) {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === uid
? { ...workspace, tabs }
: workspace
);
persistentStore.set(uid, this.workspaces.find(workspace => workspace.uid === uid).tabs);
persistentStore.set(uid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === uid).tabs);
},
setTabFields ({ cUid, tUid, fields }) {
this.workspaces = this.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;
});
// setTabFields ({ cUid, tUid, fields }: { cUid: string; tUid: string; fields: any }) {
// this.workspaces = (this.workspaces as Workspace[]).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;
// });
persistentStore.set(cUid, this.workspaces.find(workspace => workspace.uid === cUid).tabs);
},
setTabKeyUsage ({ cUid, tUid, keyUsage }) {
this.workspaces = this.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;
});
// persistentStore.set(cUid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === cUid).tabs);
// },
// setTabKeyUsage ({ cUid, tUid, keyUsage }: { cUid: string; tUid: string; keyUsage: any }) {
// this.workspaces = (this.workspaces as Workspace[]).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;
// });
persistentStore.set(cUid, this.workspaces.find(workspace => workspace.uid === cUid).tabs);
},
setUnsavedChanges ({ uid, tUid, isChanged }) {
this.workspaces = this.workspaces.map(workspace => {
// persistentStore.set(cUid, (this.workspaces as Workspace[]).find(workspace => workspace.uid === cUid).tabs);
// },
setUnsavedChanges ({ uid, tUid, isChanged }: { uid: string; tUid: string; isChanged: boolean }) {
this.workspaces = (this.workspaces as Workspace[]).map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
@ -676,6 +748,3 @@ export const useWorkspacesStore = defineStore('workspaces', {
}
}
});
if (import.meta.webpackHot)
import.meta.webpackHot.accept(acceptHMRUpdate(useWorkspacesStore, import.meta.webpackHot));