Skip to content

Commit

Permalink
DAPP-1563 fixed useAccount and ChannelsDataStore erros (#1907)
Browse files Browse the repository at this point in the history
* fixed useAccount and ChannelsDataStore erros

* removed debug

* removed logs
  • Loading branch information
rohitmalhotra1420 authored Oct 8, 2024
1 parent 1d3a1ad commit 2ef95bc
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 78 deletions.
6 changes: 3 additions & 3 deletions src/components/ChannelDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import useModalBlur, { MODAL_POSITION } from 'hooks/useModalBlur';
import useToast from 'hooks/useToast';
import { Section } from 'primaries/SharedStyling';
import { MdCheckCircle, MdError } from 'react-icons/md';
import ChannelsDataStore from 'singletons/ChannelsDataStore';
// import ChannelsDataStore from 'singletons/ChannelsDataStore';
import RedCircleSvg from '../assets/RedCircle.svg?react';
import AddDelegateModalContent from './AddDelegateModalContent';
import ChannelSettings from './ChannelSettings';
Expand Down Expand Up @@ -109,8 +109,8 @@ export default function ChannelDetails({ isChannelExpired, setIsChannelExpired,
useEffect(() => {
if (!channelDetails || !canVerify) return;
(async function () {
let channelJson = await ChannelsDataStore.getInstance().getChannelJsonAsync(channelDetails.verifiedBy);
setVerifyingChannel(channelJson);
// let channelJson = await ChannelsDataStore.getInstance().getChannelJsonAsync(channelDetails.verifiedBy);
// setVerifyingChannel(channelJson);
})();
}, [channelDetails, canVerify]);

Expand Down
23 changes: 13 additions & 10 deletions src/components/InitState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ import {
setPushAdmin,
} from 'redux/slices/contractSlice';
import { getAliasDetails, getUserDelegations } from 'services';
import ChannelsDataStore from 'singletons/ChannelsDataStore';
// import ChannelsDataStore from 'singletons/ChannelsDataStore';
import UsersDataStore from 'singletons/UsersDataStore';

// Internals Configs
import { abis, addresses, appConfig, CHAIN_DETAILS } from 'config/index.js';
import useSDKStream from 'hooks/useStream';
import { getChannelDetails } from 'queries';

// Constants
const CORE_CHAIN_ID = appConfig.coreContractChain;
Expand Down Expand Up @@ -100,8 +101,8 @@ const InitState = () => {
// Push (EPNS) Read Provider Set
if (epnsReadProvider != null && epnsCommReadProvider != null) {
// Instantiate Data Stores
UsersDataStore.getInstance().init(account, epnsReadProvider, epnsCommReadProvider);
ChannelsDataStore.getInstance().init(account, epnsReadProvider, epnsCommReadProvider, chainId);
// UsersDataStore.getInstance().init(account, epnsReadProvider, epnsCommReadProvider);
// ChannelsDataStore.getInstance().init(account, epnsReadProvider, epnsCommReadProvider, chainId);
}
}, [epnsReadProvider, epnsCommReadProvider, epnsWriteProvider]);

Expand Down Expand Up @@ -209,14 +210,16 @@ const InitState = () => {
};

const checkUserForAlias = async (account, userPushSDKInstance) => {
let { aliasAddress = null, isAliasVerified = null } =
await ChannelsDataStore.getInstance().getChannelDetailsFromAddress(account, userPushSDKInstance);
if (aliasAddress == 'NULL') aliasAddress = null;
let { alias_address = null, is_alias_verified = null } = await getChannelDetails({
userPushSDKInstance,
address: account,
}).then((response) => response);
if (alias_address == 'NULL') alias_address = null;

if (aliasAddress) {
dispatch(setAliasAddress(aliasAddress));
dispatch(setAliasVerified(isAliasVerified));
if (isAliasVerified) {
if (alias_address) {
dispatch(setAliasAddress(alias_address));
dispatch(setAliasVerified(is_alias_verified));
if (is_alias_verified) {
dispatch(setAliasVerified(true));
dispatch(setProcessingState(0));
} else {
Expand Down
92 changes: 47 additions & 45 deletions src/components/ViewChannelItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import useToast from 'hooks/useToast';
import { cacheChannelInfo } from 'redux/slices/channelSlice';
import { addNewWelcomeNotif, incrementStepIndex } from 'redux/slices/userJourneySlice';
import ChannelTutorial, { isChannelTutorialized } from 'segments/ChannelTutorial';
import ChannelsDataStore from 'singletons/ChannelsDataStore';
// import ChannelsDataStore from 'singletons/ChannelsDataStore';
import InfoImage from '../assets/info.svg';
import RedCircleSvg from '../assets/RedCircle.svg?react';
import NotificationToast from '../primaries/NotificationToast';
Expand Down Expand Up @@ -111,7 +111,9 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
const fetchChannelJsonWithBlock = async () => {
try {
//TODO:here is the data store
const channelJson = await ChannelsDataStore.getInstance().getChannelJsonStartBlockAsync(channelObject.channel);
const channelJson = {};

// await ChannelsDataStore.getInstance().getChannelJsonStartBlockAsync(channelObject.channel);
console.log('Channel JSON !!!!!', channelJson, channelObject.channel);
return channelJson;
} catch (err) {
Expand Down Expand Up @@ -450,19 +452,19 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
placementProps={
tooltTipHeight < 250
? {
background: 'none',
// bottom: "25px",
top: '20px',
// right: "-175px",
left: mobileToolTip ? '-100px' : '5px',
}
background: 'none',
// bottom: "25px",
top: '20px',
// right: "-175px",
left: mobileToolTip ? '-100px' : '5px',
}
: {
background: 'none',
bottom: '25px',
// top: "20px",
// right: "-175px",
left: mobileToolTip ? '-100px' : '5px',
}
background: 'none',
bottom: '25px',
// top: "20px",
// right: "-175px",
left: mobileToolTip ? '-100px' : '5px',
}
}
tooltipContent={
<UpdateChannelTooltipContent
Expand Down Expand Up @@ -508,15 +510,15 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
placementProps={
tooltTipHeight < 160
? {
background: 'none',
top: '20px', // for lower displaying
left: '7px',
}
background: 'none',
top: '20px', // for lower displaying
left: '7px',
}
: {
background: 'none',
bottom: '28px', // above display
left: '7px',
}
background: 'none',
bottom: '28px', // above display
left: '7px',
}
}
tooltipContent={
<VerifiedTooltipContent
Expand Down Expand Up @@ -624,19 +626,19 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
placementProps={
tooltTipHeight < 250
? {
background: 'none',
// bottom: "25px",
top: '20px',
// right: "-175px",
left: '5px',
}
background: 'none',
// bottom: "25px",
top: '20px',
// right: "-175px",
left: '5px',
}
: {
background: 'none',
bottom: '25px',
// top: "20px",
// right: "-175px",
left: '5px',
}
background: 'none',
bottom: '25px',
// top: "20px",
// right: "-175px",
left: '5px',
}
}
tooltipContent={
<UpdateChannelTooltipContent
Expand Down Expand Up @@ -680,15 +682,15 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
placementProps={
tooltTipHeight < 160
? {
background: 'none',
top: '20px', // for lower displaying
left: '7px',
}
background: 'none',
top: '20px', // for lower displaying
left: '7px',
}
: {
background: 'none',
bottom: '28px', // above display
left: '7px',
}
background: 'none',
bottom: '28px', // above display
left: '7px',
}
}
tooltipContent={
<VerifiedTooltipContent
Expand Down Expand Up @@ -956,7 +958,7 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
>
<Button
size="small"
onClick={() => { }}
onClick={() => {}}
disabled={txInProgress}
loading={txInProgress}
>
Expand Down Expand Up @@ -997,7 +999,7 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
}}
>
<UnsubscribeButton
onClick={() => { }}
onClick={() => {}}
disabled={txInProgress}
>
{txInProgress && (
Expand Down Expand Up @@ -1238,7 +1240,7 @@ const VerifierName = styled.span`
font-weight: 400;
color: ${(props) => props.theme.color};
font-size: 16px;
letter-spacing:normal;
letter-spacing: normal;
@media (max-width: 1024px) {
margin-top: 10px;
}
Expand Down
25 changes: 21 additions & 4 deletions src/contexts/GlobalContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { createContext, useState } from 'react';
import { WalletState } from '@web3-onboard/core';
import { useConnectWallet } from '@web3-onboard/react';
import { appConfig } from 'config';
import { ethers } from 'ethers';
import { createContext, useMemo, useState } from 'react';

export enum ReadOnlyWalletMode {
READ_ONLY_MODE = '(Read Only)',
Expand All @@ -10,8 +14,11 @@ export type GlobalContextType = {
setReadOnlyWallet: React.Dispatch<React.SetStateAction<string>>;
mode: ReadOnlyWalletMode;
setMode: React.Dispatch<React.SetStateAction<ReadOnlyWalletMode>>;
sidebarCollapsed:boolean;
setSidebarCollapsed:React.Dispatch<React.SetStateAction<boolean>>;
sidebarCollapsed: boolean;
setSidebarCollapsed: React.Dispatch<React.SetStateAction<boolean>>;
globalWallet: WalletState | null;
setGlobalWallet: React.Dispatch<React.SetStateAction<WalletState | null>>;
provider: ethers.providers.JsonRpcProvider;
};

export const GlobalContext = createContext<GlobalContextType | null>(null);
Expand All @@ -20,6 +27,13 @@ const GlobalContextProvider = ({ children }) => {
const [readOnlyWallet, setReadOnlyWallet] = useState<string>('0x0000000000000000000000000000000000000001');
const [mode, setMode] = useState<ReadOnlyWalletMode>(ReadOnlyWalletMode.GUEST_MODE);
const [sidebarCollapsed, setSidebarCollapsed] = useState<boolean>(false);
const [globalWallet, setGlobalWallet] = useState<WalletState | null>(null);

const provider = useMemo(() => {
return globalWallet
? new ethers.providers.Web3Provider(globalWallet.provider, 'any')
: new ethers.providers.JsonRpcProvider(appConfig.coreRPC);
}, [globalWallet]);

return (
<GlobalContext.Provider
Expand All @@ -29,7 +43,10 @@ const GlobalContextProvider = ({ children }) => {
mode,
setMode,
sidebarCollapsed,
setSidebarCollapsed
setSidebarCollapsed,
globalWallet,
setGlobalWallet,
provider,
}}
>
{children}
Expand Down
8 changes: 1 addition & 7 deletions src/hooks/useAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ethers } from 'ethers';
import { useContext, useMemo } from 'react';

export const useAccount = () => {
const { readOnlyWallet, setMode, setReadOnlyWallet } = useContext(GlobalContext);
const { readOnlyWallet, setMode, setReadOnlyWallet, provider } = useContext(GlobalContext);

const [{ wallet, connecting }, connect, disconnect, updateBalances, setWalletModules, setPrimaryWallet] =
useConnectWallet();
Expand All @@ -16,12 +16,6 @@ export const useAccount = () => {
return setChain({ chainId: ethers.utils.hexValue(desiredChain) });
};

const provider = useMemo(() => {
return wallet
? new ethers.providers.Web3Provider(wallet.provider, 'any')
: new ethers.providers.JsonRpcProvider(appConfig.coreRPC);
}, [wallet]);

const isActive = useMemo(() => {
if (readOnlyWallet) {
return true;
Expand Down
11 changes: 7 additions & 4 deletions src/modules/channelDashboard/ChannelOwnerDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import useModalBlur from 'hooks/useModalBlur';
import { AppContext } from 'contexts/AppContext';
import { CreateChannel } from 'modules/createChannel';
import GLOBALS, { device, globalsMargin } from 'config/Globals';
import { getChannelDetails } from 'queries';

// Constants
// interval after which alias details api will be called, in seconds
Expand Down Expand Up @@ -88,11 +89,13 @@ const ChannelOwnerDashboard = () => {
}, [channelDetails, aliasAddrFromContract]);

const fetchChannelDetails = async (address: string) => {
let { aliasAddress = null, isAliasVerified = null } =
await ChannelsDataStore.getInstance().getChannelDetailsFromAddress(address);
if (aliasAddress == 'NULL') aliasAddress = null;
let { alias_address = null, is_alias_verified = null } = await getChannelDetails({
userPushSDKInstance,
address: address,
}).then((response) => response);
if (alias_address == 'NULL') alias_address = null;

return { aliasAddress: aliasAddress, aliasVerified: isAliasVerified };
return { aliasAddress: alias_address, aliasVerified: is_alias_verified };
};

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/inbox/InboxModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ const InboxModule = ({ isSpam }) => {
// Push (EPNS) Read Provider Set
if (epnsReadProvider != null && epnsCommReadProvider != null) {
// Instantiate Data Stores
UsersDataStore.getInstance().init(account, epnsReadProvider, epnsCommReadProvider);
ChannelsDataStore.getInstance().init(account, epnsReadProvider, epnsCommReadProvider, chainId);
// UsersDataStore.getInstance().init(account, epnsReadProvider, epnsCommReadProvider);
// ChannelsDataStore.getInstance().init(account, epnsReadProvider, epnsCommReadProvider, chainId);
}
}, [epnsReadProvider, epnsCommReadProvider]);

Expand Down
11 changes: 8 additions & 3 deletions src/primaries/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// React + Web3 Essentials
import { useContext, useState, useRef } from 'react';
import { useContext, useState, useRef, useEffect } from 'react';

// External Packages
import { useSelector } from 'react-redux';
Expand Down Expand Up @@ -27,7 +27,7 @@ import { getPublicAssetPath } from 'helpers/RoutesHelper.js';
const Profile = ({ isDarkMode }: { isDarkMode: boolean }) => {
const { web3NameList, removePGPKeyForUser, initializePushSdkReadMode, setUserProfileUnlocked } =
useContext<AppContextType>(AppContext);
const { setReadOnlyWallet, setMode }: GlobalContextType = useContext(GlobalContext);
const { setReadOnlyWallet, setMode, setGlobalWallet }: GlobalContextType = useContext(GlobalContext);
const { authError } = useContext(ErrorContext);
const toggleArrowRef = useRef(null);
const dropdownRef = useRef(null);
Expand Down Expand Up @@ -100,12 +100,17 @@ const Profile = ({ isDarkMode }: { isDarkMode: boolean }) => {
connect();
};

const isWalletConnected = !!wallet?.accounts?.length;

useEffect(() => {
setGlobalWallet?.(isWalletConnected ? wallet : null);
}, [isWalletConnected]);
// to create blockies
return (
<>
{account && account != '' && !authError ? (
<Body>
{!(wallet?.accounts?.length > 0) ? (
{!isWalletConnected ? (
<Wallet
bg="linear-gradient(87.17deg, #B6A0F5 0%, #F46EF7 57.29%, #FF95D5 100%)"
color="#FFF"
Expand Down

0 comments on commit 2ef95bc

Please sign in to comment.