Infer quote_id from links in status content

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak
2024-04-29 23:53:10 +02:00
parent 1d177831fe
commit 9668846ff0
6 changed files with 75 additions and 8 deletions

View File

@ -17,7 +17,7 @@ const QuotedStatusContainer: React.FC<IQuotedStatusContainer> = ({ composeId })
const status = useAppSelector(state => getStatus(state, { id: state.compose.get(composeId)?.quote! }));
const onCancel = () => {
dispatch(cancelQuoteCompose());
dispatch(cancelQuoteCompose(composeId));
};
if (!status) {

View File

@ -1,9 +1,12 @@
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { $getRoot } from 'lexical';
import { useEffect } from 'react';
import debounce from 'lodash/debounce';
import { useCallback, useEffect } from 'react';
import { setEditorState } from 'soapbox/actions/compose';
import { useAppDispatch } from 'soapbox/hooks';
import { addSuggestedQuote, setEditorState } from 'soapbox/actions/compose';
import { fetchStatus } from 'soapbox/actions/statuses';
import { useAppDispatch, useFeatures } from 'soapbox/hooks';
import { getStatusIdsFromLinksInContent } from 'soapbox/utils/status';
interface IStatePlugin {
composeId: string;
@ -12,6 +15,38 @@ interface IStatePlugin {
const StatePlugin: React.FC<IStatePlugin> = ({ composeId }) => {
const dispatch = useAppDispatch();
const [editor] = useLexicalComposerContext();
const features = useFeatures();
const getQuoteSuggestions = useCallback(debounce((text: string) => {
dispatch(async (_, getState) => {
const state = getState();
const compose = state.compose.get(composeId);
if (!features.quotePosts || compose?.quote) return;
const ids = getStatusIdsFromLinksInContent(text);
let quoteId: string | undefined;
for (const id of ids) {
if (compose?.dismissed_quotes.includes(id)) continue;
if (state.statuses.get(id)) {
quoteId = id;
break;
}
const status = await dispatch(fetchStatus(id));
if (status) {
quoteId = status.id;
break;
}
}
if (quoteId) dispatch(addSuggestedQuote(composeId, quoteId));
});
}, 2000), []);
useEffect(() => {
editor.registerUpdateListener(({ editorState }) => {
@ -19,6 +54,7 @@ const StatePlugin: React.FC<IStatePlugin> = ({ composeId }) => {
const isEmpty = text === '';
const data = isEmpty ? null : JSON.stringify(editorState.toJSON());
dispatch(setEditorState(composeId, data, text));
getQuoteSuggestions(text);
});
}, [editor]);