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