diff --git a/package-lock.json b/package-lock.json index fb2d869..9724b90 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "oh-vue-icons": "^1.0.0-rc3", "pinia": "^2.1.7", "pinia-plugin-persistedstate": "^3.2.1", + "tauri-plugin-store-api": "github:tauri-apps/tauri-plugin-store#v1", "ulidx": "^2.3.0", "vue": "^3.3.4", "vue-i18n": "^9.9.0", @@ -6372,6 +6373,14 @@ "url": "https://opencollective.com/unts" } }, + "node_modules/tauri-plugin-store-api": { + "version": "0.0.0", + "resolved": "git+ssh://git@github.com/tauri-apps/tauri-plugin-store.git#6f3daa12c4f69ca1586427307f518c9ad660d168", + "license": "MIT or APACHE-2.0", + "dependencies": { + "@tauri-apps/api": "1.5.3" + } + }, "node_modules/test-exclude": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-6.0.0.tgz", diff --git a/package.json b/package.json index 8b145da..a1e3d44 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "oh-vue-icons": "^1.0.0-rc3", "pinia": "^2.1.7", "pinia-plugin-persistedstate": "^3.2.1", + "tauri-plugin-store-api": "github:tauri-apps/tauri-plugin-store#v1", "ulidx": "^2.3.0", "vue": "^3.3.4", "vue-i18n": "^9.9.0", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index d246e1c..e4cf369 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -70,6 +70,7 @@ dependencies = [ "serde_json", "tauri", "tauri-build", + "tauri-plugin-store", ] [[package]] @@ -2714,6 +2715,18 @@ dependencies = [ "tauri-utils", ] +[[package]] +name = "tauri-plugin-store" +version = "0.0.0" +source = "git+https://github.com/tauri-apps/plugins-workspace?branch=v1#ea294c776a6a3a532bb7161905edb5fa95b8840c" +dependencies = [ + "log", + "serde", + "serde_json", + "tauri", + "thiserror", +] + [[package]] name = "tauri-runtime" version = "0.14.2" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index ebd9f38..f706649 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -15,6 +15,7 @@ tauri-build = { version = "1.5", features = [] } [dependencies] tauri = { version = "1.5", features = ["shell-open"] } serde = { version = "1.0", features = ["derive"] } +tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } serde_json = "1.0" [features] diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index f38f2aa..9864406 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -10,6 +10,7 @@ use tauri::Manager; fn main() { tauri::Builder::default() + .plugin(tauri_plugin_store::Builder::default().build()) .setup(|app| { #[cfg(debug_assertions)] // only include this code on debug builds { diff --git a/src/common/envs.ts b/src/common/envs.ts new file mode 100644 index 0000000..c6a647c --- /dev/null +++ b/src/common/envs.ts @@ -0,0 +1 @@ +export const isTauri = () => !!window.__TAURI__; diff --git a/src/common/index.ts b/src/common/index.ts index a2f530d..1651760 100644 --- a/src/common/index.ts +++ b/src/common/index.ts @@ -2,3 +2,4 @@ export * from './customError'; export * from './debounceThrottle'; export * from './pureObject'; export * from './editor'; +export * from './envs.ts'; diff --git a/src/store/clients/secretClient.ts b/src/store/clients/secretClient.ts new file mode 100644 index 0000000..88f0481 --- /dev/null +++ b/src/store/clients/secretClient.ts @@ -0,0 +1,27 @@ +import { Store } from 'tauri-plugin-store-api'; +import { Secret } from '../secretStore.ts'; +import { isTauri } from '../../common'; + +const store = new Store('.secrets.st'); +const saveSecret = async (secret: Secret) => { + if (isTauri()) { + await store.set(`${secret.id}`, secret); + + await store.save(); + } +}; +const removeSecret = async (secret: Secret) => { + if (isTauri()) { + await store.delete(`${secret.id}`); + + await store.save(); + } +}; + +const getSecrets = async () => { + if (isTauri()) { + return store.values(); + } +}; + +export const secretClient = { saveSecret, removeSecret, getSecrets }; diff --git a/src/store/secretStore.ts b/src/store/secretStore.ts index c60d564..9b0324a 100644 --- a/src/store/secretStore.ts +++ b/src/store/secretStore.ts @@ -1,5 +1,6 @@ import { defineStore } from 'pinia'; import { ulid } from 'ulidx'; +import { secretClient } from './clients/secretClient.ts'; export enum SecretType { SSH_KEY = 'SSH_KEY', PASSWORD = 'PASSWORD', @@ -23,7 +24,11 @@ export const useSecretStore = defineStore('secretStore', { }, getters: {}, actions: { - saveSecret(secret: Secret) { + async loadSecrets() { + const secrets = (await secretClient.getSecrets()) || []; + this.secrets = secrets as Secret[]; + }, + async saveSecret(secret: Secret) { if (!secret.id) { secret.id = ulid(); this.secrets.push(secret); @@ -31,10 +36,12 @@ export const useSecretStore = defineStore('secretStore', { const index = this.secrets.findIndex(s => s.id === secret.id); this.secrets[index] = secret; } + await secretClient.saveSecret(secret); }, - removeSecret(secret: Secret) { + async removeSecret(secret: Secret) { const index = this.secrets.findIndex(s => s.id === secret.id); this.secrets.splice(index, 1); + await secretClient.removeSecret(secret); }, }, }); diff --git a/src/views/secret/components/key-list.vue b/src/views/secret/components/key-list.vue index 9933f79..f01395f 100644 --- a/src/views/secret/components/key-list.vue +++ b/src/views/secret/components/key-list.vue @@ -33,7 +33,7 @@ import { useLang } from '../../../lang'; const lang = useLang(); const secretStore = useSecretStore(); -const { secrets } = toRefs(secretStore); +const [{ loadSecrets }, { secrets }] = [secretStore, toRefs(secretStore)]; const editKeyDialogRef = ref(); const editSecret = (secret: Secret) => { @@ -50,6 +50,7 @@ const handleSelect = (key: number, secret: Secret) => { const deleteSecret = (secret: Secret) => { secretStore.removeSecret(secret); }; +loadSecrets();