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

648 lines
22 KiB
TypeScript
Raw Normal View History

2025-11-25 13:13:38 +01:00
import { useEffect, useRef, useState } from 'react';
2024-03-20 13:08:08 +01:00
import { useTranslation } from 'react-i18next';
import type { TFunction } from 'i18next';
2024-04-26 19:13:23 +02:00
import { useLocation, useNavigate, useParams } from 'react-router-dom';
import { Comment, setAccount, useAccount, useEditedComment } from '@bitsocial/bitsocial-react-hooks';
import getShortAddress from '../../lib/get-short-address';
import useCommunitiesPagesStore from '@bitsocial/bitsocial-react-hooks/dist/stores/communities-pages';
2026-05-01 18:03:04 +07:00
import { getDisplayMediaInfoType, getLinkMediaInfo } from '../../lib/utils/media-utils';
2026-05-16 16:11:22 +07:00
import { getExpiringMediaLinkAlert } from '../../lib/utils/media-link-validation-utils';
2026-05-01 18:03:04 +07:00
import { getPublishURLFilename, isValidPublishURL, isValidURL } from '../../lib/utils/url-utils';
import { isAllView, isCatalogView, isModQueueView, isModView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils';
import { useAccountCommunityAddresses } from '../../hooks/use-account-community-addresses';
import { useDirectories, useDirectoryByAddress } from '../../hooks/use-directories';
import useIsMobile from '../../hooks/use-is-mobile';
import { useResolvedCommunityAddress } from '../../hooks/use-resolved-community-address';
import useSafeAccountComment from '../../hooks/use-safe-account-comment';
2025-01-30 17:09:21 +01:00
import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame';
import usePublishPost from '../../hooks/use-publish-post';
import usePublishReply from '../../hooks/use-publish-reply';
import { useFileUpload } from '../../hooks/use-file-upload';
import { getShowUploadControls, isWebRuntime } from '../../lib/media-hosting/show-upload-controls';
import { isCommentArchived } from '../../lib/utils/comment-moderation-utils';
import useMediaHostingStore from '../../stores/use-media-hosting-store';
import BoardOfflineAlert from '../board-offline-alert/board-offline-alert';
2026-05-01 21:12:39 +07:00
import LoadingEllipsis from '../loading-ellipsis';
import styles from './post-form.module.css';
import capitalize from 'lodash/capitalize';
import debounce from 'lodash/debounce';
2026-05-01 18:03:04 +07:00
const FILE_LINK_PLACEHOLDER = 'https://website.com/image.jpg';
export const LinkTypePreviewer = ({ link }: { link: string }) => {
2024-05-31 16:51:22 +02:00
const { t } = useTranslation();
const mediaInfo = getLinkMediaInfo(link);
let type = mediaInfo?.type;
const { status: gifFrameStatus } = useFetchGifFirstFrame(type === 'gif' ? mediaInfo?.url : undefined);
if (type === 'gif' && gifFrameStatus === 'ready') {
type = t('animated_gif');
} else if (type === 'gif') {
type = t('gif');
2026-05-01 18:03:04 +07:00
} else if (type) {
type = getDisplayMediaInfoType(type, t);
}
2026-05-01 18:03:04 +07:00
return isValidURL(link) ? `type: ${type}` : t('invalid_url');
};
const PostFormActions = ({
disableReplyPublish = false,
variant,
t,
isInPostView,
onPublishReply,
onPublishPost,
handleUpload,
isUploading,
showUploadControls,
}: {
disableReplyPublish?: boolean;
variant: 'reply' | 'post' | 'upload';
t: TFunction;
isInPostView: boolean;
onPublishReply: () => void;
onPublishPost: () => void;
handleUpload: () => void;
isUploading: boolean;
showUploadControls: boolean;
}) => {
if (variant === 'reply' && isInPostView) {
return (
<button type='button' onClick={onPublishReply} disabled={disableReplyPublish || isUploading}>
{t('post')}
</button>
);
}
if (variant === 'post' && !isInPostView) {
return (
<button type='button' onClick={onPublishPost}>
{t('post')}
</button>
);
}
if (variant === 'upload' && showUploadControls) {
return (
<button type='button' onClick={handleUpload} disabled={isUploading}>
{t('choose_file')}
</button>
);
}
return null;
};
interface PostFormFieldsProps {
t: TFunction;
account: ReturnType<typeof useAccount>;
displayName: string | undefined;
isInPostView: boolean;
subjectRef: React.Ref<HTMLInputElement>;
textRef: React.Ref<HTMLTextAreaElement>;
urlRef: React.Ref<HTMLInputElement>;
url: string;
lengthError: string | null;
handleContentChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
setPublishPostOptions: (opts: Record<string, unknown>) => void;
setPublishReplyOptions: (opts: Record<string, unknown>) => void;
setUrl: (url: string) => void;
isUploading: boolean;
uploadedFileName: string | null | undefined;
showUploadControls: boolean;
showSpoilerForPost: boolean;
showSpoilerForReply: boolean;
isInAllView: boolean;
isInSubscriptionsView: boolean;
isInModView: boolean;
directories: ReturnType<typeof useDirectories>;
accountCommunityAddresses: string[];
subscriptions: string[];
communityAddress: string | undefined;
requirePostLinkIsMedia: boolean;
onPublishReply: () => void;
onPublishPost: () => void;
handleUpload: () => void;
disableReplyPublish: boolean;
}
const PostFormFields = ({
t,
account,
displayName,
isInPostView,
subjectRef,
textRef,
urlRef,
url,
lengthError,
handleContentChange,
setPublishPostOptions,
setPublishReplyOptions,
setUrl,
isUploading,
uploadedFileName,
showUploadControls,
showSpoilerForPost,
showSpoilerForReply,
isInAllView,
isInSubscriptionsView,
isInModView,
directories,
accountCommunityAddresses,
subscriptions,
communityAddress,
requirePostLinkIsMedia,
onPublishReply,
onPublishPost,
handleUpload,
disableReplyPublish,
}: PostFormFieldsProps) => (
<>
<tr>
<td>{t('name')}</td>
<td>
<input
type='text'
aria-label={t('name')}
placeholder={!displayName ? capitalize(t('anonymous')) : undefined}
defaultValue={displayName || undefined}
onChange={(e) => {
const newDisplayName = e.target.value.trim() || undefined;
setAccount({ ...account, author: { ...account?.author, displayName: newDisplayName } });
if (isInPostView) {
setPublishReplyOptions({ displayName: newDisplayName });
} else {
setPublishPostOptions({ displayName: newDisplayName });
}
}}
/>
<PostFormActions
variant='reply'
t={t}
isInPostView={isInPostView}
onPublishReply={onPublishReply}
onPublishPost={onPublishPost}
handleUpload={handleUpload}
disableReplyPublish={disableReplyPublish}
isUploading={isUploading}
showUploadControls={showUploadControls}
/>
</td>
</tr>
{!isInPostView && (
<tr>
<td>{t('subject')}</td>
<td>
<input
type='text'
aria-label={t('subject')}
ref={subjectRef}
onChange={(e) => {
setPublishPostOptions({ title: e.target.value });
}}
/>
<PostFormActions
variant='post'
t={t}
isInPostView={isInPostView}
onPublishReply={onPublishReply}
onPublishPost={onPublishPost}
handleUpload={handleUpload}
disableReplyPublish={disableReplyPublish}
isUploading={isUploading}
showUploadControls={showUploadControls}
/>
</td>
</tr>
)}
<tr>
<td>{t('comment')}</td>
<td>
<textarea cols={48} rows={4} wrap='soft' ref={textRef} aria-label={t('comment')} onChange={handleContentChange} />
{lengthError && <div className={styles.error}>{lengthError}</div>}
</td>
</tr>
<tr>
<td>{requirePostLinkIsMedia ? t('link_to_file') : t('link')}</td>
<td className={styles.linkField}>
<input
type='text'
aria-label={requirePostLinkIsMedia ? t('link_to_file') : t('link')}
autoCorrect='off'
autoComplete='off'
spellCheck='false'
2026-05-01 18:03:04 +07:00
placeholder={requirePostLinkIsMedia ? FILE_LINK_PLACEHOLDER : undefined}
ref={urlRef}
disabled={isUploading}
onChange={(e) => {
setUrl(e.target.value);
if (isInPostView) {
setPublishReplyOptions({ link: e.target.value });
} else {
setPublishPostOptions({ link: e.target.value });
}
}}
/>
<span className={styles.linkType}> {url && <LinkTypePreviewer link={url} />}</span>
</td>
</tr>
{showUploadControls && (
<tr className={styles.uploadButton}>
<td>{t('file')}</td>
<td>
<PostFormActions
variant='upload'
t={t}
isInPostView={isInPostView}
onPublishReply={onPublishReply}
onPublishPost={onPublishPost}
handleUpload={handleUpload}
disableReplyPublish={disableReplyPublish}
isUploading={isUploading}
showUploadControls={showUploadControls}
/>
2026-05-01 21:12:39 +07:00
<span>{isUploading ? <LoadingEllipsis string={t('uploading')} /> : getPublishURLFilename(url) || uploadedFileName || t('no_file_chosen')}</span>
</td>
</tr>
)}
{((isInPostView && showSpoilerForReply) || (!isInPostView && showSpoilerForPost)) && (
<tr className={styles.spoilerButton}>
<td>{t('options')}</td>
<td>
[
<label>
<input
type='checkbox'
onChange={(e) => (isInPostView ? setPublishReplyOptions({ spoiler: e.target.checked }) : setPublishPostOptions({ spoiler: e.target.checked }))}
/>
{capitalize(t('spoiler'))}?
</label>
]
</td>
</tr>
)}
{(isInAllView || isInSubscriptionsView || isInModView) && (
<tr>
<td>{t('board')}</td>
<td>
<select aria-label={t('board')} onChange={(e) => setPublishPostOptions({ communityAddress: e.target.value })} value={communityAddress}>
<option value=''>{t('choose_one')}</option>
{isInAllView &&
directories.map((community) =>
community.title && community.address ? (
<option key={community.address} value={community.address}>
{community.title}
</option>
) : null,
)}
{isInModView &&
accountCommunityAddresses.map((address: string) => (
<option key={address} value={address}>
{address && getShortAddress(address)}
</option>
))}
{isInSubscriptionsView &&
subscriptions.map((sub: string) => (
<option key={sub} value={sub}>
{sub}
</option>
))}
</select>
</td>
</tr>
)}
</>
);
const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid: string }) => {
2024-04-28 17:12:19 +02:00
const { t } = useTranslation();
2025-01-30 17:09:21 +01:00
const params = useParams();
const account = useAccount();
2025-01-30 17:09:21 +01:00
const [url, setUrl] = useState('');
const author = account?.author || {};
const { displayName } = author || {};
const accountComment = useSafeAccountComment({ commentIndex: params?.accountCommentIndex });
const resolvedAddress = useResolvedCommunityAddress();
const communityAddress = resolvedAddress || accountComment?.communityAddress;
const { setPublishPostOptions, postIndex, publishPost, publishPostError, publishPostOptions, resetPublishPostOptions } = usePublishPost({
communityAddress,
});
const effectiveBoardAddress = communityAddress || publishPostOptions.communityAddress;
2024-04-26 19:13:23 +02:00
const textRef = useRef<HTMLTextAreaElement>(null);
const urlRef = useRef<HTMLInputElement>(null);
const subjectRef = useRef<HTMLInputElement>(null);
const location = useLocation();
2024-10-29 17:40:09 +01:00
const isInAllView = isAllView(location.pathname);
const isInModView = isModView(location.pathname);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
const subscriptions = account?.subscriptions || [];
const directories = useDirectories();
const directoryEntry = useDirectoryByAddress(effectiveBoardAddress);
const showSpoilerForPost = directoryEntry?.features?.noSpoilers !== true;
const showSpoilerForReply = directoryEntry?.features?.noSpoilerReplies !== true;
const requirePostLinkIsMediaFeature = directoryEntry?.features?.requirePostLinkIsMedia;
const requirePostLinkIsMedia = requirePostLinkIsMediaFeature === true || (requirePostLinkIsMediaFeature === undefined && (isInAllView || isInSubscriptionsView));
const accountCommunityAddresses = useAccountCommunityAddresses();
2025-01-31 23:53:24 +01:00
const [lengthError, setLengthError] = useState<string | null>(null);
2026-05-16 16:11:22 +07:00
const [formError, setFormError] = useState<string | null>(null);
2025-01-31 23:53:24 +01:00
const checkContentLength = useRef(
debounce((content: string, t: TFunction) => {
2025-01-31 23:53:24 +01:00
const length = content.trim().length;
if (length > 2000) {
setLengthError(`${t('error')}: ${t('comment_field_too_long', { length })}`);
} else {
setLengthError(null);
}
}, 1000),
).current;
const resetFields = () => {
if (textRef.current) {
textRef.current.value = '';
}
if (urlRef.current) {
urlRef.current.value = '';
}
if (subjectRef.current) {
subjectRef.current.value = '';
}
};
const onPublishPost = () => {
const currentTitle = subjectRef.current?.value.trim() || '';
const currentContent = textRef.current?.value.trim() || '';
const currentUrl = urlRef.current?.value.trim() || '';
2025-01-31 23:53:24 +01:00
checkContentLength.cancel();
setLengthError(null);
2026-05-16 16:11:22 +07:00
setFormError(null);
2025-01-31 23:53:24 +01:00
if (!currentTitle && !currentContent && !currentUrl) {
setFormError(`${t('error')}: ${t('empty_comment_alert')}`);
2024-04-26 19:13:23 +02:00
return;
}
if (currentUrl && !isValidPublishURL(currentUrl)) {
setFormError(`${t('error')}: ${t('invalid_url_alert')}`);
return;
}
2026-05-16 16:11:22 +07:00
const expiringMediaLinkAlert = currentUrl ? getExpiringMediaLinkAlert(currentUrl, t) : null;
if (expiringMediaLinkAlert) {
setFormError(expiringMediaLinkAlert);
return;
}
2025-01-31 23:53:24 +01:00
if (currentContent.length > 2000) {
setFormError(`${t('error')}: ${t('field_too_long')}`);
2025-01-31 23:53:24 +01:00
return;
}
if ((isInAllView || isInSubscriptionsView || isInModView) && !publishPostOptions.communityAddress) {
setFormError(`${t('error')}: ${t('no_board_selected_warning')}`);
2024-04-26 19:13:23 +02:00
return;
}
2025-01-30 17:09:21 +01:00
publishPost();
2024-04-26 19:13:23 +02:00
};
// redirect to pending page when pending comment is created
const navigate = useNavigate();
useEffect(() => {
2025-01-30 17:09:21 +01:00
if (typeof postIndex === 'number') {
resetPublishPostOptions();
resetFields();
navigate(`/pending/${postIndex}`);
2024-04-26 19:13:23 +02:00
}
2025-01-30 17:09:21 +01:00
}, [postIndex, resetPublishPostOptions, navigate]);
// in post page, publish a reply to the post
const isInPostView = isPostPageView(location.pathname, params);
const cid = params?.commentCid || '';
const { isResolvingExternalQuotes, publishReply, publishReplyError, publishReplyStateMessage, resetPublishReplyOptions, replyIndex, setPublishReplyOptions } =
usePublishReply({ cid, communityAddress, postCid });
useEffect(() => {
return () => {
checkContentLength.cancel();
if (isInPostView) {
resetPublishReplyOptions();
} else {
resetPublishPostOptions();
}
};
}, [checkContentLength, isInPostView, resetPublishPostOptions, resetPublishReplyOptions]);
const handleContentChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
const content = e.target.value;
if (isInPostView) {
setPublishReplyOptions({ content });
} else {
setPublishPostOptions({ content });
}
checkContentLength(content, t);
};
const onPublishReply = () => {
const currentContent = textRef.current?.value.trim() || '';
const currentUrl = urlRef.current?.value.trim() || '';
2025-01-31 23:53:24 +01:00
checkContentLength.cancel();
setLengthError(null);
2026-05-16 16:11:22 +07:00
setFormError(null);
2025-01-31 23:53:24 +01:00
if (!currentContent && !currentUrl) {
setFormError(`${t('error')}: ${t('empty_comment_alert')}`);
return;
}
if (currentUrl && !isValidPublishURL(currentUrl)) {
setFormError(`${t('error')}: ${t('invalid_url_alert')}`);
return;
}
2026-05-16 16:11:22 +07:00
const expiringMediaLinkAlert = currentUrl ? getExpiringMediaLinkAlert(currentUrl, t) : null;
if (expiringMediaLinkAlert) {
setFormError(expiringMediaLinkAlert);
return;
}
2025-01-31 23:53:24 +01:00
if (currentContent.length > 2000) {
setFormError(`${t('error')}: ${t('field_too_long')}`);
2025-01-31 23:53:24 +01:00
return;
}
2024-08-06 21:51:45 +02:00
publishReply();
};
useEffect(() => {
if (typeof replyIndex === 'number') {
resetFields();
closeForm();
}
}, [replyIndex, closeForm]);
const { isUploading, uploadedFileName, handleUpload } = useFileUpload({
onUploadComplete: (uploadedUrl: string) => {
if (uploadedUrl) {
setUrl(uploadedUrl);
if (urlRef.current) {
urlRef.current.value = uploadedUrl;
}
if (isInPostView) {
setPublishReplyOptions({ link: uploadedUrl });
} else {
setPublishPostOptions({ link: uploadedUrl });
}
}
},
});
const uploadMode = useMediaHostingStore((state) => state.uploadMode);
const showUploadControls = getShowUploadControls(uploadMode, isWebRuntime());
2025-01-26 22:25:56 +01:00
const hasInitializedDisplayName = useRef(false);
useEffect(() => {
if (displayName && !hasInitializedDisplayName.current) {
hasInitializedDisplayName.current = true;
if (isInPostView) {
setPublishReplyOptions({ displayName });
} else {
2025-01-30 17:09:21 +01:00
setPublishPostOptions({ displayName });
2025-01-26 22:25:56 +01:00
}
}
2025-01-30 17:09:21 +01:00
}, [displayName, isInPostView, setPublishReplyOptions, setPublishPostOptions]);
2025-01-26 22:25:56 +01:00
return (
<>
<table className={styles.postFormTable}>
<tbody>
<PostFormFields
t={t}
account={account}
displayName={displayName}
isInPostView={isInPostView}
subjectRef={subjectRef}
textRef={textRef}
urlRef={urlRef}
url={url}
lengthError={lengthError}
handleContentChange={handleContentChange}
setPublishPostOptions={setPublishPostOptions}
setPublishReplyOptions={setPublishReplyOptions}
setUrl={setUrl}
isUploading={isUploading}
uploadedFileName={uploadedFileName}
showUploadControls={showUploadControls}
showSpoilerForPost={showSpoilerForPost}
showSpoilerForReply={showSpoilerForReply}
isInAllView={isInAllView}
isInSubscriptionsView={isInSubscriptionsView}
isInModView={isInModView}
directories={directories}
accountCommunityAddresses={accountCommunityAddresses}
subscriptions={subscriptions}
communityAddress={communityAddress}
requirePostLinkIsMedia={requirePostLinkIsMedia}
onPublishReply={onPublishReply}
onPublishPost={onPublishPost}
handleUpload={handleUpload}
disableReplyPublish={isResolvingExternalQuotes}
/>
</tbody>
</table>
2026-05-16 16:11:22 +07:00
{formError && <div className={`${styles.error} ${styles.formError}`}>{formError}</div>}
{publishPostError && <div className={`${styles.error} ${styles.formError}`}>{publishPostError}</div>}
{publishReplyError && <div className={`${styles.error} ${styles.formError}`}>{publishReplyError}</div>}
{publishReplyStateMessage && <div className={styles.status}>{publishReplyStateMessage}</div>}
</>
);
};
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 isInPostView = isPostPageView(location.pathname, params);
2024-10-29 17:40:09 +01:00
const isInAllView = isAllView(location.pathname);
const isInModView = isModView(location.pathname);
const isInModQueueView = isModQueueView(location.pathname);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
2025-12-29 16:40:33 +01:00
const isInCatalogView = isCatalogView(location.pathname, params);
const isMobile = useIsMobile();
2024-04-08 17:32:12 +02:00
const commentCid = params?.commentCid;
const post = useCommunitiesPagesStore((state) => (commentCid ? state.comments[commentCid] : undefined));
let comment: Comment | undefined = post;
// handle pending mod or author edit
const { editedComment } = useEditedComment({ comment });
if (editedComment) {
comment = editedComment;
}
const { deleted, locked, removed, postCid } = comment || {};
const archived = isCommentArchived(comment);
const isThreadClosed = deleted || locked || removed || archived;
const threadStateKey = archived ? 'thread_archived' : 'thread_closed';
const [showForm, setShowForm] = useState(false);
2024-03-20 13:08:08 +01:00
const accountComment = useSafeAccountComment({ commentIndex: params?.accountCommentIndex });
const resolvedAddress = useResolvedCommunityAddress();
const communityAddress = resolvedAddress || accountComment?.communityAddress;
const shouldShowOfflineAlert = !(isInAllView || isInSubscriptionsView || isInModView) && showForm;
if (isMobile) {
return (
2024-04-24 15:20:55 +02:00
<div className={styles.postFormMobile}>
{shouldShowOfflineAlert && <BoardOfflineAlert className={styles.offlineBoard} communityAddress={communityAddress} />}
{isInModQueueView ? (
<div className={styles.modQueueTitle}>{t('moderation_queue')}</div>
) : isThreadClosed ? (
2024-04-08 17:46:36 +02:00
<div className={styles.closed}>
{t(threadStateKey)}
2024-04-08 17:46:36 +02:00
<br />
{t('may_not_reply')}
2024-04-08 17:46:36 +02:00
</div>
) : (
2024-04-24 15:20:55 +02:00
<>
<button className={`${styles.showFormButton} button`} onClick={() => setShowForm(showForm ? false : true)}>
{showForm ? t('close_post_form') : isInPostView ? t('post_a_reply') : t('start_new_thread')}
2024-04-24 15:20:55 +02:00
</button>
{showForm && <PostFormTable closeForm={() => setShowForm(false)} postCid={postCid} />}
2024-04-24 15:20:55 +02:00
</>
2024-04-08 17:46:36 +02:00
)}
2025-12-29 16:40:33 +01:00
{isInCatalogView && <hr />}
2024-03-21 15:21:33 +01:00
</div>
);
}
return (
<div className={styles.postFormDesktop}>
{shouldShowOfflineAlert && <BoardOfflineAlert className={styles.offlineBoard} communityAddress={communityAddress} />}
{isInModQueueView ? (
<div className={styles.modQueueTitle}>{t('moderation_queue')}</div>
) : isThreadClosed ? (
<div className={styles.closed}>
{t(threadStateKey)}
<br />
{t('may_not_reply')}
</div>
) : !showForm ? (
<div>
[
<button className='button' onClick={() => setShowForm(true)}>
{isInPostView ? t('post_a_reply') : t('start_new_thread')}
</button>
]
</div>
) : (
<PostFormTable closeForm={() => setShowForm(false)} postCid={postCid} />
)}
</div>
2024-03-20 13:00:11 +01:00
);
};
export default PostForm;