diff --git a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-by-name.react.js b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-by-name.react.js index 3f83272bf..95561679b 100644 --- a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-by-name.react.js +++ b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-by-name.react.js @@ -1,157 +1,174 @@ // @flow import invariant from 'invariant'; import * as React from 'react'; import { View, Text } from 'react-native'; import { createOrUpdateFarcasterChannelTagActionTypes, useCreateOrUpdateFarcasterChannelTag, } from 'lib/actions/community-actions.js'; import { NeynarClientContext } from 'lib/components/neynar-client-provider.react.js'; import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js'; import type { TagFarcasterChannelNavigationProp } from './tag-farcaster-channel-navigator.react.js'; +import { tagFarcasterChannelErrorMessages } from './tag-farcaster-channel-utils.js'; import RegistrationButton from '../../account/registration/registration-button.react.js'; import TextInput from '../../components/text-input.react.js'; import type { NavigationRoute } from '../../navigation/route-names.js'; import { useStyles, useColors } from '../../themes/colors.js'; export type TagFarcasterChannelByNameParams = { +communityID: string, }; type Props = { +navigation: TagFarcasterChannelNavigationProp<'TagFarcasterChannelByName'>, +route: NavigationRoute<'TagFarcasterChannelByName'>, }; function TagFarcasterChannelByName(prop: Props): React.Node { const { navigation, route } = prop; const { goBack } = navigation; const { communityID } = route.params; const styles = useStyles(unboundStyles); const colors = useColors(); const [channelSelectionText, setChannelSelectionText] = React.useState(''); + const [error, setError] = React.useState(null); const neynarClientContext = React.useContext(NeynarClientContext); invariant(neynarClientContext, 'NeynarClientContext is missing'); const dispatchActionPromise = useDispatchActionPromise(); const createOrUpdateFarcasterChannelTag = useCreateOrUpdateFarcasterChannelTag(); const createCreateOrUpdateActionPromise = React.useCallback( async (channelID: string) => { try { return await createOrUpdateFarcasterChannelTag({ commCommunityID: communityID, farcasterChannelID: channelID, }); } catch (e) { - // TODO: Improve error handling - console.log(e.message); + setError(e.message); throw e; } }, [communityID, createOrUpdateFarcasterChannelTag], ); const onPressTagChannel = React.useCallback(async () => { const channelInfo = await neynarClientContext.client.fetchFarcasterChannelByName( channelSelectionText, ); if (!channelInfo) { - // TODO: Improve error handling + setError('channel_not_found'); return; } await dispatchActionPromise( createOrUpdateFarcasterChannelTagActionTypes, createCreateOrUpdateActionPromise(channelInfo.id), ); goBack(); }, [ channelSelectionText, createCreateOrUpdateActionPromise, dispatchActionPromise, goBack, neynarClientContext.client, ]); + const errorMessage = React.useMemo(() => { + if (!error) { + return null; + } + + return ( + + {tagFarcasterChannelErrorMessages[error] ?? 'Unknown error.'} + + ); + }, [error, styles.error]); + const submitButtonVariant = channelSelectionText.length > 0 ? 'enabled' : 'disabled'; return ( CHANNEL NAME - - - + {errorMessage} + ); } const unboundStyles = { container: { paddingTop: 24, }, header: { color: 'panelBackgroundLabel', fontSize: 12, fontWeight: '400', paddingBottom: 4, paddingHorizontal: 16, }, panelSectionContainer: { backgroundColor: 'panelForeground', padding: 16, borderBottomColor: 'panelSeparator', borderBottomWidth: 1, borderTopColor: 'panelSeparator', borderTopWidth: 1, }, inputContainer: { flexDirection: 'row', justifyContent: 'space-between', paddingHorizontal: 16, paddingVertical: 12, borderWidth: 1, borderColor: 'panelSecondaryForegroundBorder', borderRadius: 8, + marginBottom: 8, }, input: { color: 'panelForegroundLabel', fontSize: 16, }, - buttonContainer: { - marginTop: 16, + error: { + fontSize: 12, + fontWeight: '400', + lineHeight: 18, + textAlign: 'center', + color: 'redText', }, }; export default TagFarcasterChannelByName; diff --git a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-utils.js b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-utils.js new file mode 100644 index 000000000..36384cbc2 --- /dev/null +++ b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel-utils.js @@ -0,0 +1,8 @@ +// @flow + +const tagFarcasterChannelErrorMessages: { +[string]: string } = { + already_in_use: 'This Farcaster channel is already tagged to a community.', + channel_not_found: 'Could not find a channel with the provided name.', +}; + +export { tagFarcasterChannelErrorMessages }; diff --git a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js index 85b1e8f03..5fbbb6a90 100644 --- a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js +++ b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js @@ -1,255 +1,252 @@ // @flow import { useActionSheet } from '@expo/react-native-action-sheet'; import invariant from 'invariant'; import * as React from 'react'; import { View, Text, TouchableOpacity, Platform } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { createOrUpdateFarcasterChannelTagActionTypes, useCreateOrUpdateFarcasterChannelTag, } from 'lib/actions/community-actions.js'; import { NeynarClientContext } from 'lib/components/neynar-client-provider.react.js'; import type { NeynarChannel } from 'lib/types/farcaster-types.js'; import { useCurrentUserFID } from 'lib/utils/farcaster-utils.js'; import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js'; import type { TagFarcasterChannelNavigationProp } from './tag-farcaster-channel-navigator.react.js'; +import { tagFarcasterChannelErrorMessages } from './tag-farcaster-channel-utils.js'; import RegistrationButton from '../../account/registration/registration-button.react.js'; import SWMansionIcon from '../../components/swmansion-icon.react.js'; import { type NavigationRoute } from '../../navigation/route-names.js'; import { useSelector } from '../../redux/redux-utils.js'; import { useStyles, useColors } from '../../themes/colors.js'; -const tagFarcasterErrorMessages: { +[string]: string } = { - already_in_use: 'This Farcaster channel is already tagged to a community.', -}; - export type TagFarcasterChannelParams = { +communityID: string, }; type Props = { +navigation: TagFarcasterChannelNavigationProp<'TagFarcasterChannel'>, +route: NavigationRoute<'TagFarcasterChannel'>, }; function TagFarcasterChannel(props: Props): React.Node { const { route } = props; const { communityID } = route.params; const styles = useStyles(unboundStyles); const colors = useColors(); const fid = useCurrentUserFID(); invariant(fid, 'FID should be set'); const [selectedChannel, setSelectedChannel] = React.useState(null); const [channelOptions, setChannelOptions] = React.useState< $ReadOnlyArray, >([]); const [error, setError] = React.useState(null); const neynarClientContext = React.useContext(NeynarClientContext); invariant(neynarClientContext, 'NeynarClientContext is missing'); const { client } = neynarClientContext; React.useEffect(() => { void (async () => { const channels = await client.fetchFollowedFarcasterChannels(fid); setChannelOptions(channels); })(); }, [client, fid]); const activeTheme = useSelector(state => state.globalThemeInfo.activeTheme); const { showActionSheetWithOptions } = useActionSheet(); const insets = useSafeAreaInsets(); const onOptionSelected = React.useCallback( (selectedIndex: ?number) => { if ( selectedIndex === undefined || selectedIndex === null || selectedIndex === channelOptions.length ) { return; } setError(null); setSelectedChannel(channelOptions[selectedIndex]); }, [channelOptions], ); const onPressSelectChannel = React.useCallback(() => { const channelNames = channelOptions.map(channel => channel.name); const options = Platform.OS === 'ios' ? [...channelNames, 'Cancel'] : channelNames; const cancelButtonIndex = Platform.OS === 'ios' ? options.length - 1 : -1; const containerStyle = { paddingBottom: insets.bottom, }; showActionSheetWithOptions( { options, cancelButtonIndex, containerStyle, userInterfaceStyle: activeTheme ?? 'dark', }, onOptionSelected, ); }, [ activeTheme, channelOptions, insets.bottom, onOptionSelected, showActionSheetWithOptions, ]); const dispatchActionPromise = useDispatchActionPromise(); const createOrUpdateFarcasterChannelTag = useCreateOrUpdateFarcasterChannelTag(); const createCreateOrUpdateActionPromise = React.useCallback(async () => { if (!selectedChannel) { return undefined; } try { return await createOrUpdateFarcasterChannelTag({ commCommunityID: communityID, farcasterChannelID: selectedChannel.id, }); } catch (e) { setError(e.message); throw e; } }, [communityID, createOrUpdateFarcasterChannelTag, selectedChannel]); const onPressTag = React.useCallback(() => { void dispatchActionPromise( createOrUpdateFarcasterChannelTagActionTypes, createCreateOrUpdateActionPromise(), ); }, [createCreateOrUpdateActionPromise, dispatchActionPromise]); const channelSelectionStyles = React.useMemo( () => [styles.sectionContainer, styles.touchableSectionContainer], [styles.sectionContainer, styles.touchableSectionContainer], ); const errorMessage = React.useMemo(() => { if (!error) { return null; } return ( - {tagFarcasterErrorMessages[error] ?? 'Unknown error.'} + {tagFarcasterChannelErrorMessages[error] ?? 'Unknown error.'} ); }, [error, styles.error]); const channelSelectionTextContent = selectedChannel?.name ? selectedChannel.name : 'No Farcaster channel tagged'; const buttonVariant = selectedChannel ? 'enabled' : 'disabled'; const tagFarcasterChannel = React.useMemo( () => ( Tag a Farcaster channel so followers can find your Comm community! FARCASTER CHANNEL {channelSelectionTextContent} {errorMessage} ), [ styles.sectionContainer, styles.sectionText, styles.sectionHeaderText, styles.errorContainer, channelSelectionStyles, onPressSelectChannel, channelSelectionTextContent, colors.panelForegroundSecondaryLabel, errorMessage, onPressTag, buttonVariant, ], ); return tagFarcasterChannel; } const unboundStyles = { sectionContainer: { backgroundColor: 'panelForeground', marginBottom: 24, padding: 16, }, sectionText: { color: 'panelForegroundLabel', fontSize: 14, }, sectionHeaderText: { fontSize: 14, fontWeight: '400', lineHeight: 20, color: 'panelForegroundLabel', paddingHorizontal: 16, paddingBottom: 4, }, touchableSectionContainer: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }, errorContainer: { height: 18, }, error: { fontSize: 12, fontWeight: '400', lineHeight: 18, textAlign: 'center', color: 'redText', }, }; export default TagFarcasterChannel;