antares/src/renderer/stores/scratchpad.ts

65 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-05-28 18:43:56 +02:00
import * as Store from 'electron-store';
import { defineStore } from 'pinia';
2023-12-13 18:29:45 +01:00
export type TagCode = 'all' | 'note' | 'todo' | 'query'
export interface ConnectionNote {
uid: string;
2023-12-13 18:29:45 +01:00
cUid: string | null;
2023-12-06 08:44:07 +01:00
title?: string;
2023-12-21 10:16:46 +01:00
isArchived: boolean;
2023-12-13 18:29:45 +01:00
type: TagCode;
note: string;
date: Date;
}
const persistentStore = new Store({ name: 'notes' });
// Migrate old scratchpad on new notes TODO: remove in future releases
const oldNotes = persistentStore.get('notes') as string;
if (oldNotes) {
const newNotes = persistentStore.get('connectionNotes', []) as ConnectionNote[];
newNotes.unshift({
uid: 'N:LEGACY',
cUid: null,
isArchived: false,
type: 'note',
note: oldNotes,
date: new Date()
});
persistentStore.delete('notes');
persistentStore.set('connectionNotes', newNotes);
}
2022-04-30 00:47:37 +02:00
export const useScratchpadStore = defineStore('scratchpad', {
state: () => ({
selectedTag: 'all',
/** Connection specific notes */
2023-12-13 18:29:45 +01:00
connectionNotes: persistentStore.get('connectionNotes', []) as ConnectionNote[]
2022-04-30 00:47:37 +02:00
}),
actions: {
2023-12-06 08:44:07 +01:00
changeNotes (notes: ConnectionNote[]) {
this.connectionNotes = notes;
persistentStore.set('connectionNotes', this.connectionNotes);
2023-12-21 10:16:46 +01:00
},
addNote (note: ConnectionNote) {
this.connectionNotes = [
note,
...this.connectionNotes
];
persistentStore.set('connectionNotes', this.connectionNotes);
2023-12-21 18:10:51 +01:00
},
editNote (note: ConnectionNote) {
this.connectionNotes = (this.connectionNotes as ConnectionNote[]).map(n => {
if (n.uid === note.uid)
n = note;
return n;
});
persistentStore.set('connectionNotes', this.connectionNotes);
}
}
2022-04-30 00:47:37 +02:00
});