2022-03-21 11:09:01 -07:00
|
|
|
import throttle from 'lodash/throttle';
|
|
|
|
import React from 'react';
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
import { useDispatch } from 'react-redux';
|
2022-03-22 05:28:18 -07:00
|
|
|
import { Link } from 'react-router-dom';
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
import { logOut, switchAccount } from 'soapbox/actions/auth';
|
|
|
|
import { fetchOwnAccounts } from 'soapbox/actions/auth';
|
|
|
|
import { Menu, MenuButton, MenuDivider, MenuItem, MenuLink, MenuList } from 'soapbox/components/ui';
|
2022-05-04 09:05:55 -07:00
|
|
|
import { useAppSelector, useFeatures } from 'soapbox/hooks';
|
2022-03-21 11:09:01 -07:00
|
|
|
import { makeGetAccount } from 'soapbox/selectors';
|
|
|
|
|
|
|
|
import Account from '../../../components/account';
|
|
|
|
|
2022-05-04 06:09:10 -07:00
|
|
|
import ThemeToggle from './theme-toggle';
|
|
|
|
|
2022-03-21 11:09:01 -07:00
|
|
|
import type { Account as AccountEntity } from 'soapbox/types/entities';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
add: { id: 'profile_dropdown.add_account', defaultMessage: 'Add an existing account' },
|
2022-05-04 06:40:04 -07:00
|
|
|
theme: { id: 'profile_dropdown.theme', defaultMessage: 'Theme' },
|
2022-03-21 11:09:01 -07:00
|
|
|
logout: { id: 'profile_dropdown.logout', defaultMessage: 'Log out @{acct}' },
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IProfileDropdown {
|
|
|
|
account: AccountEntity
|
|
|
|
}
|
|
|
|
|
|
|
|
type IMenuItem = {
|
2022-05-04 06:09:10 -07:00
|
|
|
text: string | React.ReactElement | null
|
|
|
|
to?: string
|
|
|
|
toggle?: JSX.Element
|
|
|
|
icon?: string
|
2022-03-21 11:09:01 -07:00
|
|
|
action?: (event: React.MouseEvent) => void
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:35:57 -07:00
|
|
|
const getAccount = makeGetAccount();
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
const ProfileDropdown: React.FC<IProfileDropdown> = ({ account, children }) => {
|
|
|
|
const dispatch = useDispatch();
|
2022-05-04 06:09:10 -07:00
|
|
|
const features = useFeatures();
|
2022-03-21 11:09:01 -07:00
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
const authUsers = useAppSelector((state) => state.auth.get('users'));
|
|
|
|
const otherAccounts = useAppSelector((state) => authUsers.map((authUser: any) => getAccount(state, authUser.get('id'))));
|
|
|
|
|
2022-03-22 08:41:44 -07:00
|
|
|
const handleLogOut = () => {
|
2022-03-21 11:09:01 -07:00
|
|
|
dispatch(logOut(intl));
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSwitchAccount = (account: AccountEntity) => {
|
2022-03-22 08:41:44 -07:00
|
|
|
return () => {
|
2022-03-21 11:09:01 -07:00
|
|
|
dispatch(switchAccount(account.id));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchOwnAccountThrottled = throttle(() => {
|
|
|
|
dispatch(fetchOwnAccounts());
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
const renderAccount = (account: AccountEntity) => {
|
|
|
|
return (
|
|
|
|
<Account account={account} showProfileHoverCard={false} hideActions />
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const menu: IMenuItem[] = React.useMemo(() => {
|
|
|
|
const menu: IMenuItem[] = [];
|
|
|
|
|
|
|
|
menu.push({ text: renderAccount(account), to: `/@${account.acct}` });
|
|
|
|
|
|
|
|
otherAccounts.forEach((otherAccount: AccountEntity) => {
|
|
|
|
if (otherAccount && otherAccount.id !== account.id) {
|
|
|
|
menu.push({
|
|
|
|
text: renderAccount(otherAccount),
|
|
|
|
action: handleSwitchAccount(otherAccount),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-04 09:05:55 -07:00
|
|
|
menu.push({ text: null });
|
|
|
|
menu.push({ text: intl.formatMessage(messages.theme), toggle: <ThemeToggle /> });
|
2022-03-21 11:09:01 -07:00
|
|
|
menu.push({ text: null });
|
|
|
|
|
2022-04-25 16:37:20 -07:00
|
|
|
menu.push({
|
|
|
|
text: intl.formatMessage(messages.add),
|
|
|
|
to: '/login',
|
|
|
|
icon: require('@tabler/icons/icons/plus.svg'),
|
|
|
|
});
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
menu.push({
|
|
|
|
text: intl.formatMessage(messages.logout, { acct: account.acct }),
|
2022-04-19 12:37:48 -07:00
|
|
|
to: '/logout',
|
2022-03-21 11:09:01 -07:00
|
|
|
action: handleLogOut,
|
|
|
|
icon: require('@tabler/icons/icons/logout.svg'),
|
|
|
|
});
|
|
|
|
|
|
|
|
return menu;
|
2022-05-04 06:09:10 -07:00
|
|
|
}, [account, authUsers, features]);
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
fetchOwnAccountThrottled();
|
|
|
|
}, [account, authUsers]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Menu>
|
|
|
|
<MenuButton>
|
|
|
|
{children}
|
|
|
|
</MenuButton>
|
|
|
|
|
|
|
|
<MenuList>
|
|
|
|
{menu.map((menuItem, idx) => {
|
2022-05-04 06:09:10 -07:00
|
|
|
if (menuItem.toggle) {
|
|
|
|
return (
|
|
|
|
<div className='flex flex-row items-center justify-between px-4 py-1 text-sm text-gray-700 dark:text-gray-400'>
|
|
|
|
<span>{menuItem.text}</span>
|
|
|
|
|
|
|
|
{menuItem.toggle}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else if (!menuItem.text) {
|
2022-03-21 11:09:01 -07:00
|
|
|
return <MenuDivider key={idx} />;
|
|
|
|
} else {
|
|
|
|
const Comp: any = menuItem.action ? MenuItem : MenuLink;
|
2022-03-22 05:28:18 -07:00
|
|
|
const itemProps = menuItem.action ? { onSelect: menuItem.action } : { to: menuItem.to, as: Link };
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
return (
|
2022-03-23 05:40:21 -07:00
|
|
|
<Comp key={idx} {...itemProps} className='truncate'>
|
2022-03-21 11:09:01 -07:00
|
|
|
{menuItem.text}
|
|
|
|
</Comp>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
</MenuList>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ProfileDropdown;
|