Files
explore/ui/address/details/AddressFavoriteButton.tsx
tom goriunovandGitHub 401dc58402 Smart wallet auth (#3220)
* basic login button implementation

* refactoring and add dynamic auth button to the other pages

* connect wallet without authenticating an user

* login and logout user in blockscout account

* refactoring and RPC overrides

* authenticate user with connected wallet

* add ENS to profile button

* override evm chains and fix contract write with waas wallet

* disconnect wallet and fix docker build

* add dynamic imports to components that use web3 wallet packages

* refactor user profile directory

* clean up

* tweaks

* update pw ci script

* fix "connect wallet" button behavior

* handle linking email to profile with dynamic provider

* fix test

* async loading web3 hooks and remove dynamic loading from components that use them

* fix pw tests

* add account navigation in horizontal layout when auth provider is dynamic

* fix address verification modal

* review fixes

* spelling fixes
2026-02-18 19:42:44 +01:00

98 lines
3.3 KiB
TypeScript

import { chakra } from '@chakra-ui/react';
import { useQueryClient } from '@tanstack/react-query';
import { useRouter } from 'next/router';
import React from 'react';
import config from 'configs/app';
import { getResourceKey } from 'lib/api/useApiQuery';
import usePreventFocusAfterModalClosing from 'lib/hooks/usePreventFocusAfterModalClosing';
import * as mixpanel from 'lib/mixpanel/index';
import { IconButton } from 'toolkit/chakra/icon-button';
import { Tooltip } from 'toolkit/chakra/tooltip';
import { useDisclosure } from 'toolkit/hooks/useDisclosure';
import IconSvg from 'ui/shared/IconSvg';
import AuthGuard from 'ui/snippets/auth/guard/AuthGuard';
import useProfileQuery from 'ui/snippets/auth/useProfileQuery';
import WatchlistAddModal from 'ui/watchlist/AddressModal/AddressModal';
import DeleteAddressModal from 'ui/watchlist/DeleteAddressModal';
interface Props {
className?: string;
hash: string;
watchListId: number | null | undefined;
}
const AddressFavoriteButton = ({ className, hash, watchListId }: Props) => {
const addModalProps = useDisclosure();
const deleteModalProps = useDisclosure();
const queryClient = useQueryClient();
const router = useRouter();
const onFocusCapture = usePreventFocusAfterModalClosing();
const profileQuery = useProfileQuery();
const handleAddToFavorite = React.useCallback(() => {
watchListId ? deleteModalProps.onOpen() : addModalProps.onOpen();
!watchListId && mixpanel.logEvent(mixpanel.EventTypes.PAGE_WIDGET, { Type: 'Add to watchlist' });
}, [ watchListId, deleteModalProps, addModalProps ]);
const handleAddOrDeleteSuccess = React.useCallback(async() => {
const queryKey = getResourceKey('general:address', { pathParams: { hash: router.query.hash?.toString() } });
await queryClient.refetchQueries({ queryKey });
addModalProps.onClose();
}, [ addModalProps, queryClient, router.query.hash ]);
const formData = React.useMemo(() => {
if (typeof watchListId !== 'number') {
return { address_hash: hash };
}
return {
address_hash: hash,
id: watchListId,
};
}, [ hash, watchListId ]);
if (!config.features.account.isEnabled) {
return null;
}
return (
<>
<AuthGuard onAuthSuccess={ handleAddToFavorite }>
{ ({ onClick }) => (
<Tooltip content={ `${ watchListId ? 'Remove address from Watch list' : 'Add address to Watch list' }` } disableOnMobile>
<IconButton
className={ className }
aria-label="edit"
variant="icon_background"
size="md"
selected={ Boolean(watchListId) }
onClick={ onClick }
onFocusCapture={ onFocusCapture }
>
<IconSvg name={ watchListId ? 'star_filled' : 'star_outline' }/>
</IconButton>
</Tooltip>
) }
</AuthGuard>
<WatchlistAddModal
{ ...addModalProps }
isAdd
onSuccess={ handleAddOrDeleteSuccess }
data={ formData }
hasEmail={ Boolean(profileQuery.data?.email) }
showEmailAlert
/>
{ formData.id && (
<DeleteAddressModal
{ ...deleteModalProps }
data={ formData }
onSuccess={ handleAddOrDeleteSuccess }
/>
) }
</>
);
};
export default chakra(AddressFavoriteButton);