2024-06-26 20:33:38 +01:00
|
|
|
import { useCallback, useEffect } from 'react';
|
|
|
|
|
2024-07-25 18:05:54 +01:00
|
|
|
import { useIntl, defineMessages } from 'react-intl';
|
2024-06-26 20:33:38 +01:00
|
|
|
|
2024-06-27 23:01:40 +01:00
|
|
|
import { useIdentity } from '@/mastodon/identity_context';
|
2024-07-25 18:05:54 +01:00
|
|
|
import { fetchRelationships, followAccount } from 'mastodon/actions/accounts';
|
2024-06-27 23:01:40 +01:00
|
|
|
import { openModal } from 'mastodon/actions/modal';
|
2024-06-26 20:33:38 +01:00
|
|
|
import { Button } from 'mastodon/components/button';
|
|
|
|
import { LoadingIndicator } from 'mastodon/components/loading_indicator';
|
|
|
|
import { me } from 'mastodon/initial_state';
|
|
|
|
import { useAppDispatch, useAppSelector } from 'mastodon/store';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
unfollow: { id: 'account.unfollow', defaultMessage: 'Unfollow' },
|
|
|
|
follow: { id: 'account.follow', defaultMessage: 'Follow' },
|
|
|
|
followBack: { id: 'account.follow_back', defaultMessage: 'Follow back' },
|
|
|
|
mutual: { id: 'account.mutual', defaultMessage: 'Mutual' },
|
|
|
|
edit_profile: { id: 'account.edit_profile', defaultMessage: 'Edit profile' },
|
|
|
|
});
|
|
|
|
|
|
|
|
export const FollowButton: React.FC<{
|
2024-07-01 16:52:01 +01:00
|
|
|
accountId?: string;
|
2024-06-26 20:33:38 +01:00
|
|
|
}> = ({ accountId }) => {
|
|
|
|
const intl = useIntl();
|
|
|
|
const dispatch = useAppDispatch();
|
2024-06-27 23:01:40 +01:00
|
|
|
const { signedIn } = useIdentity();
|
|
|
|
const account = useAppSelector((state) =>
|
|
|
|
accountId ? state.accounts.get(accountId) : undefined,
|
|
|
|
);
|
2024-06-26 20:33:38 +01:00
|
|
|
const relationship = useAppSelector((state) =>
|
2024-07-01 16:52:01 +01:00
|
|
|
accountId ? state.relationships.get(accountId) : undefined,
|
2024-06-26 20:33:38 +01:00
|
|
|
);
|
|
|
|
const following = relationship?.following || relationship?.requested;
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-06-27 23:01:40 +01:00
|
|
|
if (accountId && signedIn) {
|
|
|
|
dispatch(fetchRelationships([accountId]));
|
|
|
|
}
|
|
|
|
}, [dispatch, accountId, signedIn]);
|
2024-06-26 20:33:38 +01:00
|
|
|
|
|
|
|
const handleClick = useCallback(() => {
|
2024-06-27 23:01:40 +01:00
|
|
|
if (!signedIn) {
|
|
|
|
dispatch(
|
|
|
|
openModal({
|
|
|
|
modalType: 'INTERACTION',
|
|
|
|
modalProps: {
|
|
|
|
type: 'follow',
|
|
|
|
accountId: accountId,
|
|
|
|
url: account?.url,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-26 20:33:38 +01:00
|
|
|
if (!relationship) return;
|
2024-06-27 23:01:40 +01:00
|
|
|
|
2024-06-26 20:33:38 +01:00
|
|
|
if (accountId === me) {
|
|
|
|
return;
|
2024-07-25 18:05:54 +01:00
|
|
|
} else if (account && (relationship.following || relationship.requested)) {
|
2024-07-01 19:10:22 +01:00
|
|
|
dispatch(
|
2024-07-25 18:05:54 +01:00
|
|
|
openModal({ modalType: 'CONFIRM_UNFOLLOW', modalProps: { account } }),
|
2024-07-01 19:10:22 +01:00
|
|
|
);
|
2024-06-26 20:33:38 +01:00
|
|
|
} else {
|
|
|
|
dispatch(followAccount(accountId));
|
|
|
|
}
|
2024-07-25 18:05:54 +01:00
|
|
|
}, [dispatch, accountId, relationship, account, signedIn]);
|
2024-06-26 20:33:38 +01:00
|
|
|
|
|
|
|
let label;
|
|
|
|
|
2024-06-27 23:01:40 +01:00
|
|
|
if (!signedIn) {
|
|
|
|
label = intl.formatMessage(messages.follow);
|
|
|
|
} else if (accountId === me) {
|
2024-06-26 20:33:38 +01:00
|
|
|
label = intl.formatMessage(messages.edit_profile);
|
|
|
|
} else if (!relationship) {
|
|
|
|
label = <LoadingIndicator />;
|
|
|
|
} else if (relationship.following && relationship.followed_by) {
|
|
|
|
label = intl.formatMessage(messages.mutual);
|
|
|
|
} else if (!relationship.following && relationship.followed_by) {
|
|
|
|
label = intl.formatMessage(messages.followBack);
|
2024-07-01 19:10:22 +01:00
|
|
|
} else if (relationship.following || relationship.requested) {
|
2024-06-26 20:33:38 +01:00
|
|
|
label = intl.formatMessage(messages.unfollow);
|
|
|
|
} else {
|
|
|
|
label = intl.formatMessage(messages.follow);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accountId === me) {
|
|
|
|
return (
|
|
|
|
<a
|
|
|
|
href='/settings/profile'
|
|
|
|
target='_blank'
|
|
|
|
rel='noreferrer noopener'
|
|
|
|
className='button button-secondary'
|
|
|
|
>
|
|
|
|
{label}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
onClick={handleClick}
|
|
|
|
disabled={relationship?.blocked_by || relationship?.blocking}
|
|
|
|
secondary={following}
|
|
|
|
className={following ? 'button--destructive' : undefined}
|
|
|
|
>
|
|
|
|
{label}
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
};
|