2022-09-13 01:21:56 -07:00
|
|
|
import React, { useCallback, useEffect } from 'react';
|
2022-05-28 12:53:22 -07:00
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
|
|
|
|
import { fetchAccount } from 'soapbox/actions/accounts';
|
|
|
|
import { addToMentions, removeFromMentions } from 'soapbox/actions/compose';
|
2022-11-25 09:04:11 -08:00
|
|
|
import AccountComponent from 'soapbox/components/account';
|
2022-11-15 06:11:30 -08:00
|
|
|
import IconButton from 'soapbox/components/icon-button';
|
2022-11-25 09:04:11 -08:00
|
|
|
import { HStack } from 'soapbox/components/ui';
|
2022-09-14 11:01:00 -07:00
|
|
|
import { useAppDispatch, useAppSelector, useCompose } from 'soapbox/hooks';
|
2022-05-28 12:53:22 -07:00
|
|
|
import { makeGetAccount } from 'soapbox/selectors';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
remove: { id: 'reply_mentions.account.remove', defaultMessage: 'Remove from mentions' },
|
|
|
|
add: { id: 'reply_mentions.account.add', defaultMessage: 'Add to mentions' },
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IAccount {
|
2022-09-10 14:52:06 -07:00
|
|
|
composeId: string,
|
2022-05-28 12:53:22 -07:00
|
|
|
accountId: string,
|
|
|
|
author: boolean,
|
|
|
|
}
|
|
|
|
|
2022-09-10 14:52:06 -07:00
|
|
|
const Account: React.FC<IAccount> = ({ composeId, accountId, author }) => {
|
2022-05-28 12:53:22 -07:00
|
|
|
const intl = useIntl();
|
|
|
|
const dispatch = useAppDispatch();
|
2022-09-13 01:21:56 -07:00
|
|
|
const getAccount = useCallback(makeGetAccount(), []);
|
2022-05-28 12:53:22 -07:00
|
|
|
|
2022-09-14 13:05:40 -07:00
|
|
|
const compose = useCompose(composeId);
|
|
|
|
|
2022-05-28 12:53:22 -07:00
|
|
|
const account = useAppSelector((state) => getAccount(state, accountId));
|
2022-09-14 13:05:40 -07:00
|
|
|
const added = !!account && compose.to?.includes(account.acct);
|
2022-05-28 12:53:22 -07:00
|
|
|
|
2022-09-14 13:05:40 -07:00
|
|
|
const onRemove = () => dispatch(removeFromMentions(composeId, accountId));
|
|
|
|
const onAdd = () => dispatch(addToMentions(composeId, accountId));
|
2022-05-28 12:53:22 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (accountId && !account) {
|
|
|
|
dispatch(fetchAccount(accountId));
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (!account) return null;
|
|
|
|
|
|
|
|
let button;
|
|
|
|
|
|
|
|
if (added) {
|
2022-11-25 09:04:11 -08:00
|
|
|
button = <IconButton src={require('@tabler/icons/x.svg')} iconClassName='h-5 w-5' title={intl.formatMessage(messages.remove)} onClick={onRemove} />;
|
2022-05-28 12:53:22 -07:00
|
|
|
} else {
|
2022-11-25 09:04:11 -08:00
|
|
|
button = <IconButton src={require('@tabler/icons/plus.svg')} iconClassName='h-5 w-5' title={intl.formatMessage(messages.add)} onClick={onAdd} />;
|
2022-05-28 12:53:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-11-25 09:04:11 -08:00
|
|
|
<HStack space={1} alignItems='center' justifyContent='between' className='p-2.5'>
|
|
|
|
<div className='w-full'>
|
|
|
|
<AccountComponent account={account} withRelationship={false} />
|
2022-05-28 12:53:22 -07:00
|
|
|
</div>
|
2022-11-25 09:04:11 -08:00
|
|
|
{!author && button}
|
|
|
|
</HStack>
|
2022-05-28 12:53:22 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Account;
|