Infer quote_id from links in status content
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
@ -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) {
|
||||
|
||||
@ -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]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user