Skip to content

Commit

Permalink
feat: Go to profile page or not after connect wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
ybgbob committed Jul 1, 2024
1 parent 526b8fa commit 9d39e5e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/components/HoverStatus.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import styled from '@emotion/styled';
import { useModal as useWalletKitModal } from '@node-real/walletkit';
import { Box, Flex, Stack } from '@totejs/uikit';
import { useImmerAtom } from 'jotai-immer';
import { MetaMaskAvatar } from 'react-metamask-avatar';
import { useNavigate } from 'react-router-dom';
import { useAccount } from 'wagmi';
import { buyAtom } from '../atoms/buyAtom';
import { useGetChainListItems } from '../hooks/buyer/useGetChainListItems';
import { useAppWallet } from '../hooks/useAppWallet';
import { useGetRelationWithAddr } from '../hooks/useGetItemRelationWithAddr';
import { trimLongStr } from '../utils';
import { Item } from '../utils/apis/types';
import { Loader } from './Loader';
import { DefaultButton } from './ui/buttons/DefaultButton';
import { YellowButton } from './ui/buttons/YellowButton';
import { Loader } from './Loader';

interface IProps {
item: Item;
Expand All @@ -32,7 +32,7 @@ export const HoverStatus = ({ item, className }: IProps) => {
doDownload,
isDownloading,
} = useGetRelationWithAddr(address, item, chainItemInfo?.creators?.[0] || '');
const { onOpen } = useWalletKitModal();
const { onOpen } = useAppWallet();
const [, setBuy] = useImmerAtom(buyAtom);

return (
Expand Down
25 changes: 12 additions & 13 deletions src/components/layout/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled from '@emotion/styled';
import { WalletKitButton, useModal } from '@node-real/walletkit';
import { WalletKitButton } from '@node-real/walletkit';
import '@node-real/walletkit/styles.css';
import {
Box,
Expand All @@ -15,6 +15,7 @@ import { MetaMaskAvatar } from 'react-metamask-avatar';
import { useLocation, useNavigate } from 'react-router-dom';
import { useAccount, useDisconnect } from 'wagmi';
import { NET_ENV } from '../../env';
import { useAppWallet } from '../../hooks/useAppWallet';
import TestNetLogo from '../../images/logo-testnet.svg';
import MainNetLogo from '../../images/logo.svg';
import { trimLongStr } from '../../utils';
Expand All @@ -29,13 +30,8 @@ const INFO_BAR_HEIGHT = '40px';

const Header = () => {
const [dropDownOpen, setDropDownOpen] = useState(false);
const { address, isConnecting, isConnected } = useAccount({
onConnect: ({ isReconnected }) => {
if (!isReconnected) {
navigate('/profile?tab=uploaded');
}
},
});
const { onOpen } = useAppWallet();
const { address, isConnecting, isConnected } = useAccount();

const { disconnect } = useDisconnect();
const handleShowDropDown = useCallback(() => {
Expand All @@ -44,7 +40,6 @@ const Header = () => {
const ref = useRef<HTMLDivElement>(null);
const ref2 = useRef<HTMLDivElement>(null);
const [{ y }] = useWindowScroll();
const { onOpen } = useModal();

const navigate = useNavigate();
const location = useLocation();
Expand All @@ -56,7 +51,6 @@ const Header = () => {

useEffect(() => {
if (!ref.current) return;
// if (location.pathname !== '/') return;

if (y && y > 10) {
ref.current.style.backgroundColor = BG_COLOR;
Expand Down Expand Up @@ -139,7 +133,7 @@ const Header = () => {
}}
onClick={() => {
if (!address) {
onOpen();
onOpen('GO_TO_PROFILE');
return;
}
if (
Expand All @@ -163,9 +157,14 @@ const Header = () => {
{!isConnected && !isConnecting ? (
// <WalletKitButton />
<WalletKitButton.Custom>
{({ show }) => {
{() => {
return (
<StyledButton h="40px" onClick={show}>
<StyledButton
h="40px"
onClick={() => {
onOpen();
}}
>
Connect Wallet
</StyledButton>
);
Expand Down
34 changes: 34 additions & 0 deletions src/hooks/useAppWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useModal } from '@node-real/walletkit';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useAccount } from 'wagmi';

/**
* 'HOME': NOT REDIRECTED
* 'GO_TO_PROFILE': Go To profile page after connect wallet
*/
type Tag = 'DEFAULT' | 'GO_TO_PROFILE';

export function useAppWallet() {
const navigate = useNavigate();
const [tag, setTag] = useState<Tag>('DEFAULT');

const { address } = useAccount({
onConnect() {
if (tag === 'DEFAULT') return;

navigate('/profile?tab=uploaded');
},
});

const { isOpen, onOpen } = useModal();

return {
address,
isOpen,
onOpen(tag: Tag = 'DEFAULT') {
setTag(tag);
onOpen();
},
};
}
5 changes: 4 additions & 1 deletion src/hooks/useGetItemRelationWithAddr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ export const useGetRelationWithAddr = (
});

useEffect(() => {
if (!addr || !data) return;
if (!addr || !data) {
setRelation('UNKNOWN');
return;
}

// console.log('storageInfo', storageInfo);

Expand Down
10 changes: 5 additions & 5 deletions src/pages/R.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import NiceModal from '@ebay/nice-modal-react';
import styled from '@emotion/styled';
import { Box } from '@totejs/uikit';
import _ from 'lodash';
import { useEffect, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { Loader } from '../components/Loader';
import { useRedirectFromExternal } from '../hooks/useRedirectFromExternal';
import NiceModal from '@ebay/nice-modal-react';
import { useModal } from '@node-real/walletkit';
import { Tips } from '../components/modal/Tips';
import { useAppWallet } from '../hooks/useAppWallet';
import { useRedirectFromExternal } from '../hooks/useRedirectFromExternal';
import { trimLongStr } from '../utils';
import styled from '@emotion/styled';

export const R = () => {
const navigator = useNavigate();
Expand All @@ -21,7 +21,7 @@ export const R = () => {
detailOid,
} = useRedirectFromExternal();

const { onOpen } = useModal();
const { onOpen } = useAppWallet();

// console.log(
// 'status',
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Resource.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import styled from '@emotion/styled';
import { useModal as useWalletKitModal } from '@node-real/walletkit';
import { LinkArrowIcon } from '@totejs/icons';
import { Box, Button, Flex, Image, Link, Stack } from '@totejs/uikit';
import { useImmerAtom } from 'jotai-immer';
Expand All @@ -21,6 +20,7 @@ import { useGetItemByObjectIds } from '../hooks/apis/useGetItemByObjectIds';
import { useGetChainListItems } from '../hooks/buyer/useGetChainListItems';
import { useGetBnbUsdtExchangeRate } from '../hooks/price/useGetBnbUsdtExchangeRate';
import { useDelist } from '../hooks/seller/useDelist';
import { useAppWallet } from '../hooks/useAppWallet';
import { useGetObjectById } from '../hooks/useGetBucketOrObj';
import { useGetRelationWithGroupId } from '../hooks/useGetItemRelationWithAddr';
import {
Expand Down Expand Up @@ -106,7 +106,7 @@ const Resource = () => {
// navigator(`detail?bid=${bid}&oid=${oid}`);
// },

const { onOpen } = useWalletKitModal();
const { onOpen } = useAppWallet();

const { data: usdExchange } = useGetBnbUsdtExchangeRate();

Expand Down

0 comments on commit 9d39e5e

Please sign in to comment.