2024-08-06 20:12:07 +02:00
|
|
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
2024-03-20 13:08:08 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-04-26 19:13:23 +02:00
|
|
|
import { useLocation, useNavigate, useParams } from 'react-router-dom';
|
2024-07-19 22:13:51 +02:00
|
|
|
import {
|
|
|
|
|
Comment,
|
|
|
|
|
PublishCommentOptions,
|
|
|
|
|
setAccount,
|
|
|
|
|
useAccount,
|
|
|
|
|
useAccountComment,
|
|
|
|
|
useComment,
|
|
|
|
|
useEditedComment,
|
|
|
|
|
usePublishComment,
|
|
|
|
|
useSubplebbit,
|
|
|
|
|
} from '@plebbit/plebbit-react-hooks';
|
2024-04-26 19:13:23 +02:00
|
|
|
import { create } from 'zustand';
|
|
|
|
|
import { alertChallengeVerificationFailed } from '../../lib/utils/challenge-utils';
|
2024-04-24 08:48:31 +02:00
|
|
|
import { getLinkMediaInfo } from '../../lib/utils/media-utils';
|
2024-08-27 18:46:42 +02:00
|
|
|
import { formatMarkdown } from '../../lib/utils/post-utils';
|
2024-04-24 08:48:31 +02:00
|
|
|
import { isValidURL } from '../../lib/utils/url-utils';
|
2024-05-17 14:56:09 +02:00
|
|
|
import { isAllView, isDescriptionView, isPostPageView, isRulesView, isSubscriptionsView } from '../../lib/utils/view-utils';
|
|
|
|
|
import { useDefaultSubplebbitAddresses } from '../../hooks/use-default-subplebbits';
|
2024-08-06 21:57:28 +02:00
|
|
|
import usePublishReply from '../../hooks/use-publish-reply';
|
2024-05-31 10:28:57 +02:00
|
|
|
import useChallengesStore from '../../stores/use-challenges-store';
|
2024-05-17 14:56:09 +02:00
|
|
|
import styles from './post-form.module.css';
|
2024-04-28 17:12:19 +02:00
|
|
|
import _ from 'lodash';
|
2024-07-07 20:02:58 +02:00
|
|
|
import useIsSubplebbitOffline from '../../hooks/use-is-subplebbit-offline';
|
2024-08-03 22:41:40 +02:00
|
|
|
import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame';
|
2024-08-06 20:12:07 +02:00
|
|
|
import useAnonMode from '../../hooks/use-anon-mode';
|
2024-04-26 19:13:23 +02:00
|
|
|
|
|
|
|
|
type SubmitState = {
|
2024-08-08 17:48:45 +02:00
|
|
|
author?: any | undefined;
|
|
|
|
|
displayName?: string | undefined;
|
2024-08-07 16:25:53 +02:00
|
|
|
signer?: any | undefined;
|
2024-04-26 19:13:23 +02:00
|
|
|
subplebbitAddress: string | undefined;
|
|
|
|
|
title: string | undefined;
|
|
|
|
|
content: string | undefined;
|
|
|
|
|
link: string | undefined;
|
2024-05-31 16:10:38 +02:00
|
|
|
spoiler: boolean | undefined;
|
2024-04-26 19:13:23 +02:00
|
|
|
publishCommentOptions: PublishCommentOptions;
|
|
|
|
|
setSubmitStore: (data: Partial<SubmitState>) => void;
|
|
|
|
|
resetSubmitStore: () => void;
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-31 10:28:57 +02:00
|
|
|
const { addChallenge } = useChallengesStore.getState();
|
2024-04-26 19:13:23 +02:00
|
|
|
|
|
|
|
|
const useSubmitStore = create<SubmitState>((set) => ({
|
2024-08-06 20:12:07 +02:00
|
|
|
author: undefined,
|
2024-08-15 16:43:35 +02:00
|
|
|
displayName: undefined,
|
2024-08-06 20:12:07 +02:00
|
|
|
signer: undefined,
|
2024-04-26 19:13:23 +02:00
|
|
|
subplebbitAddress: undefined,
|
|
|
|
|
title: undefined,
|
|
|
|
|
content: undefined,
|
|
|
|
|
link: undefined,
|
2024-05-31 16:10:38 +02:00
|
|
|
spoiler: undefined,
|
2024-04-26 19:13:23 +02:00
|
|
|
publishCommentOptions: {},
|
2024-08-08 17:48:45 +02:00
|
|
|
setSubmitStore: ({ author, displayName, signer, subplebbitAddress, title, content, link, spoiler }) =>
|
2024-04-26 19:13:23 +02:00
|
|
|
set((state) => {
|
2024-08-08 17:48:45 +02:00
|
|
|
const updatedAuthor = displayName ? { ...author, displayName } : author;
|
2024-04-26 19:13:23 +02:00
|
|
|
const nextState = { ...state };
|
2024-08-08 17:48:45 +02:00
|
|
|
if (author !== undefined) nextState.author = updatedAuthor;
|
2024-08-06 20:12:07 +02:00
|
|
|
if (signer !== undefined) nextState.signer = signer;
|
2024-04-26 19:13:23 +02:00
|
|
|
if (subplebbitAddress !== undefined) nextState.subplebbitAddress = subplebbitAddress;
|
|
|
|
|
if (title !== undefined) nextState.title = title || undefined;
|
|
|
|
|
if (content !== undefined) nextState.content = content || undefined;
|
|
|
|
|
if (link !== undefined) nextState.link = link || undefined;
|
2024-05-31 16:10:38 +02:00
|
|
|
if (spoiler !== undefined) nextState.spoiler = spoiler || undefined;
|
2024-04-26 19:13:23 +02:00
|
|
|
|
2024-08-07 16:25:53 +02:00
|
|
|
const publishCommentOptions: PublishCommentOptions = {
|
2024-08-06 20:12:07 +02:00
|
|
|
subplebbitAddress: nextState.subplebbitAddress,
|
|
|
|
|
title: nextState.title,
|
|
|
|
|
content: nextState.content,
|
|
|
|
|
link: nextState.link,
|
|
|
|
|
spoiler: nextState.spoiler,
|
2024-04-26 19:13:23 +02:00
|
|
|
onChallenge: (...args: any) => addChallenge(args),
|
|
|
|
|
onChallengeVerification: alertChallengeVerificationFailed,
|
|
|
|
|
onError: (error: Error) => {
|
|
|
|
|
console.error(error);
|
2024-08-07 16:25:53 +02:00
|
|
|
alert(error.message);
|
2024-04-26 19:13:23 +02:00
|
|
|
},
|
|
|
|
|
};
|
2024-08-07 16:25:53 +02:00
|
|
|
|
|
|
|
|
if (nextState.signer) {
|
|
|
|
|
publishCommentOptions.signer = nextState.signer;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 17:48:45 +02:00
|
|
|
if (nextState.author) {
|
|
|
|
|
publishCommentOptions.author = nextState.author;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 16:25:53 +02:00
|
|
|
nextState.publishCommentOptions = publishCommentOptions;
|
2024-04-26 19:13:23 +02:00
|
|
|
return nextState;
|
|
|
|
|
}),
|
2024-08-06 20:12:07 +02:00
|
|
|
resetSubmitStore: () =>
|
|
|
|
|
set({
|
|
|
|
|
author: undefined,
|
2024-08-15 16:43:35 +02:00
|
|
|
displayName: undefined,
|
2024-08-06 20:12:07 +02:00
|
|
|
signer: undefined,
|
|
|
|
|
subplebbitAddress: undefined,
|
|
|
|
|
title: undefined,
|
|
|
|
|
content: undefined,
|
|
|
|
|
link: undefined,
|
|
|
|
|
spoiler: undefined,
|
|
|
|
|
publishCommentOptions: {},
|
|
|
|
|
}),
|
2024-04-26 19:13:23 +02:00
|
|
|
}));
|
2024-03-20 13:00:11 +01:00
|
|
|
|
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;
|
|
|
|
|
const gifFrameUrl = useFetchGifFirstFrame(mediaInfo?.url);
|
|
|
|
|
|
|
|
|
|
if (type === 'gif' && gifFrameUrl !== null) {
|
|
|
|
|
type = t('animated_gif');
|
|
|
|
|
} else if (type === 'gif' && gifFrameUrl === null) {
|
|
|
|
|
type = t('static_gif');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isValidURL(link) ? type : t('invalid_url');
|
2024-04-24 08:48:31 +02:00
|
|
|
};
|
|
|
|
|
|
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();
|
2024-04-24 08:48:31 +02:00
|
|
|
const account = useAccount();
|
2024-08-06 20:12:07 +02:00
|
|
|
const author = account?.author || {};
|
|
|
|
|
const { displayName } = author || {};
|
2024-04-26 19:13:23 +02:00
|
|
|
const [url, setUrl] = useState('');
|
2024-08-30 12:09:31 +02:00
|
|
|
const { link, publishCommentOptions, setSubmitStore, resetSubmitStore } = useSubmitStore();
|
2024-04-26 19:13:23 +02:00
|
|
|
const { index, publishComment } = usePublishComment(publishCommentOptions);
|
|
|
|
|
|
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-06-17 12:17:36 +02:00
|
|
|
const isInAllView = isAllView(location.pathname, useParams());
|
|
|
|
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
|
2024-05-17 14:56:09 +02:00
|
|
|
const subscriptions = account?.subscriptions || [];
|
|
|
|
|
const defaultSubplebbitAddresses = useDefaultSubplebbitAddresses();
|
|
|
|
|
|
2024-08-06 20:12:07 +02:00
|
|
|
const { anonMode, getNewSigner, getExistingSigner } = useAnonMode(postCid);
|
|
|
|
|
const comment = useComment({ commentCid: postCid });
|
|
|
|
|
const address = comment?.author?.address;
|
|
|
|
|
|
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-06 21:51:45 +02:00
|
|
|
const hasCalledAnonAddressRef = useRef(false);
|
|
|
|
|
|
|
|
|
|
const getAnonAddressForPost = useCallback(async () => {
|
2024-08-15 16:43:35 +02:00
|
|
|
if (anonMode) {
|
|
|
|
|
if (!hasCalledAnonAddressRef.current) {
|
|
|
|
|
hasCalledAnonAddressRef.current = true;
|
|
|
|
|
const newSigner = (await getNewSigner()) || {};
|
|
|
|
|
setSubmitStore({
|
|
|
|
|
signer: newSigner,
|
|
|
|
|
author: {
|
|
|
|
|
address: newSigner.address,
|
2024-08-25 19:37:04 +02:00
|
|
|
displayName: displayName || undefined,
|
2024-08-15 16:43:35 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-08-06 20:12:07 +02:00
|
|
|
setSubmitStore({
|
2024-08-15 16:43:35 +02:00
|
|
|
signer: undefined,
|
2024-08-06 20:12:07 +02:00
|
|
|
author: {
|
2024-08-15 16:43:35 +02:00
|
|
|
address: account?.author?.address,
|
2024-08-25 19:37:04 +02:00
|
|
|
displayName: displayName || undefined,
|
2024-08-06 20:12:07 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-08-25 19:37:04 +02:00
|
|
|
}, [anonMode, getNewSigner, account, setSubmitStore, displayName]);
|
2024-08-06 20:12:07 +02:00
|
|
|
|
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() || '';
|
|
|
|
|
|
|
|
|
|
if (!currentTitle && !currentContent && !currentUrl) {
|
2024-04-26 19:13:23 +02:00
|
|
|
alert(`Cannot post empty comment`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (link && !isValidURL(link)) {
|
2024-08-06 20:12:07 +02:00
|
|
|
alert('The provided link is not a valid URL.');
|
2024-04-26 19:13:23 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:51:45 +02:00
|
|
|
publishComment();
|
2024-04-26 19:13:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const params = useParams();
|
|
|
|
|
const accountComment = useAccountComment({ commentIndex: params?.accountCommentIndex as any });
|
|
|
|
|
const subplebbitAddress = params?.subplebbitAddress || accountComment?.subplebbitAddress;
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (subplebbitAddress) {
|
|
|
|
|
setSubmitStore({ subplebbitAddress });
|
|
|
|
|
}
|
|
|
|
|
}, [subplebbitAddress, setSubmitStore]);
|
|
|
|
|
|
|
|
|
|
// redirect to pending page when pending comment is created
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (typeof index === 'number') {
|
|
|
|
|
resetSubmitStore();
|
2024-05-06 22:49:23 +02:00
|
|
|
resetFields();
|
2024-04-26 19:13:23 +02:00
|
|
|
navigate(`/profile/${index}`);
|
|
|
|
|
}
|
|
|
|
|
}, [index, resetSubmitStore, 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);
|
2024-04-27 12:41:54 +02:00
|
|
|
const cid = params?.commentCid as string;
|
2024-08-06 21:57:28 +02:00
|
|
|
const { setPublishReplyOptions, resetPublishReplyOptions, replyIndex, publishReply } = usePublishReply({ cid, subplebbitAddress });
|
2024-08-06 20:12:07 +02:00
|
|
|
|
2024-08-06 21:51:45 +02:00
|
|
|
const getAnonAddressForReply = useCallback(async () => {
|
|
|
|
|
if (anonMode && !hasCalledAnonAddressRef.current) {
|
|
|
|
|
hasCalledAnonAddressRef.current = true;
|
2024-08-06 20:12:07 +02:00
|
|
|
const existingSigner = getExistingSigner(address);
|
|
|
|
|
if (existingSigner) {
|
|
|
|
|
setPublishReplyOptions({
|
|
|
|
|
signer: existingSigner,
|
|
|
|
|
author: {
|
|
|
|
|
address: existingSigner.address,
|
2024-08-25 19:37:04 +02:00
|
|
|
displayName: displayName || undefined,
|
2024-08-06 20:12:07 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2024-08-06 21:51:45 +02:00
|
|
|
const newSigner = await getNewSigner();
|
2024-08-06 20:12:07 +02:00
|
|
|
setPublishReplyOptions({
|
|
|
|
|
signer: newSigner,
|
|
|
|
|
author: {
|
|
|
|
|
address: newSigner.address,
|
2024-08-25 19:37:04 +02:00
|
|
|
displayName: displayName || undefined,
|
2024-08-06 20:12:07 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-25 19:37:04 +02:00
|
|
|
}, [address, getExistingSigner, getNewSigner, setPublishReplyOptions, anonMode, displayName]);
|
2024-04-27 12:41:54 +02:00
|
|
|
|
2024-08-27 18:46:42 +02:00
|
|
|
const handleContentChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
|
const formattedContent = formatMarkdown(e.target.value);
|
|
|
|
|
isInPostView ? setPublishReplyOptions({ content: formattedContent }) : setSubmitStore({ content: formattedContent });
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
2024-08-29 17:48:34 +02:00
|
|
|
if (!currentContent && !currentUrl) {
|
2024-04-27 12:41:54 +02:00
|
|
|
alert(`Cannot post empty comment`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentUrl && !isValidURL(currentUrl)) {
|
|
|
|
|
alert('The provided link is not a valid URL.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-08-06 20:12:07 +02:00
|
|
|
|
2024-08-06 21:51:45 +02:00
|
|
|
publishReply();
|
2024-04-27 12:41:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (typeof replyIndex === 'number') {
|
2024-08-06 20:12:07 +02:00
|
|
|
resetPublishReplyOptions();
|
2024-05-06 22:49:23 +02:00
|
|
|
resetFields();
|
|
|
|
|
closeForm();
|
2024-04-27 12:41:54 +02:00
|
|
|
}
|
2024-08-06 20:12:07 +02:00
|
|
|
}, [replyIndex, resetPublishReplyOptions, closeForm]);
|
2024-04-27 12:41:54 +02:00
|
|
|
|
2024-08-06 21:51:45 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (anonMode) {
|
|
|
|
|
if (isInPostView) {
|
|
|
|
|
getAnonAddressForReply();
|
|
|
|
|
} else {
|
|
|
|
|
getAnonAddressForPost();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [anonMode, getAnonAddressForPost, getAnonAddressForReply, isInPostView]);
|
|
|
|
|
|
2024-04-24 08:48:31 +02:00
|
|
|
return (
|
2024-04-24 15:20:55 +02:00
|
|
|
<table className={styles.postFormTable}>
|
2024-04-24 08:48:31 +02:00
|
|
|
<tbody>
|
|
|
|
|
<tr>
|
2024-04-28 17:12:19 +02:00
|
|
|
<td>{t('name')}</td>
|
2024-04-24 08:48:31 +02:00
|
|
|
<td>
|
2024-04-26 19:13:23 +02:00
|
|
|
<input
|
|
|
|
|
type='text'
|
2024-04-28 17:12:19 +02:00
|
|
|
placeholder={!displayName ? _.capitalize(t('anonymous')) : undefined}
|
2024-04-26 19:13:23 +02:00
|
|
|
defaultValue={displayName || undefined}
|
2024-08-08 17:48:45 +02:00
|
|
|
onChange={(e) => {
|
|
|
|
|
setAccount({ ...account, author: { ...account?.author, displayName: e.target.value } });
|
2024-08-12 21:55:52 +02:00
|
|
|
if (isInPostView) {
|
2024-08-25 19:37:04 +02:00
|
|
|
setPublishReplyOptions({ displayName: e.target.value || undefined });
|
2024-08-12 21:55:52 +02:00
|
|
|
} else {
|
2024-08-25 19:37:04 +02:00
|
|
|
setSubmitStore({ displayName: e.target.value || undefined });
|
2024-08-12 21:55:52 +02:00
|
|
|
}
|
2024-08-08 17:48:45 +02:00
|
|
|
}}
|
2024-04-26 19:13:23 +02:00
|
|
|
/>
|
2024-05-17 15:29:03 +02:00
|
|
|
{isInPostView && <button onClick={onPublishReply}>{t('post')}</button>}
|
2024-04-24 08:48:31 +02:00
|
|
|
</td>
|
|
|
|
|
</tr>
|
2024-05-17 15:29:03 +02:00
|
|
|
{!isInPostView && (
|
2024-04-27 12:41:54 +02:00
|
|
|
<tr>
|
2024-04-28 17:12:19 +02:00
|
|
|
<td>{t('subject')}</td>
|
2024-04-27 12:41:54 +02:00
|
|
|
<td>
|
|
|
|
|
<input
|
|
|
|
|
type='text'
|
2024-05-06 22:49:23 +02:00
|
|
|
ref={subjectRef}
|
2024-04-27 12:41:54 +02:00
|
|
|
onChange={(e) => {
|
|
|
|
|
setSubmitStore({ title: e.target.value });
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2024-04-28 17:12:19 +02:00
|
|
|
<button onClick={onPublishPost}>{t('post')}</button>
|
2024-04-27 12:41:54 +02:00
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
)}
|
2024-04-24 08:48:31 +02:00
|
|
|
<tr>
|
2024-04-28 17:12:19 +02:00
|
|
|
<td>{t('comment')}</td>
|
2024-04-24 16:47:51 +02:00
|
|
|
<td>
|
2024-08-27 18:46:42 +02:00
|
|
|
<textarea cols={48} rows={4} wrap='soft' ref={textRef} onChange={handleContentChange} />
|
2024-04-24 16:47:51 +02:00
|
|
|
</td>
|
2024-04-24 08:48:31 +02:00
|
|
|
</tr>
|
|
|
|
|
<tr>
|
2024-04-28 17:12:19 +02:00
|
|
|
<td>{t('link')}</td>
|
2024-06-11 21:09:48 +02:00
|
|
|
<td className={styles.linkField}>
|
2024-04-26 19:13:23 +02:00
|
|
|
<input
|
|
|
|
|
type='text'
|
|
|
|
|
autoCorrect='off'
|
|
|
|
|
autoComplete='off'
|
|
|
|
|
spellCheck='false'
|
2024-04-27 12:41:54 +02:00
|
|
|
ref={urlRef}
|
2024-04-26 19:13:23 +02:00
|
|
|
onChange={(e) => {
|
|
|
|
|
setUrl(e.target.value);
|
2024-08-06 20:12:07 +02:00
|
|
|
isInPostView ? setPublishReplyOptions({ link: e.target.value }) : setSubmitStore({ link: e.target.value });
|
2024-04-26 19:13:23 +02:00
|
|
|
}}
|
|
|
|
|
/>
|
2024-06-11 21:09:48 +02:00
|
|
|
<span className={styles.linkType}> {url && <LinkTypePreviewer link={url} />}</span>
|
2024-05-31 16:10:38 +02:00
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr className={styles.spoilerButton}>
|
|
|
|
|
<td>{t('options')}</td>
|
|
|
|
|
<td>
|
|
|
|
|
[
|
|
|
|
|
<label>
|
2024-08-06 20:12:07 +02:00
|
|
|
<input
|
|
|
|
|
type='checkbox'
|
|
|
|
|
onChange={(e) => (isInPostView ? setPublishReplyOptions({ spoiler: e.target.checked }) : setSubmitStore({ spoiler: e.target.checked }))}
|
|
|
|
|
/>
|
2024-05-31 17:09:20 +02:00
|
|
|
{_.capitalize(t('spoiler'))}?
|
2024-05-31 16:10:38 +02:00
|
|
|
</label>
|
|
|
|
|
]
|
2024-04-24 08:48:31 +02:00
|
|
|
</td>
|
|
|
|
|
</tr>
|
2024-05-17 14:56:09 +02:00
|
|
|
{(isInAllView || isInSubscriptionsView) && (
|
|
|
|
|
<tr>
|
|
|
|
|
<td>{t('board')}</td>
|
|
|
|
|
<td>
|
|
|
|
|
<select onChange={(e) => setSubmitStore({ subplebbitAddress: e.target.value })} value={subplebbitAddress}>
|
2024-05-31 17:09:20 +02:00
|
|
|
<option value=''>--{t('no_board_selected')}--</option>
|
2024-05-17 14:56:09 +02:00
|
|
|
{isInAllView &&
|
|
|
|
|
defaultSubplebbitAddresses.map((address: string) => (
|
|
|
|
|
<option key={address} value={address}>
|
|
|
|
|
{address}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
{isInSubscriptionsView &&
|
|
|
|
|
subscriptions.map((sub: string) => (
|
|
|
|
|
<option key={sub} value={sub}>
|
|
|
|
|
{sub}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</select>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
)}
|
2024-04-24 08:48:31 +02:00
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
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();
|
|
|
|
|
const isInDescriptionView = isDescriptionView(location.pathname, params);
|
2024-05-17 15:29:03 +02:00
|
|
|
const isInPostView = isPostPageView(location.pathname, params);
|
2024-04-08 17:32:12 +02:00
|
|
|
const isInRulesView = isRulesView(location.pathname, params);
|
2024-06-17 12:17:36 +02:00
|
|
|
const isInAllView = isAllView(location.pathname, params);
|
|
|
|
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
|
2024-04-08 17:32:12 +02:00
|
|
|
|
2024-07-19 22:13:51 +02:00
|
|
|
const post = useComment({ commentCid: useParams().commentCid });
|
|
|
|
|
let comment: Comment = post;
|
|
|
|
|
// 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 || {};
|
2024-04-08 17:32:12 +02:00
|
|
|
const isThreadClosed = deleted || locked || removed || isInDescriptionView || isInRulesView;
|
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
|
|
|
|
2024-06-08 21:22:17 +02:00
|
|
|
const subplebbit = useSubplebbit({ subplebbitAddress: params?.subplebbitAddress });
|
2024-07-07 20:02:58 +02:00
|
|
|
const { isOffline, offlineTitle } = useIsSubplebbitOffline(subplebbit);
|
2024-06-08 21:22:17 +02:00
|
|
|
|
2024-03-20 13:00:11 +01:00
|
|
|
return (
|
2024-03-21 15:21:33 +01:00
|
|
|
<>
|
2024-04-24 15:20:55 +02:00
|
|
|
<div className={styles.postFormDesktop}>
|
2024-07-09 14:59:18 +02:00
|
|
|
{!(isInAllView || isInSubscriptionsView) && showForm && (isOffline || isOffline) && <div className={styles.offlineBoard}>{offlineTitle}</div>}
|
2024-04-08 17:13:02 +02:00
|
|
|
{isThreadClosed ? (
|
|
|
|
|
<div className={styles.closed}>
|
2024-04-08 18:36:51 +02:00
|
|
|
{t('thread_closed')}
|
2024-04-08 17:13:02 +02:00
|
|
|
<br />
|
2024-04-08 18:36:51 +02:00
|
|
|
{t('may_not_reply')}
|
2024-04-08 17:13:02 +02:00
|
|
|
</div>
|
2024-04-24 08:48:31 +02:00
|
|
|
) : !showForm ? (
|
2024-04-08 17:13:02 +02:00
|
|
|
<div>
|
|
|
|
|
[
|
|
|
|
|
<button className='button' onClick={() => setShowForm(true)}>
|
2024-05-17 15:29:03 +02:00
|
|
|
{isInPostView ? t('post_a_reply') : t('start_new_thread')}
|
2024-04-08 17:13:02 +02:00
|
|
|
</button>
|
|
|
|
|
]
|
|
|
|
|
</div>
|
2024-04-24 08:48:31 +02:00
|
|
|
) : (
|
2024-08-06 20:12:07 +02:00
|
|
|
<PostFormTable closeForm={() => setShowForm(false)} postCid={postCid} />
|
2024-04-08 17:13:02 +02:00
|
|
|
)}
|
2024-03-21 15:21:33 +01:00
|
|
|
</div>
|
2024-04-24 15:20:55 +02:00
|
|
|
<div className={styles.postFormMobile}>
|
2024-07-09 14:59:18 +02:00
|
|
|
{!(isInAllView || isInSubscriptionsView) && showForm && (isOffline || isOffline) && <div className={styles.offlineBoard}>{offlineTitle}</div>}
|
2024-04-08 17:46:36 +02:00
|
|
|
{isThreadClosed ? (
|
|
|
|
|
<div className={styles.closed}>
|
2024-04-14 14:42:05 +02:00
|
|
|
{t('thread_closed')}
|
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-05-31 15:16:21 +02:00
|
|
|
<hr />
|
2024-04-24 15:20:55 +02:00
|
|
|
</>
|
2024-04-08 17:46:36 +02:00
|
|
|
)}
|
2024-03-21 15:21:33 +01:00
|
|
|
</div>
|
|
|
|
|
</>
|
2024-03-20 13:00:11 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default PostForm;
|