antares/src/renderer/stores/history.js

49 lines
1.6 KiB
JavaScript

import { defineStore, acceptHMRUpdate } from 'pinia';
import Store from 'electron-store';
import { uidGen } from 'common/libs/uidGen';
const persistentStore = new Store({ name: 'history' });
const historySize = 1000;
export const useHistoryStore = defineStore('history', {
state: () => ({
history: persistentStore.get('history', {}),
favorites: persistentStore.get('favorites', {})
}),
getters: {
getHistoryByWorkspace: state => uid => state.history[uid]
},
actions: {
saveHistory (args) {
if (this.getHistoryByWorkspace(args.uid) &&
this.getHistoryByWorkspace(args.uid).length &&
this.getHistoryByWorkspace(args.uid)[0].sql === args.query
) return;
if (!(args.uid in this.history))
this.history[args.uid] = [];
this.history[args.uid] = [
{
uid: uidGen('H'),
sql: args.query,
date: new Date(),
schema: args.schema
},
...this.history[args.uid]
];
if (this.history[args.uid].length > historySize)
this.history[args.uid] = this.history[args.uid].slice(0, historySize);
persistentStore.set('history', this.history);
},
deleteQueryFromHistory (query) {
this.history[query.workspace] = this.history[query.workspace].filter(q => q.uid !== query.uid);
persistentStore.set('history', this.history);
}
}
});
if (import.meta.webpackHot)
import.meta.webpackHot.accept(acceptHMRUpdate(useHistoryStore, import.meta.webpackHot));