forked from mattermost/mattermost-plugin-msteams
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MI-3831]: Link channels modal (#40)
* [MI-3831]: Link channels modal * [MI-3831]: Fix lint errors * [MI-3831]: Update naming * [MI-3833]: Link channel modal integration (#43) * [MI-3833]: Link Channel Modal integration * [MI-3833]: Add constants * [MI-3833]: Update constants * [MI-3837]: Integrate webhooks and enhancements (#47) * [MI-3837]: Integrate webhooks and enhancements * [MI-3837]: Remove yarn lock * [MI-3834] Add unlink button on hover state on linked channel list (#45) * [MI-3834] Add unlink button on hover state on linked channel list * [MI-3834] Update style and props of unlink button * [MI-3836] Integrate API to unlink channels and websocket event (#46) * [MI-3836] Integrate API to unlink channels and websocket event * [MI-3836] Fix issue of alert not visible * [MI-3836] Update dialog component * [MI-3836] Minor review fixes and revert some changes --------- Co-authored-by: Ayush Thakur <[email protected]> * [MI-3831]: Minor refactoring * [MI-3831]: Update UI-Lib version --------- Co-authored-by: Ayush Thakur <[email protected]>
- Loading branch information
1 parent
7b03ae6
commit 7d082b6
Showing
41 changed files
with
951 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,16 @@ | ||
import React from 'react'; | ||
import {useDispatch} from 'react-redux'; | ||
|
||
import {Dialog as MMDialog, LinearProgress, DialogProps} from '@brightscout/mattermost-ui-library'; | ||
|
||
import usePluginApi from 'hooks/usePluginApi'; | ||
import {getDialogState} from 'selectors'; | ||
import {closeDialog} from 'reducers/dialog'; | ||
|
||
export const Dialog = ({onCloseHandler, onSubmitHandler}: Pick<DialogProps, 'onCloseHandler' | 'onSubmitHandler'>) => { | ||
const dispatch = useDispatch(); | ||
const {state} = usePluginApi(); | ||
const {show, title, description, destructive, primaryButtonText, secondaryButtonText, isLoading} = getDialogState(state); | ||
|
||
const handleClose = () => dispatch(closeDialog()); | ||
|
||
return ( | ||
<MMDialog | ||
description={description} | ||
destructive={destructive} | ||
show={show} | ||
primaryButtonText={primaryButtonText} | ||
secondaryButtonText={secondaryButtonText} | ||
onCloseHandler={() => { | ||
handleClose(); | ||
onCloseHandler(); | ||
}} | ||
onSubmitHandler={onSubmitHandler} | ||
className='disconnect-dialog' | ||
title={title} | ||
> | ||
{isLoading && <LinearProgress/>} | ||
</MMDialog> | ||
); | ||
}; | ||
export const Dialog = ({ | ||
children, | ||
isLoading = false, | ||
...rest | ||
}: DialogProps & {isLoading?: boolean, children: React.ReactNode}) => ( | ||
<MMDialog | ||
{...rest} | ||
> | ||
{isLoading && <LinearProgress className='absolute w-full left-0 top-62'/>} | ||
{children} | ||
</MMDialog> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
webapp/src/components/LinkChannelModal/LinkChannelModal.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import React, {useState} from 'react'; | ||
|
||
import {LinearProgress, Modal} from '@brightscout/mattermost-ui-library'; | ||
|
||
import {useDispatch} from 'react-redux'; | ||
|
||
import usePluginApi from 'hooks/usePluginApi'; | ||
|
||
import {getCurrentTeam, getLinkModalState} from 'selectors'; | ||
|
||
import {Dialog} from 'components/Dialog'; | ||
import {pluginApiServiceConfigs} from 'constants/apiService.constant'; | ||
import useApiRequestCompletionState from 'hooks/useApiRequestCompletionState'; | ||
import {hideLinkModal, preserveState, resetState, setLinkModalLoading, showLinkModal} from 'reducers/linkModal'; | ||
import useAlert from 'hooks/useAlert'; | ||
|
||
import {refetch} from 'reducers/refetchState'; | ||
|
||
import {SearchMSChannels} from './SearchMSChannels'; | ||
import {SearchMSTeams} from './SearchMSTeams'; | ||
import {SearchMMChannels} from './SearchMMChannels'; | ||
|
||
export const LinkChannelModal = () => { | ||
const dispatch = useDispatch(); | ||
const showAlert = useAlert(); | ||
const {state, makeApiRequestWithCompletionStatus} = usePluginApi(); | ||
const {show = false, isLoading} = getLinkModalState(state); | ||
const {currentTeamId} = getCurrentTeam(state); | ||
|
||
// Show retry dialog component | ||
const [showRetryDialog, setShowRetryDialog] = useState(false); | ||
|
||
const [mmChannel, setMMChannel] = useState<MMTeamOrChannel | null>(null); | ||
const [msTeam, setMSTeam] = useState<MSTeamOrChannel | null>(null); | ||
const [msChannel, setMSChannel] = useState<MSTeamOrChannel | null>(null); | ||
const [linkChannelsPayload, setLinkChannelsPayload] = useState<LinkChannelsPayload | null>(null); | ||
|
||
const handleModalClose = (preserve?: boolean) => { | ||
if (!preserve) { | ||
setMMChannel(null); | ||
setMSTeam(null); | ||
setMSChannel(null); | ||
} | ||
dispatch(resetState()); | ||
dispatch(hideLinkModal()); | ||
}; | ||
|
||
const handleChannelLinking = () => { | ||
const payload: LinkChannelsPayload = { | ||
mattermostTeamID: currentTeamId || '', | ||
mattermostChannelID: mmChannel?.id || '', | ||
msTeamsTeamID: msTeam?.ID || '', | ||
msTeamsChannelID: msChannel?.ID || '', | ||
}; | ||
setLinkChannelsPayload(payload); | ||
makeApiRequestWithCompletionStatus(pluginApiServiceConfigs.linkChannels.apiServiceName, payload); | ||
dispatch(setLinkModalLoading(true)); | ||
}; | ||
|
||
useApiRequestCompletionState({ | ||
serviceName: pluginApiServiceConfigs.linkChannels.apiServiceName, | ||
payload: linkChannelsPayload as LinkChannelsPayload, | ||
handleSuccess: () => { | ||
dispatch(setLinkModalLoading(false)); | ||
handleModalClose(); | ||
dispatch(refetch()); | ||
showAlert({ | ||
message: 'Successfully linked channels', | ||
severity: 'success', | ||
}); | ||
}, | ||
handleError: () => { | ||
dispatch(setLinkModalLoading(false)); | ||
handleModalClose(true); | ||
setShowRetryDialog(true); | ||
}, | ||
}); | ||
|
||
return ( | ||
<> | ||
<Modal | ||
show={show} | ||
className='msteams-sync-modal' | ||
title='Link a channel' | ||
subtitle='Link a channel in Mattermost with a channel in Microsoft Teams' | ||
primaryActionText='Link Channels' | ||
secondaryActionText='Cancel' | ||
onFooterCloseHandler={handleModalClose} | ||
onHeaderCloseHandler={handleModalClose} | ||
isPrimaryButtonDisabled={!mmChannel || !msChannel || !msTeam} | ||
onSubmitHandler={handleChannelLinking} | ||
backdrop={true} | ||
> | ||
{isLoading && <LinearProgress className='fixed w-full left-0 top-100'/>} | ||
<SearchMMChannels | ||
setChannel={setMMChannel} | ||
teamId={currentTeamId} | ||
/> | ||
<hr className='w-full my-32'/> | ||
<SearchMSTeams setMSTeam={setMSTeam}/> | ||
<SearchMSChannels | ||
setChannel={setMSChannel} | ||
teamId={msTeam?.ID} | ||
/> | ||
</Modal> | ||
<Dialog | ||
show={showRetryDialog} | ||
destructive={true} | ||
primaryButtonText='Try Again' | ||
secondaryButtonText='Cancel' | ||
title='Unable to link channels' | ||
onSubmitHandler={() => { | ||
dispatch(preserveState({ | ||
mmChannel: mmChannel?.displayName ?? '', | ||
msChannel: msChannel?.DisplayName ?? '', | ||
msTeam: msTeam?.DisplayName ?? '', | ||
})); | ||
setShowRetryDialog(false); | ||
dispatch(showLinkModal()); | ||
}} | ||
onCloseHandler={() => { | ||
setShowRetryDialog(false); | ||
dispatch(resetState()); | ||
}} | ||
> | ||
{'We were not able to link the selected channels. Please try again.'} | ||
</Dialog> | ||
</> | ||
); | ||
}; |
112 changes: 112 additions & 0 deletions
112
webapp/src/components/LinkChannelModal/SearchMMChannels/SearchMMChannels.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import React, {useCallback, useEffect, useState} from 'react'; | ||
|
||
import {Client4} from 'mattermost-redux/client'; | ||
import {channels as MMChannelTypes} from 'mattermost-redux/types'; | ||
import {General as MMConstants} from 'mattermost-redux/constants'; | ||
|
||
import {ListItemType, MMSearch} from '@brightscout/mattermost-ui-library'; | ||
|
||
import {useDispatch} from 'react-redux'; | ||
|
||
import utils from 'utils'; | ||
|
||
import {Icon} from 'components/Icon'; | ||
|
||
import {debounceFunctionTimeLimitInMilliseconds} from 'constants/common.constants'; | ||
|
||
import {setLinkModalLoading} from 'reducers/linkModal'; | ||
|
||
import usePluginApi from 'hooks/usePluginApi'; | ||
import {getCurrentTeam, getLinkModalState} from 'selectors'; | ||
|
||
import {SearchMMChannelProps} from './SearchMMChannels.types'; | ||
|
||
export const SearchMMChannels = ({ | ||
setChannel, | ||
teamId, | ||
}: SearchMMChannelProps) => { | ||
const dispatch = useDispatch(); | ||
const {state} = usePluginApi(); | ||
const [searchTerm, setSearchTerm] = useState<string>(''); | ||
const {teams} = getCurrentTeam(state); | ||
const {mmChannel} = getLinkModalState(state); | ||
|
||
const [searchSuggestions, setSearchSuggestions] = useState<ListItemType[]>([]); | ||
const [suggestionsLoading, setSuggestionsLoading] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
if (!mmChannel) { | ||
handleClearInput(); | ||
} | ||
setSearchTerm(mmChannel); | ||
}, [teamId]); | ||
|
||
const searchChannels = ({searchFor}: {searchFor?: string}) => { | ||
if (searchFor) { | ||
setSuggestionsLoading(true); | ||
dispatch(setLinkModalLoading(true)); | ||
Client4.searchAllChannels(searchFor). | ||
then((channels) => { | ||
const suggestions:ListItemType[] = []; | ||
for (const channel of channels as MMChannelTypes.Channel[]) { | ||
suggestions.push({ | ||
label: channel.display_name, | ||
value: channel.id, | ||
secondaryLabel: teams[channel.team_id].display_name, | ||
icon: channel.type === MMConstants.PRIVATE_CHANNEL ? 'Lock' : 'Globe', | ||
}); | ||
} | ||
setSearchSuggestions(suggestions); | ||
}).finally(() => { | ||
setSuggestionsLoading(false); | ||
dispatch(setLinkModalLoading(false)); | ||
}); | ||
} | ||
}; | ||
|
||
const debouncedSearchChannels = useCallback(utils.debounce(searchChannels, debounceFunctionTimeLimitInMilliseconds), [searchChannels]); | ||
|
||
const handleSearch = (val: string) => { | ||
if (!val) { | ||
setSearchSuggestions([]); | ||
setChannel(null); | ||
} | ||
setSearchTerm(val); | ||
debouncedSearchChannels({searchFor: val}); | ||
}; | ||
|
||
const handleChannelSelect = (_: any, option: ListItemType) => { | ||
setChannel({ | ||
id: option.value, | ||
displayName: option.label as string, | ||
}); | ||
setSearchTerm(option.label as string); | ||
}; | ||
|
||
const handleClearInput = () => { | ||
setSearchTerm(''); | ||
setSearchSuggestions([]); | ||
setChannel(null); | ||
}; | ||
|
||
return ( | ||
<div className='d-flex flex-column gap-24'> | ||
<div className='d-flex gap-8 align-items-center'> | ||
<Icon iconName='mattermost'/> | ||
<h5 className='my-0 lh-20 wt-600'>{'Select a Mattermost channel'}</h5> | ||
</div> | ||
<MMSearch | ||
autoFocus={true} | ||
fullWidth={true} | ||
label='Search Mattermost channels' | ||
items={searchSuggestions} | ||
secondaryLabelPosition='inline' | ||
onSelect={handleChannelSelect} | ||
searchValue={searchTerm} | ||
setSearchValue={handleSearch} | ||
onClearInput={handleClearInput} | ||
optionsLoading={suggestionsLoading} | ||
/> | ||
</div> | ||
); | ||
}; |
4 changes: 4 additions & 0 deletions
4
webapp/src/components/LinkChannelModal/SearchMMChannels/SearchMMChannels.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type SearchMMChannelProps = { | ||
setChannel: React.Dispatch<React.SetStateAction<MMTeamOrChannel | null>>; | ||
teamId: string | null, | ||
} |
1 change: 1 addition & 0 deletions
1
webapp/src/components/LinkChannelModal/SearchMMChannels/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {SearchMMChannels} from './SearchMMChannels.component'; |
Oops, something went wrong.