2022-08-23 04:29:37 +03:00
|
|
|
/* eslint-disable react/no-unescaped-entities */
|
2022-08-23 04:27:47 +03:00
|
|
|
import { Input, Button, Alert, Spin, Space } from 'antd';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { FC, useState } from 'react';
|
|
|
|
import styles from './FollowModal.module.scss';
|
2022-06-25 01:55:53 +03:00
|
|
|
|
|
|
|
const ENDPOINT = '/api/remotefollow';
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export type FollowModalProps = {
|
2022-06-25 01:55:53 +03:00
|
|
|
handleClose: () => void;
|
2022-08-23 04:27:47 +03:00
|
|
|
account: string;
|
|
|
|
name: string;
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
2022-06-25 01:55:53 +03:00
|
|
|
|
|
|
|
function validateAccount(a) {
|
|
|
|
const sanitized = a.replace(/^@+/, '');
|
|
|
|
const regex =
|
|
|
|
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
|
|
|
return regex.test(String(sanitized).toLowerCase());
|
|
|
|
}
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export const FollowModal: FC<FollowModalProps> = ({ handleClose, account, name }) => {
|
2022-08-23 04:27:47 +03:00
|
|
|
const [remoteAccount, setRemoteAccount] = useState(null);
|
2022-06-25 01:55:53 +03:00
|
|
|
const [valid, setValid] = useState(false);
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [errorMessage, setErrorMessage] = useState(null);
|
|
|
|
|
|
|
|
const handleAccountChange = a => {
|
2022-08-23 04:27:47 +03:00
|
|
|
setRemoteAccount(a);
|
2022-06-25 01:55:53 +03:00
|
|
|
if (validateAccount(a)) {
|
|
|
|
setValid(true);
|
|
|
|
} else {
|
|
|
|
setValid(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-23 04:27:47 +03:00
|
|
|
const joinButtonPressed = () => {
|
|
|
|
window.open('https://owncast.online/join-fediverse', '_blank');
|
|
|
|
};
|
|
|
|
|
2022-06-25 01:55:53 +03:00
|
|
|
const remoteFollowButtonPressed = async () => {
|
|
|
|
if (!valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLoading(true);
|
|
|
|
|
|
|
|
try {
|
2022-08-23 04:27:47 +03:00
|
|
|
const sanitizedAccount = remoteAccount.replace(/^@+/, '');
|
2022-06-25 01:55:53 +03:00
|
|
|
const request = { account: sanitizedAccount };
|
|
|
|
const rawResponse = await fetch(ENDPOINT, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(request),
|
|
|
|
});
|
|
|
|
const result = await rawResponse.json();
|
|
|
|
|
|
|
|
if (result.redirectUrl) {
|
|
|
|
window.open(result.redirectUrl, '_blank');
|
|
|
|
handleClose();
|
|
|
|
}
|
|
|
|
if (!result.success) {
|
|
|
|
setErrorMessage(result.message);
|
|
|
|
setLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!result.redirectUrl) {
|
|
|
|
setErrorMessage('Unable to follow.');
|
|
|
|
setLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
setErrorMessage(e.message);
|
|
|
|
}
|
|
|
|
setLoading(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-08-23 04:27:47 +03:00
|
|
|
<Space direction="vertical">
|
2022-09-07 10:00:28 +03:00
|
|
|
<div className={styles.header}>
|
2022-08-23 04:27:47 +03:00
|
|
|
By following this stream you'll get notified on the Fediverse when it goes live. Now is a
|
2022-08-23 04:29:37 +03:00
|
|
|
great time to
|
2022-08-23 04:27:47 +03:00
|
|
|
<a href="https://owncast.online/join-fediverse" target="_blank" rel="noreferrer">
|
|
|
|
learn about the Fediverse
|
2022-08-23 04:29:37 +03:00
|
|
|
</a>
|
2022-08-23 04:27:47 +03:00
|
|
|
if it's new to you.
|
2022-06-25 01:55:53 +03:00
|
|
|
</div>
|
2022-08-23 04:27:47 +03:00
|
|
|
|
|
|
|
<Spin spinning={loading}>
|
|
|
|
{errorMessage && (
|
|
|
|
<Alert message="Follow Error" description={errorMessage} type="error" showIcon />
|
|
|
|
)}
|
2022-09-07 10:00:28 +03:00
|
|
|
<div className={styles.account}>
|
|
|
|
<img src="/logo" alt="logo" className={styles.logo} />
|
|
|
|
<div className={styles.username}>
|
|
|
|
<div className={styles.name}>{name}</div>
|
2022-08-23 04:27:47 +03:00
|
|
|
<div>{account}</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
2022-09-07 10:00:28 +03:00
|
|
|
<div className={styles.instructions}>Enter your username @server to follow</div>
|
2022-08-23 04:27:47 +03:00
|
|
|
<Input
|
|
|
|
value={account}
|
|
|
|
size="large"
|
|
|
|
onChange={e => handleAccountChange(e.target.value)}
|
|
|
|
placeholder="Your fediverse account @account@server"
|
|
|
|
defaultValue={account}
|
|
|
|
/>
|
2022-09-07 10:00:28 +03:00
|
|
|
<div className={styles.footer}>
|
2022-08-23 04:27:47 +03:00
|
|
|
You'll be redirected to your Fediverse server and asked to confirm the action.
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-07 10:00:28 +03:00
|
|
|
<Space className={styles.buttons}>
|
2022-08-30 09:17:12 +03:00
|
|
|
<Button disabled={!valid} type="primary" onClick={remoteFollowButtonPressed}>
|
2022-08-23 04:27:47 +03:00
|
|
|
Follow
|
|
|
|
</Button>
|
2022-08-30 09:17:12 +03:00
|
|
|
<Button onClick={joinButtonPressed} type="primary">
|
|
|
|
Join the Fediverse
|
|
|
|
</Button>
|
2022-08-23 04:27:47 +03:00
|
|
|
</Space>
|
|
|
|
</Spin>
|
|
|
|
</Space>
|
2022-06-25 01:55:53 +03:00
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|