From d0c4140b2e4df8ab85dccd03427f202e9aaf6655 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Wed, 4 Dec 2024 13:58:13 -0300 Subject: [PATCH] refactor: create useReblog hook --- src/components/pure-status.tsx | 7 ++++--- src/hooks/useReblog.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/hooks/useReblog.ts diff --git a/src/components/pure-status.tsx b/src/components/pure-status.tsx index f3da09e52..4a3328022 100644 --- a/src/components/pure-status.tsx +++ b/src/components/pure-status.tsx @@ -7,7 +7,6 @@ import { useIntl, FormattedMessage, defineMessages } from 'react-intl'; import { Link, useHistory } from 'react-router-dom'; import { mentionCompose } from 'soapbox/actions/compose.ts'; -import { toggleReblog } from 'soapbox/actions/interactions.ts'; import { openModal } from 'soapbox/actions/modals.ts'; import { toggleStatusHidden, unfilterStatus } from 'soapbox/actions/statuses.ts'; import TranslateButton from 'soapbox/components/translate-button.tsx'; @@ -22,6 +21,7 @@ import { HotKeys } from 'soapbox/features/ui/components/hotkeys.tsx'; import { useAppDispatch } from 'soapbox/hooks/useAppDispatch.ts'; import { useAppSelector } from 'soapbox/hooks/useAppSelector.ts'; import { useFavourite } from 'soapbox/hooks/useFavourite.ts'; +import { useReblog } from 'soapbox/hooks/useReblog.ts'; import { useReplyCompose } from 'soapbox/hooks/useReplyCompose.ts'; import { useSettings } from 'soapbox/hooks/useSettings.ts'; import { makeGetStatus } from 'soapbox/selectors/index.ts'; @@ -106,6 +106,7 @@ const PureStatus: React.FC = (props) => { const { replyCompose } = useReplyCompose(); const { toggleFavourite } = useFavourite(); + const { toggleReblog } = useReblog(); // Track height changes we know about to compensate scrolling. useEffect(() => { @@ -176,11 +177,11 @@ const PureStatus: React.FC = (props) => { }; const handleHotkeyBoost = (e?: KeyboardEvent): void => { - const modalReblog = () => dispatch(toggleReblog(statusImmutable)); // fix later + const modalReblog = () => toggleReblog(status.id); if ((e && e.shiftKey) || !boostModal) { modalReblog(); } else { - dispatch(openModal('BOOST', { status: actualStatus, onReblog: modalReblog })); + dispatch(openModal('BOOST', { status: statusImmutable, onReblog: modalReblog })); // fix later } }; diff --git a/src/hooks/useReblog.ts b/src/hooks/useReblog.ts new file mode 100644 index 000000000..d0028dbae --- /dev/null +++ b/src/hooks/useReblog.ts @@ -0,0 +1,31 @@ +import { reblog as reblogAction, unreblog as unreblogAction, toggleReblog as toggleReblogAction } from 'soapbox/actions/interactions.ts'; +import { useAppDispatch } from 'soapbox/hooks/useAppDispatch.ts'; +import { useGetState } from 'soapbox/hooks/useGetState.ts'; + +export function useReblog() { + const getState = useGetState(); + const dispatch = useAppDispatch(); + + const reblog = (statusId: string) => { + const status = getState().statuses.get(statusId); + if (status) { + dispatch(reblogAction(status)); + } + }; + + const unreblog = (statusId: string) => { + const status = getState().statuses.get(statusId); + if (status) { + dispatch(unreblogAction(status)); + } + }; + + const toggleReblog = (statusId: string) => { + const status = getState().statuses.get(statusId); + if (status) { + dispatch(toggleReblogAction(status)); + } + }; + + return { reblog, unreblog, toggleReblog }; +}