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

182 lines
5 KiB
TypeScript
Raw Normal View History

import { useFloating } from '@floating-ui/react';
import clsx from 'clsx';
2022-03-21 11:09:01 -07:00
import throttle from 'lodash/throttle';
import React, { useEffect, useMemo, useState } from 'react';
2022-03-21 11:09:01 -07:00
import { defineMessages, useIntl } from 'react-intl';
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';
import { MenuDivider } from 'soapbox/components/ui';
import { useAppDispatch, useAppSelector, useClickOutside, 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
2023-01-10 15:03:15 -08:00
children: React.ReactNode
2022-03-21 11:09:01 -07:00
}
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 = useAppDispatch();
2022-05-04 06:09:10 -07:00
const features = useFeatures();
2022-03-21 11:09:01 -07:00
const intl = useIntl();
const [visible, setVisible] = useState(false);
const { x, y, strategy, refs } = useFloating<HTMLButtonElement>({ placement: 'bottom-end' });
const authUsers = useAppSelector((state) => state.auth.users);
const otherAccounts = useAppSelector((state) => authUsers.map((authUser: any) => getAccount(state, authUser.id)!));
2022-03-21 11:09:01 -07:00
const handleLogOut = () => {
dispatch(logOut());
2022-03-21 11:09:01 -07:00
};
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 (
2022-07-01 13:07:01 -07:00
<Account account={account} showProfileHoverCard={false} withLinkToProfile={false} hideActions />
2022-03-21 11:09:01 -07:00
);
};
const menu: IMenuItem[] = useMemo(() => {
2022-03-21 11:09:01 -07:00
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/add',
icon: require('@tabler/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/logout.svg'),
2022-03-21 11:09:01 -07:00
});
return menu;
2022-05-04 06:09:10 -07:00
}, [account, authUsers, features]);
2022-03-21 11:09:01 -07:00
const toggleVisible = () => setVisible(!visible);
useEffect(() => {
2022-03-21 11:09:01 -07:00
fetchOwnAccountThrottled();
}, [account, authUsers]);
useClickOutside(refs, () => {
setVisible(false);
});
2022-03-21 11:09:01 -07:00
return (
<>
<button type='button' ref={refs.setReference} onClick={toggleVisible}>
2022-03-21 11:09:01 -07:00
{children}
</button>
{visible && (
<div
ref={refs.setFloating}
2023-02-07 10:02:37 -08:00
className='z-[1003] mt-2 max-w-xs rounded-md bg-white shadow-lg focus:outline-none dark:bg-gray-900 dark:ring-2 dark:ring-primary-700'
style={{
position: strategy,
top: y ?? 0,
left: x ?? 0,
width: 'max-content',
}}
>
{menu.map((menuItem, i) => (
<MenuItem key={i} menuItem={menuItem} />
))}
</div>
)}
</>
2022-03-21 11:09:01 -07:00
);
};
interface MenuItemProps {
className?: string
menuItem: IMenuItem
}
const MenuItem: React.FC<MenuItemProps> = ({ className, menuItem }) => {
const baseClassName = clsx(className, 'block w-full cursor-pointer truncate px-4 py-2.5 text-left text-sm text-gray-700 hover:bg-gray-100 rtl:text-right dark:text-gray-500 dark:hover:bg-gray-800');
if (menuItem.toggle) {
return (
<div className='flex flex-row items-center justify-between space-x-4 px-4 py-1 text-sm text-gray-700 dark:text-gray-400'>
<span>{menuItem.text}</span>
{menuItem.toggle}
</div>
);
} else if (!menuItem.text) {
return <MenuDivider />;
} else if (menuItem.action) {
return (
<button
type='button'
onClick={menuItem.action}
className={baseClassName}
>
{menuItem.text}
</button>
);
} else if (menuItem.to) {
return (
<Link
to={menuItem.to}
className={baseClassName}
>
{menuItem.text}
</Link>
);
} else {
throw menuItem;
}
};
2022-03-21 11:09:01 -07:00
export default ProfileDropdown;