diff --git a/app/javascript/mastodon/components/account_header/buttons.tsx b/app/javascript/mastodon/components/account_header/buttons.tsx index a4db87e044d..cdc2d9e6979 100644 --- a/app/javascript/mastodon/components/account_header/buttons.tsx +++ b/app/javascript/mastodon/components/account_header/buttons.tsx @@ -3,10 +3,9 @@ import type { FC } from 'react'; import { defineMessages, useIntl } from 'react-intl'; -import { useLocation } from 'react-router-dom'; - import { followAccount } from '@/mastodon/actions/accounts'; import { useAccount } from '@/mastodon/hooks/useAccount'; +import { useFollowReference } from '@/mastodon/hooks/useFollowReference'; import { getAccountHidden } from '@/mastodon/selectors/accounts'; import { useAppDispatch, useAppSelector } from '@/mastodon/store'; import NotificationsIcon from '@/material-icons/400-24px/notifications.svg?react'; @@ -83,10 +82,8 @@ const AccountButtonsOther: FC< }); } }, [accountUrl]); - const { state } = useLocation<{ - reference?: string; - } | null>(); - const reference = state?.reference ?? 'profile'; + + const reference = useFollowReference('profile'); if (!account) { return null; diff --git a/app/javascript/mastodon/hooks/useFollowReference.ts b/app/javascript/mastodon/hooks/useFollowReference.ts new file mode 100644 index 00000000000..e43259a4c5a --- /dev/null +++ b/app/javascript/mastodon/hooks/useFollowReference.ts @@ -0,0 +1,9 @@ +import { useLocation } from 'react-router-dom'; + +import type { LocationState } from '../components/router'; + +export function useFollowReference(fallbackReference: string) { + const { state } = useLocation(); + + return state?.reference ?? fallbackReference; +}