From df2f38a60f32c83712b08d1a9f7f0baf4c4b62da Mon Sep 17 00:00:00 2001 From: Chewbacca Date: Thu, 3 Nov 2022 12:13:54 -0400 Subject: [PATCH] Add ability to DM from profile --- .../features/account/components/header.tsx | 67 +++++++++++-------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/app/soapbox/features/account/components/header.tsx b/app/soapbox/features/account/components/header.tsx index 1db50793c..84ec7c569 100644 --- a/app/soapbox/features/account/components/header.tsx +++ b/app/soapbox/features/account/components/header.tsx @@ -1,5 +1,7 @@ 'use strict'; +import { useMutation } from '@tanstack/react-query'; +import { AxiosError } from 'axios'; import { List as ImmutableList } from 'immutable'; import React from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; @@ -24,6 +26,8 @@ import ActionButton from 'soapbox/features/ui/components/action-button'; import SubscriptionButton from 'soapbox/features/ui/components/subscription-button'; import { useAppDispatch, useFeatures, useOwnAccount } from 'soapbox/hooks'; import { normalizeAttachment } from 'soapbox/normalizers'; +import { ChatKeys, useChats } from 'soapbox/queries/chats'; +import { queryClient } from 'soapbox/queries/client'; import { Account } from 'soapbox/types/entities'; import { isRemote } from 'soapbox/utils/accounts'; @@ -80,6 +84,21 @@ const Header: React.FC = ({ account }) => { const features = useFeatures(); const ownAccount = useOwnAccount(); + const { getOrCreateChatByAccountId } = useChats(); + + const createAndNavigateToChat = useMutation((accountId: string) => { + return getOrCreateChatByAccountId(accountId); + }, { + onError: (error: AxiosError) => { + const data = error.response?.data as any; + dispatch(snackbar.error(data?.error)); + }, + onSuccess: (response) => { + history.push(`/chats/${response.data.id}`); + queryClient.invalidateQueries(ChatKeys.chatSearch()); + }, + }); + if (!account) { return (
@@ -480,34 +499,28 @@ const Header: React.FC = ({ account }) => { return info; }; - // const renderMessageButton = () => { - // if (!ownAccount || !account || account.id === ownAccount?.id) { - // return null; - // } + const renderMessageButton = () => { + if (!ownAccount || !account || account.id === ownAccount?.id) { + return null; + } - // const canChat = account.getIn(['pleroma', 'accepts_chat_messages']) === true; + const canChat = account.relationship?.followed_by; + if (!canChat) { + return null; + } - // if (canChat) { - // return ( - // - // ); - // } else { - // return ( - // - // ); - // } - // }; + return ( + createAndNavigateToChat.mutate(account.id)} + title={intl.formatMessage(messages.chat, { name: account.username })} + theme='outlined' + className='px-2' + iconClassName='w-4 h-4' + disabled={createAndNavigateToChat.isLoading} + /> + ); + }; const renderShareButton = () => { const canShare = 'share' in navigator; @@ -610,7 +623,7 @@ const Header: React.FC = ({ account }) => { )} {renderShareButton()} - {/* {renderMessageButton()} */} + {renderMessageButton()}