From 0ffa528e2aa914a55dd4dfc89a128f42583dc074 Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Silva Date: Mon, 20 May 2024 15:01:45 -0400 Subject: [PATCH] fix: fixes sending files to start a thread (#5670) Co-authored-by: Diego Mello --- .../MessageComposer/hooks/useChooseMedia.ts | 2 +- app/stacks/types.ts | 2 +- app/views/ShareView/Header.tsx | 36 ++++++++++++++----- app/views/ShareView/index.tsx | 13 ++++--- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/app/containers/MessageComposer/hooks/useChooseMedia.ts b/app/containers/MessageComposer/hooks/useChooseMedia.ts index 2a43ba5a7..a99d9a148 100644 --- a/app/containers/MessageComposer/hooks/useChooseMedia.ts +++ b/app/containers/MessageComposer/hooks/useChooseMedia.ts @@ -136,7 +136,7 @@ export const useChooseMedia = ({ // FIXME: use useNavigation Navigation.navigate('ShareView', { room, - thread, + thread: thread || tmid, attachments, action, finishShareView, diff --git a/app/stacks/types.ts b/app/stacks/types.ts index 0356c01c6..da4af0599 100644 --- a/app/stacks/types.ts +++ b/app/stacks/types.ts @@ -276,7 +276,7 @@ export type InsideStackParamList = { serverInfo: IServer; text: string; room: TSubscriptionModel; - thread: TThreadModel; + thread: TThreadModel | string; action: TMessageAction; finishShareView: (text?: string, selectedMessages?: string[]) => void | undefined; startShareView: () => { text: string; selectedMessages: string[] }; diff --git a/app/views/ShareView/Header.tsx b/app/views/ShareView/Header.tsx index 0e8113305..63cf0cb43 100644 --- a/app/views/ShareView/Header.tsx +++ b/app/views/ShareView/Header.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { StyleSheet, Text, View } from 'react-native'; import I18n from '../../i18n'; @@ -9,6 +9,7 @@ import sharedStyles from '../Styles'; import { makeThreadName } from '../../lib/methods/helpers/room'; import { ISubscription, TThreadModel } from '../../definitions'; import { getRoomTitle, isGroupChat, isAndroid, isTablet } from '../../lib/methods/helpers'; +import { getMessageById } from '../../lib/database/services/Message'; const androidMarginLeft = isTablet ? 0 : 4; @@ -36,13 +37,14 @@ const styles = StyleSheet.create({ interface IHeader { room: ISubscription; - thread: TThreadModel; + thread: TThreadModel | string; } const Header = React.memo(({ room, thread }: IHeader) => { + const [title, setTitle] = useState(''); const { theme } = useTheme(); let type; - if (thread?.id) { + if ((thread as TThreadModel)?.id || typeof thread === 'string') { type = 'thread'; } else if (room?.prid) { type = 'discussion'; @@ -70,12 +72,28 @@ const Header = React.memo(({ room, thread }: IHeader) => { const textColor = themes[theme].fontDefault; - let title; - if (thread?.id) { - title = makeThreadName(thread); - } else { - title = getRoomTitle(room); - } + useEffect(() => { + (async () => { + if ((thread as TThreadModel)?.id) { + const name = makeThreadName(thread as TThreadModel); + if (name) { + setTitle(name); + return; + } + } + if (typeof thread === 'string') { + // only occurs when sending images and there is no message in the thread + const data = await getMessageById(thread); + const msg = data?.asPlain()?.msg; + if (msg) { + setTitle(msg); + return; + } + } + const name = getRoomTitle(room); + setTitle(name); + })(); + }, []); return ( diff --git a/app/views/ShareView/index.tsx b/app/views/ShareView/index.tsx index c1df448aa..bbe8c3e9f 100644 --- a/app/views/ShareView/index.tsx +++ b/app/views/ShareView/index.tsx @@ -43,7 +43,7 @@ interface IShareViewState { attachments: IShareAttachment[]; text: string; room: TSubscriptionModel; - thread: TThreadModel; + thread: TThreadModel | string; maxFileSize?: number; mediaAllowList?: string; selectedMessages: string[]; @@ -88,7 +88,7 @@ class ShareView extends Component { attachments: [], text: props.route.params?.text ?? '', room: props.route.params?.room ?? {}, - thread: props.route.params?.thread ?? {}, + thread: props.route.params?.thread ?? '', maxFileSize: this.isShareExtension ? this.serverInfo?.FileUpload_MaxFileSize : props.FileUpload_MaxFileSize, mediaAllowList: this.isShareExtension ? this.serverInfo?.FileUpload_MediaTypeWhiteList @@ -265,7 +265,7 @@ class ShareView extends Component { store: 'Uploads', msg }, - thread?.id, + (thread as TThreadModel)?.id || (thread as string), server, { id: user.id, token: user.token } ); @@ -276,7 +276,10 @@ class ShareView extends Component { // Send text message } else if (text.length) { - await sendMessage(room.rid, text, thread?.id, { id: user.id, token: user.token } as IUser); + await sendMessage(room.rid, text, (thread as TThreadModel)?.id || (thread as string), { + id: user.id, + token: user.token + } as IUser); } } catch { if (!this.isShareExtension) { @@ -344,7 +347,7 @@ class ShareView extends Component { value={{ rid: room.rid, t: room.t, - tmid: thread.id, + tmid: (thread as TThreadModel)?.id || (thread as string), sharing: true, action: route.params?.action, selectedMessages,