Skip to content

Commit d013ec1

Browse files
authored
[MM-402] Remove react-markdown package and use functions exposed by mattermost webapp (#491)
* [MM-402] Remove react-markdown package and use functions exposed by mattermost webapp * [MM-402] Update activityFunc function type * [MM-402] Fix type error * [MM-402] Remove as to assign type and add if condition
1 parent 191a11a commit d013ec1

File tree

7 files changed

+67
-657
lines changed

7 files changed

+67
-657
lines changed

webapp/package-lock.json

Lines changed: 12 additions & 642 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"react-bootstrap": "1.3.0",
2727
"react-custom-scrollbars": "4.2.1",
2828
"react-dom": "16.13.1",
29-
"react-markdown": "^4.3.1",
3029
"react-redux": "7.2.1",
3130
"redux": "4.0.5",
3231
"reselect": "4.1.6"

webapp/src/components/link_tooltip/index.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, {useEffect, useState} from 'react';
2-
import ReactMarkdown from 'react-markdown';
32
import {useDispatch, useSelector} from 'react-redux';
43

54
import {logError} from 'mattermost-redux/actions/errors';
@@ -122,6 +121,7 @@ const LinkTooltip = ({href, show}: Props) => {
122121
};
123122

124123
const date = new Date(data.created_at).toDateString();
124+
const {formatText, messageHtmlToComponent} = window.PostUtils;
125125

126126
return (
127127
<div className='gitlab-tooltip'>
@@ -148,11 +148,7 @@ const LinkTooltip = ({href, show}: Props) => {
148148
<span className='mr-number'>{`#${data.iid}`}</span>
149149
</a>
150150
<div className='markdown-text mt-1 mb-1'>
151-
<ReactMarkdown
152-
source={data.description}
153-
disallowedTypes={['heading']}
154-
linkTarget='_blank'
155-
/>
151+
{messageHtmlToComponent(formatText(data.description), false)}
156152
</div>
157153

158154
{data.type === LINK_TYPES.MERGE_REQUESTS && (

webapp/src/hooks/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import {isDesktopApp} from 'src/utils/user_agent';
77
import {connectUsingBrowserMessage} from 'src/constants';
88

99
import {sendEphemeralPost} from '../actions';
10-
11-
type ContextArgs = {channel_id: string};
10+
import {ContextArgs} from '../types/mattermost-webapp';
1211

1312
const connectCommand = '/gitlab connect';
1413

webapp/src/index.js renamed to webapp/src/index.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
import {getConfig} from 'mattermost-redux/selectors/entities/general';
55

6+
import {GlobalState} from 'mattermost-redux/types/store';
7+
8+
import {Store, Action} from 'redux';
9+
610
import SidebarHeader from './components/sidebar_header';
711
import TeamSidebar from './components/team_sidebar';
812
import RHSSidebar from './components/rhs_sidebar';
@@ -24,19 +28,21 @@ import Client from './client';
2428
import {getPluginServerRoute} from './selectors';
2529
import Hooks from './hooks';
2630

27-
let activityFunc;
31+
import {FormatTextOptions, MessageHtmlToComponentOptions, PluginRegistry} from './types/mattermost-webapp';
32+
33+
let activityFunc: (() => void) | undefined;
2834
let lastActivityTime = Number.MAX_SAFE_INTEGER;
2935
const activityTimeout = 60 * 60 * 1000; // 1 hour
3036
const {id} = manifest;
3137

3238
class PluginClass {
33-
async initialize(registry, store) {
39+
async initialize(registry: PluginRegistry, store: Store<GlobalState, Action<any>>) {
3440
registry.registerReducer(Reducer);
3541

3642
// This needs to be called before any API calls below
3743
Client.setServerRoute(getPluginServerRoute(store.getState()));
3844

39-
await getConnected(true)(store.dispatch, store.getState);
45+
await getConnected(true)(store.dispatch);
4046

4147
registry.registerLeftSidebarHeaderComponent(SidebarHeader);
4248
registry.registerBottomTeamSidebarComponent(TeamSidebar);
@@ -91,8 +97,20 @@ class PluginClass {
9197
}
9298

9399
deinitialize() {
94-
document.removeEventListener('click', activityFunc);
100+
if (activityFunc) {
101+
document.removeEventListener('click', activityFunc);
102+
}
103+
}
104+
}
105+
106+
declare global {
107+
interface Window {
108+
registerPlugin(pluginId: string, plugin: PluginClass): void
109+
PostUtils: {
110+
formatText(text: string, options?: FormatTextOptions): string,
111+
messageHtmlToComponent(html: string, isRHS: boolean, option?: MessageHtmlToComponentOptions): React.ReactNode,
112+
}
95113
}
96114
}
97115

98-
global.window.registerPlugin(id, new PluginClass());
116+
window.registerPlugin(manifest.id, new PluginClass());
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
3+
export type ContextArgs = {channel_id: string};
4+
5+
type FormatTextOptions = {
6+
atMentions?: boolean;
7+
markdown?: boolean;
8+
}
9+
10+
type MessageHtmlToComponentOptions = {
11+
mentionHighlight: boolean;
12+
}
13+
14+
export interface PluginRegistry {
15+
registerReducer(reducer)
16+
registerPostTypeComponent(typeName: string, component: React.ElementType)
17+
registerRightHandSidebarComponent(component: React.ReactNode, title: string | JSX.Element)
18+
registerSlashCommandWillBePostedHook(hook: (rawMessage: string, contextArgs: ContextArgs) => Promise<{}>)
19+
registerWebSocketEventHandler(event: string, handler: (msg: any) => void)
20+
registerAppBarComponent(iconUrl: string, action: () => void, tooltipText: string)
21+
registerLeftSidebarHeaderComponent(component: React.ReactNode)
22+
registerBottomTeamSidebarComponent(component: React.ReactNode)
23+
registerPopoverUserAttributesComponent(component: React.ReactNode)
24+
registerLinkTooltipComponent(component: React.ReactNode)
25+
registerReconnectHandler(handler: any)
26+
27+
// Add more if needed from https://developers.mattermost.com/extend/plugins/webapp/reference
28+
}

webapp/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var path = require('path');
22

33
module.exports = {
44
entry: [
5-
'./src/index.js',
5+
'./src/index.ts',
66
],
77
resolve: {
88
modules: [

0 commit comments

Comments
 (0)