Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove hard reloads #4183

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions apps/web/src/components/Common/Layout.tsx
Expand Up @@ -48,23 +48,21 @@ const Layout: FC<LayoutProps> = ({ children }) => {

const { id: sessionProfileId } = getCurrentSession();

const logout = (reload = false) => {
const logout = () => {
resetPreferences();
resetFeatureFlags();
resetPro();
Comment on lines 52 to 54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can move all these inside signout method itself so we can just call signout everywhere!

signOut();
disconnect?.();
if (reload) {
location.reload();
}
setCurrentProfile(null);
};

const { loading } = useCurrentProfileQuery({
onCompleted: ({ profile, userSigNonces }) => {
setCurrentProfile(profile as Profile);
setLensHubOnchainSigNonce(userSigNonces.lensHubOnchainSigNonce);
},
onError: () => logout(true),
onError: () => logout(),
skip: !sessionProfileId || isAddress(sessionProfileId),
variables: { request: { forProfileId: sessionProfileId } }
});
Expand Down
Expand Up @@ -14,11 +14,16 @@ import { isSupported, share } from 'shared-zustand';
import { useNonceStore } from 'src/store/non-persisted/useNonceStore';
import { signOut } from 'src/store/persisted/useAuthStore';
import { useNotificationStore } from 'src/store/persisted/useNotificationStore';
import useProfileStore from 'src/store/persisted/useProfileStore';
import { useUpdateEffect } from 'usehooks-ts';
import { isAddress } from 'viem';
import { useAccount } from 'wagmi';
import { useAccount, useDisconnect } from 'wagmi';

const LensSubscriptionsProvider: FC = () => {
const { disconnect } = useDisconnect();

const setCurrentProfile = useProfileStore((state) => state.setCurrentProfile);

const setLatestNotificationId = useNotificationStore(
(state) => state.setLatestNotificationId
);
Expand Down Expand Up @@ -84,7 +89,8 @@ const LensSubscriptionsProvider: FC = () => {
// Using not null assertion because api returns null if revoked
if (!authorizationRecordRevoked) {
signOut();
location.reload();
disconnect?.();
setCurrentProfile(null);
}
}, [authorizationRecordRevokedData]);
// End: Authorization Record Revoked
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Shared/GlobalModals.tsx
Expand Up @@ -141,7 +141,7 @@ const GlobalModals: FC = () => {
show={showAuthModal}
title="Login"
>
<Login />
<Login onClose={setShowAuthModal} />
</Modal>
<Modal
onClose={() => setShowWrongNetworkModal(false)}
Expand Down
13 changes: 11 additions & 2 deletions apps/web/src/components/Shared/Login/WalletSelector.tsx
Expand Up @@ -30,6 +30,7 @@ import { useState } from 'react';
import toast from 'react-hot-toast';
import { CHAIN_ID } from 'src/constants';
import { signIn } from 'src/store/persisted/useAuthStore';
import useProfileStore from 'src/store/persisted/useProfileStore';
import { useIsMounted } from 'usehooks-ts';
import {
useAccount,
Expand All @@ -42,14 +43,18 @@ import {
import UserProfile from '../UserProfile';

interface WalletSelectorProps {
onClose?: (value: boolean) => void;
setHasConnected?: Dispatch<SetStateAction<boolean>>;
setShowSignup?: Dispatch<SetStateAction<boolean>>;
}

const WalletSelector: FC<WalletSelectorProps> = ({
onClose,
setHasConnected,
setShowSignup
}) => {
const setCurrentProfile = useProfileStore((state) => state.setCurrentProfile);

const [isLoading, setIsLoading] = useState(false);
const [loggingInProfileId, setLoggingInProfileId] = useState<null | string>(
null
Expand Down Expand Up @@ -90,6 +95,8 @@ const WalletSelector: FC<WalletSelectorProps> = ({
}
});

const allProfiles = profilesManaged?.profilesManaged.items || [];

const onConnect = async (connector: Connector) => {
try {
const account = await connectAsync({ connector });
Expand Down Expand Up @@ -129,12 +136,14 @@ const WalletSelector: FC<WalletSelectorProps> = ({
const accessToken = auth.data?.authenticate.accessToken;
const refreshToken = auth.data?.authenticate.refreshToken;
signIn({ accessToken, refreshToken });
setCurrentProfile(
allProfiles.find((p) => p.id === loggingInProfileId) as Profile
);
Leafwatch.track(AUTH.SIWL);
location.reload();
onClose?.(false);
} catch {}
};

const allProfiles = profilesManaged?.profilesManaged.items || [];
const lastLogin = profilesManaged?.lastLoggedInProfile;

const remainingProfiles = lastLogin
Expand Down
7 changes: 6 additions & 1 deletion apps/web/src/components/Shared/Login/index.tsx
Expand Up @@ -5,7 +5,11 @@ import WalletSelector from '@components/Shared/Login/WalletSelector';
import { APP_NAME } from '@hey/data/constants';
import { useState } from 'react';

const Login: FC = () => {
interface LoginProps {
onClose: (value: boolean) => void;
}

const Login: FC<LoginProps> = ({ onClose }) => {
const [hasConnected, setHasConnected] = useState(false);
const [showSignup, setShowSignup] = useState(false);

Expand Down Expand Up @@ -41,6 +45,7 @@ const Login: FC = () => {
</div>
)}
<WalletSelector
onClose={onClose}
setHasConnected={setHasConnected}
setShowSignup={setShowSignup}
/>
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/components/Shared/Navbar/MenuItems.tsx
Expand Up @@ -18,6 +18,8 @@ export const NextLink = ({ children, href, ...rest }: Record<string, any>) => (
const MenuItems: FC = () => {
const currentProfile = useProfileStore((state) => state.currentProfile);
const { id: sessionProfileId } = getCurrentSession();
console.log(currentProfile, 'currentProfile');
console.log(sessionProfileId, 'sessionProfileId');
Comment on lines +21 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove it


if (currentProfile) {
return <SignedUser />;
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/components/Shared/Navbar/NavItems/Logout.tsx
Expand Up @@ -10,6 +10,7 @@ import { Leafwatch } from '@lib/leafwatch';
import { useState } from 'react';
import { usePreferencesStore } from 'src/store/non-persisted/usePreferencesStore';
import { signOut } from 'src/store/persisted/useAuthStore';
import useProfileStore from 'src/store/persisted/useProfileStore';
import { useDisconnect } from 'wagmi';

interface LogoutProps {
Expand All @@ -30,14 +31,15 @@ const Logout: FC<LogoutProps> = ({ className = '', onClick }) => {
setRevoking(false);
errorToast(error);
};
const setCurrentProfile = useProfileStore((state) => state.setCurrentProfile);

const [revokeAuthentication] = useRevokeAuthenticationMutation({
onCompleted: () => {
Leafwatch.track(PROFILE.LOGOUT);
resetPreferences();
signOut();
disconnect?.();
location.reload();
setCurrentProfile(null);
},
onError
});
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/components/Shared/SwitchProfiles.tsx
Expand Up @@ -32,6 +32,8 @@ import { useAccount, useSignMessage } from 'wagmi';
import Loader from './Loader';

const SwitchProfiles: FC = () => {
const setCurrentProfile = useProfileStore((state) => state.setCurrentProfile);

const currentProfile = useProfileStore((state) => state.currentProfile);
const setShowProfileSwitchModal = useGlobalModalStateStore(
(state) => state.setShowProfileSwitchModal
Expand Down Expand Up @@ -96,7 +98,7 @@ const SwitchProfiles: FC = () => {
signOut();
signIn({ accessToken, refreshToken });
Leafwatch.track(PROFILE.SWITCH_PROFILE, { switch_profile_to: id });
location.reload();
setCurrentProfile(null);
} catch (error) {
onError(error);
}
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/store/persisted/useAuthStore.ts
Expand Up @@ -30,6 +30,7 @@ export const useAuthStore = create(
signIn: ({ accessToken, refreshToken }) =>
set({ accessToken, refreshToken }),
signOut: async () => {
set({ accessToken: null, refreshToken: null });
// Clear Localstorage
const allLocalstorageStores = Object.values(Localstorage).filter(
(value) => value !== Localstorage.LeafwatchStore
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/getStampFyiURL.ts
Expand Up @@ -7,6 +7,10 @@ import urlcat from 'urlcat';
* @returns The cdn.stamp.fyi URL.
*/
const getStampFyiURL = (address: string): string => {
// race condition fix when loging out
if (!address) {
return '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can return stamp url with null address aka export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';

}
const lowerCaseAddress = address.toLowerCase();
return urlcat('https://cdn.stamp.fyi/avatar/eth::address', {
address: lowerCaseAddress,
Expand Down