Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shelegdmitriy committed Jan 10, 2024
1 parent 5e98a73 commit c24fe0b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
48 changes: 20 additions & 28 deletions src/pages/settings.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type { WalletSelector } from '@near-wallet-selector/core';
import { useRouter } from 'next/router';
import { useCallback, useEffect, useState } from 'react';
import hereImage from '@near-wallet-selector/here-wallet/assets/here-wallet-icon.png';
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 { openToast } from '@/components/lib/Toast';
import { ComponentWrapperPage } from '@/components/near-org/ComponentWrapperPage';
import { useBosComponents } from '@/hooks/useBosComponents';
import { useDefaultLayout } from '@/hooks/useLayout';
import { useAuthStore } from '@/stores/auth';
import { useIdosStore } from '@/stores/idosStore';
import type { IdosUser, NextPageWithLayout } from '@/utils/types';
import type { NextPageWithLayout } from '@/utils/types';

const SettingsPage: NextPageWithLayout = () => {
const components = useBosComponents();
Expand All @@ -17,34 +20,20 @@ const SettingsPage: NextPageWithLayout = () => {
const idOS = useIdosStore((state) => state.idOS);
const idosProfileExist = useIdosStore((state) => state.hasProfile);
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 [showTooltip, setShowTooltip] = useState(false);
const createAccountUrl =
'https://app.fractal.id/authorize?client_id=PXAbBxPErSPMXiKmMYQ3ged8Qxwqg1Px7ymhsuhaGP4&redirect_uri=https%3A%2F%2Fnear.org%2Fsettings&response_type=code&scope=contact%3Aread%20verification.uniqueness%3Aread%20verification.uniqueness.details%3Aread%20verification.idos%3Aread%20verification.idos.details%3Aread%20verification.wallet-near%3Aread%20verification.wallet-near.details%3Aread';
const router = useRouter();

const connectIdOS = useCallback(async () => {
if (!near || !idOS || !accountId) return;
const wallet = (await (await near.selector).wallet()) as WalletSelector['wallet'];
const wallet = (await (await near.selector).wallet()) as WalletSelector['wallet'] | any;
const hasProfile = await idOS.hasProfile(accountId);
setIdosStore({ hasProfile });
setIdosStore({ hasProfile, connectedWallet: wallet.id });
try {
if (hasProfile) {
await idOS.setSigner('NEAR', wallet);

const credentials = idosCredentials ?? (await idOS.data.list('credentials'));
setIdosStore({ credentials });
} else {
openToast({
type: 'INFO',
title: 'No idOS profile found.',
description: "Need an idOS profile? Now you'll be redirected to the idOS website to create one.",
});

setTimeout(() => {
router.push(createAccountUrl);
}, 3000);
}
} catch (error: any) {
console.error('Failed to init wallet + idOS: ', error);
Expand All @@ -59,23 +48,26 @@ const SettingsPage: NextPageWithLayout = () => {
});
}
}
}, [accountId, error, idOS, idosCredentials, near, router, setIdosStore]);
}, [near, idOS, accountId, setIdosStore, idosCredentials, error]);

// useEffect(() => {
// if (accountId && router && router.query && router.query.code) {
// setShowTooltip(true);
// }
// }, [router]);
const walletImages = [
{ name: 'meteor-wallet', ...meteorImage },
{ name: 'here-wallet', ...hereImage },
{ name: 'my-near-wallet', ...myNearImage },
{ name: 'nightly-wallet', ...nightlyImage },
];

return (
<ComponentWrapperPage
src={components.settings.index}
meta={{ title: 'NEAR | Settings', description: '' }}
componentProps={{
idosConnected: idosProfileExist ?? false,
idosConnected: !accountId ? false : idosProfileExist,
connectIdOS,
idosCredentials,
idosCredentials: !accountId ? undefined : idosCredentials,
showTooltip: false,
walletImages,
connectedWallet: !accountId ? undefined : connectedWallet,
}}
/>
);
Expand Down
2 changes: 2 additions & 0 deletions src/stores/idosStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type IdosState = {
idOS?: IdosTypeTemp;
currentUser?: IdosUser;
hasProfile?: boolean;
connectedWallet?: string;
credentials?: object[];
wallets?: IdosWalletInfo[];
};
Expand All @@ -24,6 +25,7 @@ export const useIdosStore = create(
idOS: undefined,
currentUser: undefined,
hasProfile: false,
connectedWallet: undefined,
credentials: undefined,
wallets: undefined,
set: (state) => set((previousState) => ({ ...previousState, ...state })),
Expand Down

0 comments on commit c24fe0b

Please sign in to comment.