Files
5chan/src/components/post-form/post-form.tsx
T

114 lines
3.3 KiB
TypeScript
Raw Normal View History

import { useState } from 'react';
2024-03-20 13:08:08 +01:00
import { useTranslation } from 'react-i18next';
2024-04-03 22:52:27 +02:00
import { useLocation, useParams } from 'react-router-dom';
import { useAccount, useComment } from '@plebbit/plebbit-react-hooks';
import { getLinkMediaInfo } from '../../lib/utils/media-utils';
import { isValidURL } from '../../lib/utils/url-utils';
2024-04-08 17:32:12 +02:00
import { isDescriptionView, isPostPageView, isRulesView } from '../../lib/utils/view-utils';
import styles from './post-form.module.css';
2024-03-20 13:00:11 +01:00
const LinkTypePreviewer = ({ link }: { link: string }) => {
const mediaInfo = getLinkMediaInfo(link);
return isValidURL(link) ? mediaInfo?.type : 'Invalid URL';
};
const PostFormTable = () => {
const account = useAccount();
const { displayName } = account || {};
const [link, setLink] = useState('');
return (
<table>
<tbody>
<tr>
<td>Name</td>
<td>
<input type='text' placeholder={!displayName ? 'Anonymous' : undefined} defaultValue={displayName || undefined} />
</td>
</tr>
<tr>
<td>Subject</td>
<td>
<input type='text' />
<button>Post</button>
</td>
</tr>
<tr>
<td>Comment</td>
<textarea cols={48} rows={4} wrap='soft' />
</tr>
<tr>
<td>Link</td>
<td>
<input type='text' onChange={(e) => setLink(e.target.value)} />
<span className={styles.linkType}>
{link && (
<>
(<LinkTypePreviewer link={link} />)
</>
)}
</span>
</td>
</tr>
</tbody>
</table>
);
};
const PostForm = () => {
2024-03-20 13:08:08 +01:00
const { t } = useTranslation();
2024-04-08 17:32:12 +02:00
const location = useLocation();
const params = useParams();
const isInPostPage = isPostPageView(location.pathname, params);
const isInDescriptionView = isDescriptionView(location.pathname, params);
const isInRulesView = isRulesView(location.pathname, params);
const { subplebbitAddress, commentCid } = params || {};
const comment = useComment({ commentCid });
const { deleted, locked, removed } = comment || {};
2024-04-08 17:32:12 +02:00
const isThreadClosed = deleted || locked || removed || isInDescriptionView || isInRulesView;
const [showForm, setShowForm] = useState(false);
2024-03-20 13:08:08 +01:00
2024-03-20 13:00:11 +01:00
return (
2024-03-21 15:21:33 +01:00
<>
<div className={styles.postFormButtonDesktop}>
{isThreadClosed ? (
<div className={styles.closed}>
{t('thread_closed')}
<br />
{t('may_not_reply')}
</div>
) : !showForm ? (
<div>
[
<button className='button' onClick={() => setShowForm(true)}>
{isInPostPage ? t('post_a_reply') : t('start_new_thread')}
</button>
]
</div>
) : (
<PostFormTable />
)}
2024-03-21 15:21:33 +01:00
</div>
<div className={styles.postFormButtonMobile}>
2024-04-08 17:46:36 +02:00
{isThreadClosed ? (
<div className={styles.closed}>
{t('thread_closed')}
2024-04-08 17:46:36 +02:00
<br />
{t('may_not_reply')}
2024-04-08 17:46:36 +02:00
</div>
) : (
<button className='button' onClick={() => setShowForm(true)}>
{isInPostPage ? t('post_a_reply') : t('start_new_thread')}
</button>
)}
2024-03-21 15:21:33 +01:00
</div>
</>
2024-03-20 13:00:11 +01:00
);
};
export default PostForm;