import { useCallback, useEffect, useRef, useState } from 'react'; import { useAccount, useAccountComment } from '@plebbit/plebbit-react-hooks'; import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js'; 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, { useDefaultSubplebbitAddresses } from '../../hooks/use-default-subplebbits'; import _, { debounce } from 'lodash'; import { TimeFilter } from '../board-buttons'; const SearchBar = ({ setShowSearchBar }: { setShowSearchBar: (show: boolean) => void }) => { const navigate = useNavigate(); const searchBarRef = useRef(null); const searchInputRef = useRef(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]); useEffect(() => { const handleEscapeKey = (event: KeyboardEvent) => { if (event.key === 'Escape') { setShowSearchBar(false); } }; document.addEventListener('keydown', handleEscapeKey); return () => { document.removeEventListener('keydown', handleEscapeKey); }; }, [setShowSearchBar]); const handleSearchSubmit = (event: React.FormEvent) => { event.preventDefault(); const searchInput = searchInputRef.current?.value; if (searchInput) { searchInputRef.current.value = ''; navigate(`/p/${searchInput}`); setShowSearchBar(false); } }; return (
); }; const renderBoardsList = (subplebbits: any, isInCatalogView: boolean, subscriptions: string[]) => subplebbits.length > 0 && ( <> [ {subplebbits.map((sub: any, index: any) => ( {index === 0 ? null : ' '} {sub.address.endsWith('.eth') || sub.address.endsWith('.sol') ? sub.address : sub.address.slice(0, 10).concat('...')} {index !== subplebbits?.length - 1 ? ' /' : null} ))} ]{' '} {subscriptions.length > 0 && ( <> [ {subscriptions.map((address: any, index: any) => ( {index === 0 ? null : ' '} {address.endsWith('.eth') || address.endsWith('.sol') ? address : address.slice(0, 10).concat('...')} {index !== subscriptions?.length - 1 ? ' /' : null} ))} ]{' '} )} ); const TopBarDesktop = () => { const { t } = useTranslation(); const account = useAccount(); const location = useLocation(); const params = useParams(); const isInCatalogView = isCatalogView(location.pathname, params); const subplebbits = useDefaultSubplebbits(); const [showSearchBar, setShowSearchBar] = useState(false); return (
[all / subscriptions]{' '} {renderBoardsList(subplebbits.slice(0, 15), isInCatalogView, account?.subscriptions)}[ { e.preventDefault(); }} > {t('create')} ] [ { e.preventDefault(); }} > {t('vote')} ] [{t('settings')}] [ setShowSearchBar(!showSearchBar)}>{t('search')}] [{t('home')}] {showSearchBar && }
); }; const TopBarMobile = ({ subplebbitAddress }: { subplebbitAddress: string }) => { const { t } = useTranslation(); const navigate = useNavigate(); const subplebbitAddresses = useDefaultSubplebbitAddresses(); const displaySubplebbitAddress = subplebbitAddress && subplebbitAddress.length > 30 ? subplebbitAddress.slice(0, 30).concat('...') : subplebbitAddress; const [showSearchBar, setShowSearchBar] = useState(false); const currentSubplebbitIsInList = subplebbitAddresses.some((address: string) => address === subplebbitAddress); const location = useLocation(); const params = useParams(); const isInAllView = isAllView(location.pathname); const isInCatalogView = isCatalogView(location.pathname, params); const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams()); const selectValue = isInAllView ? 'all' : isInSubscriptionsView ? 'subscriptions' : subplebbitAddress; const boardSelect = ( ); // 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 ( ); }; const TopBar = () => { const params = useParams(); const accountComment = useAccountComment({ commentIndex: params?.accountCommentIndex as any }); const subplebbitAddress = params?.subplebbitAddress || accountComment?.subplebbitAddress; return ( <> ); }; export default TopBar;