2024-06-03 11:19:48 +02:00
|
|
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
2025-02-03 13:12:02 +01:00
|
|
|
import { useAccount, useAccountComment } from '@plebbit/plebbit-react-hooks';
|
2024-10-27 16:37:21 +01:00
|
|
|
import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js';
|
2024-04-13 16:40:06 +02:00
|
|
|
import { Link, useLocation, useNavigate, useParams } from 'react-router-dom';
|
2024-03-20 21:35:53 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-05-17 16:10:38 +02:00
|
|
|
import { isAllView, isCatalogView, isSubscriptionsView } from '../../lib/utils/view-utils';
|
2024-05-29 21:00:13 +02:00
|
|
|
import styles from './topbar.module.css';
|
2025-02-03 13:12:02 +01:00
|
|
|
import useDefaultSubplebbits, { useDefaultSubplebbitAddresses } from '../../hooks/use-default-subplebbits';
|
2024-06-03 11:19:48 +02:00
|
|
|
import _, { debounce } from 'lodash';
|
2024-06-09 16:40:32 +02:00
|
|
|
import { TimeFilter } from '../board-buttons';
|
2024-06-03 11:19:48 +02:00
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
|
2025-01-31 12:33:52 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
const handleEscapeKey = (event: KeyboardEvent) => {
|
|
|
|
|
if (event.key === 'Escape') {
|
|
|
|
|
setShowSearchBar(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.addEventListener('keydown', handleEscapeKey);
|
|
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener('keydown', handleEscapeKey);
|
|
|
|
|
};
|
|
|
|
|
}, [setShowSearchBar]);
|
|
|
|
|
|
2024-06-03 11:19:48 +02:00
|
|
|
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>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-03-19 13:18:12 +01:00
|
|
|
|
2025-02-03 13:12:02 +01:00
|
|
|
const renderBoardsList = (subplebbits: any, isInCatalogView: boolean, subscriptions: string[]) =>
|
2025-02-03 22:12:00 +01:00
|
|
|
subplebbits?.length > 0 && (
|
2025-02-03 13:12:02 +01:00
|
|
|
<>
|
|
|
|
|
[
|
|
|
|
|
{subplebbits.map((sub: any, index: any) => (
|
|
|
|
|
<span key={index}>
|
|
|
|
|
{index === 0 ? null : ' '}
|
|
|
|
|
<Link to={`/p/${sub.address}${isInCatalogView ? '/catalog' : ''}`}>
|
|
|
|
|
{sub.address.endsWith('.eth') || sub.address.endsWith('.sol') ? sub.address : sub.address.slice(0, 10).concat('...')}
|
|
|
|
|
</Link>
|
2025-02-03 22:12:00 +01:00
|
|
|
{index !== subplebbits.length - 1 ? ' /' : null}
|
2025-02-03 13:12:02 +01:00
|
|
|
</span>
|
|
|
|
|
))}
|
|
|
|
|
]{' '}
|
2025-02-03 22:12:00 +01:00
|
|
|
{subscriptions?.length > 0 && (
|
2025-02-03 13:12:02 +01:00
|
|
|
<>
|
|
|
|
|
[
|
|
|
|
|
{subscriptions.map((address: any, index: any) => (
|
|
|
|
|
<span key={index}>
|
|
|
|
|
{index === 0 ? null : ' '}
|
|
|
|
|
<Link to={`/p/${address}${isInCatalogView ? '/catalog' : ''}`}>
|
|
|
|
|
{address.endsWith('.eth') || address.endsWith('.sol') ? address : address.slice(0, 10).concat('...')}
|
|
|
|
|
</Link>
|
2025-02-03 13:33:11 +01:00
|
|
|
{index !== subscriptions?.length - 1 ? ' /' : null}
|
2025-02-03 13:12:02 +01:00
|
|
|
</span>
|
|
|
|
|
))}
|
|
|
|
|
]{' '}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-29 21:00:13 +02:00
|
|
|
const TopBarDesktop = () => {
|
2024-03-20 21:35:53 +01:00
|
|
|
const { t } = useTranslation();
|
2025-02-03 13:12:02 +01:00
|
|
|
const account = useAccount();
|
2024-05-09 16:04:24 +02:00
|
|
|
const location = useLocation();
|
2024-06-09 16:40:32 +02:00
|
|
|
const params = useParams();
|
|
|
|
|
const isInCatalogView = isCatalogView(location.pathname, params);
|
2024-05-17 16:10:38 +02:00
|
|
|
const subplebbits = useDefaultSubplebbits();
|
2024-06-03 11:19:48 +02:00
|
|
|
const [showSearchBar, setShowSearchBar] = useState(false);
|
2024-05-17 16:10:38 +02:00
|
|
|
|
2024-03-19 13:18:12 +01:00
|
|
|
return (
|
|
|
|
|
<div className={styles.boardNavDesktop}>
|
2024-03-20 21:35:53 +01:00
|
|
|
<span className={styles.boardList}>
|
2025-02-03 13:12:02 +01:00
|
|
|
[<Link to='/p/all'>all</Link> / <Link to='/p/subscriptions'>subscriptions</Link>]{' '}
|
|
|
|
|
{renderBoardsList(subplebbits.slice(0, 15), isInCatalogView, account?.subscriptions)}[
|
2024-07-10 17:42:38 +02:00
|
|
|
<Link
|
2024-09-08 10:41:39 +02:00
|
|
|
className={styles.disabledButton}
|
2024-07-10 17:42:38 +02:00
|
|
|
to='boards/create'
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-11-05 12:12:55 +01:00
|
|
|
{t('create')}
|
2024-07-10 17:42:38 +02:00
|
|
|
</Link>
|
|
|
|
|
] [
|
|
|
|
|
<Link
|
2024-09-08 10:41:39 +02:00
|
|
|
className={styles.disabledButton}
|
2024-07-10 17:42:38 +02:00
|
|
|
to='boards'
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-11-05 12:12:55 +01:00
|
|
|
{t('vote')}
|
2024-07-10 17:42:38 +02:00
|
|
|
</Link>
|
2024-09-08 10:41:39 +02:00
|
|
|
]
|
2024-03-20 21:35:53 +01:00
|
|
|
</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>]
|
2024-03-20 21:35:53 +01:00
|
|
|
</span>
|
2024-06-03 11:19:48 +02:00
|
|
|
{showSearchBar && <SearchBar setShowSearchBar={setShowSearchBar} />}
|
2024-03-19 13:18:12 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-29 21:00:13 +02:00
|
|
|
const TopBarMobile = ({ subplebbitAddress }: { subplebbitAddress: string }) => {
|
2024-03-22 12:22:20 +01:00
|
|
|
const { t } = useTranslation();
|
2024-03-19 13:18:12 +01:00
|
|
|
const navigate = useNavigate();
|
2024-05-17 16:10:38 +02:00
|
|
|
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-03-26 20:45:41 +01:00
|
|
|
|
2024-05-04 21:33:25 +02:00
|
|
|
const currentSubplebbitIsInList = subplebbitAddresses.some((address: string) => address === subplebbitAddress);
|
2024-03-19 13:18:12 +01:00
|
|
|
|
2024-05-17 16:10:38 +02:00
|
|
|
const location = useLocation();
|
2024-06-09 16:40:32 +02:00
|
|
|
const params = useParams();
|
2024-10-29 17:40:09 +01:00
|
|
|
const isInAllView = isAllView(location.pathname);
|
2024-06-09 16:40:32 +02:00
|
|
|
const isInCatalogView = isCatalogView(location.pathname, params);
|
2024-06-17 12:17:36 +02:00
|
|
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
|
2024-05-17 16:10:38 +02:00
|
|
|
const selectValue = isInAllView ? 'all' : isInSubscriptionsView ? 'subscriptions' : subplebbitAddress;
|
2024-04-13 16:40:06 +02:00
|
|
|
|
2024-03-19 14:43:25 +01:00
|
|
|
const boardSelect = (
|
2024-05-17 16:10:38 +02:00
|
|
|
<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) => {
|
2024-10-27 16:37:21 +01:00
|
|
|
const subplebbitAddress = address?.includes('.') ? address : Plebbit.getShortAddress(address);
|
2024-03-26 18:08:50 +01:00
|
|
|
return (
|
2024-03-28 09:31:28 +01:00
|
|
|
<option key={index} value={address}>
|
2024-10-27 16:37:21 +01:00
|
|
|
{subplebbitAddress.endsWith('.eth') || subplebbitAddress.endsWith('.sol') ? subplebbitAddress.slice(0, -4) : subplebbitAddress}
|
2024-03-26 18:08:50 +01:00
|
|
|
</option>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2024-03-19 14:43:25 +01:00
|
|
|
</select>
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-29 17:09:43 +02:00
|
|
|
// navbar animation on scroll
|
|
|
|
|
const [visible, setVisible] = useState(true);
|
2024-05-29 17:17:46 +02:00
|
|
|
const prevScrollPosRef = useRef(0);
|
|
|
|
|
|
2024-05-29 17:09:43 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
const debouncedHandleScroll = debounce(() => {
|
|
|
|
|
const currentScrollPos = window.scrollY;
|
2024-05-29 17:17:46 +02:00
|
|
|
const prevScrollPos = prevScrollPosRef.current;
|
|
|
|
|
|
2024-05-29 17:09:43 +02:00
|
|
|
setVisible(prevScrollPos > currentScrollPos || currentScrollPos < 10);
|
2024-05-29 17:17:46 +02:00
|
|
|
prevScrollPosRef.current = currentScrollPos;
|
2024-10-14 11:57:17 +02:00
|
|
|
}, 50);
|
2024-05-29 17:09:43 +02:00
|
|
|
|
|
|
|
|
window.addEventListener('scroll', debouncedHandleScroll);
|
|
|
|
|
|
|
|
|
|
return () => window.removeEventListener('scroll', debouncedHandleScroll);
|
2024-05-29 17:17:46 +02:00
|
|
|
}, []);
|
2024-05-29 17:09:43 +02:00
|
|
|
|
2024-03-19 13:18:12 +01:00
|
|
|
return (
|
2025-02-20 00:12:06 +01:00
|
|
|
<div className={styles.boardNavMobile} style={{ transform: visible ? 'translateY(0)' : 'translateY(-23px)' }}>
|
2024-03-19 13:18:12 +01:00
|
|
|
<div className={styles.boardSelect}>
|
2024-03-22 12:22:20 +01:00
|
|
|
<strong>{t('board')}</strong>
|
2024-03-19 14:43:25 +01:00
|
|
|
{boardSelect}
|
2024-06-09 16:50:51 +02:00
|
|
|
{(isInAllView || isInSubscriptionsView) && (
|
2024-07-02 16:01:17 +02:00
|
|
|
<TimeFilter isTopbar={true} isInAllView={isInAllView} isInCatalogView={isInCatalogView} isInSubscriptionsView={isInSubscriptionsView} />
|
2024-06-09 16:50:51 +02:00
|
|
|
)}
|
2024-03-19 14:43:25 +01:00
|
|
|
</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>
|
2024-03-22 12:22:20 +01:00
|
|
|
<Link to='/'>{t('home')}</Link>
|
2024-06-03 11:19:48 +02:00
|
|
|
{showSearchBar && <SearchBar setShowSearchBar={setShowSearchBar} />}
|
2024-03-19 13:18:12 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-29 21:00:13 +02:00
|
|
|
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;
|
|
|
|
|
|
2024-03-19 13:18:12 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
2024-05-29 21:00:13 +02:00
|
|
|
<TopBarDesktop />
|
|
|
|
|
<TopBarMobile subplebbitAddress={subplebbitAddress} />
|
2024-03-19 13:18:12 +01:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-29 21:00:13 +02:00
|
|
|
export default TopBar;
|