Skip to content

Commit

Permalink
fix: fetch data from idOS
Browse files Browse the repository at this point in the history
  • Loading branch information
shelegdmitriy committed Jan 15, 2024
1 parent 3600202 commit dcfebd4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/components/vm/VmInitializer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default function VmInitializer() {
const setVmStore = useVmStore((store) => store.set);
const { requestAuthentication, saveCurrentUrl } = useSignInRedirect();
const idOS = useIdOS();
const idosSDK = useIdosStore((state) => state.idOS);

useEffect(() => {
initNear &&
Expand Down Expand Up @@ -188,6 +189,7 @@ export default function VmInitializer() {
if (!near) {
return;
}
await idosSDK?.reset({ enclave: true });
useIdosStore.persist.clearStorage();
const wallet = await (await near.selector).wallet();
wallet.signOut();
Expand All @@ -196,7 +198,7 @@ export default function VmInitializer() {
setSignedAccountId(null);
resetAnalytics();
localStorage.removeItem('accountId');
}, [near]);
}, [idosSDK, near]);

const refreshAllowance = useCallback(async () => {
alert("You're out of access key allowance. Need sign in again to refresh it");
Expand Down
48 changes: 30 additions & 18 deletions src/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import hereImage from '@near-wallet-selector/here-wallet/assets/here-wallet-icon
import meteorImage from '@near-wallet-selector/meteor-wallet/assets/meteor-icon.png';
import myNearImage from '@near-wallet-selector/my-near-wallet/assets/my-near-wallet-icon.png';
import nightlyImage from '@near-wallet-selector/nightly/assets/nightly.png';
import { useCallback, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';

import { openToast } from '@/components/lib/Toast';
import type { ToastType } from '@/components/lib/Toast/store';
import { ComponentWrapperPage } from '@/components/near-org/ComponentWrapperPage';
import { useBosComponents } from '@/hooks/useBosComponents';
import { useDefaultLayout } from '@/hooks/useLayout';
Expand All @@ -22,7 +23,14 @@ const SettingsPage: NextPageWithLayout = () => {
const idosCredentials = useIdosStore((state) => state.credentials);
const connectedWallet = useIdosStore((state) => state.connectedWallet);
const setIdosStore = useIdosStore((state) => state.set);
const [error, setError] = useState<string | null>(null);
const [error, setError] = useState<{ type: ToastType; title: string; description?: string } | null>(null);

const walletImages = [
{ name: 'meteor-wallet', ...meteorImage },
{ name: 'here-wallet', ...hereImage },
{ name: 'my-near-wallet', ...myNearImage },
{ name: 'nightly-wallet', ...nightlyImage },
];

const connectIdOS = useCallback(async () => {
if (!near || !idOS || !accountId) return;
Expand All @@ -34,28 +42,32 @@ const SettingsPage: NextPageWithLayout = () => {
await idOS.setSigner('NEAR', wallet);
const credentials = idosCredentials ?? (await idOS.data.list('credentials'));
setIdosStore({ credentials });
} else {
setError({
type: 'INFO',
title: `No idOS profile found for ${accountId}`,
});
}
} catch (error: any) {
console.error('Failed to init wallet + idOS: ', error);
const errorMessage = error.message ? error.message : 'unknown';
setError(errorMessage);
} finally {
if (!idosCredentials && error) {
openToast({
type: 'ERROR',
title: 'Failed to init wallet + idOS:',
description: `${error}`,
});
}
setError({
type: 'ERROR',
title: 'Falilure during idOS initialization:',
description: `${errorMessage}`,
});
}
}, [near, idOS, accountId, setIdosStore, idosCredentials, error]);
}, [near, idOS, accountId, setIdosStore, idosCredentials]);

const walletImages = [
{ name: 'meteor-wallet', ...meteorImage },
{ name: 'here-wallet', ...hereImage },
{ name: 'my-near-wallet', ...myNearImage },
{ name: 'nightly-wallet', ...nightlyImage },
];
useEffect(() => {
if (!idosCredentials && error) {
openToast({
type: error.type,
title: error.title,
description: error.description,
});
}
}, [error, idosCredentials]);

return (
<ComponentWrapperPage
Expand Down

0 comments on commit dcfebd4

Please sign in to comment.