|
| 1 | +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. |
| 2 | +// See LICENSE.txt for license information. |
| 3 | + |
| 4 | +import React, {useState, useCallback, useEffect} from 'react'; |
| 5 | +import {useIntl, type IntlShape} from 'react-intl'; |
| 6 | +import {Keyboard, ScrollView} from 'react-native'; |
| 7 | +import {SafeAreaView} from 'react-native-safe-area-context'; |
| 8 | + |
| 9 | +import CompassIcon from '@components/compass_icon'; |
| 10 | +import FloatingTextInput from '@components/floating_input/floating_text_input_label'; |
| 11 | +import {useTheme} from '@context/theme'; |
| 12 | +import useAndroidHardwareBackHandler from '@hooks/android_back_handler'; |
| 13 | +import useNavButtonPressed from '@hooks/navigation_button_pressed'; |
| 14 | +import {usePreventDoubleTap} from '@hooks/utils'; |
| 15 | +import {createPlaybookRun, fetchPlaybookRunsForChannel} from '@playbooks/actions/remote/runs'; |
| 16 | +import {goToPlaybookRun} from '@playbooks/screens/navigation'; |
| 17 | +import {popTopScreen, setButtons} from '@screens/navigation'; |
| 18 | +import {getFullErrorMessage} from '@utils/errors'; |
| 19 | +import {logError} from '@utils/log'; |
| 20 | +import {showPlaybookErrorSnackbar} from '@utils/snack_bar'; |
| 21 | +import {makeStyleSheetFromTheme} from '@utils/theme'; |
| 22 | + |
| 23 | +import type {AvailableScreens} from '@typings/screens/navigation'; |
| 24 | +import type {OptionsTopBarButton} from 'react-native-navigation'; |
| 25 | + |
| 26 | +type Props = { |
| 27 | + componentId: AvailableScreens; |
| 28 | + channelId: string; |
| 29 | + channelName: string; |
| 30 | + currentUserId: string; |
| 31 | + currentTeamId: string; |
| 32 | + serverUrl: string; |
| 33 | +} |
| 34 | + |
| 35 | +const getStyleSheet = makeStyleSheetFromTheme((theme: Theme) => { |
| 36 | + return { |
| 37 | + container: { |
| 38 | + flex: 1, |
| 39 | + backgroundColor: theme.centerChannelBg, |
| 40 | + }, |
| 41 | + content: { |
| 42 | + flex: 1, |
| 43 | + paddingHorizontal: 20, |
| 44 | + paddingVertical: 24, |
| 45 | + }, |
| 46 | + contentContainer: { |
| 47 | + gap: 16, |
| 48 | + }, |
| 49 | + }; |
| 50 | +}); |
| 51 | + |
| 52 | +const CLOSE_BUTTON_ID = 'close-create-quick-checklist'; |
| 53 | +const CREATE_BUTTON_ID = 'create-quick-checklist'; |
| 54 | + |
| 55 | +async function makeLeftButton(theme: Theme): Promise<OptionsTopBarButton> { |
| 56 | + return { |
| 57 | + id: CLOSE_BUTTON_ID, |
| 58 | + icon: await CompassIcon.getImageSource('close', 24, theme.sidebarHeaderTextColor), |
| 59 | + testID: 'create_quick_checklist.close.button', |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +function makeRightButton(theme: Theme, intl: IntlShape, enabled: boolean): OptionsTopBarButton { |
| 64 | + return { |
| 65 | + color: theme.sidebarHeaderTextColor, |
| 66 | + id: CREATE_BUTTON_ID, |
| 67 | + text: intl.formatMessage({id: 'mobile.create_channel', defaultMessage: 'Create'}), |
| 68 | + showAsAction: 'always', |
| 69 | + testID: 'create_quick_checklist.create.button', |
| 70 | + enabled, |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +function CreateQuickChecklist({ |
| 75 | + componentId, |
| 76 | + channelId, |
| 77 | + channelName, |
| 78 | + currentUserId, |
| 79 | + currentTeamId, |
| 80 | + serverUrl, |
| 81 | +}: Props) { |
| 82 | + const theme = useTheme(); |
| 83 | + const intl = useIntl(); |
| 84 | + const styles = getStyleSheet(theme); |
| 85 | + |
| 86 | + const [checklistName, setChecklistName] = useState(`${channelName} Checklist`); |
| 87 | + const [description, setDescription] = useState(''); |
| 88 | + const canSave = Boolean(checklistName.trim()); |
| 89 | + |
| 90 | + const handleCreate = usePreventDoubleTap(useCallback(async () => { |
| 91 | + const res = await createPlaybookRun( |
| 92 | + serverUrl, |
| 93 | + '', // empty playbook_id for standalone checklist |
| 94 | + currentUserId, |
| 95 | + currentTeamId, |
| 96 | + checklistName.trim(), |
| 97 | + description.trim(), |
| 98 | + channelId, |
| 99 | + ); |
| 100 | + |
| 101 | + if (res.error || !res.data) { |
| 102 | + logError('error on createPlaybookRun', getFullErrorMessage(res.error)); |
| 103 | + showPlaybookErrorSnackbar(); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + // Pop the create screen first, then navigate to the run |
| 108 | + // This ensures the back button goes back to the channel, not the create screen |
| 109 | + await popTopScreen(componentId); |
| 110 | + await fetchPlaybookRunsForChannel(serverUrl, channelId); |
| 111 | + await goToPlaybookRun(intl, res.data.id); |
| 112 | + }, [serverUrl, currentUserId, currentTeamId, checklistName, description, channelId, componentId, intl])); |
| 113 | + |
| 114 | + useEffect(() => { |
| 115 | + async function asyncWrapper() { |
| 116 | + const leftButton = await makeLeftButton(theme); |
| 117 | + const rightButton = makeRightButton(theme, intl, canSave); |
| 118 | + |
| 119 | + setButtons( |
| 120 | + componentId, |
| 121 | + { |
| 122 | + leftButtons: [leftButton], |
| 123 | + rightButtons: [rightButton], |
| 124 | + }, |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + asyncWrapper(); |
| 129 | + }, [componentId, theme, intl, canSave]); |
| 130 | + |
| 131 | + const close = useCallback(() => { |
| 132 | + Keyboard.dismiss(); |
| 133 | + popTopScreen(componentId); |
| 134 | + }, [componentId]); |
| 135 | + |
| 136 | + useNavButtonPressed(CREATE_BUTTON_ID, componentId, handleCreate, [handleCreate]); |
| 137 | + useNavButtonPressed(CLOSE_BUTTON_ID, componentId, close, [close]); |
| 138 | + useAndroidHardwareBackHandler(componentId, close); |
| 139 | + |
| 140 | + return ( |
| 141 | + <SafeAreaView style={styles.container}> |
| 142 | + <ScrollView |
| 143 | + style={styles.content} |
| 144 | + contentContainerStyle={styles.contentContainer} |
| 145 | + > |
| 146 | + <FloatingTextInput |
| 147 | + label={intl.formatMessage({ |
| 148 | + id: 'playbooks.start_run.run_name_label', |
| 149 | + defaultMessage: 'Name', |
| 150 | + })} |
| 151 | + placeholder={intl.formatMessage({ |
| 152 | + id: 'playbooks.start_run.run_name_placeholder', |
| 153 | + defaultMessage: 'Add a name', |
| 154 | + })} |
| 155 | + value={checklistName} |
| 156 | + onChangeText={setChecklistName} |
| 157 | + theme={theme} |
| 158 | + testID='create_quick_checklist.name_input' |
| 159 | + error={checklistName.trim() ? undefined : intl.formatMessage({ |
| 160 | + id: 'playbooks.start_run.run_name_error', |
| 161 | + defaultMessage: 'Please add a name', |
| 162 | + })} |
| 163 | + /> |
| 164 | + <FloatingTextInput |
| 165 | + label={intl.formatMessage({ |
| 166 | + id: 'playbooks.start_checklist.description_label', |
| 167 | + defaultMessage: 'Description (optional)', |
| 168 | + })} |
| 169 | + value={description} |
| 170 | + onChangeText={setDescription} |
| 171 | + multiline={true} |
| 172 | + multilineInputHeight={100} |
| 173 | + theme={theme} |
| 174 | + testID='create_quick_checklist.description_input' |
| 175 | + /> |
| 176 | + </ScrollView> |
| 177 | + </SafeAreaView> |
| 178 | + ); |
| 179 | +} |
| 180 | + |
| 181 | +export default CreateQuickChecklist; |
| 182 | + |
0 commit comments