Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Createpost to v10 version #225

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions webapp/src/components/llmbot_post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {doPostbackSummary, doRegenerate, doStopGenerating} from '@/client';

import {useSelectNotAIPost, useSelectPost} from '@/hooks';

import {PostMessagePreview} from '@/mm_webapp';

import PostText from './post_text';
import IconRegenerate from './assets/icon_regenerate';
import IconCancel from './assets/icon_cancel';

const PostMessagePreview = (window as any).Components.PostMessagePreview;

const FixPostHover = createGlobalStyle<{disableHover?: string}>`
${(props) => props.disableHover && css`
&&&& {
Expand Down
4 changes: 3 additions & 1 deletion webapp/src/components/rhs/rhs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import {useBotlist} from '@/bots';

import RHSImage from '../assets/rhs_image';

import {ThreadViewer as UnstyledThreadViewer} from '@/mm_webapp';

import ThreadItem from './thread_item';
import RHSHeader from './rhs_header';
import RHSNewTab from './rhs_new_tab';
import {RHSPaddingContainer, RHSText, RHSTitle} from './common';

const ThreadViewer = (window as any).Components.ThreadViewer && styled((window as any).Components.ThreadViewer)`
const ThreadViewer = UnstyledThreadViewer && styled(UnstyledThreadViewer)`
height: 100%;
`;

Expand Down
109 changes: 69 additions & 40 deletions webapp/src/components/rhs/rhs_new_tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import RHSImage from '../assets/rhs_image';

import {createPost} from '@/client';

import {Button, RHSPaddingContainer, RHSText, RHSTitle} from './common';
import {AdvancedTextEditor, CreatePost} from '@/mm_webapp';

const CreatePost = (window as any).Components.CreatePost;
import {Button, RHSPaddingContainer, RHSText, RHSTitle} from './common';

const CreatePostContainer = styled.div`
.custom-textarea {
Expand Down Expand Up @@ -74,9 +74,14 @@ const setEditorText = (text: string) => {
};

const RHSNewTab = ({botChannelId, selectPost, setCurrentTab}: Props) => {
const dispatch = useDispatch();
const intl = useIntl();

// Compatibility with pre v10 create post export
const dispatch = useDispatch();

// Compatibility with pre v10 create post export
const [draft, updateDraft] = useState<any>(null);

const addBrainstormingIdeas = useCallback(() => {
setEditorText(intl.formatMessage({defaultMessage: 'Brainstorm ideas about '}));
}, []);
Expand All @@ -92,6 +97,66 @@ const RHSNewTab = ({botChannelId, selectPost, setCurrentTab}: Props) => {
const addProsAndCons = useCallback(() => {
setEditorText(intl.formatMessage({defaultMessage: 'Write a pros and cons list about '}));
}, []);

// Compatibility with pre v10 create post export
let editorComponent;
if (AdvancedTextEditor) {
editorComponent = (
<AdvancedTextEditor
data-testid='rhs-new-tab-create-post'
channelId={botChannelId}
placeholder={intl.formatMessage({defaultMessage: 'Ask Copilot anything...'})}
isThreadView={true}
afterSubmit={(result: {created?: {id: string}}) => {
if (result.created?.id) {
hmhealey marked this conversation as resolved.
Show resolved Hide resolved
selectPost(result.created?.id);
setCurrentTab('thread');
}
}}
/>
);
} else {
editorComponent = (
<CreatePost
data-testid='rhs-new-tab-create-post'
channelId={botChannelId}
placeholder={intl.formatMessage({defaultMessage: 'Ask Copilot anything...'})}
rootId={'ai_copilot'}
onSubmit={async (p: any) => {
const post = {...p};
post.channel_id = botChannelId || '';
post.props = {};
post.uploadsInProgress = [];
post.file_ids = p.fileInfos.map((f: any) => f.id);
const created = await createPost(post);
selectPost(created.id);
setCurrentTab('thread');
dispatch({
type: 'SET_GLOBAL_ITEM',
data: {
name: 'comment_draft_ai_copilot',
value: {message: '', fileInfos: [], uploadsInProgress: []},
},
});
}}
draft={draft}
onUpdateCommentDraft={(newDraft: any) => {
updateDraft(newDraft);
const timestamp = new Date().getTime();
newDraft.updateAt = timestamp;
newDraft.createAt = newDraft.createAt || timestamp;
dispatch({
type: 'SET_GLOBAL_ITEM',
data: {
name: 'comment_draft_ai_copilot',
value: newDraft,
},
});
}}
/>
);
}

return (
<RHSPaddingContainer>
<RHSImage/>
Expand All @@ -116,43 +181,7 @@ const RHSNewTab = ({botChannelId, selectPost, setCurrentTab}: Props) => {
</OptionButton>
</QuestionOptions>
<CreatePostContainer>
<CreatePost
data-testid='rhs-new-tab-create-post'
channelId={botChannelId}
placeholder={intl.formatMessage({defaultMessage: 'Ask Copilot anything...'})}
rootId={'ai_copilot'}
onSubmit={async (p: any) => {
const post = {...p};
post.channel_id = botChannelId || '';
post.props = {};
post.uploadsInProgress = [];
post.file_ids = p.fileInfos.map((f: any) => f.id);
const created = await createPost(post);
selectPost(created.id);
setCurrentTab('thread');
dispatch({
type: 'SET_GLOBAL_ITEM',
data: {
name: 'comment_draft_ai_copilot',
value: {message: '', fileInfos: [], uploadsInProgress: []},
},
});
}}
draft={draft}
onUpdateCommentDraft={(newDraft: any) => {
updateDraft(newDraft);
const timestamp = new Date().getTime();
newDraft.updateAt = timestamp;
newDraft.createAt = newDraft.createAt || timestamp;
dispatch({
type: 'SET_GLOBAL_ITEM',
data: {
name: 'comment_draft_ai_copilot',
value: newDraft,
},
});
}}
/>
{editorComponent}
</CreatePostContainer>
</RHSPaddingContainer>
);
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/components/rhs/thread_item.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import styled from 'styled-components';

import {Timestamp} from '@/mm_webapp';

import {GrayPill} from '../pill';

const ThreadItemContainer = styled.div`
Expand All @@ -9,8 +11,6 @@ const ThreadItemContainer = styled.div`
border-bottom: 1px solid rgba(var(--center-channel-color-rgb), 0.12)
`;

const Timestamp = (window as any).Components.Timestamp;

const Title = styled.div`
color: var(--center-channel-color);
display: flex;
Expand Down
6 changes: 4 additions & 2 deletions webapp/src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {selectPost, openRHS, selectRegularPost} from 'src/redux_actions';

import {viewMyChannel} from 'src/client';

import {isRHSCompatable} from './mm_webapp';

const selectPostLegacy = (postid: string, channelid: string) => {
return {
type: 'SELECT_POST',
Expand All @@ -14,8 +16,8 @@ const selectPostLegacy = (postid: string, channelid: string) => {
};

export const doSelectPost = (postId: string, channelId: string, dispatch: any) => {
// This if is for legacy mode where the AdvancedCreatecomment is not exported
if ((window as any).Components.CreatePost) {
// This if is for legacy mode where the neither createpost or advancedtexteditor is loaded
if (isRHSCompatable()) {
dispatch(selectPost(postId));
dispatch(openRHS());
} else {
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import PostEventListener from './websocket';
import {BotsHandler, setupRedux} from './redux';
import UnreadsSummarize from './components/unreads_summarize';
import {PostbackPost} from './components/postback_post';
import {isRHSCompatable} from './mm_webapp';

type WebappStore = Store<GlobalState, Action<Record<string, unknown>>>

Expand Down Expand Up @@ -81,7 +82,7 @@ export default class Plugin {
});

let rhs: any = null;
if ((window as any).Components.CreatePost) {
if (isRHSCompatable()) {
rhs = registry.registerRightHandSidebarComponent(RHS, RHSTitle);
setOpenRHSAction(rhs.showRHSPlugin);
}
Expand Down
15 changes: 15 additions & 0 deletions webapp/src/mm_webapp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

export const AdvancedTextEditor = (window as any).Components.AdvancedTextEditor;

// Compatibility with pre v10 create post export
export const CreatePost = (window as any).Components.CreatePost;

export function isRHSCompatable(): boolean {
return AdvancedTextEditor || CreatePost;
}

export const PostMessagePreview = (window as any).Components.PostMessagePreview;

export const Timestamp = (window as any).Components.Timestamp;

export const ThreadViewer = (window as any).Components.ThreadViewer;