SidebarMenu: convert to tsx

This commit is contained in:
Alex Gleason 2022-04-12 19:52:20 -05:00
parent 5f8c7c8db6
commit 9c79ae386a
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 50 additions and 40 deletions

View File

@ -1,23 +1,24 @@
import classNames from 'classnames'; import classNames from 'classnames';
import PropTypes from 'prop-types';
import React from 'react'; import React from 'react';
import { defineMessages, useIntl } from 'react-intl'; import { defineMessages, useIntl } from 'react-intl';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch } from 'react-redux';
import { Link, NavLink } from 'react-router-dom'; import { Link, NavLink } from 'react-router-dom';
import { logOut, switchAccount } from 'soapbox/actions/auth'; import { logOut, switchAccount } from 'soapbox/actions/auth';
import { fetchOwnAccounts } from 'soapbox/actions/auth'; import { fetchOwnAccounts } from 'soapbox/actions/auth';
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
import Account from 'soapbox/components/account'; import Account from 'soapbox/components/account';
import { Stack } from 'soapbox/components/ui'; import { Stack } from 'soapbox/components/ui';
import ProfileStats from 'soapbox/features/ui/components/profile_stats'; import ProfileStats from 'soapbox/features/ui/components/profile_stats';
import { getFeatures } from 'soapbox/utils/features'; import { useAppSelector, useSoapboxConfig, useFeatures } from 'soapbox/hooks';
import { closeSidebar } from '../actions/sidebar'; import { closeSidebar } from '../actions/sidebar';
import { makeGetAccount, makeGetOtherAccounts } from '../selectors'; import { makeGetAccount, makeGetOtherAccounts } from '../selectors';
import { HStack, Icon, IconButton, Text } from './ui'; import { HStack, Icon, IconButton, Text } from './ui';
import type { List as ImmutableList } from 'immutable';
import type { Account as AccountEntity } from 'soapbox/types/entities';
const messages = defineMessages({ const messages = defineMessages({
followers: { id: 'account.followers', defaultMessage: 'Followers' }, followers: { id: 'account.followers', defaultMessage: 'Followers' },
follows: { id: 'account.follows', defaultMessage: 'Follows' }, follows: { id: 'account.follows', defaultMessage: 'Follows' },
@ -33,7 +34,14 @@ const messages = defineMessages({
logout: { id: 'navigation_bar.logout', defaultMessage: 'Logout' }, logout: { id: 'navigation_bar.logout', defaultMessage: 'Logout' },
}); });
const SidebarLink = ({ to, icon, text, onClick }) => ( interface ISidebarLink {
to: string,
icon: string,
text: string,
onClick: React.EventHandler<React.MouseEvent>,
}
const SidebarLink: React.FC<ISidebarLink> = ({ to, icon, text, onClick }) => (
<NavLink className='group py-1 rounded-md' to={to} onClick={onClick}> <NavLink className='group py-1 rounded-md' to={to} onClick={onClick}>
<HStack space={2} alignItems='center'> <HStack space={2} alignItems='center'>
<div className='bg-primary-50 dark:bg-slate-700 relative rounded inline-flex p-2'> <div className='bg-primary-50 dark:bg-slate-700 relative rounded inline-flex p-2'>
@ -45,25 +53,19 @@ const SidebarLink = ({ to, icon, text, onClick }) => (
</NavLink> </NavLink>
); );
SidebarLink.propTypes = { const getOtherAccounts = makeGetOtherAccounts();
to: PropTypes.string.isRequired,
icon: PropTypes.string.isRequired,
text: PropTypes.string.isRequired,
onClick: PropTypes.func.isRequired,
};
const SidebarMenu = () => { const SidebarMenu: React.FC = (): JSX.Element | null => {
const intl = useIntl(); const intl = useIntl();
const dispatch = useDispatch(); const dispatch = useDispatch();
const logo = useSelector((state) => getSoapboxConfig(state).get('logo')); const { logo } = useSoapboxConfig();
const features = useSelector((state) => getFeatures(state.get('instance'))); const features = useFeatures();
const getAccount = makeGetAccount(); const getAccount = makeGetAccount();
const getOtherAccounts = makeGetOtherAccounts(); const me = useAppSelector((state) => state.me);
const me = useSelector((state) => state.get('me')); const account = useAppSelector((state) => me ? getAccount(state, me) : null);
const account = useSelector((state) => getAccount(state, me)); const otherAccounts: ImmutableList<AccountEntity> = useAppSelector((state) => getOtherAccounts(state));
const otherAccounts = useSelector((state) => getOtherAccounts(state)); const sidebarOpen = useAppSelector((state) => state.sidebar.sidebarOpen);
const sidebarOpen = useSelector((state) => state.get('sidebar').sidebarOpen);
const closeButtonRef = React.useRef(null); const closeButtonRef = React.useRef(null);
@ -76,25 +78,27 @@ const SidebarMenu = () => {
onClose(); onClose();
}; };
const handleSwitchAccount = (event, account) => { const handleSwitchAccount = (account: AccountEntity): React.EventHandler<React.MouseEvent> => {
event.preventDefault(); return (e) => {
switchAccount(account); e.preventDefault();
dispatch(switchAccount(account.get('id'))); switchAccount(account);
dispatch(switchAccount(account.id));
};
}; };
const onClickLogOut = (event) => { const onClickLogOut: React.EventHandler<React.MouseEvent> = (e) => {
event.preventDefault(); e.preventDefault();
dispatch(logOut(intl)); dispatch(logOut(intl));
}; };
const handleSwitcherClick = (e) => { const handleSwitcherClick: React.EventHandler<React.MouseEvent> = (e) => {
e.preventDefault(); e.preventDefault();
setSwitcher((prevState) => (!prevState)); setSwitcher((prevState) => (!prevState));
}; };
const renderAccount = (account) => ( const renderAccount = (account: AccountEntity) => (
<a href='/' className='block py-2' onClick={(event) => handleSwitchAccount(event, account)} key={account.get('id')}> <a href='/' className='block py-2' onClick={handleSwitchAccount(account)} key={account.id}>
<Account account={account} showProfileHoverCard={false} /> <Account account={account} showProfileHoverCard={false} />
</a> </a>
); );
@ -103,17 +107,13 @@ const SidebarMenu = () => {
dispatch(fetchOwnAccounts()); dispatch(fetchOwnAccounts());
}, []); }, []);
if (!account) { if (!account) return null;
return null;
}
const acct = account.get('acct');
const classes = classNames('sidebar-menu__root', {
'sidebar-menu__root--visible': sidebarOpen,
});
return ( return (
<div className={classes}> <div className={classNames('sidebar-menu__root', {
'sidebar-menu__root--visible': sidebarOpen,
})}
>
<div <div
className={classNames({ className={classNames({
'fixed inset-0 bg-gray-600 bg-opacity-90 z-1000': true, 'fixed inset-0 bg-gray-600 bg-opacity-90 z-1000': true,
@ -150,7 +150,7 @@ const SidebarMenu = () => {
</HStack> </HStack>
<Stack space={1}> <Stack space={1}>
<Link to={`/@${acct}`} onClick={onClose}> <Link to={`/@${account.acct}`} onClick={onClose}>
<Account account={account} showProfileHoverCard={false} /> <Account account={account} showProfileHoverCard={false} />
</Link> </Link>
@ -184,7 +184,7 @@ const SidebarMenu = () => {
<hr /> <hr />
<SidebarLink <SidebarLink
to={`/@${acct}`} to={`/@${account.acct}`}
icon={require('@tabler/icons/icons/user.svg')} icon={require('@tabler/icons/icons/user.svg')}
text={intl.formatMessage(messages.profile)} text={intl.formatMessage(messages.profile)}
onClick={onClose} onClick={onClose}

View File

@ -1,6 +1,16 @@
import { SIDEBAR_OPEN, SIDEBAR_CLOSE } from '../actions/sidebar'; import { SIDEBAR_OPEN, SIDEBAR_CLOSE } from '../actions/sidebar';
export default function sidebar(state={}, action) { import type { AnyAction } from 'redux';
type State = {
sidebarOpen: boolean,
};
const initialState: State = {
sidebarOpen: false,
};
export default function sidebar(state: State = initialState, action: AnyAction): State {
switch(action.type) { switch(action.type) {
case SIDEBAR_OPEN: case SIDEBAR_OPEN:
return { sidebarOpen: true }; return { sidebarOpen: true };