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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/web/src/components/Common/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import getCurrentSession from '@lib/getCurrentSession';
import getToastOptions from '@lib/getToastOptions';
import { useTheme } from 'next-themes';
import Head from 'next/head';
import { useRouter } from 'next/router';
import { Toaster } from 'react-hot-toast';
import { useNonceStore } from 'src/store/non-persisted/useNonceStore';
import { usePreferencesStore } from 'src/store/non-persisted/usePreferencesStore';
Expand All @@ -29,6 +30,7 @@ interface LayoutProps {
}

const Layout: FC<LayoutProps> = ({ children }) => {
const router = useRouter();
const { resolvedTheme } = useTheme();
const currentProfile = useProfileStore((state) => state.currentProfile);
const setCurrentProfile = useProfileStore((state) => state.setCurrentProfile);
Expand All @@ -55,7 +57,7 @@ const Layout: FC<LayoutProps> = ({ children }) => {
signOut();
disconnect?.();
if (reload) {
location.reload();
router.push('/');
neo773 marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ import {
import { BrowserPush } from '@lib/browserPush';
import getCurrentSession from '@lib/getCurrentSession';
import getPushNotificationData from '@lib/getPushNotificationData';
import { useRouter } from 'next/router';
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 { useUpdateEffect } from 'usehooks-ts';
import { isAddress } from 'viem';
import { useAccount } from 'wagmi';
import { useAccount, useDisconnect } from 'wagmi';

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

const router = useRouter();

const setLatestNotificationId = useNotificationStore(
(state) => state.setLatestNotificationId
);
Expand Down Expand Up @@ -84,7 +89,10 @@ const LensSubscriptionsProvider: FC = () => {
// Using not null assertion because api returns null if revoked
if (!authorizationRecordRevoked) {
signOut();
location.reload();
disconnect?.();
setTimeout(() => {
router.push('/');
neo773 marked this conversation as resolved.
Show resolved Hide resolved
}, 200);
}
}, [authorizationRecordRevokedData]);
// End: Authorization Record Revoked
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Shared/GlobalModals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const GlobalModals: FC = () => {
show={showAuthModal}
title="Login"
>
<Login />
<Login onClose={setShowAuthModal} />
</Modal>
<Modal
onClose={() => setShowWrongNetworkModal(false)}
Expand Down
7 changes: 6 additions & 1 deletion apps/web/src/components/Shared/Login/WalletSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Button, Card, Spinner } from '@hey/ui';
import cn from '@hey/ui/cn';
import errorToast from '@lib/errorToast';
import { Leafwatch } from '@lib/leafwatch';
import { useRouter } from 'next/router';
import { useState } from 'react';
import toast from 'react-hot-toast';
import { CHAIN_ID } from 'src/constants';
Expand All @@ -42,11 +43,13 @@ 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
}) => {
Expand All @@ -69,6 +72,7 @@ const WalletSelector: FC<WalletSelectorProps> = ({
isLoading: isConnectLoading,
pendingConnector
} = useConnect({ chainId: CHAIN_ID });
const router = useRouter();

const { disconnect } = useDisconnect();
const { address, connector: activeConnector } = useAccount();
Expand Down Expand Up @@ -130,7 +134,8 @@ const WalletSelector: FC<WalletSelectorProps> = ({
const refreshToken = auth.data?.authenticate.refreshToken;
signIn({ accessToken, refreshToken });
Leafwatch.track(AUTH.SIWL);
location.reload();
onClose?.(false);
router.push('/');
neo773 marked this conversation as resolved.
Show resolved Hide resolved
} catch {}
};

Expand Down
7 changes: 6 additions & 1 deletion apps/web/src/components/Shared/Login/index.tsx
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions apps/web/src/components/Shared/Navbar/MenuItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const MenuItems: FC = () => {
const currentProfile = useProfileStore((state) => state.currentProfile);
const { id: sessionProfileId } = getCurrentSession();

if (Boolean(currentProfile)) {
if (currentProfile !== null) {
neo773 marked this conversation as resolved.
Show resolved Hide resolved
return <SignedUser />;
}

// If the currentSessionProfileId is a valid eth address, we can assume that address don't have a profile yet
if (isAddress(sessionProfileId)) {
if (sessionProfileId !== null && isAddress(sessionProfileId)) {
neo773 marked this conversation as resolved.
Show resolved Hide resolved
return <WalletUser />;
}

Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/components/Shared/Navbar/NavItems/Logout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cn from '@hey/ui/cn';
import errorToast from '@lib/errorToast';
import getCurrentSession from '@lib/getCurrentSession';
import { Leafwatch } from '@lib/leafwatch';
import { useRouter } from 'next/router';
import { useState } from 'react';
import { usePreferencesStore } from 'src/store/non-persisted/usePreferencesStore';
import { signOut } from 'src/store/persisted/useAuthStore';
Expand All @@ -22,6 +23,7 @@ const Logout: FC<LogoutProps> = ({ className = '', onClick }) => {
(state) => state.resetPreferences
);
const [revoking, setRevoking] = useState(false);
const router = useRouter();

const { disconnect } = useDisconnect();
const { authorizationId } = getCurrentSession();
Expand All @@ -37,7 +39,6 @@ const Logout: FC<LogoutProps> = ({ className = '', onClick }) => {
resetPreferences();
signOut();
disconnect?.();
location.reload();
},
onError
});
Expand Down Expand Up @@ -65,6 +66,7 @@ const Logout: FC<LogoutProps> = ({ className = '', onClick }) => {
onClick={async () => {
await logout();
onClick?.();
router.push('/');
}}
type="button"
>
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/components/Shared/SwitchProfiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import cn from '@hey/ui/cn';
import errorToast from '@lib/errorToast';
import { Leafwatch } from '@lib/leafwatch';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { useState } from 'react';
import toast from 'react-hot-toast';
import { useGlobalModalStateStore } from 'src/store/non-persisted/useGlobalModalStateStore';
Expand All @@ -46,6 +47,8 @@ const SwitchProfiles: FC = () => {
errorToast(error);
};

const router = useRouter();

const { address } = useAccount();
const { signMessageAsync } = useSignMessage({ onError });

Expand Down Expand Up @@ -96,7 +99,7 @@ const SwitchProfiles: FC = () => {
signOut();
signIn({ accessToken, refreshToken });
Leafwatch.track(PROFILE.SWITCH_PROFILE, { switch_profile_to: id });
location.reload();
router.push('/');
} catch (error) {
onError(error);
}
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/store/persisted/useAuthStore.ts
Original file line number Diff line number Diff line change
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
Loading