mirror of
https://github.com/mastodon/mastodon.git
synced 2025-10-16 14:01:42 +00:00

Some checks are pending
Check i18n / check-i18n (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (ruby) (push) Waiting to run
Crowdin / Upload translations / upload-translations (push) Waiting to run
Check formatting / lint (push) Waiting to run
CSS Linting / lint (push) Waiting to run
JavaScript Linting / lint (push) Waiting to run
Ruby Linting / lint (push) Waiting to run
JavaScript Testing / test (push) Waiting to run
Historical data migration test / test (14-alpine) (push) Waiting to run
Historical data migration test / test (15-alpine) (push) Waiting to run
Ruby Testing / build (production) (push) Waiting to run
Ruby Testing / build (test) (push) Waiting to run
Ruby Testing / test (.ruby-version) (push) Blocked by required conditions
Ruby Testing / test (3.1) (push) Blocked by required conditions
Ruby Testing / test (3.2) (push) Blocked by required conditions
Ruby Testing / Libvips tests (.ruby-version) (push) Blocked by required conditions
Ruby Testing / Libvips tests (3.1) (push) Blocked by required conditions
Ruby Testing / Libvips tests (3.2) (push) Blocked by required conditions
Ruby Testing / End to End testing (.ruby-version) (push) Blocked by required conditions
Ruby Testing / End to End testing (3.1) (push) Blocked by required conditions
Ruby Testing / End to End testing (3.2) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.10.2) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (3.1, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (3.2, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
Co-authored-by: Renaud Chaput <renchap@gmail.com>
110 lines
3.3 KiB
TypeScript
110 lines
3.3 KiB
TypeScript
import { useCallback, useEffect } from 'react';
|
|
|
|
import { useIntl, defineMessages } from 'react-intl';
|
|
|
|
import { useIdentity } from '@/mastodon/identity_context';
|
|
import { fetchRelationships, followAccount } from 'mastodon/actions/accounts';
|
|
import { openModal } from 'mastodon/actions/modal';
|
|
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<{
|
|
accountId?: string;
|
|
}> = ({ accountId }) => {
|
|
const intl = useIntl();
|
|
const dispatch = useAppDispatch();
|
|
const { signedIn } = useIdentity();
|
|
const account = useAppSelector((state) =>
|
|
accountId ? state.accounts.get(accountId) : undefined,
|
|
);
|
|
const relationship = useAppSelector((state) =>
|
|
accountId ? state.relationships.get(accountId) : undefined,
|
|
);
|
|
const following = relationship?.following || relationship?.requested;
|
|
|
|
useEffect(() => {
|
|
if (accountId && signedIn) {
|
|
dispatch(fetchRelationships([accountId]));
|
|
}
|
|
}, [dispatch, accountId, signedIn]);
|
|
|
|
const handleClick = useCallback(() => {
|
|
if (!signedIn) {
|
|
dispatch(
|
|
openModal({
|
|
modalType: 'INTERACTION',
|
|
modalProps: {
|
|
type: 'follow',
|
|
accountId: accountId,
|
|
url: account?.url,
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
|
|
if (!relationship) return;
|
|
|
|
if (accountId === me) {
|
|
return;
|
|
} else if (account && (relationship.following || relationship.requested)) {
|
|
dispatch(
|
|
openModal({ modalType: 'CONFIRM_UNFOLLOW', modalProps: { account } }),
|
|
);
|
|
} else {
|
|
dispatch(followAccount(accountId));
|
|
}
|
|
}, [dispatch, accountId, relationship, account, signedIn]);
|
|
|
|
let label;
|
|
|
|
if (!signedIn) {
|
|
label = intl.formatMessage(messages.follow);
|
|
} else if (accountId === me) {
|
|
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);
|
|
} else if (relationship.following || relationship.requested) {
|
|
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>
|
|
);
|
|
};
|