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

Feat/add selector network #1329

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 22 additions & 8 deletions src/components/sidebar-navigation/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as HoverCard from '@radix-ui/react-hover-card';
import Link from 'next/link';
import React, { useCallback, useEffect, useRef, useState } from 'react';

import { networkId } from '@/config';
import useDebounce from '@/hooks/useDebounce';
import { fetchSearchHits } from '@/utils/algoliaSearchApi';
import { fetchCatalog } from '@/utils/catalogSearchApi';
Expand Down Expand Up @@ -51,11 +52,15 @@ export const Search = ({ inputRef }: { inputRef: any }) => {
const fetchResults = useCallback(async () => {
setIsLoading(true);

const [docs, apps, components] = await Promise.all([
const isMainnet = networkId === 'mainnet';

const fetchPromises = [
fetchSearchHits('Docs', debouncedSearchTerm),
fetchCatalog(debouncedSearchTerm),
fetchSearchHits('Components', debouncedSearchTerm),
]);
isMainnet ? fetchCatalog(debouncedSearchTerm) : Promise.resolve(),
isMainnet ? fetchSearchHits('Components', debouncedSearchTerm) : Promise.resolve(),
];

const [docs, apps, components] = await Promise.all(fetchPromises);

setResults({
Docs: renderResults('Docs', docs),
Expand Down Expand Up @@ -135,11 +140,20 @@ export const Search = ({ inputRef }: { inputRef: any }) => {
<S.Tab $active={activeTab === 'Docs'} onClick={() => handleTabChange('Docs')} $isFirst={true}>
Docs
</S.Tab>
<S.Tab $active={activeTab === 'Apps'} onClick={() => handleTabChange('Apps')}>
Apps
<S.Tab
$active={activeTab === 'Apps'}
onClick={() => handleTabChange('Apps')}
disabled={networkId == 'testnet'}
>
Apps {networkId == 'testnet' && '(Mainnet only)'}
</S.Tab>
<S.Tab $active={activeTab === 'Components'} onClick={() => handleTabChange('Components')} $isLast={true}>
Components
<S.Tab
$active={activeTab === 'Components'}
onClick={() => handleTabChange('Components')}
disabled={networkId == 'testnet'}
$isLast={true}
>
Components {networkId == 'testnet' && '(Mainnet only)'}
</S.Tab>
</S.TabContainer>

Expand Down
62 changes: 60 additions & 2 deletions src/components/sidebar-navigation/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Tooltip } from '@near-pagoda/ui';
import { Dropdown, SvgIcon, Tooltip } from '@near-pagoda/ui';
import { CaretDown } from '@phosphor-icons/react';
import Image from 'next/image';
import { useRouter } from 'next/router';
import { useCallback, useEffect, useRef } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useContext } from 'react';
import styled from 'styled-components';

import { networkId } from '@/config';
import { useSignInRedirect } from '@/hooks/useSignInRedirect';

import { NearContext } from '../wallet-selector/WalletSelector';
Expand All @@ -14,6 +17,26 @@ import * as S from './styles';
import { UserDropdownMenu } from './UserDropdownMenu';
import { currentPathMatchesRoute } from './utils';

const Redirect = styled.a<{ selected?: boolean }>`
text-decoration: none;
color: #444;
`;

const Badge = styled.div`
display: flex;
align-items: center;
justify-content: center;
padding: 0.25rem 0.5rem;
gap: 4px;
font-size: 0.75rem;
font-weight: 600;
color: ${() => (networkId === 'mainnet' ? '#0072de' : '#d14e00')};
background-color: ${() => (networkId === 'mainnet' ? '#0084f116' : '#f9900026')};
text-transform: capitalize;
border-radius: 0.25rem;
letter-spacing: 0.05em;
`;

export const Sidebar = () => {
const router = useRouter();
const expandedDrawer = useNavigationStore((store) => store.expandedDrawer);
Expand All @@ -25,6 +48,13 @@ export const Sidebar = () => {
const { signedAccountId } = useContext(NearContext);
const { requestAuthentication } = useSignInRedirect();
const inputRef = useRef<HTMLInputElement>(null);
const [isOpenNetwork, setIsOpenNetwork] = useState(false);

const preventRedirect = (network: string) => (e: React.MouseEvent) => {
if (networkId == network) {
e.preventDefault();
}
};

const handleCreateAccount = () => {
requestAuthentication(true);
Expand Down Expand Up @@ -53,6 +83,34 @@ export const Sidebar = () => {
<S.Logo href="/" aria-label="Go Home">
<Image src={NearIconSvg} alt="NEAR" />
</S.Logo>
<S.Network>
<Dropdown.Root open={isOpenNetwork} onOpenChange={(open) => setIsOpenNetwork(open)}>
<Dropdown.Trigger asChild>
<Badge>
{networkId}{' '}
<SvgIcon
icon={<CaretDown />}
size="xs"
style={{
marginBottom: '1px',
transform: isOpenNetwork ? 'rotate(180deg)' : 'rotate(0deg)',
transition: 'all 200ms',
}}
/>
</Badge>
</Dropdown.Trigger>
<Dropdown.Content>
<Dropdown.Section>
<Redirect href="https://dev.near.org" target="_blank" onClick={preventRedirect('mainnet')}>
<Dropdown.Item>Mainnet</Dropdown.Item>
</Redirect>
<Redirect href="https://test.near.org" target="_blank" onClick={preventRedirect('testnet')}>
<Dropdown.Item>Testnet</Dropdown.Item>
</Redirect>
</Dropdown.Section>
</Dropdown.Content>
</Dropdown.Root>
</S.Network>

<S.ToggleExpandButton type="button" aria-label="Expand/Collapse Menu" onClick={toggleExpandedSidebar}>
<i className={`ph-bold ${isSidebarExpanded ? 'ph-arrow-line-left' : 'ph-list'}`} />
Expand Down
51 changes: 41 additions & 10 deletions src/components/sidebar-navigation/SmallScreenHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Button } from '@near-pagoda/ui';
import { MagnifyingGlass } from '@phosphor-icons/react';
import { Button, Dropdown } from '@near-pagoda/ui';
import { MagnifyingGlass, WifiHigh } from '@phosphor-icons/react';
import Image from 'next/image';
import { useRouter } from 'next/router';
import { useContext } from 'react';
import styled from 'styled-components';

import { networkId } from '@/config';
import { useSignInRedirect } from '@/hooks/useSignInRedirect';

import { NearContext } from '../wallet-selector/WalletSelector';
Expand All @@ -12,6 +14,11 @@ import { useNavigationStore } from './store';
import * as S from './styles';
import { UserDropdownMenu } from './UserDropdownMenu';

const Redirect = styled.a<{ selected?: boolean }>`
text-decoration: none;
color: #444;
`;

export const SmallScreenHeader = () => {
const router = useRouter();
const redirect = (url: string) => () => router.push(url);
Expand All @@ -23,6 +30,12 @@ export const SmallScreenHeader = () => {
const { signedAccountId } = useContext(NearContext);
const { requestAuthentication } = useSignInRedirect();

const preventRedirect = (network: string) => (e: React.MouseEvent) => {
if (networkId == network) {
e.preventDefault();
}
};

const handleCreateAccount = () => {
requestAuthentication(true);
};
Expand Down Expand Up @@ -50,23 +63,41 @@ export const SmallScreenHeader = () => {
<Image src={NearIconSvg} alt="NEAR" />
</S.SmallScreenHeaderLogo>
)}

{signedAccountId ? (
<S.SmallScreenHeaderActions $hidden={isOpenedOnSmallScreens} $gap="16px">
<Button label="search" icon={<MagnifyingGlass />} variant="secondary" onClick={redirect('/search')} />
<UserDropdownMenu />
</S.SmallScreenHeaderActions>
) : (
<>
<Button
label="Sign-up or Login"
variant="primary"
onClick={handleCreateAccount}
style={{ alignSelf: 'center', marginRight: '1rem' }}
/>
)}
<Dropdown.Root>
<Dropdown.Trigger asChild>
<Button
label="Sign-up or Login"
variant="primary"
onClick={handleCreateAccount}
style={{ alignSelf: 'center', marginRight: '1rem' }}
label={networkId}
icon={<WifiHigh fill="bold" style={{ color: networkId == 'mainnet' ? '#0072de' : '#d14e00' }} />}
fill="outline"
style={{ alignSelf: 'center' }}
type="button"
/>
</>
)}
</Dropdown.Trigger>

<Dropdown.Content>
<Dropdown.Section>
<Redirect href="https://dev.near.org" target="_blank" onClick={preventRedirect('mainnet')}>
<Dropdown.Item>Mainnet</Dropdown.Item>
</Redirect>
<Redirect href="https://test.near.org" target="_blank" onClick={preventRedirect('testnet')}>
<Dropdown.Item>Testnet</Dropdown.Item>
</Redirect>
</Dropdown.Section>
</Dropdown.Content>
</Dropdown.Root>
<S.SmallScreenHeaderIconButton
type="button"
aria-label="Expand/Collapse Menu"
Expand Down
6 changes: 4 additions & 2 deletions src/components/sidebar-navigation/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export const Logo = styled(Link)`
}
`;

export const Network = styled.div``;

export const ToggleExpandButton = styled.button`
all: unset;
box-sizing: border-box;
Expand Down Expand Up @@ -605,13 +607,13 @@ export const Sidebar = styled.div<{
${NavigationItem} span,
${SectionLabelIconLink},
${SectionLabel},
${Logo} {
${Logo},
${Network} {
pointer-events: none;
opacity: 0;
padding: 0;
width: 0;
}

${SectionLabel} {
margin-bottom: -2rem;
}
Expand Down