Skip to content

Commit

Permalink
refactor: replace useSession with useSelector for session handling
Browse files Browse the repository at this point in the history
- Updated `src/sections/publication/publication-new-wizard.tsx` to use `useSelector` for session retrieval.
- Modified `src/sections/user/profile-header.tsx`, replacing `useSession` with `useSelector`.
- Refactored session fetching in `src/components/poster/variants/poster-top-titles.tsx` using `useSelector`.
- Updated `src/hooks/*` files to consistently utilize `useSelector` instead of `useSession`.
- Standardized session handling across all relevant components and sections to remove redundancy.
  • Loading branch information
cswni committed Dec 18, 2024
1 parent 327ac9b commit c00e73d
Show file tree
Hide file tree
Showing 23 changed files with 49 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/components/follow-unfollow-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useLazyProfile } from '@lens-protocol/react';

// REDUX IMPORTS
import { openLoginModal } from '@redux/auth';
import { useDispatch } from 'react-redux';
import {useDispatch, useSelector} from 'react-redux';
import { removeFollowing, addFollowing } from '@redux/followers';

// NOTIFICATIONS IMPORTS
Expand All @@ -42,7 +42,7 @@ const FollowUnfollowButton = ({ profileId, size = 'medium', followButtonMinWidth
const dispatch = useDispatch();
const { enqueueSnackbar } = useSnackbar();
const { data: profile, execute: getProfile } = useLazyProfile();
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const [isFollowed, setIsFollowed] = useState(profile?.operations?.isFollowedByMe?.value);

// State to handle error and success messages
Expand Down
3 changes: 2 additions & 1 deletion src/components/login-modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ProfileSelectView } from '@src/components/login-modal/profile-select-vi
import { ProfileFormView } from '@src/components/login-modal/profile-form-view.tsx';
import { WatchitLoader } from '../watchit-loader';
import { useSnackbar } from 'notistack';
import {useSelector} from "react-redux";

// ----------------------------------------------------------------------

Expand All @@ -29,7 +30,7 @@ export const LoginModal: React.FC<LoginModalProps> = ({ open, onClose }) => {
const [address, setAddress] = useState('');

const { web3Auth: w3 } = useWeb3Auth();
const { data: sessionData } = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const { execute: logoutExecute } = useLogout();
const { execute: loginExecute, error } = useLogin();
const { enqueueSnackbar } = useSnackbar();
Expand Down
2 changes: 1 addition & 1 deletion src/components/login-modal/profile-form-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const ProfileFormView: React.FC<ProfileFormProps> = ({
profile: Profile;
} | null>(null);

const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
// Create profile and set profile metadata functions from Lens Protocol
const {
execute: createProfileExecute,
Expand Down
10 changes: 5 additions & 5 deletions src/components/login-modal/profile-select-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { Box, Typography, List, Button, Avatar } from '@mui/material';

// UTILS IMPORTS
import { truncateAddress } from '@src/utils/wallet';
import { Profile, useSession, useLazyProfiles, LoginError } from '@lens-protocol/react-web';
import { Profile, useLazyProfiles, LoginError } from '@lens-protocol/react-web';
// @ts-ignore
import Alert from '@mui/material/Alert';

import { useDispatch } from 'react-redux';
import {useDispatch, useSelector} from 'react-redux';
import { setAuthLoading, setBalance } from '@redux/auth';
import { useResponsive } from '@src/hooks/use-responsive.ts';
import { UserItem } from '../user-item';
Expand Down Expand Up @@ -42,10 +42,10 @@ export const ProfileSelectView: React.FC<ProfileSelectionProps> = ({

const [profiles, setProfiles] = useState([] as Profile[]);
const { execute: getProfiles } = useLazyProfiles();
const { data: sessionData } = useSession();
const sessionData = useSelector((state: any) => state.auth.session);

useEffect(() => {
if (!error) dispatch(setAuthLoading({ isAuthLoading: false }));
if (!error) dispatch(setAuthLoading({ isSessionLoading: false }));
if (error) enqueueSnackbar(error.message, { variant: 'error' });
}, [error]);

Expand All @@ -65,7 +65,7 @@ export const ProfileSelectView: React.FC<ProfileSelectionProps> = ({
dispatch(setBalance({ balance: 0 }));

onClose();
dispatch(setAuthLoading({ isAuthLoading: true }));
dispatch(setAuthLoading({ isSessionLoading: true }));
await login(profile);
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/components/poster/variants/poster-top-titles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import { IconBookmark, IconBookmarkFilled, IconPlayerPlay } from '@tabler/icons-
import Box from '@mui/material/Box';
import TextMaxLine from '@src/components/text-max-line';
import { CircularProgress } from '@mui/material';
import { ProfileSession, useBookmarkToggle, useSession } from '@lens-protocol/react-web';
import { useBookmarkToggle} from '@lens-protocol/react-web';
import { openLoginModal } from '@redux/auth';
// @ts-ignore
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';
import { useDispatch } from 'react-redux';
import {useDispatch, useSelector} from 'react-redux';
import { addBookmark, removeBookmark } from '@redux/bookmark';

// ----------------------------------------------------------------------

const PosterTopTitles = ({ post }: { post: any }) => {
const router = useRouter();
const { execute: toggleBookMarkFunction, loading: loadingBookMark } = useBookmarkToggle();
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const dispatch = useDispatch();

const handlePosterClick = () => {
Expand Down
6 changes: 2 additions & 4 deletions src/components/publication-detail-main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import {
hasReacted,
useReactionToggle,
useBookmarkToggle,
ProfileSession,
useSession,
} from '@lens-protocol/react-web';
import { useHidePublication } from '@lens-protocol/react';

Expand Down Expand Up @@ -58,7 +56,7 @@ import Popover from '@mui/material/Popover';
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';
import {useNotifications} from "@src/hooks/use-notifications.ts";
import { openLoginModal } from '@redux/auth';
import { useDispatch } from 'react-redux';
import {useDispatch, useSelector} from 'react-redux';
import { addBookmark, removeBookmark } from '@redux/bookmark';
import { useNotificationPayload } from '@src/hooks/use-notification-payload.ts';

Expand Down Expand Up @@ -93,7 +91,7 @@ export default function PublicationDetailMain({
// LOCAL HOOKS
const router = useRouter();
const theme = useTheme();
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const dispatch = useDispatch();

// LENS HOOKS
Expand Down
2 changes: 1 addition & 1 deletion src/components/subscribe-profile-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const SubscribeProfileModal = ({
const [errorMessage, setErrorMessage] = useState('');

// Hook to get the user's session data
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const { balance: balanceFromContract, refetch } = useGetBalance(sessionData?.address);

// Hooks for subscription and terms resolution
Expand Down
5 changes: 3 additions & 2 deletions src/components/update-modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Modal, Box, Fade, Backdrop } from '@mui/material';
import { ProfileFormView } from '@src/components/login-modal/profile-form-view.tsx';
// @ts-ignore
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';
import { ProfileSession, useSession } from '@lens-protocol/react-web';
import { useSnackbar } from 'notistack';
import {useSelector} from "react-redux";

// ----------------------------------------------------------------------

Expand All @@ -21,7 +21,8 @@ interface UpdateModalProps {
// ----------------------------------------------------------------------

export const UpdateModal: React.FC<UpdateModalProps> = ({ open, onClose }) => {
const { data: sessionData, loading }: ReadResult<ProfileSession> = useSession();
const loading = useSelector((state: any) => state.auth.isSessionLoading);
const sessionData = useSelector((state: any) => state.auth.session);
const { enqueueSnackbar } = useSnackbar();

const handleProfileUpdateSuccess = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/user-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import ListItemText from '@mui/material/ListItemText';

// LENS IMPORTS
import { Profile } from '@lens-protocol/api-bindings';
import { ProfileSession, useSession } from '@lens-protocol/react-web';
// @ts-ignore
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';

Expand All @@ -17,6 +16,7 @@ import Image from '../image';
import { paths } from '../../routes/paths';
import { useRouter } from '@src/routes/hooks';
import FollowUnfollowButton from '@src/components/follow-unfollow-button.tsx';
import {useSelector} from "react-redux";

// ----------------------------------------------------------------------

Expand All @@ -38,7 +38,7 @@ export const UserItem = ({
onClick,
canFollow = true,
}: FollowerItemProps) => {
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const router = useRouter();

const goToProfile = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/use-authorize-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import RightsPolicyAuthorizerAbi from '@src/config/abi/RightsPolicyAuthorizer.js
import { GLOBAL_CONSTANTS } from '@src/config-global.ts';
// @ts-ignore
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';
import { ProfileSession, useSession } from '@lens-protocol/react-web';
import {useSelector} from "react-redux";

// ----------------------------------------------------------------------

Expand Down Expand Up @@ -41,7 +41,7 @@ export const useAuthorizePolicy = (): useAuthorizePolicyHook => {
const [data, setData] = useState();
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<AuthorizeError | null>(null);
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const { web3Auth } = useWeb3Auth();

/**
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/use-get-attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SubscriptionPolicyAbi from '@src/config/abi/SubscriptionPolicy.json';
import { ProfileSession, useSession } from '@lens-protocol/react-web';
// @ts-ignore
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';
import {useSelector} from "react-redux";

interface AttestationError {
message: string;
Expand All @@ -30,7 +31,7 @@ interface UseGetAttestationHook {
* @returns An object containing the attestation data, loading state, error, and a refetch function.
*/
export function useGetAttestation(recipient: Address, holder?: Address): UseGetAttestationHook {
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const userAddress = sessionData?.profile?.ownedBy?.address as Address | undefined;

const [attestation, setAttestation] = useState<string | undefined>(undefined);
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/use-has-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { Address } from 'viem';
import { publicClient } from '@src/clients/viem/publicClient';
import SubscriptionPolicyAbi from '@src/config/abi/SubscriptionPolicy.json';
import { GLOBAL_CONSTANTS } from '@src/config-global.ts';
// LENS IMPORTS
import { ProfileSession, useSession } from '@lens-protocol/react-web';
// @ts-ignore
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';
import {useSelector} from "react-redux";

interface HasAccessError {
message: string;
Expand All @@ -28,7 +27,7 @@ interface UseHasAccessHook {
* @returns An object containing the access data, loading state, error, and a refetch function.
*/
export const useHasAccess = (ownerAddress?: Address): UseHasAccessHook => {
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const userAddress = sessionData?.profile?.ownedBy?.address as Address | undefined;

const [hasAccess, setHasAccess] = useState<boolean | undefined>(undefined);
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/use-is-policy-authorized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { GLOBAL_CONSTANTS } from '@src/config-global.ts';
import { ProfileSession, useSession } from '@lens-protocol/react-web';
// @ts-ignore
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';
import {useSelector} from "react-redux";

interface HasAccessError {
message: string;
Expand All @@ -31,7 +32,7 @@ export const useIsPolicyAuthorized = (
policy: Address,
holder?: Address
): UseIsPolicyAuthorizedHook => {
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const userAddress = sessionData?.profile?.ownedBy?.address as Address | undefined;

const [isAuthorized, setIsAuthorized] = useState<boolean | undefined>(undefined);
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/use-subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import AgreementPortalAbi from '@src/config/abi/AgreementPortal.json';
import { GLOBAL_CONSTANTS } from '@src/config-global.ts';
// @ts-ignore
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';
import { ProfileSession, useSession } from '@lens-protocol/react-web';
import {useSelector} from "react-redux";

// ----------------------------------------------------------------------

Expand Down Expand Up @@ -50,7 +50,7 @@ export const useSubscribe = (): UseSubscribeHook => {
const [data, setData] = useState<SubscribeData>();
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<SubscribeError | null>(null);
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const { web3Auth } = useWeb3Auth();

/**
Expand Down
5 changes: 3 additions & 2 deletions src/layouts/_common/account-popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ export default function AccountPopover() {
dispatch(setAuthLoading({ isSessionLoading: loading }))
}, [loading]);

const parsedSessionData = JSON.stringify(sessionData);

useEffect(() => {
console.log('sessionData', sessionData)
dispatch(setSession({ session: sessionData }))
}, [JSON.stringify(sessionData)]);
}, [parsedSessionData]);

useEffect(() => {
popover.onClose();
Expand Down
3 changes: 1 addition & 2 deletions src/layouts/_common/header-balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Stack from '@mui/material/Stack';
import { IconCoinMonero } from '@tabler/icons-react';
// @ts-ignore
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';
import { ProfileSession, useSession } from '@lens-protocol/react-web';
import { useDispatch, useSelector } from 'react-redux';
import { useEffect } from 'react';
import { setBalance } from '@redux/auth';
Expand All @@ -19,7 +18,7 @@ import { useGetBalance } from '@src/hooks/use-get-balance.ts';
export default function HeaderBalance() {
const dispatch = useDispatch();
const { balance: balanceFromRedux } = useSelector((state: any) => state.auth);
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const { balance: balanceFromContract } = useGetBalance(sessionData?.address);

useEffect(() => {
Expand Down
3 changes: 1 addition & 2 deletions src/layouts/dashboard/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import { AccountPopover, HeaderBalance, NotificationsPopover } from '../_common'
import { PropsWithChildren } from 'react';
// @ts-ignore
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';
import { ProfileSession, useSession } from '@lens-protocol/react-web';

import { useDispatch, useSelector } from 'react-redux';
import { toggleDrawer } from '@redux/drawer';
// ----------------------------------------------------------------------

export default function Header({ children }: PropsWithChildren) {
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);

const theme = useTheme();

Expand Down
5 changes: 2 additions & 3 deletions src/routes/sections/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { dashboardRoutes } from './dashboard';
import NotFoundPage from '../../pages/404';
import {useEffect} from "react";
import { subscribeToNotifications } from '@src/utils/subscribe-notifications-supabase.ts';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
// @ts-ignore
import {ReadResult} from "@lens-protocol/react/dist/declarations/src/helpers/reads";
import {ProfileSession, useSession} from "@lens-protocol/react-web";
import {useNotifications} from "@src/hooks/use-notifications.ts";

export default function Router() {
const dispatch = useDispatch();
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const { getNotifications } = useNotifications();
useEffect(() => {
if (sessionData?.profile?.id) {
Expand Down
2 changes: 1 addition & 1 deletion src/sections/publication/publication-comment-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default function PublicationCommentItem({ comment, hasReply, canReply }:
);
const { execute: hide } = useHidePublication();
const [showComments, setShowComments] = useState(false);
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const dispatch = useDispatch();
const { sendNotification } = useNotifications();
const { generatePayload } = useNotificationPayload(sessionData);
Expand Down
6 changes: 3 additions & 3 deletions src/sections/publication/publication-details-comment-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useForm, Controller } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import { Stack, CircularProgress } from '@mui/material';
import LoadingButton from '@mui/lab/LoadingButton';
import { ProfileSession, useCreateComment, useSession } from '@lens-protocol/react-web';
import { useCreateComment } from '@lens-protocol/react-web';
import {
textOnly,
PublicationMetadataSchema,
Expand All @@ -21,7 +21,7 @@ import Iconify from '@src/components/iconify';
import { ReadResult } from '@lens-protocol/react/dist/declarations/src/helpers/reads';
import { uploadMetadataToIPFS, verifyIpfsData } from '@src/utils/ipfs';
import uuidv4 from '@src/utils/uuidv4.ts';
import { useDispatch } from 'react-redux';
import {useDispatch, useSelector} from 'react-redux';
import {
refetchCommentsByPublication,
addPendingComment,
Expand Down Expand Up @@ -72,7 +72,7 @@ const MovieCommentForm = ({ commentOn, owner, root }: MovieCommentFormProps) =>
} = methods;

const { execute: createComment } = useCreateComment();
const { data: sessionData }: ReadResult<ProfileSession> = useSession();
const sessionData = useSelector((state: any) => state.auth.session);
const dispatch = useDispatch();
const { sendNotification } = useNotifications();
const { generatePayload } = useNotificationPayload(sessionData);
Expand Down
Loading

0 comments on commit c00e73d

Please sign in to comment.