Files
5chan/src/components/topbar/topbar.tsx
T

168 lines
6.6 KiB
TypeScript
Raw Normal View History

2024-06-03 11:19:48 +02:00
import { useCallback, useEffect, useRef, useState } from 'react';
2024-04-25 22:02:55 +02:00
import { useAccountComment } from '@plebbit/plebbit-react-hooks';
import { Link, useLocation, useNavigate, useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { isAllView, isCatalogView, isSubscriptionsView } from '../../lib/utils/view-utils';
import styles from './topbar.module.css';
import useDefaultSubplebbits, { categorizeSubplebbits, useDefaultSubplebbitAddresses } from '../../hooks/use-default-subplebbits';
2024-06-03 11:19:48 +02:00
import _, { debounce } from 'lodash';
const SearchBar = ({ setShowSearchBar }: { setShowSearchBar: (show: boolean) => void }) => {
const navigate = useNavigate();
const searchBarRef = useRef<HTMLDivElement>(null);
const searchInputRef = useRef<HTMLInputElement>(null);
const placeholder = `"community.eth/.sol" / "12D3KooW..."`;
useEffect(() => {
searchInputRef.current?.focus();
}, []);
const handleClickOutside = useCallback(
(event: MouseEvent) => {
if (searchBarRef.current && !searchBarRef.current.contains(event.target as Node)) {
setShowSearchBar(false);
}
},
[searchBarRef, setShowSearchBar],
);
useEffect(() => {
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [handleClickOutside]);
const handleSearchSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const searchInput = searchInputRef.current?.value;
if (searchInput) {
searchInputRef.current.value = '';
navigate(`/p/${searchInput}`);
setShowSearchBar(false);
}
};
return (
<div className={styles.searchBar} ref={searchBarRef}>
<form onSubmit={handleSearchSubmit}>
<input type='text' autoCorrect='off' autoComplete='off' spellCheck='false' autoCapitalize='off' placeholder={placeholder} ref={searchInputRef} />
</form>
</div>
);
};
const TopBarDesktop = () => {
const { t } = useTranslation();
2024-05-09 16:04:24 +02:00
const location = useLocation();
2024-05-17 16:39:02 +02:00
const isInCatalogView = isCatalogView(location.pathname);
const subplebbits = useDefaultSubplebbits();
2024-06-03 11:19:48 +02:00
const [showSearchBar, setShowSearchBar] = useState(false);
const { plebbitSubs, interestsSubs, randomSubs, internationalSubs, projectsSubs } = categorizeSubplebbits(subplebbits);
const renderSubplebbits = (subplebbits: any) => {
return subplebbits.map((sub: any, index: any) => (
<span key={index}>
{index === 0 ? null : ' '}
<Link to={`/p/${sub.address}${isInCatalogView ? '/catalog' : ''}`}>{sub.address.includes('.') ? sub.address : sub.address.slice(0, 10).concat('...')}</Link>
{index !== subplebbits.length - 1 ? ' /' : null}
</span>
));
};
return (
<div className={styles.boardNavDesktop}>
<span className={styles.boardList}>
[<Link to='/p/all'>all</Link> / <Link to='/p/subscriptions'>subscriptions</Link>] [{renderSubplebbits(plebbitSubs)}] [{renderSubplebbits(projectsSubs)}] [
{renderSubplebbits(interestsSubs)}] [{renderSubplebbits(randomSubs)}] [{renderSubplebbits(internationalSubs)}]
</span>
<span className={styles.navTopRight}>
2024-05-31 11:36:37 +02:00
[<Link to={!location.pathname.endsWith('settings') ? location.pathname.replace(/\/$/, '') + '/settings' : location.pathname}>{t('settings')}</Link>] [
2024-06-03 11:19:48 +02:00
<span onClick={() => setShowSearchBar(!showSearchBar)}>{t('search')}</span>] [<Link to='/'>{t('home')}</Link>]
</span>
2024-06-03 11:19:48 +02:00
{showSearchBar && <SearchBar setShowSearchBar={setShowSearchBar} />}
</div>
);
};
const TopBarMobile = ({ subplebbitAddress }: { subplebbitAddress: string }) => {
const { t } = useTranslation();
const navigate = useNavigate();
const subplebbitAddresses = useDefaultSubplebbitAddresses();
2024-04-18 17:44:35 +02:00
const displaySubplebbitAddress = subplebbitAddress && subplebbitAddress.length > 30 ? subplebbitAddress.slice(0, 30).concat('...') : subplebbitAddress;
2024-06-03 11:19:48 +02:00
const [showSearchBar, setShowSearchBar] = useState(false);
2024-05-04 21:33:25 +02:00
const currentSubplebbitIsInList = subplebbitAddresses.some((address: string) => address === subplebbitAddress);
const location = useLocation();
const isInAllView = isAllView(location.pathname);
2024-05-17 16:39:02 +02:00
const isInCatalogView = isCatalogView(location.pathname);
const isInSubscriptionsView = isSubscriptionsView(location.pathname);
const selectValue = isInAllView ? 'all' : isInSubscriptionsView ? 'subscriptions' : subplebbitAddress;
2024-03-19 14:43:25 +01:00
const boardSelect = (
<select value={selectValue} onChange={(e) => navigate(`/p/${e.target.value}${isInCatalogView ? '/catalog' : ''}`)}>
2024-04-18 17:44:35 +02:00
{!currentSubplebbitIsInList && subplebbitAddress && <option value={subplebbitAddress}>{displaySubplebbitAddress}</option>}
2024-06-01 18:13:41 +02:00
<option value='all'>all</option>
<option value='subscriptions'>subscriptions</option>
2024-05-04 21:33:25 +02:00
{subplebbitAddresses.map((address: any, index: number) => {
const subplebbitAddress = address?.includes('.') ? address : address?.slice(0, 10).concat('...');
return (
2024-03-28 09:31:28 +01:00
<option key={index} value={address}>
2024-05-04 21:33:25 +02:00
{subplebbitAddress}
</option>
);
})}
2024-03-19 14:43:25 +01:00
</select>
);
// navbar animation on scroll
const [visible, setVisible] = useState(true);
const prevScrollPosRef = useRef(0);
useEffect(() => {
const debouncedHandleScroll = debounce(() => {
const currentScrollPos = window.scrollY;
const prevScrollPos = prevScrollPosRef.current;
setVisible(prevScrollPos > currentScrollPos || currentScrollPos < 10);
prevScrollPosRef.current = currentScrollPos;
}, 50);
window.addEventListener('scroll', debouncedHandleScroll);
return () => window.removeEventListener('scroll', debouncedHandleScroll);
}, []);
return (
<div className={styles.boardNavMobile} id='sticky-menu' style={{ top: visible ? 0 : '-23px' }}>
<div className={styles.boardSelect}>
<strong>{t('board')}</strong>
2024-03-19 14:43:25 +01:00
{boardSelect}
</div>
<div className={styles.pageJump}>
2024-05-31 11:36:37 +02:00
<Link to={useLocation().pathname.replace(/\/$/, '') + '/settings'}>{t('settings')}</Link>
2024-06-03 11:19:48 +02:00
<span onClick={() => setShowSearchBar(!showSearchBar)}>{_.capitalize(t('search'))}</span>
<Link to='/'>{t('home')}</Link>
2024-06-03 11:19:48 +02:00
{showSearchBar && <SearchBar setShowSearchBar={setShowSearchBar} />}
</div>
</div>
);
};
const TopBar = () => {
2024-04-25 22:02:55 +02:00
const params = useParams();
const accountComment = useAccountComment({ commentIndex: params?.accountCommentIndex as any });
const subplebbitAddress = params?.subplebbitAddress || accountComment?.subplebbitAddress;
return (
<>
<TopBarDesktop />
<TopBarMobile subplebbitAddress={subplebbitAddress} />
</>
);
};
export default TopBar;