bigbuffet-rw/app/soapbox/features/ui/components/profile-dropdown.tsx

138 lines
4 KiB
TypeScript
Raw Normal View History

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 { fetchOwnAccounts, logOut, switchAccount } from 'soapbox/actions/auth';
import Account from 'soapbox/components/account';
2022-03-21 11:09:01 -07:00
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';
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
}
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'))));
const handleLogOut = () => {
2022-03-21 11:09:01 -07:00
dispatch(logOut(intl));
};
const handleSwitchAccount = (account: AccountEntity) => {
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 });
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 (
2022-05-24 06:46:21 -07:00
<div key={idx} className='flex flex-row items-center justify-between px-4 py-1 text-sm text-gray-700 dark:text-gray-400'>
2022-05-04 06:09:10 -07:00
<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 (
<Comp key={idx} {...itemProps} className='truncate'>
2022-03-21 11:09:01 -07:00
{menuItem.text}
</Comp>
);
}
})}
</MenuList>
</Menu>
);
};
export default ProfileDropdown;