Skip to content

Commit

Permalink
feat: persists secrets #2
Browse files Browse the repository at this point in the history
Signed-off-by: seven <zilisheng1996@gmail.com>
  • Loading branch information
Blankll committed Mar 5, 2024
1 parent 925d423 commit a20ccce
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 4 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions src/common/envs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isTauri = () => !!window.__TAURI__;
1 change: 1 addition & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './customError';
export * from './debounceThrottle';
export * from './pureObject';
export * from './editor';
export * from './envs.ts';
27 changes: 27 additions & 0 deletions src/store/clients/secretClient.ts
Original file line number Diff line number Diff line change
@@ -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 };
11 changes: 9 additions & 2 deletions src/store/secretStore.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -23,18 +24,24 @@ 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);
} else {
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);
},
},
});
3 changes: 2 additions & 1 deletion src/views/secret/components/key-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -50,6 +50,7 @@ const handleSelect = (key: number, secret: Secret) => {
const deleteSecret = (secret: Secret) => {
secretStore.removeSecret(secret);
};
loadSecrets();
</script>

<style lang="scss" scoped>
Expand Down
2 changes: 1 addition & 1 deletion src/views/secret/components/new-key-dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const validationPassed = watch(formData.value, async () => {
const submitSaveSecret = async (event: MouseEvent) => {
event.preventDefault();
saveLoading.value = !saveLoading.value;
saveSecret({ ...formData.value } as Secret);
await saveSecret({ ...formData.value } as Secret);
closeModal();
saveLoading.value = !saveLoading.value;
};
Expand Down

0 comments on commit a20ccce

Please sign in to comment.