Commit 1a0e35a0 authored by Max Alekseenko's avatar Max Alekseenko

redesigned nav link

parent 4f8801c7
...@@ -2,10 +2,6 @@ import React from 'react'; ...@@ -2,10 +2,6 @@ import React from 'react';
import type { NavItem } from 'types/client/navigation'; import type { NavItem } from 'types/client/navigation';
import { route } from 'nextjs-routes';
import { isInternalItem } from 'lib/hooks/useNavItems';
import NavLinkBase from './NavLinkBase'; import NavLinkBase from './NavLinkBase';
type Props = { type Props = {
...@@ -14,19 +10,12 @@ type Props = { ...@@ -14,19 +10,12 @@ type Props = {
onClick?: () => void; onClick?: () => void;
} }
const NavLink = ({ item, isCollapsed, onClick }: Props) => { const NavLink = ({ item, isCollapsed, onClick }: Props) => (
const isInternalLink = isInternalItem(item); <NavLinkBase
return ( item={ item }
<NavLinkBase onClick={ onClick }
item={ item } isCollapsed={ isCollapsed }
nextRoute={ 'nextRoute' in item ? item.nextRoute : undefined } />
onClick={ onClick } );
href={ isInternalLink ? route(item.nextRoute) : item.url }
isActive={ isInternalLink && item.isActive }
isExternal={ !isInternalLink }
isCollapsed={ isCollapsed }
/>
);
};
export default React.memo(NavLink); export default React.memo(NavLink);
...@@ -2,9 +2,12 @@ import { Link, Text, HStack, Tooltip, Box, useBreakpointValue } from '@chakra-ui ...@@ -2,9 +2,12 @@ import { Link, Text, HStack, Tooltip, Box, useBreakpointValue } from '@chakra-ui
import NextLink from 'next/link'; import NextLink from 'next/link';
import React from 'react'; import React from 'react';
import type { NavItem, NavItemExternal, NavItemInternal } from 'types/client/navigation'; import type { NavItem } from 'types/client/navigation';
import { route } from 'nextjs-routes';
import useIsMobile from 'lib/hooks/useIsMobile'; import useIsMobile from 'lib/hooks/useIsMobile';
import { isInternalItem } from 'lib/hooks/useNavItems';
import IconSvg from 'ui/shared/IconSvg'; import IconSvg from 'ui/shared/IconSvg';
import LightningLabel, { LIGHTNING_LABEL_CLASS_NAME } from '../LightningLabel'; import LightningLabel, { LIGHTNING_LABEL_CLASS_NAME } from '../LightningLabel';
...@@ -15,32 +18,29 @@ import { checkRouteHighlight } from '../utils'; ...@@ -15,32 +18,29 @@ import { checkRouteHighlight } from '../utils';
type Props = { type Props = {
item: NavItem; item: NavItem;
nextRoute?: NavItemInternal['nextRoute']; onClick?: (e: React.MouseEvent) => void;
isCollapsed?: boolean; isCollapsed?: boolean;
onClick?: () => void;
as?: 'a' | 'button';
href?: string;
isExternal?: boolean;
isActive?: boolean;
isDisabled?: boolean; isDisabled?: boolean;
} }
const NavLinkBase = ({ item, nextRoute, isCollapsed, onClick, isExternal, as = 'a', href, isActive, isDisabled }: Props) => { const NavLinkBase = ({ item, onClick, isCollapsed, isDisabled }: Props) => {
const isMobile = useIsMobile(); const isMobile = useIsMobile();
const colors = useColors(); const colors = useColors();
const isInternalLink = isInternalItem(item);
const href = isInternalLink ? route(item.nextRoute) : item.url;
const isExpanded = isCollapsed === false; const isExpanded = isCollapsed === false;
const styleProps = useNavLinkStyleProps({ isCollapsed, isExpanded, isActive }); const styleProps = useNavLinkStyleProps({ isCollapsed, isExpanded, isActive: isInternalLink && item.isActive });
const isXLScreen = useBreakpointValue({ base: false, xl: true }); const isXLScreen = useBreakpointValue({ base: false, xl: true });
const isHighlighted = checkRouteHighlight(nextRoute ? { nextRoute } as NavItemInternal : {} as NavItemExternal); const isHighlighted = checkRouteHighlight(item);
const content = ( const content = (
<Link <Link
as={ as }
href={ href } href={ href }
target={ isExternal ? '_blank' : '_self' } target={ isInternalLink ? '_self' : '_blank' }
{ ...styleProps.itemProps } { ...styleProps.itemProps }
w={{ base: '100%', lg: isExpanded ? '100%' : '60px', xl: isCollapsed ? '60px' : '100%' }} w={{ base: '100%', lg: isExpanded ? '100%' : '60px', xl: isCollapsed ? '60px' : '100%' }}
display="flex" display="flex"
...@@ -62,14 +62,14 @@ const NavLinkBase = ({ item, nextRoute, isCollapsed, onClick, isExternal, as = ' ...@@ -62,14 +62,14 @@ const NavLinkBase = ({ item, nextRoute, isCollapsed, onClick, isExternal, as = '
placement="right" placement="right"
variant="nav" variant="nav"
gutter={ 20 } gutter={ 20 }
color={ isActive ? colors.text.active : colors.text.hover } color={ isInternalLink && item.isActive ? colors.text.active : colors.text.hover }
margin={ 0 } margin={ 0 }
> >
<HStack spacing={ 0 } overflow="hidden"> <HStack spacing={ 0 } overflow="hidden">
<NavLinkIcon item={ item }/> <NavLinkIcon item={ item }/>
<Text { ...styleProps.textProps } as="span" ml={ 3 }> <Text { ...styleProps.textProps } as="span" ml={ 3 }>
<span>{ item.text }</span> <span>{ item.text }</span>
{ isExternal && <IconSvg name="link_external" boxSize={ 3 } color="icon_link_external" verticalAlign="middle"/> } { !isInternalLink && <IconSvg name="link_external" boxSize={ 3 } color="icon_link_external" verticalAlign="middle"/> }
</Text> </Text>
{ isHighlighted && ( { isHighlighted && (
<LightningLabel iconColor={ styleProps.itemProps.bgColor } isCollapsed={ isCollapsed }/> <LightningLabel iconColor={ styleProps.itemProps.bgColor } isCollapsed={ isCollapsed }/>
...@@ -81,8 +81,8 @@ const NavLinkBase = ({ item, nextRoute, isCollapsed, onClick, isExternal, as = ' ...@@ -81,8 +81,8 @@ const NavLinkBase = ({ item, nextRoute, isCollapsed, onClick, isExternal, as = '
return ( return (
<Box as="li" listStyleType="none" w="100%"> <Box as="li" listStyleType="none" w="100%">
{ nextRoute ? ( { isInternalLink ? (
<NextLink href={ nextRoute } passHref legacyBehavior> <NextLink href={ item.nextRoute } passHref legacyBehavior>
{ content } { content }
</NextLink> </NextLink>
) : content } ) : content }
......
...@@ -3,7 +3,6 @@ import React, { useCallback } from 'react'; ...@@ -3,7 +3,6 @@ import React, { useCallback } from 'react';
import type { NavItem } from 'types/client/navigation'; import type { NavItem } from 'types/client/navigation';
import { route } from 'nextjs-routes';
import type { Route } from 'nextjs-routes'; import type { Route } from 'nextjs-routes';
import config from 'configs/app'; import config from 'configs/app';
...@@ -23,10 +22,9 @@ const NavLinkRewards = ({ isCollapsed, onClick }: Props) => { ...@@ -23,10 +22,9 @@ const NavLinkRewards = ({ isCollapsed, onClick }: Props) => {
const pathname = '/account/rewards'; const pathname = '/account/rewards';
const nextRoute = { pathname } as Route; const nextRoute = { pathname } as Route;
const isLogedIn = isInitialized && apiToken; const handleClick = useCallback((e: React.MouseEvent) => {
const handleClick = useCallback(() => {
if (isInitialized && !apiToken) { if (isInitialized && !apiToken) {
e.preventDefault();
openLoginModal(); openLoginModal();
} }
onClick?.(); onClick?.();
...@@ -41,14 +39,12 @@ const NavLinkRewards = ({ isCollapsed, onClick }: Props) => { ...@@ -41,14 +39,12 @@ const NavLinkRewards = ({ isCollapsed, onClick }: Props) => {
item={{ item={{
text: 'Merits', text: 'Merits',
icon: dailyRewardQuery.data?.available ? 'merits_with_dot' : 'merits', icon: dailyRewardQuery.data?.available ? 'merits_with_dot' : 'merits',
nextRoute: nextRoute,
isActive: router.pathname === pathname,
} as NavItem} } as NavItem}
nextRoute={ isLogedIn ? nextRoute : undefined }
onClick={ handleClick } onClick={ handleClick }
as={ isLogedIn ? 'a' : 'button' }
href={ isLogedIn ? route(nextRoute) : undefined }
isActive={ router.pathname === pathname }
isDisabled={ !isInitialized }
isCollapsed={ isCollapsed } isCollapsed={ isCollapsed }
isDisabled={ !isInitialized }
/> />
); );
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment