Skip to content

Commit

Permalink
feat: move edit logic to store
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Schmidt <tim@launchbadge.com>
  • Loading branch information
Sheng-Long committed Feb 12, 2025
1 parent 349bf16 commit 629b960
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 101 deletions.
11 changes: 3 additions & 8 deletions front-end/src/renderer/components/ComplexKey/ComplexKey.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { onBeforeMount, ref } from 'vue';
import { Key, KeyList } from '@hashgraph/sdk';
import ComplexKeyThreshold from '@renderer/components/ComplexKey/ComplexKeyThreshold.vue';
Expand All @@ -10,7 +10,7 @@ const props = defineProps<{
}>();
/* Emits */
const emit = defineEmits(['update:modelKey', 'update:nickname']);
const emit = defineEmits(['update:modelKey']);
/* State */
const keyList = ref<KeyList>(new KeyList());
Expand All @@ -25,12 +25,8 @@ const handleKeyListRemove = () => {
emit('update:modelKey', keyList.value);
};
function handleUpdateNickname(nickname: string, keyId: string, keyList: KeyList) {
emit('update:nickname', nickname, keyId, keyList);
}
/* Hooks */
onMounted(() => {
onBeforeMount(() => {
if (props.modelKey instanceof KeyList) {
keyList.value = props.modelKey;
}
Expand All @@ -40,7 +36,6 @@ onMounted(() => {
<ComplexKeyThreshold
v-model:key-list="keyList"
@update:key-list="handleKeyListChange"
@update:nickname="handleUpdateNickname"
:on-remove-key-list="handleKeyListRemove"
:depth="'0'"
/>
Expand Down
25 changes: 8 additions & 17 deletions front-end/src/renderer/components/ComplexKey/ComplexKeyModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import AppModal from '@renderer/components/ui/AppModal.vue';
import AppCustomIcon from '@renderer/components/ui/AppCustomIcon.vue';
import ComplexKey from '@renderer/components/ComplexKey/ComplexKey.vue';
import KeyStructure from '@renderer/components/KeyStructure.vue';
import useNicknamesStore from '@renderer/stores/storeNicknames';
/* Props */
const props = defineProps<{
Expand All @@ -18,14 +19,15 @@ const props = defineProps<{
onSaveComplexKey?: () => void;
}>();
const nicknames = useNicknamesStore();
/* Emits */
const emit = defineEmits(['update:show', 'update:modelKey']);
/* State */
const currentKey = ref<Key | null>(props.modelKey);
const errorModalShow = ref(false);
const summaryMode = ref(false);
const updatedNicknames = ref<Map<string, { nickname: string; keyList: KeyList }>>(new Map());
/* Computed */
const currentKeyInvalid = computed(
Expand All @@ -39,12 +41,6 @@ const handleShowUpdate = (show: boolean) => emit('update:show', show);
const handleComplexKeyUpdate = (key: KeyList) => (currentKey.value = key);
function handleUpdateNickname(nickname: string, keyId: string, keyList: KeyList) {
updatedNicknames.value.set(keyId, { nickname, keyList });
console.log(updatedNicknames.value);
console.log(updatedNicknames.value.get('new')?.nickname);
}
const handleSaveComplexKeyButtonClick = () => {
if (currentKeyInvalid.value) {
errorModalShow.value = true;
Expand All @@ -64,12 +60,12 @@ const handleDoneButtonClick = async () => {
return;
}
emit('update:modelKey', currentKey.value, updatedNicknames.value);
emit('update:modelKey', currentKey.value);
emit('update:show', false);
updatedNicknames.value = new Map();
};
function handleExit() {
async function handleExit() {
await nicknames.clearNicknames();
emit('update:show', false);
}
Expand All @@ -82,8 +78,7 @@ function handleSummaryEdit() {
return;
}
emit('update:modelKey', currentKey.value, updatedNicknames.value, true);
updatedNicknames.value = new Map();
emit('update:modelKey', currentKey.value);
}
}
Expand Down Expand Up @@ -135,11 +130,7 @@ const modalContentContainerStyle = { padding: '0 10%', height: '80%' };
<div v-if="show" class="mt-5 h-100 overflow-auto">
<Transition name="fade" :mode="'out-in'">
<div v-if="!summaryMode">
<ComplexKey
:model-key="currentKey"
@update:model-key="handleComplexKeyUpdate"
@update:nickname="handleUpdateNickname"
/>
<ComplexKey :model-key="currentKey" @update:model-key="handleComplexKeyUpdate" />
</div>
<div v-else>
<KeyStructure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<script setup lang="ts">
import { onBeforeMount, ref, toRaw } from 'vue';
import { onBeforeMount, ref } from 'vue';
import { Key, KeyList, PublicKey } from '@hashgraph/sdk';
import useUserStore from '@renderer/stores/storeUser';
import useContactsStore from '@renderer/stores/storeContacts';
import useNicknamesStore from '@renderer/stores/storeNicknames';
import { useToast } from 'vue-toast-notification';
import { decodeKeyList, isPublicKeyInKeyList, isUserLoggedIn } from '@renderer/utils';
import { isPublicKeyInKeyList, isUserLoggedIn } from '@renderer/utils';
import * as ush from '@renderer/utils/userStoreHelpers';
import AppButton from '@renderer/components/ui/AppButton.vue';
import AppInput from '@renderer/components/ui/AppInput.vue';
import AppPublicKeyInput from '@renderer/components/ui/AppPublicKeyInput.vue';
import ComplexKeyAddPublicKeyModal from '@renderer/components/ComplexKey/ComplexKeyAddPublicKeyModal.vue';
import ComplexKeySelectAccountModal from '@renderer/components/ComplexKey/ComplexKeySelectAccountModal.vue';
import { getComplexKeys } from '@renderer/services/complexKeysService';
/* Props */
const props = defineProps<{
Expand All @@ -25,11 +25,12 @@ const props = defineProps<{
}>();
/* Emits */
const emit = defineEmits(['update:keyList', 'update:nickname']);
const emit = defineEmits(['update:keyList']);
/* Stores */
const user = useUserStore();
const contacts = useContactsStore();
const nicknames = useNicknamesStore();
/* Composables */
const toast = useToast();
Expand All @@ -39,7 +40,7 @@ const areChildrenShown = ref(true);
const addPublicKeyModalShown = ref(false);
const selectAccountModalShown = ref(false);
const nickname = ref('');
const complexKeyId = ref('');
const keyId = ref('');
/* Handlers */
const handleThresholdChange = (e: Event) => {
Expand Down Expand Up @@ -105,47 +106,28 @@ const handleKeyListUpdate = (index: number, newKeyList: KeyList) => {
newParentKeyList.splice(index, 1, newKeyList);
emit('update:keyList', newParentKeyList);
emit('update:nickname', nickname.value, complexKeyId.value, props.keyList);
nicknames.updateKey(nickname.value, newParentKeyList, keyId.value);
};
function handleUpdateNickname(value: string, keyId?: string, keyList?: KeyList) {
emit(
'update:nickname',
value,
keyId ? keyId : complexKeyId.value,
keyList ? keyList : props.keyList,
);
function handleUpdateNickname(value: string) {
nicknames.updateNickname(value, nickname.value);
nickname.value = value;
}
/* Funtions */
function emitNewKeyList(keys: Key[], threshold: number | null) {
const newKeyList = new KeyList(keys, threshold);
console.log(nickname.value);
handleUpdateNickname(nickname.value, complexKeyId.value, KeyList.from(keys));
emit('update:keyList', newKeyList);
nicknames.updateKey(nickname.value, newKeyList, keyId.value);
}
/* Hooks */
onBeforeMount(async () => {
console.log('onBeforeMount');
if (!isUserLoggedIn(user.personal)) {
throw new Error('User is not logged in');
}
complexKeyId.value = 'new';
const keyLists = await getComplexKeys(user.personal.id);
for (const keyList of keyLists) {
const list = decodeKeyList(keyList.protobufEncoded).toArray();
const propList = toRaw(props.keyList).toArray();
if (JSON.stringify(list) === JSON.stringify(propList) && !nickname.value) {
nickname.value = keyList.nickname;
complexKeyId.value = keyList.id;
}
}
[nickname.value, keyId.value] = await nicknames.getNickName(props.keyList);
});
</script>
<template>
Expand Down Expand Up @@ -269,7 +251,6 @@ onBeforeMount(async () => {
<ComplexKeyThreshold
:key-list="key"
@update:key-list="newKeyList => handleKeyListUpdate(i, newKeyList)"
@update:nickname="handleUpdateNickname"
:on-remove-key-list="() => handleRemoveThreshold(i)"
:depth="`${depth || 0}-${i}`"
/>
Expand Down
55 changes: 10 additions & 45 deletions front-end/src/renderer/components/KeyField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,9 @@ import useContactsStore from '@renderer/stores/storeContacts';
import { useToast } from 'vue-toast-notification';
import {
addComplexKey,
getComplexKey,
updateComplexKey,
} from '@renderer/services/complexKeysService';
import { getComplexKey, updateComplexKey } from '@renderer/services/complexKeysService';
import {
isPublicKey,
decodeKeyList,
encodeKey,
isUserLoggedIn,
isKeyListValid,
} from '@renderer/utils';
import { isPublicKey, decodeKeyList, encodeKey, isUserLoggedIn } from '@renderer/utils';
import * as ush from '@renderer/utils/userStoreHelpers';
import AppButton from '@renderer/components/ui/AppButton.vue';
Expand All @@ -32,6 +22,7 @@ import ComplexKeyModal from '@renderer/components/ComplexKey/ComplexKeyModal.vue
import ComplexKeyAddPublicKeyModal from '@renderer/components/ComplexKey/ComplexKeyAddPublicKeyModal.vue';
import ComplexKeySelectSavedKey from '@renderer/components/ComplexKey/ComplexKeySelectSavedKey.vue';
import ComplexKeySaveKeyModal from '@renderer/components/ComplexKey/ComplexKeySaveKeyModal.vue';
import useNicknamesStore from '@renderer/stores/storeNicknames';
/* Props */
const props = withDefaults(
Expand All @@ -57,9 +48,7 @@ enum Tabs {
/* Stores */
const user = useUserStore();
const contacts = useContactsStore();
/* Composables */
const toast = useToast();
const nicknames = useNicknamesStore();
/* State */
const currentTab = ref(Tabs.SIGNLE);
Expand Down Expand Up @@ -115,42 +104,18 @@ const handleEditComplexKey = () => {
complexKeyModalShown.value = true;
};
const handleComplexKeyUpdate = async (
keyList: KeyList,
updatedNicknames: Map<string, { nickname: string; keyList: KeyList }>,
silent: boolean,
) => {
console.log(updatedNicknames);
const handleComplexKeyUpdate = async (keyList: KeyList) => {
if (!isUserLoggedIn(user.personal)) {
throw new Error('User is not logged in');
}
emit('update:modelKey', keyList);
if (selectedComplexKey.value) {
const keyListBytes = encodeKey(keyList);
await nicknames.saveNicknames();
const updatedKey = await updateComplexKey(selectedComplexKey.value.id, keyListBytes);
selectedComplexKey.value = updatedKey;
if (!silent) toast.success('Key list updated successfully');
}
if (updatedNicknames) {
if (!isUserLoggedIn(user.personal)) {
throw new Error('User is not logged in');
}
console.log(updatedNicknames);
for (const [id, nicknameKeyList] of updatedNicknames) {
const keyListBytes = encodeKey(nicknameKeyList.keyList);
if (isKeyListValid(nicknameKeyList.keyList)) {
if (id == 'new' && keyListBytes) {
await addComplexKey(user.personal.id, keyListBytes, nicknameKeyList.nickname);
} else {
await updateComplexKey(id, keyListBytes, nicknameKeyList.nickname);
}
}
}
if (props.modelKey instanceof KeyList) {
selectedComplexKey.value = (await getComplexKey(user.personal.id, props.modelKey)) || null;
}
}
};
Expand Down
75 changes: 75 additions & 0 deletions front-end/src/renderer/stores/storeNicknames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { KeyList, Key } from '@hashgraph/sdk';
import { encodeKey, isUserLoggedIn } from '@renderer/utils';
import { acceptHMRUpdate, defineStore } from 'pinia';
import useUserStore from './storeUser';
import { addComplexKey, getComplexKey, updateComplexKey } from '@renderer/services/complexKeysService';

interface KeyAndId {
key: Key;
keyId: string;
}

interface State {
nickNamesKeys: Map<string, KeyAndId>;
}

const useNicknamesStore = defineStore('nicknames', {
state: (): State => ({
nickNamesKeys: new Map(),
}),
actions: {
async getNickName(selectedKeyList: KeyList) {
const user = useUserStore();

if (!isUserLoggedIn(user.personal)) {
throw new Error('User is not logged in');
}

const keyList = await getComplexKey(user.personal.id, selectedKeyList);

if (keyList) {
this.nickNamesKeys.set(keyList.nickname, { key: selectedKeyList, keyId: keyList.id })
return [keyList.nickname, keyList.id];
} else {
return ['', ''];
}
},
updateNickname(newNickname: string, oldNickname: string) {
const oldKey = this.nickNamesKeys.get(oldNickname);

if (oldKey) {
this.nickNamesKeys.set(newNickname, oldKey);
this.nickNamesKeys.delete(oldNickname);
}
},
updateKey(nickname: string, key: KeyList, keyId: string) {
this.nickNamesKeys.set(nickname, { key, keyId });
},
async saveNicknames() {
const user = useUserStore();

if (!isUserLoggedIn(user.personal)) {
throw new Error('User is not logged in');
}

for (const nickNameKey of this.nickNamesKeys) {
const keyListBytes = encodeKey(nickNameKey[1].key);
if (nickNameKey[1].keyId) {
await updateComplexKey(nickNameKey[1].keyId, keyListBytes, nickNameKey[0]);
} else {
await addComplexKey(user.personal.id, keyListBytes, nickNameKey[0])
}
}
},
clearNicknames() {
this.nickNamesKeys = new Map();
}
},
});

// Allows for hot loading with Store changes
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useNicknamesStore, import.meta.hot));
}

export default useNicknamesStore;

0 comments on commit 629b960

Please sign in to comment.