From 2cc4b9c1ba57ae24911ded382d65fbf07fad3345 Mon Sep 17 00:00:00 2001
From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com>
Date: Tue, 15 Mar 2022 11:41:23 -0300
Subject: [PATCH] Chore: Migrate REST API - getCommandPreview and
 executeCommandPreview to Typescript (#3897)

---
 .../MessageBox/CommandsPreview/index.tsx         |  3 ++-
 app/containers/MessageBox/index.tsx              |  6 +++---
 app/definitions/ISlashCommand.ts                 |  7 +++++++
 app/definitions/rest/v1/commands.ts              | 16 +++++++++++++---
 app/lib/rocketchat/services/restApi.ts           | 11 +++++------
 5 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/app/containers/MessageBox/CommandsPreview/index.tsx b/app/containers/MessageBox/CommandsPreview/index.tsx
index c5434f66..d55545dd 100644
--- a/app/containers/MessageBox/CommandsPreview/index.tsx
+++ b/app/containers/MessageBox/CommandsPreview/index.tsx
@@ -6,9 +6,10 @@ import Item from './Item';
 import styles from '../styles';
 import { themes } from '../../../constants/colors';
 import { withTheme } from '../../../theme';
+import { IPreviewItem } from '../../../definitions';
 
 interface IMessageBoxCommandsPreview {
-	commandPreview: [];
+	commandPreview: IPreviewItem[];
 	showCommandPreview: boolean;
 	theme?: string;
 }
diff --git a/app/containers/MessageBox/index.tsx b/app/containers/MessageBox/index.tsx
index edbb76d5..073d04b7 100644
--- a/app/containers/MessageBox/index.tsx
+++ b/app/containers/MessageBox/index.tsx
@@ -49,7 +49,7 @@ import { sanitizeLikeString } from '../../lib/database/utils';
 import { CustomIcon } from '../../lib/Icons';
 import { IMessage } from '../../definitions/IMessage';
 import { forceJpgExtension } from './forceJpgExtension';
-import { IUser } from '../../definitions';
+import { IPreviewItem, IUser } from '../../definitions';
 
 if (isAndroid) {
 	require('./EmojiKeyboard');
@@ -114,7 +114,7 @@ interface IMessageBoxState {
 	showSend: any;
 	recording: boolean;
 	trackingType: string;
-	commandPreview: [];
+	commandPreview: IPreviewItem[];
 	showCommandPreview: boolean;
 	command: {
 		appId?: any;
@@ -643,7 +643,7 @@ class MessageBox extends Component<IMessageBoxProps, IMessageBoxState> {
 		try {
 			const response = await RocketChat.getCommandPreview(name, rid, params);
 			if (response.success) {
-				return this.setState({ commandPreview: response.preview?.items, showCommandPreview: true, command });
+				return this.setState({ commandPreview: response.preview?.items || [], showCommandPreview: true, command });
 			}
 		} catch (e) {
 			log(e);
diff --git a/app/definitions/ISlashCommand.ts b/app/definitions/ISlashCommand.ts
index 96639273..1f62bffd 100644
--- a/app/definitions/ISlashCommand.ts
+++ b/app/definitions/ISlashCommand.ts
@@ -14,3 +14,10 @@ export interface ISlashCommandResult extends ISlashCommand {
 }
 
 export type TSlashCommandModel = ISlashCommand & Model;
+
+// For Command Preview ex: /giphy or /tenor in open.rocket.chat
+export interface IPreviewItem {
+	id: string;
+	type: string;
+	value: string;
+}
diff --git a/app/definitions/rest/v1/commands.ts b/app/definitions/rest/v1/commands.ts
index 2aa9dabd..3dd81b68 100644
--- a/app/definitions/rest/v1/commands.ts
+++ b/app/definitions/rest/v1/commands.ts
@@ -1,10 +1,20 @@
+import { IPreviewItem } from '../../ISlashCommand';
+
 export type CommandsEndpoints = {
 	'commands.preview': {
-		GET: (params: { command: string; roomId: string; params: string }) => {
-			preview: {
+		GET: (params: { command: string; params: string; roomId: string }) => {
+			preview?: {
 				i18nTitle: string;
-				items: any;
+				items: IPreviewItem[];
 			};
 		};
+		POST: (params: {
+			command: string;
+			params: string;
+			roomId: string;
+			previewItem: IPreviewItem;
+			triggerId: string;
+			tmid?: string;
+		}) => {};
 	};
 };
diff --git a/app/lib/rocketchat/services/restApi.ts b/app/lib/rocketchat/services/restApi.ts
index 38754af6..721d2f29 100644
--- a/app/lib/rocketchat/services/restApi.ts
+++ b/app/lib/rocketchat/services/restApi.ts
@@ -7,7 +7,8 @@ import {
 	IRoomNotifications,
 	TRocketChat,
 	IMessage,
-	IRoom
+	IRoom,
+	IPreviewItem
 } from '../../../definitions';
 import { ISpotlight } from '../../../definitions/ISpotlight';
 import { IAvatarSuggestion, IParams } from '../../../definitions/IProfileViewInterfaces';
@@ -696,15 +697,13 @@ export const getCommandPreview = (command: string, roomId: string, params: strin
 
 export const executeCommandPreview = (
 	command: string,
-	params: any,
+	params: string,
 	roomId: string,
-	previewItem: any,
+	previewItem: IPreviewItem,
 	triggerId: string,
 	tmid?: string
-): any =>
+) =>
 	// RC 0.65.0
-	// TODO: missing definitions from server
-	// @ts-ignore
 	sdk.post('commands.preview', {
 		command,
 		params,