;
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) => (
<>
| {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 });
}
}}
/>
|
{!isInPostView && (
| {t('subject')} |
{
setPublishPostOptions({ title: e.target.value });
}}
/>
|
)}
| {t('comment')} |
{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 ? t('uploading') : uploadedFileName || t('no_file_chosen')}
|
)}
{((isInPostView && showSpoilerForReply) || (!isInPostView && showSpoilerForPost)) && (
| {t('options')} |
[
]
|
)}
{(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 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 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();
const [lengthError, setLengthError] = useState(null);
const checkContentLength = useRef(
debounce((content: string, t: Function) => {
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() || '';
checkContentLength.cancel();
setLengthError(null);
if (!currentTitle && !currentContent && !currentUrl) {
alert(t('empty_comment_alert'));
return;
}
if (currentUrl && !isValidPublishURL(currentUrl)) {
alert(t('invalid_url_alert'));
return;
}
if (currentContent.length > 2000) {
alert(t('error') + ': ' + t('field_too_long'));
return;
}
if ((isInAllView || isInSubscriptionsView || isInModView) && !publishPostOptions.communityAddress) {
alert(t('no_board_selected_warning'));
return;
}
publishPost();
};
// redirect to pending page when pending comment is created
const navigate = useNavigate();
useEffect(() => {
if (typeof postIndex === 'number') {
resetPublishPostOptions();
resetFields();
navigate(`/pending/${postIndex}`);
}
}, [postIndex, resetPublishPostOptions, navigate]);
// in post page, publish a reply to the post
const isInPostView = isPostPageView(location.pathname, params);
const cid = params?.commentCid as string;
const { isResolvingExternalQuotes, publishReply, publishReplyError, publishReplyStateMessage, resetPublishReplyOptions, replyIndex, setPublishReplyOptions } =
usePublishReply({ cid, communityAddress, postCid });
const handleContentChange = (e: React.ChangeEvent) => {
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() || '';
checkContentLength.cancel();
setLengthError(null);
if (!currentContent && !currentUrl) {
alert(t('empty_comment_alert'));
return;
}
if (currentUrl && !isValidPublishURL(currentUrl)) {
alert(t('invalid_url_alert'));
return;
}
if (currentContent.length > 2000) {
alert(t('error') + ': ' + t('field_too_long'));
return;
}
publishReply();
};
useEffect(() => {
if (typeof replyIndex === 'number') {
resetPublishReplyOptions();
resetFields();
closeForm();
}
}, [replyIndex, resetPublishReplyOptions, 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());
const hasInitializedDisplayName = useRef(false);
useEffect(() => {
if (displayName && !hasInitializedDisplayName.current) {
hasInitializedDisplayName.current = true;
if (isInPostView) {
setPublishReplyOptions({ displayName });
} else {
setPublishPostOptions({ displayName });
}
}
}, [displayName, isInPostView, setPublishReplyOptions, setPublishPostOptions]);
return (
<>
{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) => state.comments[commentCid as string]);
let comment: Comment = 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;