Skip to content

Commit

Permalink
[MM-402] Remove react-markdown package and use functions exposed by m…
Browse files Browse the repository at this point in the history
…attermost 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
  • Loading branch information
ayusht2810 authored May 13, 2024
1 parent 191a11a commit d013ec1
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 657 deletions.
654 changes: 12 additions & 642 deletions webapp/package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"react-bootstrap": "1.3.0",
"react-custom-scrollbars": "4.2.1",
"react-dom": "16.13.1",
"react-markdown": "^4.3.1",
"react-redux": "7.2.1",
"redux": "4.0.5",
"reselect": "4.1.6"
Expand Down
8 changes: 2 additions & 6 deletions webapp/src/components/link_tooltip/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, {useEffect, useState} from 'react';
import ReactMarkdown from 'react-markdown';
import {useDispatch, useSelector} from 'react-redux';

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

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

return (
<div className='gitlab-tooltip'>
Expand All @@ -148,11 +148,7 @@ const LinkTooltip = ({href, show}: Props) => {
<span className='mr-number'>{`#${data.iid}`}</span>
</a>
<div className='markdown-text mt-1 mb-1'>
<ReactMarkdown
source={data.description}
disallowedTypes={['heading']}
linkTarget='_blank'
/>
{messageHtmlToComponent(formatText(data.description), false)}
</div>

{data.type === LINK_TYPES.MERGE_REQUESTS && (
Expand Down
3 changes: 1 addition & 2 deletions webapp/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {isDesktopApp} from 'src/utils/user_agent';
import {connectUsingBrowserMessage} from 'src/constants';

import {sendEphemeralPost} from '../actions';

type ContextArgs = {channel_id: string};
import {ContextArgs} from '../types/mattermost-webapp';

const connectCommand = '/gitlab connect';

Expand Down
28 changes: 23 additions & 5 deletions webapp/src/index.js → webapp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

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

import {GlobalState} from 'mattermost-redux/types/store';

import {Store, Action} from 'redux';

import SidebarHeader from './components/sidebar_header';
import TeamSidebar from './components/team_sidebar';
import RHSSidebar from './components/rhs_sidebar';
Expand All @@ -24,19 +28,21 @@ import Client from './client';
import {getPluginServerRoute} from './selectors';
import Hooks from './hooks';

let activityFunc;
import {FormatTextOptions, MessageHtmlToComponentOptions, PluginRegistry} from './types/mattermost-webapp';

let activityFunc: (() => void) | undefined;
let lastActivityTime = Number.MAX_SAFE_INTEGER;
const activityTimeout = 60 * 60 * 1000; // 1 hour
const {id} = manifest;

class PluginClass {
async initialize(registry, store) {
async initialize(registry: PluginRegistry, store: Store<GlobalState, Action<any>>) {
registry.registerReducer(Reducer);

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

await getConnected(true)(store.dispatch, store.getState);
await getConnected(true)(store.dispatch);

registry.registerLeftSidebarHeaderComponent(SidebarHeader);
registry.registerBottomTeamSidebarComponent(TeamSidebar);
Expand Down Expand Up @@ -91,8 +97,20 @@ class PluginClass {
}

deinitialize() {
document.removeEventListener('click', activityFunc);
if (activityFunc) {
document.removeEventListener('click', activityFunc);
}
}
}

declare global {
interface Window {
registerPlugin(pluginId: string, plugin: PluginClass): void
PostUtils: {
formatText(text: string, options?: FormatTextOptions): string,
messageHtmlToComponent(html: string, isRHS: boolean, option?: MessageHtmlToComponentOptions): React.ReactNode,
}
}
}

global.window.registerPlugin(id, new PluginClass());
window.registerPlugin(manifest.id, new PluginClass());
28 changes: 28 additions & 0 deletions webapp/src/types/mattermost-webapp/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';

export type ContextArgs = {channel_id: string};

type FormatTextOptions = {
atMentions?: boolean;
markdown?: boolean;
}

type MessageHtmlToComponentOptions = {
mentionHighlight: boolean;
}

export interface PluginRegistry {
registerReducer(reducer)
registerPostTypeComponent(typeName: string, component: React.ElementType)
registerRightHandSidebarComponent(component: React.ReactNode, title: string | JSX.Element)
registerSlashCommandWillBePostedHook(hook: (rawMessage: string, contextArgs: ContextArgs) => Promise<{}>)
registerWebSocketEventHandler(event: string, handler: (msg: any) => void)
registerAppBarComponent(iconUrl: string, action: () => void, tooltipText: string)
registerLeftSidebarHeaderComponent(component: React.ReactNode)
registerBottomTeamSidebarComponent(component: React.ReactNode)
registerPopoverUserAttributesComponent(component: React.ReactNode)
registerLinkTooltipComponent(component: React.ReactNode)
registerReconnectHandler(handler: any)

// Add more if needed from https://developers.mattermost.com/extend/plugins/webapp/reference
}
2 changes: 1 addition & 1 deletion webapp/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var path = require('path');

module.exports = {
entry: [
'./src/index.js',
'./src/index.ts',
],
resolve: {
modules: [
Expand Down

0 comments on commit d013ec1

Please sign in to comment.