2022-04-11 18:01:43 +00:00
|
|
|
/* eslint-disable react-hooks/rules-of-hooks */
|
2020-02-11 14:01:35 +00:00
|
|
|
import React, { useContext } from 'react';
|
2020-05-18 16:25:13 +00:00
|
|
|
import { StyleSheet, Text } from 'react-native';
|
2022-05-26 14:07:17 +00:00
|
|
|
import {
|
|
|
|
UiKitParserMessage,
|
|
|
|
UiKitParserModal,
|
|
|
|
uiKitMessage,
|
|
|
|
uiKitModal,
|
|
|
|
BlockContext,
|
|
|
|
Markdown as IMarkdown,
|
|
|
|
PlainText
|
|
|
|
} from '@rocket.chat/ui-kit';
|
2020-02-11 14:01:35 +00:00
|
|
|
|
2022-02-17 15:27:01 +00:00
|
|
|
import Markdown, { MarkdownPreview } from '../markdown';
|
2020-02-11 14:01:35 +00:00
|
|
|
import Button from '../Button';
|
2022-06-27 18:46:59 +00:00
|
|
|
import { FormTextInput } from '../TextInput';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { textParser, useBlockContext } from './utils';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2020-05-18 16:25:13 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2020-02-11 14:01:35 +00:00
|
|
|
import { Divider } from './Divider';
|
|
|
|
import { Section } from './Section';
|
|
|
|
import { Actions } from './Actions';
|
|
|
|
import { Image } from './Image';
|
|
|
|
import { Select } from './Select';
|
|
|
|
import { Context } from './Context';
|
|
|
|
import { MultiSelect } from './MultiSelect';
|
|
|
|
import { Input } from './Input';
|
|
|
|
import { DatePicker } from './DatePicker';
|
|
|
|
import { Overflow } from './Overflow';
|
|
|
|
import { ThemeContext } from '../../theme';
|
2022-05-26 14:07:17 +00:00
|
|
|
import { IActions, IButton, IElement, IInputIndex, IParser, ISection } from './interfaces';
|
2022-11-24 11:37:08 +00:00
|
|
|
import VideoConferenceBlock from './VideoConferenceBlock';
|
2020-02-11 14:01:35 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
input: {
|
|
|
|
marginBottom: 0
|
|
|
|
},
|
|
|
|
multiline: {
|
|
|
|
height: 130
|
|
|
|
},
|
|
|
|
button: {
|
|
|
|
marginBottom: 16
|
2020-05-18 16:25:13 +00:00
|
|
|
},
|
|
|
|
text: {
|
|
|
|
fontSize: 16,
|
|
|
|
lineHeight: 22,
|
|
|
|
textAlignVertical: 'center',
|
|
|
|
...sharedStyles.textRegular
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const plainText = ({ text } = { text: '' }) => text;
|
|
|
|
|
2022-05-26 14:07:17 +00:00
|
|
|
class MessageParser extends UiKitParserMessage<React.ReactElement> {
|
2022-03-16 19:07:49 +00:00
|
|
|
get current() {
|
|
|
|
return this as unknown as IParser;
|
|
|
|
}
|
|
|
|
|
2022-05-26 14:07:17 +00:00
|
|
|
plain_text(element: PlainText, context: BlockContext): React.ReactElement {
|
2022-03-16 19:07:49 +00:00
|
|
|
const { theme } = useContext(ThemeContext);
|
2022-05-26 14:07:17 +00:00
|
|
|
|
|
|
|
const isContext = context === BlockContext.CONTEXT;
|
|
|
|
if (isContext) {
|
|
|
|
return (
|
|
|
|
<MarkdownPreview msg={element.text} style={[isContext && { color: themes[theme].auxiliaryText }]} numberOfLines={0} />
|
|
|
|
);
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
2022-05-26 14:07:17 +00:00
|
|
|
return <Text style={[styles.text, { color: themes[theme].bodyText }]}>{element.text}</Text>;
|
|
|
|
}
|
|
|
|
|
|
|
|
mrkdwn(element: IMarkdown, context: BlockContext) {
|
|
|
|
const { theme } = useContext(ThemeContext);
|
2020-02-11 14:01:35 +00:00
|
|
|
|
2022-05-26 14:07:17 +00:00
|
|
|
const isContext = context === BlockContext.CONTEXT;
|
2022-02-17 15:27:01 +00:00
|
|
|
if (isContext) {
|
2022-05-26 14:07:17 +00:00
|
|
|
return (
|
|
|
|
<MarkdownPreview msg={element.text} style={[isContext && { color: themes[theme].auxiliaryText }]} numberOfLines={0} />
|
|
|
|
);
|
2022-02-17 15:27:01 +00:00
|
|
|
}
|
2022-07-04 18:10:14 +00:00
|
|
|
return <Markdown msg={element.text} theme={theme} style={[isContext && { color: themes[theme].auxiliaryText }]} />;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 19:07:49 +00:00
|
|
|
button(element: IButton, context: BlockContext) {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { text, value, actionId, style } = element;
|
2022-03-16 19:07:49 +00:00
|
|
|
const [{ loading }, action] = useBlockContext(element, context);
|
2020-02-11 14:01:35 +00:00
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
key={actionId}
|
|
|
|
type={style}
|
2020-05-18 16:25:13 +00:00
|
|
|
title={textParser([text])}
|
2020-02-11 14:01:35 +00:00
|
|
|
loading={loading}
|
|
|
|
onPress={() => action({ value })}
|
|
|
|
style={styles.button}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
divider() {
|
2022-03-29 20:06:50 +00:00
|
|
|
return <Divider />;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
section(args: ISection) {
|
|
|
|
return <Section {...args} parser={this.current} />;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
actions(args: IActions) {
|
|
|
|
return <Actions {...args} parser={this.current} />;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
overflow(element: IElement, context: BlockContext) {
|
|
|
|
const [{ loading }, action] = useBlockContext(element, context);
|
|
|
|
return <Overflow element={element} context={context} loading={loading} action={action} parser={this.current} />;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
datePicker(element: IElement, context: BlockContext) {
|
|
|
|
const [{ loading, value, error, language }, action] = useBlockContext(element, context);
|
2020-02-11 14:01:35 +00:00
|
|
|
return (
|
|
|
|
<DatePicker
|
|
|
|
element={element}
|
|
|
|
language={language}
|
|
|
|
value={value}
|
|
|
|
action={action}
|
|
|
|
context={context}
|
|
|
|
loading={loading}
|
|
|
|
error={error}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
image(element: IElement, context: BlockContext) {
|
|
|
|
return <Image element={element} context={context} />;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
context(args: any) {
|
2020-02-11 14:01:35 +00:00
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
return <Context {...args} theme={theme} parser={this} />;
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
multiStaticSelect(element: IElement, context: BlockContext) {
|
|
|
|
const [{ loading, value }, action] = useBlockContext(element, context);
|
2023-08-03 19:37:14 +00:00
|
|
|
const valueFiltered = element.options?.filter(option => value.includes(option.value));
|
|
|
|
return <MultiSelect {...element} value={valueFiltered} onChange={action} context={context} loading={loading} multiselect />;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
staticSelect(element: IElement, context: BlockContext) {
|
|
|
|
const [{ loading, value }, action] = useBlockContext(element, context);
|
|
|
|
return <Select {...element} value={value} onChange={action} loading={loading} />;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
selectInput(element: IElement, context: BlockContext) {
|
|
|
|
const [{ loading, value }, action] = useBlockContext(element, context);
|
|
|
|
return <MultiSelect {...element} value={value} onChange={action} context={context} loading={loading} />;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
2022-11-24 11:37:08 +00:00
|
|
|
|
|
|
|
video_conf(element: IElement & { callId: string }) {
|
|
|
|
return <VideoConferenceBlock callId={element.callId} blockId={element.blockId!} />;
|
|
|
|
}
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 14:07:17 +00:00
|
|
|
// plain_text and mrkdwn functions are created in MessageParser and the ModalParser's constructor use the same functions
|
|
|
|
// @ts-ignore
|
|
|
|
class ModalParser extends UiKitParserModal<React.ReactElement> {
|
2020-02-11 14:01:35 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
2021-09-13 20:41:05 +00:00
|
|
|
Object.getOwnPropertyNames(MessageParser.prototype).forEach(method => {
|
2022-05-26 14:07:17 +00:00
|
|
|
// @ts-ignore
|
2020-02-11 14:01:35 +00:00
|
|
|
ModalParser.prototype[method] = ModalParser.prototype[method] || MessageParser.prototype[method];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-16 19:07:49 +00:00
|
|
|
get current() {
|
|
|
|
return this as unknown as IParser;
|
|
|
|
}
|
|
|
|
|
|
|
|
input({ element, blockId, appId, label, description, hint }: IInputIndex, context: number) {
|
2022-03-29 20:06:50 +00:00
|
|
|
const [{ error }] = useBlockContext({ ...element, appId, blockId }, context);
|
|
|
|
const { theme } = useContext(ThemeContext);
|
2020-02-11 14:01:35 +00:00
|
|
|
return (
|
|
|
|
<Input
|
2022-03-16 19:07:49 +00:00
|
|
|
parser={this.current}
|
2020-02-11 14:01:35 +00:00
|
|
|
element={{ ...element, appId, blockId }}
|
|
|
|
label={plainText(label)}
|
|
|
|
description={plainText(description)}
|
|
|
|
hint={plainText(hint)}
|
|
|
|
error={error}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
image(element: IElement, context: BlockContext) {
|
|
|
|
return <Image element={element} context={context} />;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
plainInput(element: IElement, context: BlockContext) {
|
|
|
|
const [{ loading, value, error }, action] = useBlockContext(element, context);
|
2020-02-11 14:01:35 +00:00
|
|
|
const { multiline, actionId, placeholder } = element;
|
|
|
|
return (
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2022-03-22 18:01:54 +00:00
|
|
|
key={actionId}
|
2020-02-11 14:01:35 +00:00
|
|
|
placeholder={plainText(placeholder)}
|
|
|
|
multiline={multiline}
|
|
|
|
loading={loading}
|
2022-03-22 18:01:54 +00:00
|
|
|
onChangeText={text => action({ value: text })}
|
2020-02-11 14:01:35 +00:00
|
|
|
inputStyle={multiline && styles.multiline}
|
|
|
|
containerStyle={styles.input}
|
|
|
|
value={value}
|
|
|
|
error={{ error }}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const messageParser = new MessageParser();
|
|
|
|
export const modalParser = new ModalParser();
|
|
|
|
|
2022-05-26 14:07:17 +00:00
|
|
|
export const UiKitMessage = uiKitMessage(messageParser, { engine: 'rocket.chat' }) as any;
|
|
|
|
export const UiKitModal = uiKitModal(modalParser) as any;
|
2020-02-11 14:01:35 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
export const UiKitComponent = ({ render, blocks }: any) => render(blocks);
|