Skip to content

Commit a0ae47c

Browse files
feat: superhero id translations and refactoring
1 parent be69336 commit a0ae47c

File tree

5 files changed

+62
-34
lines changed

5 files changed

+62
-34
lines changed

src/composables/addressBook.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export const useAddressBook = createCustomScopedComposable(() => {
143143
Object.assign(savedEntry, entry);
144144
}
145145

146-
function addAddressBookEntriesFromJson(json: string) {
146+
function addAddressBookEntriesFromJson(json: string, showModal = true) {
147147
try {
148148
const newEntries: IAddressBookEntry[] = JSON.parse(json);
149149
const totalEntries = Object.keys(newEntries).length;
@@ -162,11 +162,13 @@ export const useAddressBook = createCustomScopedComposable(() => {
162162
}
163163
});
164164

165-
openModal(MODAL_ADDRESS_BOOK_IMPORT, {
166-
totalEntries,
167-
successfulEntriesCount,
168-
existingEntriesCount,
169-
});
165+
if (showModal) {
166+
openModal(MODAL_ADDRESS_BOOK_IMPORT, {
167+
totalEntries,
168+
successfulEntriesCount,
169+
existingEntriesCount,
170+
});
171+
}
170172
} catch (error) {
171173
handleUnknownError(error);
172174
}

src/composables/superheroId.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Ref, ref, watch } from 'vue';
1+
import {
2+
Ref,
3+
ref,
4+
watch,
5+
computed,
6+
} from 'vue';
27
import { SuperheroIDService } from '@/protocols/aeternity/libs/SuperheroIDService';
38
import type { Encoded } from '@aeternity/aepp-sdk';
49
import { useAccounts } from '@/composables/accounts';
@@ -7,6 +12,7 @@ import { useModals } from '@/composables/modals';
712
import { MODAL_CONFIRM_TRANSACTION_SIGN, PROTOCOLS } from '@/constants';
813
import { unpackTx } from '@aeternity/aepp-sdk';
914
import { useNetworks } from '@/composables/networks';
15+
import { useAddressBook } from '@/composables/addressBook';
1016

1117
const superheroSvcRef = ref<SuperheroIDService | null>(null);
1218
const hasSuperheroIdRef = ref(false);
@@ -16,16 +22,20 @@ export function useSuperheroId(): {
1622
hasSuperheroId: Ref<boolean>;
1723
refreshHasSuperheroId: () => Promise<void>;
1824
syncAddressBook: (json: string) => Promise<void>;
19-
loadAddressBook: () => Promise<string | undefined>;
25+
loadAddressBook: () => Promise<void>;
2026
} { // eslint-disable-line
2127
const { aeAccounts } = useAccounts();
2228
const { getAeSdk } = useAeSdk();
2329
const { openModal } = useModals();
30+
const { addAddressBookEntriesFromJson } = useAddressBook();
31+
32+
const firstAeAddress = computed(() => (
33+
aeAccounts.value?.[0]?.address as Encoded.AccountAddress | undefined));
2434

2535
const svcRef = superheroSvcRef as unknown as Ref<SuperheroIDService | null>;
2636

2737
async function refreshHasSuperheroId(): Promise<void> {
28-
const addr = aeAccounts.value?.[0]?.address;
38+
const addr = firstAeAddress.value;
2939
if (!addr) {
3040
hasSuperheroIdRef.value = false;
3141
return;
@@ -39,7 +49,7 @@ export function useSuperheroId(): {
3949
}
4050

4151
async function syncAddressBook(json: string): Promise<void> {
42-
const addr = aeAccounts.value?.[0]?.address;
52+
const addr = firstAeAddress.value;
4353
if (!addr) throw new Error('No æternity account');
4454
if (!superheroSvcRef.value) throw new Error('Connect Superhero ID first');
4555
const svc = superheroSvcRef.value;
@@ -59,13 +69,15 @@ export function useSuperheroId(): {
5969
hasSuperheroIdRef.value = true;
6070
}
6171

62-
async function loadAddressBook(): Promise<string | undefined> {
63-
const addr = aeAccounts.value?.[0]?.address;
64-
if (!addr) return undefined;
72+
async function loadAddressBook() {
73+
const addr = firstAeAddress.value;
74+
if (!addr) throw new Error('No æternity account');
6575
const svc = superheroSvcRef.value || new SuperheroIDService();
6676
const val = await svc.getId();
6777
hasSuperheroIdRef.value = !!val;
68-
return val;
78+
if (val) {
79+
addAddressBookEntriesFromJson(val, false);
80+
}
6981
}
7082

7183
// Re-init on network change if an AE account exists
@@ -75,9 +87,8 @@ export function useSuperheroId(): {
7587
});
7688

7789
// Auto-initialize service when first AE account is available; clear on removal
78-
7990
watch(
80-
() => aeAccounts.value?.[0]?.address,
91+
() => firstAeAddress.value,
8192
async (addr) => {
8293
if (addr) {
8394
if (!superheroSvcRef.value) superheroSvcRef.value = new SuperheroIDService();

src/popup/locales/en.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@
162162
"latestTransactionCard": {
163163
"title": "Latest transactions"
164164
},
165+
"superheroId": {
166+
"title": "Superhero ID",
167+
"createDescription": "Create your Superhero ID to store your settings to the blockchain",
168+
"connectDescription": "Connect to restore your settings from the blockchain",
169+
"createBtn": "Create",
170+
"connectBtn": "Connect",
171+
"createdMsg": "Created Superhero ID.",
172+
"createFailed": "Create failed",
173+
"connectFailed": "Connection to Superhero ID contract failed",
174+
"restoreMsg": "Your address book and preferences have been restored",
175+
"deployFailed": "Deploy failed",
176+
"deployedMsg": "Deployed: {ct}"
177+
},
165178
"pendingMultisigCard": {
166179
"title": "Pending multisig transaction"
167180
}

src/popup/pages/AddressBook.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<script lang="ts">
4444
import { IonPage } from '@ionic/vue';
4545
import { defineComponent, ref } from 'vue';
46+
import { useI18n } from 'vue-i18n';
4647
4748
import { ROUTE_ADDRESS_BOOK_ADD } from '@/popup/router/routeNames';
4849
import {
@@ -68,6 +69,7 @@ export default defineComponent({
6869
},
6970
setup() {
7071
const hideButtons = ref(false);
72+
const { t } = useI18n();
7173
7274
const { exportAddressBook, importAddressBook, addressBook } = useAddressBook();
7375
const { openDefaultModal } = useModals();
@@ -82,9 +84,9 @@ export default defineComponent({
8284
const addr = aeAccounts.value?.[0]?.address as `ak_${string}`;
8385
if (!addr) throw new Error('No æternity account');
8486
await syncAddressBook(JSON.stringify(addressBook.value));
85-
openDefaultModal({ title: 'Superhero ID', msg: 'Address Book Sync with SH ID completed' });
87+
openDefaultModal({ title: t('dashboard.superheroId.title'), msg: t('pages.addressBook.export.title') });
8688
} catch (e) {
87-
openDefaultModal({ title: 'Superhero ID', msg: 'Sync failed' });
89+
openDefaultModal({ title: t('dashboard.superheroId.title'), msg: t('common.connectionFailed') });
8890
handleUnknownError(e);
8991
} finally {
9092
isSyncing.value = false;

src/popup/pages/Dashboard.vue

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@
4343
<template #cards>
4444
<DashboardCard
4545
v-if="!hasSuperheroId"
46-
title="Superhero ID"
47-
description="Create your Superhero ID to store your settings to the blockchain"
48-
btn-text="Create"
46+
:title="$t('dashboard.superheroId.title')"
47+
:description="$t('dashboard.superheroId.createDescription')"
48+
:btn-text="$t('dashboard.superheroId.createBtn')"
4949
@click="onCreateSuperheroId"
5050
/>
5151

5252
<DashboardCard
5353
v-if="hasSuperheroId"
54-
title="Superhero ID"
55-
description="Connect to restore your settings from the blockchain"
56-
btn-text="Connect"
54+
:title="$t('dashboard.superheroId.title')"
55+
:description="$t('dashboard.superheroId.connectDescription')"
56+
:btn-text="$t('dashboard.superheroId.connectBtn')"
5757
@click="onConnectSuperheroId"
5858
/>
5959

@@ -113,6 +113,7 @@ import {
113113
onIonViewWillEnter,
114114
onIonViewDidLeave,
115115
} from '@ionic/vue';
116+
import { useI18n } from 'vue-i18n';
116117
import { useRoute } from 'vue-router';
117118
118119
import {
@@ -175,6 +176,7 @@ export default defineComponent({
175176
},
176177
setup() {
177178
const pageIsActive = ref(true);
179+
const { t } = useI18n();
178180
179181
const route = useRoute();
180182
@@ -186,10 +188,9 @@ export default defineComponent({
186188
activeAccountGlobalIdx,
187189
setActiveAccountByGlobalIdx,
188190
setActiveAccountByAddressAndProtocol,
189-
aeAccounts,
190191
} = useAccounts();
191192
const { multisigAccounts } = useMultisigAccounts();
192-
const { addressBook, addAddressBookEntriesFromJson } = useAddressBook();
193+
const { addressBook } = useAddressBook();
193194
const {
194195
superheroSvc,
195196
hasSuperheroId,
@@ -236,30 +237,29 @@ export default defineComponent({
236237
const svc = superheroSvc.value!;
237238
const ct = await svc.deployFromSource(source);
238239
connectedContractId.value = ct;
239-
openDefaultModal({ title: 'Contract', msg: `Deployed: ${ct}` });
240+
openDefaultModal({ title: t('dashboard.superheroId.title'), msg: t('dashboard.superheroId.deployedMsg', { ct }) });
240241
} catch (e) {
241-
openDefaultModal({ title: 'Contract', msg: 'Deploy failed' });
242+
openDefaultModal({ title: t('dashboard.superheroId.title'), msg: t('dashboard.superheroId.deployFailed') });
242243
handleUnknownError(e);
243244
}
244245
}
245246
246247
async function onConnectSuperheroId() {
247-
if (!aeAccounts.value.length) return;
248248
try {
249-
const json = await loadAddressBook();
250-
if (json) addAddressBookEntriesFromJson(json);
249+
await loadAddressBook();
250+
openDefaultModal({ title: t('dashboard.superheroId.title'), msg: t('dashboard.superheroId.restoreMsg') });
251251
} catch (e) {
252-
openDefaultModal({ title: 'Superhero ID', msg: 'Connection to Superhero ID contract failed' });
252+
openDefaultModal({ title: t('dashboard.superheroId.title'), msg: t('dashboard.superheroId.connectFailed') });
253253
handleUnknownError(e);
254254
}
255255
}
256256
257257
async function onCreateSuperheroId() {
258258
try {
259259
await syncAddressBook(JSON.stringify(addressBook.value));
260-
openDefaultModal({ title: 'Superhero ID', msg: 'Created Superhero ID.' });
260+
openDefaultModal({ title: t('dashboard.superheroId.title'), msg: t('dashboard.superheroId.createdMsg') });
261261
} catch (e) {
262-
openDefaultModal({ title: 'Superhero ID', msg: 'Create failed' });
262+
openDefaultModal({ title: t('dashboard.superheroId.title'), msg: t('dashboard.superheroId.createFailed') });
263263
}
264264
}
265265

0 commit comments

Comments
 (0)