2024-04-25 22:02:55 +02:00
|
|
|
import { useAccountComment } from '@plebbit/plebbit-react-hooks';
|
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-04-13 16:40:06 +02:00
|
|
|
import { isCatalogView } from '../../lib/utils/view-utils';
|
2024-03-19 13:18:12 +01:00
|
|
|
import styles from './board-nav.module.css';
|
2024-05-04 21:33:25 +02:00
|
|
|
import { useDefaultSubplebbitAddresses } from '../../hooks/use-default-subplebbits';
|
2024-03-19 13:18:12 +01:00
|
|
|
|
|
|
|
|
interface BoardNavProps {
|
2024-05-04 21:33:25 +02:00
|
|
|
subplebbitAddresses: string[];
|
2024-04-18 17:44:35 +02:00
|
|
|
subplebbitAddress?: string;
|
2024-03-19 13:18:12 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-04 21:33:25 +02:00
|
|
|
const BoardNavDesktop = ({ subplebbitAddresses }: BoardNavProps) => {
|
2024-03-20 21:35:53 +01:00
|
|
|
const { t } = useTranslation();
|
2024-05-09 16:04:24 +02:00
|
|
|
const location = useLocation();
|
|
|
|
|
const params = useParams();
|
|
|
|
|
const isInCatalogView = isCatalogView(location.pathname, params);
|
2024-03-20 21:35:53 +01: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}>
|
|
|
|
|
[
|
2024-05-04 21:33:25 +02:00
|
|
|
{subplebbitAddresses.map((address: string, index: number) => {
|
2024-03-27 15:10:58 +01:00
|
|
|
return (
|
2024-03-28 09:31:28 +01:00
|
|
|
<span key={index}>
|
2024-03-27 15:10:58 +01:00
|
|
|
{index === 0 ? null : ' '}
|
2024-05-04 21:33:25 +02:00
|
|
|
<Link to={`/p/${address}${isInCatalogView ? '/catalog' : ''}`}>{address.includes('.') ? address : address.slice(0, 10).concat('...')}</Link>
|
|
|
|
|
{index !== subplebbitAddresses.length - 1 ? ' /' : null}
|
2024-03-27 15:10:58 +01:00
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2024-03-20 21:35:53 +01:00
|
|
|
]
|
|
|
|
|
</span>
|
|
|
|
|
<span className={styles.navTopRight}>
|
2024-05-09 16:04:24 +02:00
|
|
|
[<Link to={!location.pathname.endsWith('settings') ? location.pathname + '/settings' : location.pathname}>{t('settings')}</Link>] [<Link to='/'>{t('home')}</Link>
|
|
|
|
|
]
|
2024-03-20 21:35:53 +01:00
|
|
|
</span>
|
2024-03-19 13:18:12 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-04 21:33:25 +02:00
|
|
|
const BoardNavMobile = ({ subplebbitAddresses, subplebbitAddress }: BoardNavProps) => {
|
2024-03-22 12:22:20 +01:00
|
|
|
const { t } = useTranslation();
|
2024-03-19 13:18:12 +01:00
|
|
|
const navigate = useNavigate();
|
2024-04-18 17:44:35 +02:00
|
|
|
const displaySubplebbitAddress = subplebbitAddress && subplebbitAddress.length > 30 ? subplebbitAddress.slice(0, 30).concat('...') : subplebbitAddress;
|
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-04-13 16:40:06 +02:00
|
|
|
const isInCatalogView = isCatalogView(useLocation().pathname, useParams());
|
|
|
|
|
|
2024-03-19 14:43:25 +01:00
|
|
|
const boardSelect = (
|
2024-04-18 17:44:35 +02:00
|
|
|
<select value={subplebbitAddress || 'all'} onChange={(e) => navigate(`/p/${e.target.value}${isInCatalogView ? '/catalog' : ''}`)}>
|
|
|
|
|
{!currentSubplebbitIsInList && subplebbitAddress && <option value={subplebbitAddress}>{displaySubplebbitAddress}</option>}
|
2024-03-26 16:08:06 +01:00
|
|
|
<option value='all'>{t('all')}</option>
|
|
|
|
|
<option value='subscriptions'>{t('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('...');
|
2024-03-26 18:08:50 +01:00
|
|
|
return (
|
2024-03-28 09:31:28 +01:00
|
|
|
<option key={index} value={address}>
|
2024-05-04 21:33:25 +02:00
|
|
|
{subplebbitAddress}
|
2024-03-26 18:08:50 +01:00
|
|
|
</option>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2024-03-19 14:43:25 +01:00
|
|
|
</select>
|
|
|
|
|
);
|
|
|
|
|
|
2024-03-19 13:18:12 +01:00
|
|
|
return (
|
2024-04-29 15:36:29 +02:00
|
|
|
<div className={styles.boardNavMobile} id='sticky-menu'>
|
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}
|
|
|
|
|
</div>
|
|
|
|
|
<div className={styles.pageJump}>
|
2024-05-09 16:04:24 +02:00
|
|
|
<Link to={useLocation().pathname + '/settings'}>{t('settings')}</Link>
|
2024-03-22 12:22:20 +01:00
|
|
|
<Link to='/'>{t('home')}</Link>
|
2024-03-19 13:18:12 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-29 15:36:29 +02:00
|
|
|
// sticky menu animation
|
|
|
|
|
// will trigger more than once with hot reloading during development
|
|
|
|
|
if (!window.STICKY_MENU_SCROLL_LISTENER) {
|
|
|
|
|
window.STICKY_MENU_SCROLL_LISTENER = true;
|
|
|
|
|
|
|
|
|
|
const scrollRange = 50; // the animation css px range in stickyMenuAnimation, must also edit css animation 100%: {top}
|
|
|
|
|
let currentScrollInRange = 0,
|
|
|
|
|
previousScroll = 0;
|
|
|
|
|
|
|
|
|
|
window.addEventListener('scroll', () => {
|
|
|
|
|
// find difference between current and last scroll position
|
|
|
|
|
const currentScroll = window.scrollY;
|
|
|
|
|
const scrollDifference = currentScroll - previousScroll;
|
|
|
|
|
previousScroll = currentScroll;
|
|
|
|
|
|
|
|
|
|
// find new current scroll in range
|
|
|
|
|
const previousScrollInRange = currentScrollInRange;
|
|
|
|
|
currentScrollInRange += scrollDifference;
|
|
|
|
|
if (currentScrollInRange > scrollRange) {
|
|
|
|
|
currentScrollInRange = scrollRange;
|
|
|
|
|
} else if (currentScrollInRange < 0) {
|
|
|
|
|
currentScrollInRange = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fix mobile overflow scroll bug
|
|
|
|
|
if (currentScroll <= 0) {
|
|
|
|
|
currentScrollInRange = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// no changes
|
|
|
|
|
if (currentScrollInRange === previousScrollInRange) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the menu element
|
|
|
|
|
const menuElement = document.getElementById('sticky-menu');
|
|
|
|
|
if (!menuElement) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// control progress of the animation using negative animation-delay (0 to -1s)
|
|
|
|
|
const animationPercent = currentScrollInRange / scrollRange;
|
|
|
|
|
menuElement.style.animationDelay = animationPercent * -1 + 's';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-18 17:44:35 +02:00
|
|
|
const BoardNav = () => {
|
2024-05-04 21:33:25 +02:00
|
|
|
const subplebbitAddresses = useDefaultSubplebbitAddresses();
|
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-04 21:33:25 +02:00
|
|
|
<BoardNavDesktop subplebbitAddresses={subplebbitAddresses} />
|
|
|
|
|
<BoardNavMobile subplebbitAddresses={subplebbitAddresses} subplebbitAddress={subplebbitAddress} />
|
2024-03-19 13:18:12 +01:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default BoardNav;
|