Skip to content

Commit

Permalink
feat: adding logic for connect wallet and create note account when us…
Browse files Browse the repository at this point in the history
…er click on the Account tab
  • Loading branch information
AtelyPham committed Nov 2, 2023
1 parent 4d34cdf commit 8444041
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 12 deletions.
31 changes: 26 additions & 5 deletions apps/bridge-dapp/src/hooks/useSidebarProps.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { useWebContext } from '@webb-tools/api-provider-environment';
import BillFillIcon from '@webb-tools/icons/BillFillIcon';
import { useNoteAccount } from '@webb-tools/react-hooks/useNoteAccount';
import type { SideBarItemProps } from '@webb-tools/webb-ui-components/components/SideBar/types';
import type { EventFor } from '@webb-tools/webb-ui-components/types';
import { useLocation } from 'react-router';
import sidebar from '../constants/sidebar';
import useChainsFromRoute from './useChainsFromRoute';
import { useConnectWallet } from './useConnectWallet';

const accountItemCfg = {
name: 'Account',
Expand All @@ -17,15 +21,32 @@ const accountItemCfg = {
* **Must be used inside the `WebbProvider` component**.
*/
function useSidebarProps() {
const { noteManager } = useWebContext();
const { pathname } = useLocation();
const { srcTypedChainId } = useChainsFromRoute();

const { activeWallet, noteManager } = useWebContext();
const { hasNoteAccount, setOpenNoteAccountModal } = useNoteAccount();
const { toggleModal } = useConnectWallet();

const handleClick = (event: EventFor<'a', 'onClick'>) => {
event.preventDefault();

if (typeof srcTypedChainId !== 'number') {
return;
}

if (!activeWallet) {
return toggleModal(true, srcTypedChainId);
}

if (!hasNoteAccount) {
setOpenNoteAccountModal(true);
}
};

const accountItem = {
...accountItemCfg,
info: noteManager?.getKeypair()
? undefined
: 'Connect your wallet and create a note account to access this feature.',
isDisabled: !noteManager?.getKeypair(),
onClick: noteManager && activeWallet ? undefined : handleClick,
} satisfies SideBarItemProps;

sidebar.items = [
Expand Down
2 changes: 2 additions & 0 deletions libs/webb-ui-components/src/components/SideBar/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const SideBarItem: FC<SideBarItemProps & SideBarExtraItemProps> = ({
setIsActive,
isDisabled,
pathnameOrHash,
onClick,
info,
}) => {
const [isMounted, setIsMounted] = useState(false);
Expand Down Expand Up @@ -75,6 +76,7 @@ const SideBarItem: FC<SideBarItemProps & SideBarExtraItemProps> = ({
isNext,
isDisabled,
hasSubItem: subItems.length > 0,
onClick,
});

if (!isMounted) return null;
Expand Down
11 changes: 7 additions & 4 deletions libs/webb-ui-components/src/components/SideBar/SubItem.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ExternalLinkLine } from '@webb-tools/icons';
import cx from 'classnames';
import React, { useMemo } from 'react';
import isSideBarItemActive from '../../utils/isSideBarItemActive';
import type { EventFor } from '../../types';
import { Typography } from '../../typography/Typography';
import isSideBarItemActive from '../../utils/isSideBarItemActive';
import { Link } from '../Link';
import cx from 'classnames';
import WithInfo from './WithInfo';
import { SideBarExtraSubItemProps, SideBarSubItemProps } from './types';
import useLinkProps from './useLinkProps';
import WithInfo from './WithInfo';

export const SubItem: React.FC<
SideBarSubItemProps & SideBarExtraSubItemProps
Expand All @@ -20,11 +21,13 @@ export const SubItem: React.FC<
setItemIsActive,
setSubItemIsActive,
info,
onClick,
pathnameOrHash,
}) => {
const linkProps = useLinkProps({ href, isInternal, isNext, isDisabled });

const setIsActive = () => {
const setIsActive = (event: EventFor<'a', 'onClick'>) => {
onClick?.(event);
if (setItemIsActive && setSubItemIsActive && isInternal) {
setItemIsActive();
setSubItemIsActive();
Expand Down
7 changes: 7 additions & 0 deletions libs/webb-ui-components/src/components/SideBar/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { IconBase } from '@webb-tools/icons/types';
import type { LogoProps } from '../Logo/types';
import type { DialogContentProps } from '@radix-ui/react-dialog';
import { MouseEventHandler } from 'react';

export type SideBarFooterType = {
name: string;
Expand Down Expand Up @@ -55,6 +56,9 @@ export type SideBarItemProps = {
/** The extra info tooltip for the item */
info?: string | React.ReactElement;

/** The callback function when the item is clicked */
onClick?: MouseEventHandler<HTMLAnchorElement>;

pathnameOrHash?: string;

/** The item sub items */
Expand All @@ -81,6 +85,9 @@ export type SideBarSubItemProps = {
/** The extra info tooltip for the item */
info?: string | React.ReactElement;

/** The callback function when the item is clicked */
onClick?: MouseEventHandler<HTMLAnchorElement>;

pathnameOrHash?: string;
};

Expand Down
12 changes: 9 additions & 3 deletions libs/webb-ui-components/src/components/SideBar/useLinkProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import type { SideBarExtraItemProps, SideBarItemProps } from './types';
type Props = SideBarItemProps & SideBarExtraItemProps;

function useLinkProps(
args: Pick<Props, 'isInternal' | 'href' | 'isNext' | 'isDisabled'> & {
args: Pick<
Props,
'isInternal' | 'href' | 'isNext' | 'isDisabled' | 'onClick'
> & {
hasSubItem?: boolean;
}
) {
const { isInternal, isNext, href, isDisabled, hasSubItem } = args;
const { isInternal, isNext, href, isDisabled, hasSubItem, onClick } = args;

return useMemo(() => {
if (isDisabled || hasSubItem) {
Expand All @@ -22,25 +25,28 @@ function useLinkProps(
return {
href,
target: '_blank',
onClick,
} as const satisfies PropsOf<'a'>;
}

if (isInternal && isNext) {
return {
href,
isInternal,
onClick,
} as const satisfies PropsOf<typeof NextLink> & { isInternal: true };
}

if (isInternal && !isNext) {
return {
to: href,
isInternal,
onClick,
} as const satisfies PropsOf<typeof ReactLink> & { isInternal: true };
}

return {};
}, [hasSubItem, href, isDisabled, isInternal, isNext]);
}, [hasSubItem, href, isDisabled, isInternal, isNext, onClick]);
}

export default useLinkProps;
21 changes: 21 additions & 0 deletions libs/webb-ui-components/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,24 @@ export type ArrayElement<ArrayType extends readonly unknown[]> =

export type NonNullableArrayItem<T extends Array<NullableUnknownType>> =
NonNullable<ArrayElement<T>>;

type GetEventHandlers<T extends keyof JSX.IntrinsicElements> = Extract<
keyof JSX.IntrinsicElements[T],
`on${string}`
>;

/**
* Provides the event type for a given element and handler.
*
* @example
*
* type MyEvent = EventFor<"input", "onChange">;
*/
export type EventFor<
TElement extends keyof JSX.IntrinsicElements,
THandler extends GetEventHandlers<TElement>
> = JSX.IntrinsicElements[TElement][THandler] extends
| ((e: infer TEvent) => any)
| undefined
? TEvent
: never;

0 comments on commit 8444041

Please sign in to comment.