/* eslint-disable react/no-unescaped-entities */ import { Input, Button, Alert, Spin, Space } from 'antd'; import { FC, useState } from 'react'; import styles from './FollowModal.module.scss'; const ENDPOINT = '/api/remotefollow'; export type FollowModalProps = { handleClose: () => void; account: string; name: string; }; 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()); } export const FollowModal: FC = ({ handleClose, account, name }) => { const [remoteAccount, setRemoteAccount] = useState(null); const [valid, setValid] = useState(false); const [loading, setLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(null); const handleAccountChange = a => { setRemoteAccount(a); if (validateAccount(a)) { setValid(true); } else { setValid(false); } }; const joinButtonPressed = () => { window.open('https://owncast.online/join-fediverse', '_blank'); }; const remoteFollowButtonPressed = async () => { if (!valid) { return; } setLoading(true); try { const sanitizedAccount = remoteAccount.replace(/^@+/, ''); 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 (
By following this stream you'll get notified on the Fediverse when it goes live. Now is a great time to learn about the Fediverse if it's new to you.
{errorMessage && ( )}
logo
{name}
{account}
Enter your username @server to follow
handleAccountChange(e.target.value)} placeholder="Your fediverse account @account@server" defaultValue={account} />
You'll be redirected to your Fediverse server and asked to confirm the action.
); };