Skip to content

Commit 2ad7409

Browse files
committed
Use extra property to determine the content type
1 parent 69f1d72 commit 2ad7409

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

ui/src/message/Message.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Container from '../common/Container';
88
import * as config from '../config';
99
import {StyleRulesCallback} from '@material-ui/core/styles/withStyles';
1010
import ReactMarkdown from 'react-markdown';
11+
import {RenderMode, contentType} from './extras';
1112

1213
const styles: StyleRulesCallback = () => ({
1314
header: {
@@ -53,6 +54,7 @@ interface IProps {
5354
date: string;
5455
content: string;
5556
fDelete: VoidFunction;
57+
extras?: IMessageExtras;
5658
height: (height: number) => void;
5759
}
5860

@@ -62,8 +64,20 @@ class Message extends React.PureComponent<IProps & WithStyles<typeof styles>> {
6264
public componentDidMount = () =>
6365
this.props.height(this.node ? this.node.getBoundingClientRect().height : 0);
6466

67+
private renderContent = () => {
68+
const content = this.props.content;
69+
switch (contentType(this.props.extras)) {
70+
case RenderMode.Markdown:
71+
return <ReactMarkdown source={content} escapeHtml={true} />;
72+
case RenderMode.Plain:
73+
default:
74+
return content;
75+
}
76+
};
77+
6578
public render(): React.ReactNode {
66-
const {fDelete, classes, title, date, content, image} = this.props;
79+
const {fDelete, classes, title, date, image} = this.props;
80+
6781
return (
6882
<div className={`${classes.wrapperPadding} message`} ref={(ref) => (this.node = ref)}>
6983
<Container style={{display: 'flex'}}>
@@ -93,7 +107,7 @@ class Message extends React.PureComponent<IProps & WithStyles<typeof styles>> {
93107
</IconButton>
94108
</div>
95109
<Typography component="div" className={`${classes.content} content`}>
96-
<ReactMarkdown source={content} escapeHtml={true} />
110+
{this.renderContent()}
97111
</Typography>
98112
</div>
99113
</Container>

ui/src/message/Messages.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class Messages extends Component<IProps & Stores<'messagesStore' | 'appStore'>,
116116
date={message.date}
117117
content={message.message}
118118
image={message.image}
119+
extras={message.extras}
119120
/>
120121
);
121122
};

ui/src/message/extras.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export enum RenderMode {
2+
Markdown = 'text/markdown',
3+
Plain = 'text/plain',
4+
}
5+
6+
export const contentType = (extras?: IMessageExtras): RenderMode => {
7+
const type = extract(extras, 'client::display', 'contentType');
8+
const valid = Object.keys(RenderMode)
9+
.map((k) => RenderMode[k])
10+
.some((mode) => mode === type);
11+
return valid ? type : RenderMode.Plain;
12+
};
13+
14+
// tslint:disable-next-line:no-any
15+
const extract = (extras: IMessageExtras | undefined, key: string, path: string): any => {
16+
if (!extras) {
17+
return null;
18+
}
19+
20+
if (!extras[key]) {
21+
return null;
22+
}
23+
24+
if (!extras[key][path]) {
25+
return null;
26+
}
27+
28+
return extras[key][path];
29+
};

0 commit comments

Comments
 (0)