antares/src/renderer/stores/history.ts

53 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-05-28 18:43:56 +02:00
import { defineStore } from 'pinia';
import * as Store from 'electron-store';
2022-04-30 00:47:37 +02:00
import { uidGen } from 'common/libs/uidGen';
const persistentStore = new Store({ name: 'history' });
const historySize = 1000;
2022-05-28 18:43:56 +02:00
export interface HistoryRecord {
uid: string;
sql: string;
date: Date;
schema?: string;
}
2022-04-30 00:47:37 +02:00
export const useHistoryStore = defineStore('history', {
state: () => ({
2022-05-28 18:43:56 +02:00
history: persistentStore.get('history', {}) as {[key: string]: HistoryRecord[]},
2022-04-30 00:47:37 +02:00
favorites: persistentStore.get('favorites', {})
}),
getters: {
2022-05-28 18:43:56 +02:00
getHistoryByWorkspace: state => (uid: string) => state.history[uid]
2022-04-30 00:47:37 +02:00
},
actions: {
2022-05-28 18:43:56 +02:00
saveHistory (args: { uid: string; query: string; schema: string; tabUid: string }) {
2022-04-30 00:47:37 +02:00
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);
},
2022-05-28 18:43:56 +02:00
deleteQueryFromHistory (query: Partial<HistoryRecord> & { workspace: string}) {
this.history[query.workspace] = (this.history[query.workspace] as HistoryRecord[]).filter(q => q.uid !== query.uid);
2022-04-30 00:47:37 +02:00
persistentStore.set('history', this.history);
}
}
});