Rocket.Chat.ReactNative/app/containers/UIKit/index.tsx

221 lines
6.6 KiB
TypeScript
Raw Normal View History

/* eslint-disable react-hooks/rules-of-hooks */
2020-02-11 14:01:35 +00:00
import React, { useContext } from 'react';
import { StyleSheet, Text } from 'react-native';
import {
UiKitParserMessage,
UiKitParserModal,
uiKitMessage,
uiKitModal,
BlockContext,
Markdown as IMarkdown,
PlainText
} from '@rocket.chat/ui-kit';
2020-02-11 14:01:35 +00:00
import Markdown, { MarkdownPreview } from '../markdown';
2020-02-11 14:01:35 +00:00
import Button from '../Button';
import { FormTextInput } from '../TextInput';
import { textParser, useBlockContext } from './utils';
import { themes } from '../../lib/constants';
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';
import { IActions, IButton, IElement, IInputIndex, IParser, ISection } from './interfaces';
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
},
text: {
fontSize: 16,
lineHeight: 22,
textAlignVertical: 'center',
...sharedStyles.textRegular
2020-02-11 14:01:35 +00:00
}
});
const plainText = ({ text } = { text: '' }) => text;
class MessageParser extends UiKitParserMessage<React.ReactElement> {
get current() {
return this as unknown as IParser;
}
plain_text(element: PlainText, context: BlockContext): React.ReactElement {
const { theme } = useContext(ThemeContext);
const isContext = context === BlockContext.CONTEXT;
if (isContext) {
return (
feat: mobile color normalization (#5616) * chore: remove auxiliaryText color * chore: remove titleText * chore: password colors change * chore: use fontDefault on ActionSheet item * wip: type * chore: set custom icon default color * remove tintActive color * only set color when checked * remove icon color * remove tintActive * chore: remove STATUS_COLORS * chore: remove mentions colors * chore: remove switch color * chore: background color * chore: auxiliaryBackground * chore: one local colors * wip: some colors * wip: colors * wip: colors * wip: end colors * test: update * chore: fix some colors * chore: fix lint * chore: back to text props * chore: fix ts errors * revert * chore: fix lint * test: update snapshot * update storybook * cocoapods * fix app theme color * remove unused color * fix login service button color * remove unused color * unused backgroundColor * fix background color * fix transparent color * fix background color * wip: key * fix color * chore: revert to 1 tick * test: update * chore: use same color as front end * test: update * fix radius * fix background color * fix wrong color * change some colors * chore: update stories * chore: fix button style * chore: fix item color * lint * update snapshot * link * remove background color * change send to channel color * call icons * video conf colors * fix app default color * bottom sheet * remove background * two factor color * update tests * feat: add force-logout stream listener * remove icon colors * improve badge color * update tests * fix header colors * fix collapsible icon color * imagePreview color * wip * update test * lint --------- Co-authored-by: Diego Mello <diegolmello@gmail.com>
2024-04-18 10:19:54 +00:00
<MarkdownPreview msg={element.text} style={[isContext && { color: themes[theme].fontSecondaryInfo }]} numberOfLines={0} />
);
2020-02-11 14:01:35 +00:00
}
feat: mobile color normalization (#5616) * chore: remove auxiliaryText color * chore: remove titleText * chore: password colors change * chore: use fontDefault on ActionSheet item * wip: type * chore: set custom icon default color * remove tintActive color * only set color when checked * remove icon color * remove tintActive * chore: remove STATUS_COLORS * chore: remove mentions colors * chore: remove switch color * chore: background color * chore: auxiliaryBackground * chore: one local colors * wip: some colors * wip: colors * wip: colors * wip: end colors * test: update * chore: fix some colors * chore: fix lint * chore: back to text props * chore: fix ts errors * revert * chore: fix lint * test: update snapshot * update storybook * cocoapods * fix app theme color * remove unused color * fix login service button color * remove unused color * unused backgroundColor * fix background color * fix transparent color * fix background color * wip: key * fix color * chore: revert to 1 tick * test: update * chore: use same color as front end * test: update * fix radius * fix background color * fix wrong color * change some colors * chore: update stories * chore: fix button style * chore: fix item color * lint * update snapshot * link * remove background color * change send to channel color * call icons * video conf colors * fix app default color * bottom sheet * remove background * two factor color * update tests * feat: add force-logout stream listener * remove icon colors * improve badge color * update tests * fix header colors * fix collapsible icon color * imagePreview color * wip * update test * lint --------- Co-authored-by: Diego Mello <diegolmello@gmail.com>
2024-04-18 10:19:54 +00:00
return <Text style={[styles.text, { color: themes[theme].fontDefault }]}>{element.text}</Text>;
}
mrkdwn(element: IMarkdown, context: BlockContext) {
const { theme } = useContext(ThemeContext);
2020-02-11 14:01:35 +00:00
const isContext = context === BlockContext.CONTEXT;
if (isContext) {
return (
feat: mobile color normalization (#5616) * chore: remove auxiliaryText color * chore: remove titleText * chore: password colors change * chore: use fontDefault on ActionSheet item * wip: type * chore: set custom icon default color * remove tintActive color * only set color when checked * remove icon color * remove tintActive * chore: remove STATUS_COLORS * chore: remove mentions colors * chore: remove switch color * chore: background color * chore: auxiliaryBackground * chore: one local colors * wip: some colors * wip: colors * wip: colors * wip: end colors * test: update * chore: fix some colors * chore: fix lint * chore: back to text props * chore: fix ts errors * revert * chore: fix lint * test: update snapshot * update storybook * cocoapods * fix app theme color * remove unused color * fix login service button color * remove unused color * unused backgroundColor * fix background color * fix transparent color * fix background color * wip: key * fix color * chore: revert to 1 tick * test: update * chore: use same color as front end * test: update * fix radius * fix background color * fix wrong color * change some colors * chore: update stories * chore: fix button style * chore: fix item color * lint * update snapshot * link * remove background color * change send to channel color * call icons * video conf colors * fix app default color * bottom sheet * remove background * two factor color * update tests * feat: add force-logout stream listener * remove icon colors * improve badge color * update tests * fix header colors * fix collapsible icon color * imagePreview color * wip * update test * lint --------- Co-authored-by: Diego Mello <diegolmello@gmail.com>
2024-04-18 10:19:54 +00:00
<MarkdownPreview msg={element.text} style={[isContext && { color: themes[theme].fontSecondaryInfo }]} numberOfLines={0} />
);
}
feat: mobile color normalization (#5616) * chore: remove auxiliaryText color * chore: remove titleText * chore: password colors change * chore: use fontDefault on ActionSheet item * wip: type * chore: set custom icon default color * remove tintActive color * only set color when checked * remove icon color * remove tintActive * chore: remove STATUS_COLORS * chore: remove mentions colors * chore: remove switch color * chore: background color * chore: auxiliaryBackground * chore: one local colors * wip: some colors * wip: colors * wip: colors * wip: end colors * test: update * chore: fix some colors * chore: fix lint * chore: back to text props * chore: fix ts errors * revert * chore: fix lint * test: update snapshot * update storybook * cocoapods * fix app theme color * remove unused color * fix login service button color * remove unused color * unused backgroundColor * fix background color * fix transparent color * fix background color * wip: key * fix color * chore: revert to 1 tick * test: update * chore: use same color as front end * test: update * fix radius * fix background color * fix wrong color * change some colors * chore: update stories * chore: fix button style * chore: fix item color * lint * update snapshot * link * remove background color * change send to channel color * call icons * video conf colors * fix app default color * bottom sheet * remove background * two factor color * update tests * feat: add force-logout stream listener * remove icon colors * improve badge color * update tests * fix header colors * fix collapsible icon color * imagePreview color * wip * update test * lint --------- Co-authored-by: Diego Mello <diegolmello@gmail.com>
2024-04-18 10:19:54 +00:00
return <Markdown msg={element.text} theme={theme} style={[isContext && { color: themes[theme].fontSecondaryInfo }]} />;
2020-02-11 14:01:35 +00:00
}
button(element: IButton, context: BlockContext) {
const { text, value, actionId, style } = element;
const [{ loading }, action] = useBlockContext(element, context);
2020-02-11 14:01:35 +00:00
return (
<Button
key={actionId}
type={style}
title={textParser([text])}
2020-02-11 14:01:35 +00:00
loading={loading}
onPress={() => action({ value })}
style={styles.button}
/>
);
}
divider() {
return <Divider />;
2020-02-11 14:01:35 +00:00
}
section(args: ISection) {
return <Section {...args} parser={this.current} />;
2020-02-11 14:01:35 +00:00
}
actions(args: IActions) {
return <Actions {...args} parser={this.current} />;
2020-02-11 14:01:35 +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
}
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}
/>
);
}
image(element: IElement, context: BlockContext) {
return <Image element={element} context={context} />;
2020-02-11 14:01:35 +00:00
}
context(args: any) {
2020-02-11 14:01:35 +00:00
const { theme } = useContext(ThemeContext);
return <Context {...args} theme={theme} parser={this} />;
}
multiStaticSelect(element: IElement, context: BlockContext) {
const [{ loading, value }, action] = useBlockContext(element, context);
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
}
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
}
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
}
video_conf(element: IElement & { callId: string }) {
return <VideoConferenceBlock callId={element.callId} blockId={element.blockId!} />;
}
2020-02-11 14:01:35 +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();
Object.getOwnPropertyNames(MessageParser.prototype).forEach(method => {
// @ts-ignore
2020-02-11 14:01:35 +00:00
ModalParser.prototype[method] = ModalParser.prototype[method] || MessageParser.prototype[method];
});
}
get current() {
return this as unknown as IParser;
}
input({ element, blockId, appId, label, description, hint }: IInputIndex, context: number) {
const [{ error }] = useBlockContext({ ...element, appId, blockId }, context);
const { theme } = useContext(ThemeContext);
2020-02-11 14:01:35 +00:00
return (
<Input
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}
/>
);
}
image(element: IElement, context: BlockContext) {
return <Image element={element} context={context} />;
2020-02-11 14:01:35 +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 (
<FormTextInput
key={actionId}
2020-02-11 14:01:35 +00:00
placeholder={plainText(placeholder)}
multiline={multiline}
loading={loading}
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();
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
export const UiKitComponent = ({ render, blocks }: any) => render(blocks);