From 88d848ee17149fbc89ff0dfe74d079f25348ae16 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 16 Sep 2022 14:33:31 -0400 Subject: [PATCH] Add welcome screen to Chats main page --- .../chats/components/chat-page/chat-page.tsx | 13 +- .../chat-page/components/chat-page-main.tsx | 203 ++++++++++-------- .../components/chat-page-sidebar.tsx | 31 ++- .../chat-page/components/welcome.tsx | 70 ++++++ app/soapbox/normalizers/account.ts | 2 + 5 files changed, 211 insertions(+), 108 deletions(-) create mode 100644 app/soapbox/features/chats/components/chat-page/components/welcome.tsx diff --git a/app/soapbox/features/chats/components/chat-page/chat-page.tsx b/app/soapbox/features/chats/components/chat-page/chat-page.tsx index eb3a80b06e..56e54285bf 100644 --- a/app/soapbox/features/chats/components/chat-page/chat-page.tsx +++ b/app/soapbox/features/chats/components/chat-page/chat-page.tsx @@ -1,6 +1,6 @@ import React from 'react'; -import { Card } from 'soapbox/components/ui'; +import { Card, Stack } from 'soapbox/components/ui'; import ChatPageMain from './components/chat-page-main'; import ChatPageSidebar from './components/chat-page-sidebar'; @@ -9,8 +9,15 @@ const ChatPage = () => { return (
- - + + + + + + +
); diff --git a/app/soapbox/features/chats/components/chat-page/components/chat-page-main.tsx b/app/soapbox/features/chats/components/chat-page/components/chat-page-main.tsx index 88c5badce7..f1a065a3fc 100644 --- a/app/soapbox/features/chats/components/chat-page/components/chat-page-main.tsx +++ b/app/soapbox/features/chats/components/chat-page/components/chat-page-main.tsx @@ -1,18 +1,23 @@ +import { useMutation } from '@tanstack/react-query'; import React from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { blockAccount } from 'soapbox/actions/accounts'; +import { patchMeSuccess } from 'soapbox/actions/me'; import { openModal } from 'soapbox/actions/modals'; import { initReport } from 'soapbox/actions/reports'; +import snackbar from 'soapbox/actions/snackbar'; import List, { ListItem } from 'soapbox/components/list'; -import { Avatar, Divider, HStack, Icon, IconButton, Menu, MenuButton, MenuItem, MenuList, Stack, Text, Toggle } from 'soapbox/components/ui'; +import { Avatar, CardBody, CardHeader, CardTitle, Divider, HStack, Icon, IconButton, Menu, MenuButton, MenuItem, MenuList, Stack, Text, Toggle } from 'soapbox/components/ui'; import VerificationBadge from 'soapbox/components/verification_badge'; import { useChatContext } from 'soapbox/contexts/chat-context'; -import { useAppDispatch } from 'soapbox/hooks'; +import { useApi, useAppDispatch, useOwnAccount } from 'soapbox/hooks'; import { useChat, useChatSilence } from 'soapbox/queries/chats'; import Chat from '../../chat'; +import Welcome from './welcome'; + const messages = defineMessages({ blockMessage: { id: 'chat_settings.block.message', defaultMessage: 'Blocking will prevent this profile from direct messaging you and viewing your content. You can unblock later.' }, blockHeading: { id: 'chat_settings.block.heading', defaultMessage: 'Block @{acct}' }, @@ -28,6 +33,8 @@ const messages = defineMessages({ const ChatPageMain = () => { const dispatch = useAppDispatch(); const intl = useIntl(); + const account = useOwnAccount(); + const api = useApi(); const { chat } = useChatContext(); const { isSilenced, handleSilence } = useChatSilence(chat); @@ -55,107 +62,113 @@ const ChatPageMain = () => { const handleReportChat = () => dispatch(initReport(chat?.account as any)); + if (!account?.chats_onboarded) { + return ( + + ); + } + + if (!chat) { + return null; + } + return ( - - {chat && ( - - - - + + + + - -
- {chat.account?.display_name || `@${chat.account.username}`} - {chat.account?.verified && } -
+ +
+ {chat.account?.display_name || `@${chat.account.username}`} + {chat.account?.verified && } +
- - {chat.account.acct} - -
-
+ + {chat.account.acct} + +
+
- - + + - - - - - - {chat.account.display_name} - @{chat.account.acct} - - - - - - - - - - - - - - - -
- - {intl.formatMessage(messages.blockUser, { acct: chat.account.acct })} -
-
- - -
- - {intl.formatMessage(messages.reportUser, { acct: chat.account.acct })} -
-
- - -
- - {intl.formatMessage(messages.leaveChat)} -
-
-
+ + + + + + {chat.account.display_name} + @{chat.account.acct} - -
- + -
- -
- - )} + + + + + + + + + + + + +
+ + {intl.formatMessage(messages.blockUser, { acct: chat.account.acct })} +
+
+ + +
+ + {intl.formatMessage(messages.reportUser, { acct: chat.account.acct })} +
+
+ + +
+ + {intl.formatMessage(messages.leaveChat)} +
+
+
+ + +
+
+ +
+ +
); }; -export default ChatPageMain; \ No newline at end of file +export default ChatPageMain; diff --git a/app/soapbox/features/chats/components/chat-page/components/chat-page-sidebar.tsx b/app/soapbox/features/chats/components/chat-page/components/chat-page-sidebar.tsx index 7b2ac5486e..daddf47012 100644 --- a/app/soapbox/features/chats/components/chat-page/components/chat-page-sidebar.tsx +++ b/app/soapbox/features/chats/components/chat-page/components/chat-page-sidebar.tsx @@ -1,17 +1,18 @@ +import { useMutation } from '@tanstack/react-query'; +import { AxiosError } from 'axios'; import React from 'react'; import { defineMessages, useIntl } from 'react-intl'; -import { useHistory } from 'react-router-dom'; -import { launchChat } from 'soapbox/actions/chats'; +import snackbar from 'soapbox/actions/snackbar'; import AccountSearch from 'soapbox/components/account_search'; import { CardTitle, Stack } from 'soapbox/components/ui'; import { useChatContext } from 'soapbox/contexts/chat-context'; import { useAppDispatch } from 'soapbox/hooks'; +import { IChat, useChats } from 'soapbox/queries/chats'; +import { queryClient } from 'soapbox/queries/client'; import ChatList from '../../chat-list'; -import type { IChat } from 'soapbox/queries/chats'; - const messages = defineMessages({ title: { id: 'column.chats', defaultMessage: 'Messages' }, searchPlaceholder: { id: 'chats.search_placeholder', defaultMessage: 'Start a chat with…' }, @@ -19,22 +20,32 @@ const messages = defineMessages({ const ChatPageSidebar = () => { const dispatch = useAppDispatch(); - const history = useHistory(); const intl = useIntl(); const { setChat } = useChatContext(); + const { getOrCreateChatByAccountId } = useChats(); const handleSuggestion = (accountId: string) => { - dispatch(launchChat(accountId, history, true)); + handleClickOnSearchResult.mutate(accountId); }; + const handleClickOnSearchResult = useMutation((accountId: string) => { + return getOrCreateChatByAccountId(accountId); + }, { + onError: (error: AxiosError) => { + const data = error.response?.data as any; + dispatch(snackbar.error(data?.error)); + }, + onSuccess: (response) => { + setChat(response.data); + queryClient.invalidateQueries(['chats']); + }, + }); + const handleClickChat = (chat: IChat) => setChat(chat); return ( - + { + const account = useOwnAccount(); + const api = useApi(); + const dispatch = useAppDispatch(); + + const [data, setData] = useState({ + chats_onboarded: true, + accepting_messages: account?.accepting_messages, + }); + + const updateSettings = useMutation(() => api.patch('/api/v1/accounts/update_credentials', data), { + onSuccess(response) { + dispatch(patchMeSuccess(response.data)); + dispatch(snackbar.success('Chat Settings updated successfully')); + }, + onError() { + dispatch(snackbar.success('Chat Settings failed to update.')); + }, + }); + + const handleSubmit = (event: React.FormEvent) => { + event.preventDefault(); + + updateSettings.mutate(); + }; + + return ( + + + +
+ + + + + + setData((prevData) => ({ ...prevData, accepting_messages: event.target.checked }))} + /> + + + + + + +
+ ); +}; + +export default Welcome; \ No newline at end of file diff --git a/app/soapbox/normalizers/account.ts b/app/soapbox/normalizers/account.ts index 37f42ab8f1..bc95d03c3a 100644 --- a/app/soapbox/normalizers/account.ts +++ b/app/soapbox/normalizers/account.ts @@ -21,11 +21,13 @@ import type { Emoji, Field, EmbeddedEntity, Relationship } from 'soapbox/types/e // https://docs.joinmastodon.org/entities/account/ export const AccountRecord = ImmutableRecord({ + accepting_messages: false, acct: '', avatar: '', avatar_static: '', birthday: '', bot: false, + chats_onboarded: false, created_at: '', discoverable: false, display_name: '',