Files
5chan/src/components/board-nav/board-nav.tsx
T

140 lines
4.9 KiB
TypeScript
Raw Normal View History

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 { isCatalogView } from '../../lib/utils/view-utils';
import styles from './board-nav.module.css';
2024-05-04 21:33:25 +02:00
import { useDefaultSubplebbitAddresses } from '../../hooks/use-default-subplebbits';
interface BoardNavProps {
2024-05-04 21:33:25 +02:00
subplebbitAddresses: string[];
2024-04-18 17:44:35 +02:00
subplebbitAddress?: string;
}
2024-05-04 21:33:25 +02:00
const BoardNavDesktop = ({ subplebbitAddresses }: BoardNavProps) => {
const { t } = useTranslation();
const isInCatalogView = isCatalogView(useLocation().pathname, useParams());
return (
<div className={styles.boardNavDesktop}>
<span className={styles.boardList}>
[
2024-05-04 21:33:25 +02:00
{subplebbitAddresses.map((address: string, index: number) => {
return (
2024-03-28 09:31:28 +01:00
<span key={index}>
{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}
</span>
);
})}
]
</span>
<span className={styles.navTopRight}>
2024-03-21 22:16:24 +01:00
[<Link to='/settings'>{t('settings')}</Link>] [<Link to='/'>{t('home')}</Link>]
</span>
</div>
);
};
2024-05-04 21:33:25 +02:00
const BoardNavMobile = ({ subplebbitAddresses, subplebbitAddress }: BoardNavProps) => {
const { t } = useTranslation();
const navigate = useNavigate();
2024-04-18 17:44:35 +02:00
const displaySubplebbitAddress = subplebbitAddress && subplebbitAddress.length > 30 ? subplebbitAddress.slice(0, 30).concat('...') : subplebbitAddress;
2024-05-04 21:33:25 +02:00
const currentSubplebbitIsInList = subplebbitAddresses.some((address: string) => address === subplebbitAddress);
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>}
<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('...');
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>
);
return (
<div className={styles.boardNavMobile} id='sticky-menu'>
<div className={styles.boardSelect}>
<strong>{t('board')}</strong>
2024-03-19 14:43:25 +01:00
{boardSelect}
</div>
<div className={styles.pageJump}>
<Link to='settings'>{t('settings')}</Link>
<Link to='/'>{t('home')}</Link>
</div>
</div>
);
};
// 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;
return (
<>
2024-05-04 21:33:25 +02:00
<BoardNavDesktop subplebbitAddresses={subplebbitAddresses} />
<BoardNavMobile subplebbitAddresses={subplebbitAddresses} subplebbitAddress={subplebbitAddress} />
</>
);
};
export default BoardNav;