pleroma/app/soapbox/components/sidebar_menu.tsx

279 lines
9.8 KiB
TypeScript
Raw Normal View History

import classNames from 'classnames';
import React from 'react';
2022-03-21 11:09:01 -07:00
import { defineMessages, useIntl } from 'react-intl';
2022-04-12 17:52:20 -07:00
import { useDispatch } from 'react-redux';
import { Link, NavLink } from 'react-router-dom';
2021-03-26 20:34:30 -07:00
import { logOut, switchAccount } from 'soapbox/actions/auth';
import { fetchOwnAccounts } from 'soapbox/actions/auth';
2022-03-21 11:09:01 -07:00
import Account from 'soapbox/components/account';
import { Stack } from 'soapbox/components/ui';
import ProfileStats from 'soapbox/features/ui/components/profile_stats';
2022-04-12 17:52:20 -07:00
import { useAppSelector, useSoapboxConfig, useFeatures } from 'soapbox/hooks';
2022-01-10 14:01:24 -08:00
import { closeSidebar } from '../actions/sidebar';
import { makeGetAccount, makeGetOtherAccounts } from '../selectors';
2022-03-21 11:09:01 -07:00
import { HStack, Icon, IconButton, Text } from './ui';
2020-03-27 13:59:38 -07:00
2022-04-12 17:52:20 -07:00
import type { List as ImmutableList } from 'immutable';
import type { Account as AccountEntity } from 'soapbox/types/entities';
2020-03-27 13:59:38 -07:00
const messages = defineMessages({
followers: { id: 'account.followers', defaultMessage: 'Followers' },
follows: { id: 'account.follows', defaultMessage: 'Follows' },
profile: { id: 'account.profile', defaultMessage: 'Profile' },
preferences: { id: 'navigation_bar.preferences', defaultMessage: 'Preferences' },
blocks: { id: 'navigation_bar.blocks', defaultMessage: 'Blocked users' },
2022-03-21 11:39:12 -07:00
domainBlocks: { id: 'navigation_bar.domain_blocks', defaultMessage: 'Hidden domains' },
2020-03-27 13:59:38 -07:00
mutes: { id: 'navigation_bar.mutes', defaultMessage: 'Muted users' },
filters: { id: 'navigation_bar.filters', defaultMessage: 'Muted words' },
2022-03-21 11:09:01 -07:00
soapboxConfig: { id: 'navigation_bar.soapbox_config', defaultMessage: 'Soapbox config' },
importData: { id: 'navigation_bar.import_data', defaultMessage: 'Import data' },
accountMigration: { id: 'navigation_bar.account_migration', defaultMessage: 'Move account' },
2020-03-27 13:59:38 -07:00
logout: { id: 'navigation_bar.logout', defaultMessage: 'Logout' },
2020-04-14 11:44:40 -07:00
});
2020-03-27 13:59:38 -07:00
2022-04-12 17:52:20 -07:00
interface ISidebarLink {
to: string,
icon: string,
text: string,
onClick: React.EventHandler<React.MouseEvent>,
}
const SidebarLink: React.FC<ISidebarLink> = ({ to, icon, text, onClick }) => (
2022-03-21 11:09:01 -07:00
<NavLink className='group py-1 rounded-md' to={to} onClick={onClick}>
<HStack space={2} alignItems='center'>
<div className='bg-primary-50 dark:bg-slate-700 relative rounded inline-flex p-2'>
2022-03-21 11:09:01 -07:00
<Icon src={icon} className='text-primary-600 h-5 w-5' />
</div>
<Text tag='span' weight='medium' theme='muted' className='group-hover:text-gray-800 dark:group-hover:text-gray-200'>{text}</Text>
2022-03-21 11:09:01 -07:00
</HStack>
</NavLink>
);
2022-04-12 17:52:20 -07:00
const getOtherAccounts = makeGetOtherAccounts();
2022-03-21 11:09:01 -07:00
2022-04-12 17:52:20 -07:00
const SidebarMenu: React.FC = (): JSX.Element | null => {
2022-03-21 11:09:01 -07:00
const intl = useIntl();
const dispatch = useDispatch();
2022-04-12 17:52:20 -07:00
const { logo } = useSoapboxConfig();
const features = useFeatures();
2020-03-27 13:59:38 -07:00
const getAccount = makeGetAccount();
2022-04-12 17:52:20 -07:00
const me = useAppSelector((state) => state.me);
const account = useAppSelector((state) => me ? getAccount(state, me) : null);
const otherAccounts: ImmutableList<AccountEntity> = useAppSelector((state) => getOtherAccounts(state));
const sidebarOpen = useAppSelector((state) => state.sidebar.sidebarOpen);
2020-03-27 13:59:38 -07:00
2022-03-21 11:09:01 -07:00
const closeButtonRef = React.useRef(null);
2022-03-21 11:09:01 -07:00
const [switcher, setSwitcher] = React.useState(false);
2020-03-27 13:59:38 -07:00
2022-03-21 11:09:01 -07:00
const onClose = () => dispatch(closeSidebar());
2020-03-27 13:59:38 -07:00
2022-03-21 11:09:01 -07:00
const handleClose = () => {
setSwitcher(false);
onClose();
2020-03-27 13:59:38 -07:00
};
2022-04-12 17:52:20 -07:00
const handleSwitchAccount = (account: AccountEntity): React.EventHandler<React.MouseEvent> => {
return (e) => {
e.preventDefault();
switchAccount(account);
dispatch(switchAccount(account.id));
};
2022-03-21 11:09:01 -07:00
};
2022-04-12 17:52:20 -07:00
const onClickLogOut: React.EventHandler<React.MouseEvent> = (e) => {
e.preventDefault();
2022-03-21 11:09:01 -07:00
dispatch(logOut(intl));
};
2021-03-26 20:34:30 -07:00
2022-04-12 17:52:20 -07:00
const handleSwitcherClick: React.EventHandler<React.MouseEvent> = (e) => {
2021-03-26 20:34:30 -07:00
e.preventDefault();
2022-03-21 11:09:01 -07:00
setSwitcher((prevState) => (!prevState));
};
2021-03-26 20:34:30 -07:00
2022-04-12 17:52:20 -07:00
const renderAccount = (account: AccountEntity) => (
<a href='/' className='block py-2' onClick={handleSwitchAccount(account)} key={account.id}>
2022-03-21 11:09:01 -07:00
<Account account={account} showProfileHoverCard={false} />
</a>
);
2022-03-21 11:09:01 -07:00
React.useEffect(() => {
dispatch(fetchOwnAccounts());
}, []);
2022-04-12 17:52:20 -07:00
if (!account) return null;
2022-03-21 11:09:01 -07:00
return (
2022-04-12 17:52:20 -07:00
<div className={classNames('sidebar-menu__root', {
'sidebar-menu__root--visible': sidebarOpen,
})}
>
2022-03-21 11:09:01 -07:00
<div
className={classNames({
'fixed inset-0 bg-gray-600 bg-opacity-90 z-1000': true,
'hidden': !sidebarOpen,
})}
role='button'
onClick={handleClose}
/>
<div className='sidebar-menu'>
<div className='relative overflow-y-scroll overflow-auto h-full w-full'>
<div className='p-4'>
<Stack space={4}>
<HStack alignItems='center' justifyContent='between'>
<Link to='/' onClick={onClose}>
{logo ? (
<img alt='Logo' src={logo} className='h-5 w-auto min-w-[140px] cursor-pointer' />
): (
<Icon
alt='Logo'
src={require('@tabler/icons/icons/home.svg')}
className='h-6 w-6 text-gray-400 hover:text-gray-600 dark:text-gray-200 cursor-pointer'
/>
)}
2022-03-21 11:09:01 -07:00
</Link>
2021-03-26 20:34:30 -07:00
2022-03-21 11:09:01 -07:00
<IconButton
title='close'
onClick={handleClose}
src={require('@tabler/icons/icons/x.svg')}
ref={closeButtonRef}
className='text-gray-400 hover:text-gray-600'
/>
</HStack>
<Stack space={1}>
2022-04-12 17:52:20 -07:00
<Link to={`/@${account.acct}`} onClick={onClose}>
2022-03-21 11:09:01 -07:00
<Account account={account} showProfileHoverCard={false} />
2020-03-27 13:59:38 -07:00
</Link>
{account.staff && (
2022-03-21 11:09:01 -07:00
<Stack>
<button type='button' onClick={handleSwitcherClick} className='py-1'>
<HStack alignItems='center' justifyContent='between'>
<Text tag='span' size='sm' weight='medium'>Switch accounts</Text>
<Icon
src={switcher ? require('@tabler/icons/icons/chevron-up.svg') : require('@tabler/icons/icons/chevron-down.svg')} className='sidebar-menu-profile__caret'
/>
</HStack>
</button>
{switcher && (
<div className='border-t border-solid border-gray-200'>
{otherAccounts.map(account => renderAccount(account))}
</div>
)}
</Stack>
)}
</Stack>
<ProfileStats
account={account}
onClickHandler={handleClose}
/>
<Stack space={2}>
<hr />
<SidebarLink
2022-04-12 17:52:20 -07:00
to={`/@${account.acct}`}
2022-03-21 11:09:01 -07:00
icon={require('@tabler/icons/icons/user.svg')}
text={intl.formatMessage(messages.profile)}
onClick={onClose}
/>
<hr />
<SidebarLink
to='/blocks'
icon={require('@tabler/icons/icons/ban.svg')}
text={intl.formatMessage(messages.blocks)}
onClick={onClose}
/>
<SidebarLink
to='/mutes'
icon={require('@tabler/icons/icons/circle-x.svg')}
text={intl.formatMessage(messages.mutes)}
onClick={onClose}
/>
<SidebarLink
to='/settings/preferences'
icon={require('@tabler/icons/icons/settings.svg')}
text={intl.formatMessage(messages.preferences)}
onClick={onClose}
/>
{features.federating && (
<SidebarLink
to='/domain_blocks'
icon={require('@tabler/icons/icons/ban.svg')}
2022-03-21 11:39:12 -07:00
text={intl.formatMessage(messages.domainBlocks)}
2022-03-21 11:09:01 -07:00
onClick={onClose}
/>
)}
{features.filters && (
<SidebarLink
to='/filters'
icon={require('@tabler/icons/icons/filter.svg')}
text={intl.formatMessage(messages.filters)}
onClick={onClose}
/>
)}
{account.admin && (
2022-03-21 11:09:01 -07:00
<SidebarLink
to='/soapbox/config'
icon={require('@tabler/icons/icons/settings.svg')}
text={intl.formatMessage(messages.soapboxConfig)}
onClick={onClose}
/>
)}
{features.importAPI && (
<SidebarLink
to='/settings/import'
icon={require('@tabler/icons/icons/cloud-upload.svg')}
text={intl.formatMessage(messages.importData)}
onClick={onClose}
/>
)}
{(features.federating && features.accountMoving) && (
<SidebarLink
to='/settings/migration'
icon={require('@tabler/icons/icons/briefcase.svg')}
text={intl.formatMessage(messages.accountMigration)}
onClick={onClose}
/>
)}
<hr />
<SidebarLink
to='/auth/sign_out'
icon={require('@tabler/icons/icons/logout.svg')}
2022-03-21 11:09:01 -07:00
text={intl.formatMessage(messages.logout)}
onClick={onClickLogOut}
/>
</Stack>
</Stack>
2020-03-27 13:59:38 -07:00
</div>
</div>
</div>
2022-03-21 11:09:01 -07:00
</div>
);
};
2020-03-27 13:59:38 -07:00
2022-03-21 11:09:01 -07:00
export default SidebarMenu;