MCPcopy Create free account
hub / github.com/OpenSIST/OpenSIST.github.io / AddModifyPost

Function AddModifyPost

src/Components/Modify/Post/AddModifyPost.jsx:131–240  ·  view source on GitHub ↗
({type})

Source from the content-addressed store, hash-verified

129}
130
131export default function AddModifyPost({type}) {
132 const mode = type === "new" ? "添加" : "编辑";
133 const navigate = useNavigate();
134 const submit = useSubmit();
135 const loaderData = useLoaderData();
136 const postObj = loaderData?.postObj;
137 const preparedPostContent = useMemo(
138 () => preparePostContentForEditing(postObj?.content ?? ""),
139 [postObj?.content]
140 );
141 const [title, setTitle] = useState(postObj?.title ?? "");
142 const [content, setContent] = useState(preparedPostContent.content);
143 const [attachments, setAttachments] = useState(preparedPostContent.attachments);
144 const [contentError, setContentError] = useState("");
145 const [submitting, setSubmitting] = useState(false);
146 const attachmentsRef = useRef(attachments);
147 const theme = useTheme();
148 const darkMode = theme.palette.mode === 'dark';
149 const titleTrimmed = title.trim();
150 const titleTooLong = title.length > POST_TITLE_MAX_LENGTH;
151 const contentBytes = estimateFinalPostContentBytes(content, attachments);
152 const contentTooLarge = contentBytes > POST_CONTENT_MAX_BYTES;
153 const postContentMessage = contentTooLarge
154 ? `文章内容不能超过 ${formatPostContentSize(POST_CONTENT_MAX_BYTES)},当前 ${formatPostContentSize(contentBytes)}。`
155 : contentError;
156 useEffect(() => {
157 attachmentsRef.current = attachments;
158 }, [attachments]);
159
160 const handleAttachmentPrepared = useCallback((file) => {
161 const attachment = createPostAttachmentFromFile(file);
162 const nextAttachments = new Map(attachmentsRef.current);
163 nextAttachments.set(attachment.token, attachment);
164 attachmentsRef.current = nextAttachments;
165 setAttachments(nextAttachments);
166 return attachment.token;
167 }, []);
168 const resolveAttachmentPreview = useCallback(
169 (src) => getPostAttachmentPreviewUrl(src, attachmentsRef.current),
170 []
171 );
172
173 async function handleSubmit(event) {
174 event.preventDefault();
175 if (!titleTrimmed || titleTooLong || contentTooLarge || submitting) {
176 return;
177 }
178 const form = event.currentTarget;
179 const submitter = event.nativeEvent.submitter;
180 setSubmitting(true);
181 try {
182 const finalizedContent = await finalizePostContentAttachments(content, attachmentsRef.current);
183 const finalizedContentBytes = getUtf8ByteLength(finalizedContent);
184 if (finalizedContentBytes > POST_CONTENT_MAX_BYTES) {
185 setContentError(`文章内容不能超过 ${formatPostContentSize(POST_CONTENT_MAX_BYTES)},当前 ${formatPostContentSize(finalizedContentBytes)}。`);
186 return;
187 }
188 const formData = new FormData(form);

Callers

nothing calls this directly

Tested by

no test coverage detected