;
accountCommunityAddresses: string[];
subscriptions: string[];
communityAddress: string | undefined;
rulesPath: string;
requirePostLinkIsMedia: boolean;
showBbcodeToolbar: boolean;
onBbcodePreviewToggle: () => void;
onPublishReply: () => void;
onPublishPost: () => void;
handleUpload: () => void;
disableReplyPublish: boolean;
}
const PostFormFields = ({
t,
account,
displayName,
bbcodePreviewContent,
isInPostView,
isBbcodePreviewing,
postCid,
subjectRef,
optionsRef,
textRef,
urlRef,
url,
lengthError,
handleContentChange,
handleContentValueChange,
handleOptionsChange,
setPublishPostOptions,
setPublishReplyOptions,
setUrl,
isUploading,
uploadedFileName,
showUploadControls,
showSpoilerForPost,
showSpoilerForReply,
isInAllView,
isInSubscriptionsView,
isInModView,
directories,
accountCommunityAddresses,
subscriptions,
communityAddress,
rulesPath,
requirePostLinkIsMedia,
showBbcodeToolbar,
onBbcodePreviewToggle,
onPublishReply,
onPublishPost,
handleUpload,
disableReplyPublish,
}: PostFormFieldsProps) => (
<>
| {t('name')} |
{
const newDisplayName = e.target.value.trim() || undefined;
setAccount({ ...account, author: { ...account?.author, displayName: newDisplayName } });
if (isInPostView) {
setPublishReplyOptions({ displayName: newDisplayName });
} else {
setPublishPostOptions({ displayName: newDisplayName });
}
}}
/>
|
| {t('options')} |
|
{!isInPostView && (
| {t('subject')} |
{
setPublishPostOptions({ title: e.target.value });
}}
/>
|
)}
{showBbcodeToolbar ? (
| format |
handleContentValueChange(content)}
isPreviewing={isBbcodePreviewing}
onPreviewToggle={onBbcodePreviewToggle}
/>
|
) : null}
| {t('comment')} |
{showBbcodeToolbar && isBbcodePreviewing && (
)}
{lengthError && {lengthError} }
|
| {requirePostLinkIsMedia ? t('link_to_file') : t('link')} |
{
setUrl(e.target.value);
if (isInPostView) {
setPublishReplyOptions({ link: e.target.value });
} else {
setPublishPostOptions({ link: e.target.value });
}
}}
/>
{url && }
|
{showUploadControls && (
| {t('file')} |
{isUploading ? : getPostFormFileDisplayLabel(url, uploadedFileName, t('no_file_chosen'))}
|
)}
{((isInPostView && showSpoilerForReply) || (!isInPostView && showSpoilerForPost)) && (
| {capitalize(t('spoiler'))} |
[
]
|
)}
{(isInAllView || isInSubscriptionsView || isInModView) && (
| {t('board')} |
|
)}
|
|
>
);
const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid: string }) => {
const { t } = useTranslation();
const params = useParams();
const account = useAccount();
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;
const textRef = useRef(null);
const urlRef = useRef(null);
const subjectRef = useRef(null);
const optionsRef = useRef(null);
const fortuneEntryRef = useRef(null);
const diceRollRef = useRef(null);
const nonokoRedirectPathRef = useRef(null);
const location = useLocation();
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 pendingPostBoardPath = effectiveBoardAddress ? getBoardPath(effectiveBoardAddress, directories) : undefined;
const rulesPath = effectiveBoardAddress ? `/rules/${getBoardPath(effectiveBoardAddress, directories)}` : '/rules';
const showSpoilerForPost = directoryEntry?.features?.noSpoilers !== true;
const showSpoilerForReply = directoryEntry?.features?.noSpoilerReplies !== true;
const postOptionsDirectoryCode = getPostOptionsDirectoryCode(directoryEntry, location.pathname);
const requirePostLinkIsMediaFeature = directoryEntry?.features?.requirePostLinkIsMedia;
const requirePostLinkIsMedia = requirePostLinkIsMediaFeature === true || (requirePostLinkIsMediaFeature === undefined && (isInAllView || isInSubscriptionsView));
const accountCommunityAddresses = useAccountCommunityAddresses();
const accountAddress = account?.author?.address;
const roles = useCommunityField(effectiveBoardAddress, (community) => community?.roles);
const accountRole = accountAddress ? roles?.[accountAddress]?.role : undefined;
const showBbcodeToolbar = hasModQueueAccessRole(accountRole) || (!effectiveBoardAddress && isInModView && accountCommunityAddresses.length > 0);
const [lengthError, setLengthError] = useState(null);
const [formError, setFormError] = useState(null);
const [isBbcodePreviewing, setIsBbcodePreviewing] = useState(false);
const [bbcodePreviewContent, setBbcodePreviewContent] = useState('');
const checkContentLength = useRef(
debounce((content: string, t: TFunction) => {
const length = content.trim().length;
if (length > 2000) {
setLengthError(`${t('error')}: ${t('comment_field_too_long', { length })}`);
} else {
setLengthError(null);
}
}, 1000),
).current;
const checkPostOptions = useRef(
debounce((options: string, directoryCode: string | undefined) => {
const nextOptionsError = getPostOptionsValidationError(options, directoryCode);
if (nextOptionsError) {
setFormError(nextOptionsError);
}
}, POST_OPTIONS_VALIDATION_DELAY_MS),
).current;
const resetFields = () => {
if (textRef.current) {
textRef.current.value = '';
}
if (urlRef.current) {
urlRef.current.value = '';
}
if (subjectRef.current) {
subjectRef.current.value = '';
}
if (optionsRef.current) {
optionsRef.current.value = '';
}
checkContentLength.cancel();
checkPostOptions.cancel();
fortuneEntryRef.current = null;
diceRollRef.current = null;
setFormError(null);
setIsBbcodePreviewing(false);
setBbcodePreviewContent('');
};
const getBoardIndexPath = () => {
if (effectiveBoardAddress) {
return `/${getBoardPath(effectiveBoardAddress, directories)}`;
}
return params?.boardIdentifier ? `/${params.boardIdentifier}` : null;
};
const onPublishPost = () => {
const currentTitle = subjectRef.current?.value.trim() || '';
const currentContent = textRef.current?.value || '';
const currentUrl = urlRef.current?.value.trim() || '';
const currentOptions = optionsRef.current?.value || '';
const currentOptionsError = getPostOptionsValidationError(currentOptions, postOptionsDirectoryCode);
const publishContent = getContentWithOptions(currentContent, currentOptions, fortuneEntryRef, diceRollRef, postOptionsDirectoryCode);
checkContentLength.cancel();
checkPostOptions.cancel();
setLengthError(null);
setFormError(null);
nonokoRedirectPathRef.current = null;
if (currentOptionsError) {
setFormError(currentOptionsError);
return;
}
if (!currentTitle && !publishContent.trim() && !currentUrl) {
setFormError(`${t('error')}: ${t('empty_comment_alert')}`);
return;
}
if (currentUrl && !isValidPublishURL(currentUrl)) {
setFormError(`${t('error')}: ${t('invalid_url_alert')}`);
return;
}
const expiringMediaLinkAlert = currentUrl ? getExpiringMediaLinkAlert(currentUrl, t) : null;
if (expiringMediaLinkAlert) {
setFormError(expiringMediaLinkAlert);
return;
}
if (publishContent.trim().length > 2000) {
setFormError(`${t('error')}: ${t('field_too_long')}`);
return;
}
if ((isInAllView || isInSubscriptionsView || isInModView) && !publishPostOptions.communityAddress) {
setFormError(`${t('error')}: ${t('no_board_selected_warning')}`);
return;
}
nonokoRedirectPathRef.current = hasNonokoOption(currentOptions) ? getBoardIndexPath() : null;
publishPost({ content: publishContent });
};
// redirect to pending page when pending comment is created
const navigate = useNavigate();
useEffect(() => {
if (typeof postIndex === 'number') {
const nonokoRedirectPath = nonokoRedirectPathRef.current;
nonokoRedirectPathRef.current = null;
resetPublishPostOptions();
resetFields();
if (nonokoRedirectPath) {
navigate(nonokoRedirectPath, { state: getNonokoPendingRouteState(postIndex) });
} else {
navigate(`/pending/${postIndex}`, pendingPostBoardPath ? { state: { boardPath: pendingPostBoardPath } } : undefined);
}
}
}, [postIndex, pendingPostBoardPath, 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();
checkPostOptions.cancel();
if (isInPostView) {
resetPublishReplyOptions();
} else {
resetPublishPostOptions();
}
};
}, [checkContentLength, checkPostOptions, isInPostView, resetPublishPostOptions, resetPublishReplyOptions]);
const handleContentValueChange = (content: string, options = optionsRef.current?.value || '') => {
const publishContent = getContentWithOptions(content, options, fortuneEntryRef, diceRollRef, postOptionsDirectoryCode);
if (isBbcodePreviewing) {
setBbcodePreviewContent(content);
}
if (isInPostView) {
setPublishReplyOptions({ content: publishContent });
} else {
setPublishPostOptions({ content: publishContent });
}
checkContentLength(publishContent, t);
};
const handleContentChange = (e: React.ChangeEvent) => {
handleContentValueChange(e.target.value);
};
const handleOptionsChange = (e: React.ChangeEvent) => {
const options = e.target.value;
handleContentValueChange(textRef.current?.value || '', options);
setFormError((currentError) => (isPostOptionsValidationError(currentError) ? null : currentError));
checkPostOptions(options, postOptionsDirectoryCode);
};
const handleBbcodePreviewToggle = () => {
if (isBbcodePreviewing) {
setIsBbcodePreviewing(false);
window.requestAnimationFrame(() => textRef.current?.focus());
return;
}
setBbcodePreviewContent(textRef.current?.value ?? '');
setIsBbcodePreviewing(true);
};
const onPublishReply = () => {
const currentUrl = urlRef.current?.value.trim() || '';
const currentOptions = optionsRef.current?.value || '';
const currentOptionsError = getPostOptionsValidationError(currentOptions, postOptionsDirectoryCode);
const publishContent = getContentWithOptions(textRef.current?.value || '', currentOptions, fortuneEntryRef, diceRollRef, postOptionsDirectoryCode);
checkContentLength.cancel();
checkPostOptions.cancel();
setLengthError(null);
setFormError(null);
nonokoRedirectPathRef.current = null;
if (currentOptionsError) {
setFormError(currentOptionsError);
return;
}
if (!publishContent.trim() && !currentUrl) {
setFormError(`${t('error')}: ${t('empty_comment_alert')}`);
return;
}
if (currentUrl && !isValidPublishURL(currentUrl)) {
setFormError(`${t('error')}: ${t('invalid_url_alert')}`);
return;
}
const expiringMediaLinkAlert = currentUrl ? getExpiringMediaLinkAlert(currentUrl, t) : null;
if (expiringMediaLinkAlert) {
setFormError(expiringMediaLinkAlert);
return;
}
if (publishContent.trim().length > 2000) {
setFormError(`${t('error')}: ${t('field_too_long')}`);
return;
}
nonokoRedirectPathRef.current = hasNonokoOption(currentOptions) ? getBoardIndexPath() : null;
publishReply({ content: publishContent });
};
useEffect(() => {
if (typeof replyIndex === 'number') {
const nonokoRedirectPath = nonokoRedirectPathRef.current;
nonokoRedirectPathRef.current = null;
resetFields();
closeForm();
if (nonokoRedirectPath) {
navigate(nonokoRedirectPath);
}
}
}, [replyIndex, closeForm, navigate]);
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());
const hasInitializedDisplayName = useRef(false);
useEffect(() => {
if (displayName && !hasInitializedDisplayName.current) {
hasInitializedDisplayName.current = true;
if (isInPostView) {
setPublishReplyOptions({ displayName });
} else {
setPublishPostOptions({ displayName });
}
}
}, [displayName, isInPostView, setPublishReplyOptions, setPublishPostOptions]);
return (
<>
{showBbcodeToolbar ? warning: posting as moderator
: null}
{formError ? (
{isPostOptionsValidationError(formError) ?
: formError}
) : null}
{publishPostError && {publishPostError}
}
{publishReplyError && {publishReplyError}
}
{publishReplyStateMessage && {publishReplyStateMessage}
}
>
);
};
const PostForm = () => {
const { t } = useTranslation();
const location = useLocation();
const params = useParams();
const isInPostView = isPostPageView(location.pathname, params);
const isInAllView = isAllView(location.pathname);
const isInModView = isModView(location.pathname);
const isInModQueueView = isModQueueView(location.pathname);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
const isInCatalogView = isCatalogView(location.pathname, params);
const isMobile = useIsMobile();
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);
const accountComment = useSafeAccountComment({ commentIndex: params?.accountCommentIndex });
const resolvedAddress = useResolvedCommunityAddress();
const communityAddress = resolvedAddress || accountComment?.communityAddress;
const shouldShowOfflineAlert = !(isInAllView || isInSubscriptionsView || isInModView) && showForm;
if (isMobile) {
return (
{shouldShowOfflineAlert &&
}
{isInModQueueView ? (
{t('moderation_queue')}
) : isThreadClosed ? (
{t(threadStateKey)}
{t('may_not_reply')}
) : (
<>
{showForm &&
setShowForm(false)} postCid={postCid} />}
>
)}
{isInCatalogView &&
}
);
}
return (
{shouldShowOfflineAlert &&
}
{isInModQueueView ? (
{t('moderation_queue')}
) : isThreadClosed ? (
{t(threadStateKey)}
{t('may_not_reply')}
) : !showForm ? (
[
]
) : (
setShowForm(false)} postCid={postCid} />
)}
);
};
export default PostForm;