UNPKG

@onereach/channel-transformer

Version:

Convert data model between different channels (Slack, Microsoft Teams, Generic...)

264 lines (209 loc) 7.92 kB
import { IGenericSelect } from '../../../generic/types'; import { ISlackTextObject, ISlackSelectStatic, ISlackSelectUsers, ISlackChannelsSelect, ISlackSelectConversations, ISlackMultiUsersSelect, ISlackMultiConversationSelect, ISlackOverflow, ISlackConfirm, ISlackMultiStaticSelect, ISlackInput } from '../../types'; import { transformTextObject } from './text'; import { convertGenericOptions, convertGenericOptionsGroup, convertGenericSingleOption, convertGenericSingleOptionGroup } from '../helpers/convert-options'; import { getElementWithSectionWrapper } from '../helpers/section-wrapper'; import { transformConfirm } from './confirm'; import { getErrorObject } from '../../../../helpers/get-error-object'; function transformSelect({ props }: IGenericSelect, type: String): ISlackSelectStatic function transformSelect({ props }: IGenericSelect, type: String): ISlackSelectUsers function transformSelect({ props }: IGenericSelect, type: String): ISlackMultiConversationSelect function transformSelect({ props }: IGenericSelect, type: String): ISlackOverflow function transformSelect({ props }: IGenericSelect, type: String): ISlackChannelsSelect function transformSelect({ props }: IGenericSelect, type: String): ISlackSelectConversations function transformSelect({ props }: IGenericSelect, type: String): ISlackMultiUsersSelect function transformSelect({ props }: IGenericSelect, type: String): ISlackMultiStaticSelect function transformSelect({ props }: IGenericSelect, type: String): ISlackInput function transformSelect({ props }: IGenericSelect, type: String) { const slackType = props.slack?.type || 'static_select'; let label: ISlackTextObject; let placeholder: ISlackTextObject; let confirm: ISlackConfirm; if (props.label) { label = transformTextObject({ value: props.label, type: 'plain' }); } if (props.placeholder) { placeholder = transformTextObject({ value: props.placeholder, type: 'plain', slack: { type: props.slack?.placeholder_type, emoji: props.slack?.placeholder_emoji, verbatim: props.slack?.placeholder_verbatim } }); } const action_id = props.slack?.action_id || props.variableName || props.web?.variableName; const interactive = props?.web?.interactive; if (props.slack?.confirm) { confirm = transformConfirm(props.slack?.confirm); } if (slackType === 'static_select') { const res: ISlackSelectStatic = { type: 'static_select', placeholder, action_id }; if (props.options) { res.options = convertGenericOptions(props.options); if (props.slack?.initial_option) { res.initial_option = convertGenericSingleOption(props.slack?.initial_option); } } if (props.slack?.option_groups) { res.option_groups = convertGenericOptionsGroup(props.slack?.option_groups); if (props.slack?.initial_option) { res.initial_option = convertGenericSingleOptionGroup(props.slack?.initial_option); } } if (confirm) res.confirm = confirm; if (interactive || label) { return getElementWithSectionWrapper(res, label, interactive); } return res; } if (slackType === 'multi_static_select') { const res: ISlackMultiStaticSelect = { type: 'multi_static_select', placeholder, action_id }; if (props.options) { res.options = convertGenericOptions(props.options); if (props.value) { res.initial_options = convertGenericSingleOption(props.value); } } if (props.slack?.option_groups) { res.option_groups = convertGenericOptionsGroup(props.slack?.option_groups); if (props.value) { res.initial_options = convertGenericSingleOptionGroup(props.value); } } const max_selected_items = props.slack?.max_selected_items; if (max_selected_items) res.max_selected_items = max_selected_items; if (confirm) res.confirm = confirm; if (interactive || label) { return getElementWithSectionWrapper(res, label, interactive); } return res; } if (slackType === 'users_select') { const res: ISlackSelectUsers = { type: 'users_select', placeholder, action_id }; const initial_user = props.value; if (initial_user) res.initial_user = initial_user; if (confirm) res.confirm = confirm; if (interactive || label) { return getElementWithSectionWrapper(res, label, interactive); } return res; } if (slackType === 'multi_conversations_select') { const res: ISlackMultiConversationSelect = { type: 'multi_conversations_select', placeholder, action_id }; const initial_conversation = props.value; const default_to_current_conversation = props.slack?.default_to_current_conversation; const max_selected_items = props.slack?.max_selected_items; const filter = props.slack?.filter; if (initial_conversation) res.initial_conversation = initial_conversation; if (default_to_current_conversation) res.default_to_current_conversation = default_to_current_conversation; if (confirm) res.confirm = confirm; if (max_selected_items) res.max_selected_items = max_selected_items; if (filter) res.filter = filter; if (interactive || label) { return getElementWithSectionWrapper(res, label, interactive); } return res; } if (slackType === 'overflow') { const res: ISlackOverflow = { type: 'overflow', action_id, options: convertGenericOptions(props.options) }; if (confirm) res.confirm = confirm; if (interactive || label) { return getElementWithSectionWrapper(res, label, interactive); } return res; } if (slackType === 'conversations_select') { const res: ISlackSelectConversations = { type: 'conversations_select', placeholder, action_id }; const initial_conversation = props.value; const default_to_current_conversation = props.slack?.default_to_current_conversation; const response_url_enabled = props.slack?.response_url_enabled; const filter = props.slack?.filter; if (initial_conversation) res.initial_conversation = initial_conversation; if (default_to_current_conversation) res.default_to_current_conversation = default_to_current_conversation; if (confirm) res.confirm = confirm; if (response_url_enabled) res.response_url_enabled = response_url_enabled; if (filter) res.filter = filter; if (interactive || label) { return getElementWithSectionWrapper(res, label, interactive); } return res; } if (slackType === 'channels_select') { const res: ISlackChannelsSelect = { type: 'channels_select', placeholder, action_id }; const initial_channel = props.value; const response_url_enabled = props.slack?.response_url_enabled; if (initial_channel) res.initial_channel = initial_channel; if (response_url_enabled) res.response_url_enabled = response_url_enabled; if (confirm) res.confirm = confirm; if (interactive || label) { return getElementWithSectionWrapper(res, label, interactive); } return res; } if (slackType === 'multi_users_select') { const res: ISlackMultiUsersSelect = { type: 'multi_users_select', placeholder, action_id }; const initial_users = props.value; const max_selected_items = props.slack?.max_selected_items; if (initial_users) res.initial_users = initial_users; if (confirm) res.confirm = confirm; if (max_selected_items) res.max_selected_items = max_selected_items; if (interactive || label) { return getElementWithSectionWrapper(res, label, interactive); } return res; } return getErrorObject({ type: slackType, message: 'transformSelect fromGeneric method' })(); } export { transformSelect };