Add max display count

This commit is contained in:
Bill 2021-07-03 22:50:03 -04:00
parent 2d2e04764f
commit 2e93a72c56

View File

@ -3,13 +3,12 @@ import { useFriendListContext } from '../../context/FriendListContext';
import { FriendBarItemView } from '../friend-bar-item/FriendBarItemView';
import { FriendBarViewProps } from './FriendBarView.types';
const MAX_DISPLAY_COUNT: number = 3;
export const FriendBarView: FC<FriendBarViewProps> = props =>
{
const { friendListState = null } = useFriendListContext();
const { friends = null } = friendListState;
const [ indexOffset, setIndexOffset ] = useState(0);
const [ maxDisplayCount, setMaxDisplayCount ] = useState(3);
const onlineFriends = useMemo(() =>
{
@ -25,19 +24,20 @@ export const FriendBarView: FC<FriendBarViewProps> = props =>
const canIncreaseIndex = useMemo(() =>
{
if((onlineFriends.length <= MAX_DISPLAY_COUNT) || (indexOffset === (onlineFriends.length - 1))) return false;
if((onlineFriends.length <= maxDisplayCount) || (indexOffset === (onlineFriends.length - 1))) return false;
return true;
}, [ indexOffset, onlineFriends ]);
}, [ maxDisplayCount, indexOffset, onlineFriends ]);
return (
<div className="d-flex friend-bar align-items-center">
<button type="button" className="btn btn-sm btn-black align-self-center friend-bar-button" disabled={ !canDecreaseIndex } onClick={ event => setIndexOffset(indexOffset - 1) }>
<i className="fas fa-chevron-left" />
</button>
<FriendBarItemView friend={ (onlineFriends[ indexOffset ] || null) } />
<FriendBarItemView friend={ (onlineFriends[ indexOffset + 1 ] || null) } />
<FriendBarItemView friend={ (onlineFriends[ indexOffset + 2 ] || null) } />
{ Array.from(Array(maxDisplayCount), (e, i) =>
{
return <FriendBarItemView friend={ (onlineFriends[ indexOffset + i ] || null) } />;
}) }
<button type="button" className="btn btn-sm btn-black align-self-center friend-bar-button" disabled={ !canIncreaseIndex } onClick={ event => setIndexOffset(indexOffset + 1) }>
<i className="fas fa-chevron-right" />
</button>