bigbuffet-rw/app/soapbox/components/sidebar_menu.tsx

345 lines
13 KiB
TypeScript
Raw Normal View History

import classNames from 'clsx';
import React from 'react';
2022-04-12 18:10:47 -07:00
import { defineMessages, useIntl, FormattedMessage } from 'react-intl';
2022-04-12 17:52:20 -07:00
import { useDispatch } from 'react-redux';
import { Link, NavLink } from 'react-router-dom';
import { fetchOwnAccounts, logOut, switchAccount } from 'soapbox/actions/auth';
import { getSettings } from 'soapbox/actions/settings';
import { closeSidebar } from 'soapbox/actions/sidebar';
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';
import { useAppSelector, useFeatures } from 'soapbox/hooks';
import { makeGetAccount, makeGetOtherAccounts } from 'soapbox/selectors';
import { Divider, 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' },
accountAliases: { id: 'navigation_bar.account_aliases', defaultMessage: 'Account aliases' },
2020-03-27 13:59:38 -07:00
logout: { id: 'navigation_bar.logout', defaultMessage: 'Logout' },
bookmarks: { id: 'column.bookmarks', defaultMessage: 'Bookmarks' },
lists: { id: 'column.lists', defaultMessage: 'Lists' },
invites: { id: 'navigation_bar.invites', defaultMessage: 'Invites' },
developers: { id: 'navigation.developers', defaultMessage: 'Developers' },
addAccount: { id: 'profile_dropdown.add_account', defaultMessage: 'Add an existing account' },
followRequests: { id: 'navigation_bar.follow_requests', defaultMessage: 'Follow requests' },
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 {
href?: string,
to?: string,
2022-04-12 17:52:20 -07:00
icon: string,
2022-04-12 18:10:47 -07:00
text: string | JSX.Element,
2022-04-12 17:52:20 -07:00
onClick: React.EventHandler<React.MouseEvent>,
}
const SidebarLink: React.FC<ISidebarLink> = ({ href, to, icon, text, onClick }) => {
const body = (
2022-03-21 11:09:01 -07:00
<HStack space={2} alignItems='center'>
<div className='bg-primary-50 dark:bg-gray-800 relative rounded-full inline-flex p-2'>
<Icon src={icon} className='text-primary-500 h-5 w-5' />
2022-03-21 11:09:01 -07:00
</div>
<Text tag='span' weight='medium' theme='inherit'>{text}</Text>
2022-03-21 11:09:01 -07:00
</HStack>
);
if (to) {
return (
<NavLink className='group rounded-full text-gray-900 dark:text-gray-100 hover:bg-gray-50 dark:hover:bg-gray-800' to={to} onClick={onClick}>
{body}
</NavLink>
);
}
return (
<a className='group rounded-full text-gray-900 dark:text-gray-100 hover:bg-gray-50 dark:hover:bg-gray-800' href={href} target='_blank' onClick={onClick}>
{body}
</a>
);
};
2022-03-21 11:09:01 -07:00
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 features = useFeatures();
2020-03-27 13:59:38 -07:00
const getAccount = makeGetAccount();
2022-04-12 18:10:47 -07:00
const instance = useAppSelector((state) => state.instance);
2022-04-12 17:52:20 -07:00
const me = useAppSelector((state) => state.me);
2022-07-01 13:07:01 -07:00
const account = useAppSelector((state) => me ? getAccount(state, me) : null);
2022-04-12 17:52:20 -07:00
const otherAccounts: ImmutableList<AccountEntity> = useAppSelector((state) => getOtherAccounts(state));
const sidebarOpen = useAppSelector((state) => state.sidebar.sidebarOpen);
const settings = useAppSelector((state) => getSettings(state));
const followRequestsCount = useAppSelector((state) => state.user_lists.follow_requests.items.count());
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-25 16:26:17 -07:00
const handleSwitchAccount = (account: AccountEntity): React.MouseEventHandler => {
2022-04-12 17:52:20 -07:00
return (e) => {
e.preventDefault();
dispatch(switchAccount(account.id));
};
2022-03-21 11:09:01 -07:00
};
2022-04-25 16:26:17 -07:00
const onClickLogOut: React.MouseEventHandler = (e) => {
2022-04-12 17:52:20 -07:00
e.preventDefault();
dispatch(logOut());
2022-03-21 11:09:01 -07:00
};
2021-03-26 20:34:30 -07:00
2022-04-25 16:26:17 -07:00
const handleSwitcherClick: React.MouseEventHandler = (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) => (
2022-04-25 16:26:17 -07:00
<a href='#' className='block py-2' onClick={handleSwitchAccount(account)} key={account.id}>
<div className='pointer-events-none'>
2022-07-01 13:07:01 -07:00
<Account account={account} showProfileHoverCard={false} withRelationship={false} withLinkToProfile={false} />
2022-04-25 16:26:17 -07:00
</div>
2022-03-21 11:09:01 -07:00
</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-500/90 dark:bg-gray-700/90 z-1000': true,
2022-03-21 11:09:01 -07:00
'hidden': !sidebarOpen,
})}
role='button'
onClick={handleClose}
>
<IconButton
title='close'
onClick={handleClose}
src={require('@tabler/icons/x.svg')}
ref={closeButtonRef}
iconClassName='h-6 w-6'
className='fixed top-5 right-5 text-gray-600 dark:text-gray-400 hover:text-gray-600 dark:hover:text-gray-300'
/>
</div>
2022-03-21 11:09:01 -07:00
<div className='sidebar-menu'>
<div className='relative overflow-y-scroll overflow-auto h-full w-full'>
<div className='p-4'>
<Stack space={4}>
<Link to={`/@${account.acct}`} onClick={onClose}>
<Account account={account} showProfileHoverCard={false} withLinkToProfile={false} />
</Link>
2022-03-21 11:09:01 -07:00
<ProfileStats
account={account}
onClickHandler={handleClose}
/>
<Stack space={4}>
<Divider />
2022-03-21 11:09:01 -07:00
<SidebarLink
2022-04-12 17:52:20 -07:00
to={`/@${account.acct}`}
icon={require('@tabler/icons/user.svg')}
2022-03-21 11:09:01 -07:00
text={intl.formatMessage(messages.profile)}
onClick={onClose}
/>
{(account.locked || followRequestsCount > 0) && (
<SidebarLink
to='/follow_requests'
icon={require('@tabler/icons/user-plus.svg')}
text={intl.formatMessage(messages.followRequests)}
onClick={onClose}
/>
)}
{features.bookmarks && (
<SidebarLink
to='/bookmarks'
icon={require('@tabler/icons/bookmark.svg')}
text={intl.formatMessage(messages.bookmarks)}
onClick={onClose}
/>
)}
{features.lists && (
<SidebarLink
to='/lists'
icon={require('@tabler/icons/list.svg')}
text={intl.formatMessage(messages.lists)}
onClick={onClose}
/>
)}
{settings.get('isDeveloper') && (
<SidebarLink
to='/developers'
icon={require('@tabler/icons/code.svg')}
text={intl.formatMessage(messages.developers)}
onClick={onClose}
/>
)}
2022-04-23 13:40:54 -07:00
{features.publicTimeline && <>
<Divider />
2022-04-12 18:10:47 -07:00
<SidebarLink
to='/timeline/local'
icon={features.federating ? require('@tabler/icons/users.svg') : require('@tabler/icons/world.svg')}
2022-04-12 18:10:47 -07:00
text={features.federating ? instance.title : <FormattedMessage id='tabs_bar.all' defaultMessage='All' />}
onClick={onClose}
/>
2022-04-23 13:40:54 -07:00
{features.federating && (
<SidebarLink
to='/timeline/fediverse'
icon={require('icons/fediverse.svg')}
text={<FormattedMessage id='tabs_bar.fediverse' defaultMessage='Fediverse' />}
onClick={onClose}
/>
)}
</>}
2022-04-12 18:10:47 -07:00
<Divider />
2022-03-21 11:09:01 -07:00
<SidebarLink
to='/blocks'
icon={require('@tabler/icons/ban.svg')}
2022-03-21 11:09:01 -07:00
text={intl.formatMessage(messages.blocks)}
onClick={onClose}
/>
<SidebarLink
to='/mutes'
icon={require('@tabler/icons/circle-x.svg')}
2022-03-21 11:09:01 -07:00
text={intl.formatMessage(messages.mutes)}
onClick={onClose}
/>
<SidebarLink
to='/settings/preferences'
icon={require('@tabler/icons/settings.svg')}
2022-03-21 11:09:01 -07:00
text={intl.formatMessage(messages.preferences)}
onClick={onClose}
/>
{features.federating && (
<SidebarLink
to='/domain_blocks'
icon={require('@tabler/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/filter.svg')}
2022-03-21 11:09:01 -07:00
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/settings.svg')}
2022-03-21 11:09:01 -07:00
text={intl.formatMessage(messages.soapboxConfig)}
onClick={onClose}
/>
)}
{features.import && (
2022-03-21 11:09:01 -07:00
<SidebarLink
to='/settings/import'
icon={require('@tabler/icons/cloud-upload.svg')}
2022-03-21 11:09:01 -07:00
text={intl.formatMessage(messages.importData)}
onClick={onClose}
/>
)}
<Divider />
2022-03-21 11:09:01 -07:00
<SidebarLink
2022-04-19 12:37:48 -07:00
to='/logout'
icon={require('@tabler/icons/logout.svg')}
2022-03-21 11:09:01 -07:00
text={intl.formatMessage(messages.logout)}
onClick={onClickLogOut}
/>
<Divider />
<Stack space={4}>
<button type='button' onClick={handleSwitcherClick} className='py-1'>
<HStack alignItems='center' justifyContent='between'>
<Text tag='span'>
<FormattedMessage id='profile_dropdown.switch_account' defaultMessage='Switch accounts' />
</Text>
<Icon
src={require('@tabler/icons/chevron-down.svg')}
className={classNames('w-4 h-4 text-gray-900 dark:text-gray-100 transition-transform', {
'rotate-180': switcher,
})}
/>
</HStack>
</button>
{switcher && (
<div className='border-t-2 border-gray-100 dark:border-gray-800 border-solid'>
{otherAccounts.map(account => renderAccount(account))}
<NavLink className='flex items-center py-2 space-x-1' to='/login/add' onClick={handleClose}>
<Icon className='text-primary-500 w-4 h-4' src={require('@tabler/icons/plus.svg')} />
<Text size='sm' weight='medium'>{intl.formatMessage(messages.addAccount)}</Text>
</NavLink>
</div>
)}
</Stack>
2022-03-21 11:09:01 -07:00
</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;